@@ -10,20 +10,28 @@ class RunnerClient
10
10
class << self
11
11
extend T ::Sig
12
12
13
- sig { returns ( RunnerClient ) }
14
- def create_client
13
+ sig { params ( message_queue : Thread :: Queue ) . returns ( RunnerClient ) }
14
+ def create_client ( message_queue )
15
15
if File . exist? ( "bin/rails" )
16
- new
16
+ new ( message_queue )
17
17
else
18
- $stderr. puts ( <<~MSG )
19
- Ruby LSP Rails failed to locate bin/rails in the current directory: #{ Dir . pwd } "
20
- MSG
21
- $stderr. puts ( "Server dependent features will not be available" )
18
+ message_queue << RubyLsp ::Notification . window_log_message (
19
+ <<~MESSAGE . chomp ,
20
+ Ruby LSP Rails failed to locate bin/rails in the current directory: #{ Dir . pwd }
21
+ Server dependent features will not be available
22
+ MESSAGE
23
+ type : RubyLsp ::Constant ::MessageType ::WARNING ,
24
+ )
22
25
NullClient . new
23
26
end
24
27
rescue Errno ::ENOENT , StandardError => e # rubocop:disable Lint/ShadowedException
25
- $stderr. puts ( "Ruby LSP Rails failed to initialize server: #{ e . message } \n #{ e . backtrace &.join ( "\n " ) } " )
26
- $stderr. puts ( "Server dependent features will not be available" )
28
+ message_queue << RubyLsp ::Notification . window_log_message (
29
+ <<~MESSAGE . chomp ,
30
+ Ruby LSP Rails failed to initialize server: #{ e . full_message }
31
+ Server dependent features will not be available
32
+ MESSAGE
33
+ type : Constant ::MessageType ::ERROR ,
34
+ )
27
35
NullClient . new
28
36
end
29
37
end
@@ -39,8 +47,9 @@ class EmptyMessageError < StandardError; end
39
47
sig { returns ( String ) }
40
48
attr_reader :rails_root
41
49
42
- sig { void }
43
- def initialize
50
+ sig { params ( message_queue : Thread ::Queue ) . void }
51
+ def initialize ( message_queue )
52
+ @message_queue = T . let ( message_queue , Thread ::Queue )
44
53
@mutex = T . let ( Mutex . new , Mutex )
45
54
# Spring needs a Process session ID. It uses this ID to "attach" itself to the parent process, so that when the
46
55
# parent ends, the spring process ends as well. If this is not set, Spring will throw an error while trying to
@@ -69,24 +78,27 @@ def initialize
69
78
@stdout . binmode
70
79
@stderr . binmode
71
80
72
- $stderr . puts ( "Ruby LSP Rails booting server" )
81
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails booting server" )
73
82
count = 0
74
83
75
84
begin
76
85
count += 1
77
86
initialize_response = T . must ( read_response )
78
87
@rails_root = T . let ( initialize_response [ :root ] , String )
79
88
rescue EmptyMessageError
80
- $stderr . puts ( "Ruby LSP Rails is retrying initialize (#{ count } )" )
89
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails is retrying initialize (#{ count } )" )
81
90
retry if count < MAX_RETRIES
82
91
end
83
92
84
- $stderr . puts ( "Finished booting Ruby LSP Rails server" )
93
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Finished booting Ruby LSP Rails server" )
85
94
86
95
unless ENV [ "RAILS_ENV" ] == "test"
87
96
at_exit do
88
97
if @wait_thread . alive?
89
- $stderr. puts ( "Ruby LSP Rails is force killing the server" )
98
+ @message_queue << RubyLsp ::Notification . window_log_message (
99
+ "Ruby LSP Rails is force killing the server" ,
100
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
101
+ )
90
102
sleep ( 0.5 ) # give the server a bit of time if we already issued a shutdown notification
91
103
force_kill
92
104
end
@@ -100,15 +112,21 @@ def initialize
100
112
def register_server_addon ( server_addon_path )
101
113
send_notification ( "server_addon/register" , server_addon_path : server_addon_path )
102
114
rescue IncompleteMessageError
103
- $stderr. puts ( "Ruby LSP Rails failed to register server addon #{ server_addon_path } " )
115
+ @message_queue << RubyLsp ::Notification . window_log_message (
116
+ "Ruby LSP Rails failed to register server addon #{ server_addon_path } " ,
117
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
118
+ )
104
119
nil
105
120
end
106
121
107
122
sig { params ( name : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
108
123
def model ( name )
109
124
make_request ( "model" , name : name )
110
125
rescue IncompleteMessageError
111
- $stderr. puts ( "Ruby LSP Rails failed to get model information: #{ @stderr . read } " )
126
+ @message_queue << RubyLsp ::Notification . window_log_message (
127
+ "Ruby LSP Rails failed to get model information: #{ @stderr . read } " ,
128
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
129
+ )
112
130
nil
113
131
end
114
132
@@ -125,37 +143,50 @@ def association_target_location(model_name:, association_name:)
125
143
association_name : association_name ,
126
144
)
127
145
rescue => e
128
- $stderr. puts ( "Ruby LSP Rails failed with #{ e . message } : #{ @stderr . read } " )
146
+ @message_queue << RubyLsp ::Notification . window_log_message (
147
+ "Ruby LSP Rails failed with #{ e . message } : #{ @stderr . read } " ,
148
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
149
+ )
150
+ nil
129
151
end
130
152
131
153
sig { params ( name : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
132
154
def route_location ( name )
133
155
make_request ( "route_location" , name : name )
134
156
rescue IncompleteMessageError
135
- $stderr. puts ( "Ruby LSP Rails failed to get route location: #{ @stderr . read } " )
157
+ @message_queue << RubyLsp ::Notification . window_log_message (
158
+ "Ruby LSP Rails failed to get route location: #{ @stderr . read } " ,
159
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
160
+ )
136
161
nil
137
162
end
138
163
139
164
sig { params ( controller : String , action : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
140
165
def route ( controller :, action :)
141
166
make_request ( "route_info" , controller : controller , action : action )
142
167
rescue IncompleteMessageError
143
- $stderr. puts ( "Ruby LSP Rails failed to get route information: #{ @stderr . read } " )
168
+ @message_queue << RubyLsp ::Notification . window_log_message (
169
+ "Ruby LSP Rails failed to get route information: #{ @stderr . read } " ,
170
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
171
+ )
144
172
nil
145
173
end
146
174
147
175
sig { void }
148
176
def trigger_reload
149
- $stderr . puts ( "Reloading Rails application" )
177
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Reloading Rails application" )
150
178
send_notification ( "reload" )
151
179
rescue IncompleteMessageError
152
- $stderr. puts ( "Ruby LSP Rails failed to trigger reload" )
180
+ @message_queue << RubyLsp ::Notification . window_log_message (
181
+ "Ruby LSP Rails failed to trigger reload" ,
182
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
183
+ )
153
184
nil
154
185
end
155
186
156
187
sig { void }
157
188
def shutdown
158
- $stderr . puts ( "Ruby LSP Rails shutting down server" )
189
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails shutting down server" )
159
190
send_message ( "shutdown" )
160
191
sleep ( 0.5 ) # give the server a bit of time to shutdown
161
192
[ @stdin , @stdout , @stderr ] . each ( &:close )
@@ -214,7 +245,10 @@ def read_response
214
245
response = JSON . parse ( T . must ( raw_response ) , symbolize_names : true )
215
246
216
247
if response [ :error ]
217
- $stderr. puts ( "Ruby LSP Rails error: " + response [ :error ] )
248
+ @message_queue << RubyLsp ::Notification . window_log_message (
249
+ "Ruby LSP Rails error: #{ response [ :error ] } " ,
250
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
251
+ )
218
252
return
219
253
end
220
254
0 commit comments