@@ -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,23 @@ 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" )
90
98
sleep ( 0.5 ) # give the server a bit of time if we already issued a shutdown notification
91
99
force_kill
92
100
end
@@ -100,15 +108,21 @@ def initialize
100
108
def register_server_addon ( server_addon_path )
101
109
send_notification ( "server_addon/register" , server_addon_path : server_addon_path )
102
110
rescue IncompleteMessageError
103
- $stderr. puts ( "Ruby LSP Rails failed to register server addon #{ server_addon_path } " )
111
+ @message_queue << RubyLsp ::Notification . window_log_message (
112
+ "Ruby LSP Rails failed to register server addon #{ server_addon_path } " ,
113
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
114
+ )
104
115
nil
105
116
end
106
117
107
118
sig { params ( name : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
108
119
def model ( name )
109
120
make_request ( "model" , name : name )
110
121
rescue IncompleteMessageError
111
- $stderr. puts ( "Ruby LSP Rails failed to get model information: #{ @stderr . read } " )
122
+ @message_queue << RubyLsp ::Notification . window_log_message (
123
+ "Ruby LSP Rails failed to get model information: #{ @stderr . read } " ,
124
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
125
+ )
112
126
nil
113
127
end
114
128
@@ -125,37 +139,50 @@ def association_target_location(model_name:, association_name:)
125
139
association_name : association_name ,
126
140
)
127
141
rescue => e
128
- $stderr. puts ( "Ruby LSP Rails failed with #{ e . message } : #{ @stderr . read } " )
142
+ @message_queue << RubyLsp ::Notification . window_log_message (
143
+ "Ruby LSP Rails failed with #{ e . message } : #{ @stderr . read } " ,
144
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
145
+ )
146
+ nil
129
147
end
130
148
131
149
sig { params ( name : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
132
150
def route_location ( name )
133
151
make_request ( "route_location" , name : name )
134
152
rescue IncompleteMessageError
135
- $stderr. puts ( "Ruby LSP Rails failed to get route location: #{ @stderr . read } " )
153
+ @message_queue << RubyLsp ::Notification . window_log_message (
154
+ "Ruby LSP Rails failed to get route location: #{ @stderr . read } " ,
155
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
156
+ )
136
157
nil
137
158
end
138
159
139
160
sig { params ( controller : String , action : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
140
161
def route ( controller :, action :)
141
162
make_request ( "route_info" , controller : controller , action : action )
142
163
rescue IncompleteMessageError
143
- $stderr. puts ( "Ruby LSP Rails failed to get route information: #{ @stderr . read } " )
164
+ @message_queue << RubyLsp ::Notification . window_log_message (
165
+ "Ruby LSP Rails failed to get route information: #{ @stderr . read } " ,
166
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
167
+ )
144
168
nil
145
169
end
146
170
147
171
sig { void }
148
172
def trigger_reload
149
- $stderr . puts ( "Reloading Rails application" )
173
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Reloading Rails application" )
150
174
send_notification ( "reload" )
151
175
rescue IncompleteMessageError
152
- $stderr. puts ( "Ruby LSP Rails failed to trigger reload" )
176
+ @message_queue << RubyLsp ::Notification . window_log_message (
177
+ "Ruby LSP Rails failed to trigger reload" ,
178
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
179
+ )
153
180
nil
154
181
end
155
182
156
183
sig { void }
157
184
def shutdown
158
- $stderr . puts ( "Ruby LSP Rails shutting down server" )
185
+ @message_queue << RubyLsp :: Notification . window_log_message ( "Ruby LSP Rails shutting down server" )
159
186
send_message ( "shutdown" )
160
187
sleep ( 0.5 ) # give the server a bit of time to shutdown
161
188
[ @stdin , @stdout , @stderr ] . each ( &:close )
@@ -214,7 +241,10 @@ def read_response
214
241
response = JSON . parse ( T . must ( raw_response ) , symbolize_names : true )
215
242
216
243
if response [ :error ]
217
- $stderr. puts ( "Ruby LSP Rails error: " + response [ :error ] )
244
+ @message_queue << RubyLsp ::Notification . window_log_message (
245
+ "Ruby LSP Rails error: #{ response [ :error ] } " ,
246
+ type : RubyLsp ::Constant ::MessageType ::ERROR ,
247
+ )
218
248
return
219
249
end
220
250
0 commit comments