-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruby.rb
More file actions
351 lines (308 loc) · 7.98 KB
/
Copy pathruby.rb
File metadata and controls
351 lines (308 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
require_relative "analysis/file_map"
require_relative "find_association"
require_relative "find_controller_view"
require_relative "find_controller"
require_relative "find_class"
require "active_support/all"
require "json"
require "byebug"
require "byebug/core"
class Lsp
def call
file_map = Analysis::FileMap.new
#Byebug.wait_connection = true
#Byebug.start_server("localhost", 8080)
#VS Code
loop do
buffer = STDIN.gets("\r\n\r\n")
content_length = buffer.match(/Content-Length: (\d+)/i)[1].to_i
message = STDIN.read(content_length) or raise
request = JSON.parse(message, symbolize_names: true)
log_message(request, :method)
case request[:method]
when "initialize"
file_map.root_path = request[:params][:rootPath]
respond(request)
when "textDocument/didOpen"
file_uri = request[:params][:textDocument][:uri]
content = request[:params][:textDocument][:text]
file_map.upsert(file_uri:, content:)
log("Opened #{file_uri}")
notification_response(request, file_map)
when "textDocument/didChange"
file_uri = request[:params][:textDocument][:uri]
request[:params][:contentChanges].each do |change|
file_map.upsert(file_uri:, content: change[:text])
end
log("Contents Updated")
notification_response(request, file_map)
#when "textDocument/hover"
# hover_response(request, file_map)
when "textDocument/definition"
log("DEFINITION")
definition_response(request, file_map)
when "textDocument/codeAction"
code_action_response(request, file_map)
when "textDocument/completion"
completion_provider_response(request)
end
end
end
private
def notification_response(request, file_map)
uri = request[:params][:textDocument][:uri]
content = file_map.hash[uri]
line = nil
search_word = "VS Code"
content.split("\n").each_with_index do |text_line, index|
if text_line.include?(search_word)
line = index
end
end
unless line.nil?
response = {
jsonrpc: "2.0",
method: "textDocument/publishDiagnostics",
params: {
uri:,
diagnostics: [
{
range: {
start: {
line: line,
character: 0
},
end: {
line: line,
character: 0
}
},
severity: 1,
source: "Common Sense",
message: "Please use a good editor",
}
]
}
}.to_json
write_to_stdout(response)
end
end
def completion_provider_response(request)
response = {
jsonrpc: "2.0",
id: request[:id],
result: {
items: [
{
label: "Neovim (BTW)",
detail: "Very cool editor",
documentation: "It's quite fun",
}
]
}
}.to_json
write_to_stdout(response)
end
def code_action_response(request, file_map)
uri = request[:params][:textDocument][:uri]
content = file_map.hash[uri]
line = nil
character = nil
search_word = "VS Code"
content.split("\n").each_with_index do |text_line, index|
if text_line.include?(search_word)
line = index
character = text_line.index(search_word)
break
end
end
unless line.nil?
response = {
jsonrpc: "2.0",
id: request[:id],
result: [
{
title: "Replace VS code with a superior editor",
edit: {
changes: {
uri => [
{
range: {
start: {
line:,
character:
},
end: {
line:,
character: character + search_word.size
}
},
newText: "Neovim"
}
]
}
}
}
]
}.to_json
write_to_stdout(response)
end
end
def definition_response(request, file_map)
uri = request[:params][:textDocument][:uri]
line_number = request[:params][:position][:line].to_s
line = file_map.hash_lines[uri][line_number]
char_number = request[:params][:position][:character]
association = FindAssociation.new(
line_text: line,
file_uri: uri,
root_path: file_map.root_path
).call
if association.path
response = {
jsonrpc: '2.0',
id: request[:id],
result: {
uri: "file://#{association.path}",
range: {
start: {
line: association.start_line,
character: 0
},
end: {
line: 0,
character: 0
}
}
}
}.to_json
return write_to_stdout(response)
end
controller_view = FindControllerView.new(
line_text: line,
file_uri: uri,
root_path: file_map.root_path
).call
if controller_view&.path
response = {
jsonrpc: '2.0',
id: request[:id],
result: {
uri: "file://#{controller_view.path}",
range: {
start: {
line: 0,
character: 0
},
end: {
line: 0,
character: 0
}
}
}
}.to_json
return write_to_stdout(response)
end
klass = FindClass.new(
line:,
uri:,
root_path: file_map.root_path,
char_number:
).call
if klass&.path
response = {
jsonrpc: "2.0",
id: request[:id],
result: {
priority: true,
uri: "file://#{klass.path}",
range: {
start: {
line: 0,
character: 0
},
end: {
line: 0,
character: 0
}
}
}
}.to_json
return write_to_stdout(response)
end
controller = FindController.new(
file_uri: uri,
file_map:
).call
if controller&.path
response = {
jsonrpc: "2.0",
id: request[:id],
result: {
uri: "file://#{controller.path}",
range: {
start: {
line: controller.line_number,
character: controller.char_number
},
end: {
line: 0,
character: 0
}
}
}
}.to_json
return write_to_stdout(response)
end
end
def hover_response(request, file_map)
file_uri = request[:params][:textDocument][:uri]
characters = file_map.hash[file_uri].length
response = {
jsonrpc: "2.0",
id: request[:id],
result: {
contents: "File: #{file_uri}, Characters: #{characters}"
}
}.to_json
write_to_stdout(response)
end
def respond(request)
response = {
jsonrpc: "2.0",
id: request[:id],
result: {
capabilities: {
textDocumentSync: 1,
hoverProvider: true,
definitionProvider: true,
codeActionProvider: true,
completionProvider: {},
},
serverInfo: {
name: "educationlsp",
version: "0.0.0.0.0.0-beta1.final"
}
}
}.to_json
write_to_stdout(response)
end
def write_to_stdout(json)
json_string = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
stdout = STDOUT.binmode
stdout.print json_string
$stdout.flush
log("Sending response")
end
def log_message(request, key)
# File.open("log.txt", "a") do |f|
# f.write "Received message with #{request[key]}\n"
# end
end
def log(message)
File.open("/home/catalin/.local/state/nvim/lsp.log", "a") do |f|
f.write "#{message}\n"
end
end
end
Lsp.new.call