forked from Shopify/ruby-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_test.rb
501 lines (438 loc) · 14.8 KB
/
document_test.rb
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# typed: true
# frozen_string_literal: true
require "test_helper"
class DocumentTest < Minitest::Test
def test_valid_incremental_edits
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
def foo
end
RUBY
# Write puts 'a' in incremental edits
document.push_edits(
[{ range: { start: { line: 0, character: 7 }, end: { line: 0, character: 7 } }, text: "\n " }],
version: 2,
)
document.push_edits(
[{ range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } }, text: "p" }],
version: 3,
)
document.push_edits(
[{ range: { start: { line: 1, character: 3 }, end: { line: 1, character: 3 } }, text: "u" }],
version: 4,
)
document.push_edits(
[{ range: { start: { line: 1, character: 4 }, end: { line: 1, character: 4 } }, text: "t" }],
version: 5,
)
document.push_edits(
[{ range: { start: { line: 1, character: 5 }, end: { line: 1, character: 5 } }, text: "s" }],
version: 6,
)
document.push_edits(
[{ range: { start: { line: 1, character: 6 }, end: { line: 1, character: 6 } }, text: " " }],
version: 7,
)
document.push_edits(
[{ range: { start: { line: 1, character: 7 }, end: { line: 1, character: 7 } }, text: "'" }],
version: 8,
)
document.push_edits(
[{ range: { start: { line: 1, character: 8 }, end: { line: 1, character: 8 } }, text: "a" }],
version: 9,
)
document.push_edits(
[{ range: { start: { line: 1, character: 9 }, end: { line: 1, character: 9 } }, text: "'" }], version: 10
)
assert_equal(<<~RUBY, document.source)
def foo
puts 'a'
end
RUBY
end
def test_deletion_full_node
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
def foo
puts 'a' # comment
end
RUBY
# Delete puts 'a' in incremental edits
document.push_edits(
[{ range: { start: { line: 1, character: 2 }, end: { line: 1, character: 11 } }, text: "" }],
version: 2,
)
assert_equal(<<~RUBY, document.source)
def foo
# comment
end
RUBY
end
def test_deletion_single_character
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
def foo
puts 'a'
end
RUBY
# Delete puts 'a' in incremental edits
document.push_edits(
[{ range: { start: { line: 1, character: 8 }, end: { line: 1, character: 9 } }, text: "" }],
version: 2,
)
assert_equal(<<~RUBY, document.source)
def foo
puts ''
end
RUBY
end
def test_add_delete_single_character
document = RubyLsp::Document.new(source: +"", version: 1, uri: "file:///foo.rb")
# Add a
document.push_edits(
[{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }, text: "a" }],
version: 2,
)
assert_equal("a", document.source)
# Delete a
document.push_edits(
[{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } }, text: "" }],
version: 3,
)
assert_empty(document.source)
end
def test_replace
document = RubyLsp::Document.new(source: +"puts 'a'", version: 1, uri: "file:///foo.rb")
# Replace for puts 'b'
document.push_edits(
[{
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 8 } },
text: "puts 'b'",
}],
version: 2,
)
assert_equal("puts 'b'", document.source)
end
def test_new_line_and_char_addition
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
# frozen_string_literal: true
class Foo
def foo
end
end
RUBY
# Write puts 'a' in incremental edits
document.push_edits(
[{
range: { start: { line: 3, character: 9 }, end: { line: 3, character: 9 } },
rangeLength: 0,
text: "\n ",
}],
version: 2,
)
document.push_edits(
[{
range: { start: { line: 4, character: 4 }, end: { line: 4, character: 4 } },
rangeLength: 0,
text: "a",
}],
version: 3,
)
assert_equal(<<~RUBY, document.source)
# frozen_string_literal: true
class Foo
def foo
a
end
end
RUBY
end
def test_multi_cursor_edit
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
# frozen_string_literal: true
RUBY
# Write puts 'hello' with two cursors on line 1 and 3
document.push_edits(
[
{ range: { start: { line: 3, character: 0 }, end: { line: 3, character: 0 } }, text: "p" },
{ range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } }, text: "p" },
],
version: 2,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 1 }, end: { line: 3, character: 1 } }, text: "u" },
{ range: { start: { line: 1, character: 1 }, end: { line: 1, character: 1 } }, text: "u" },
],
version: 3,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 2 }, end: { line: 3, character: 2 } }, text: "t" },
{ range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } }, text: "t" },
],
version: 4,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 3 }, end: { line: 3, character: 3 } }, text: "s" },
{ range: { start: { line: 1, character: 3 }, end: { line: 1, character: 3 } }, text: "s" },
],
version: 5,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 4 }, end: { line: 3, character: 4 } }, text: " " },
{ range: { start: { line: 1, character: 4 }, end: { line: 1, character: 4 } }, text: " " },
],
version: 6,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 5 }, end: { line: 3, character: 5 } }, text: "'" },
{ range: { start: { line: 1, character: 5 }, end: { line: 1, character: 5 } }, text: "'" },
],
version: 7,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 6 }, end: { line: 3, character: 6 } }, text: "h" },
{ range: { start: { line: 1, character: 6 }, end: { line: 1, character: 6 } }, text: "h" },
],
version: 8,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 7 }, end: { line: 3, character: 7 } }, text: "e" },
{ range: { start: { line: 1, character: 7 }, end: { line: 1, character: 7 } }, text: "e" },
],
version: 9,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 8 }, end: { line: 3, character: 8 } }, text: "l" },
{ range: { start: { line: 1, character: 8 }, end: { line: 1, character: 8 } }, text: "l" },
],
version: 10,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 9 }, end: { line: 3, character: 9 } }, text: "l" },
{ range: { start: { line: 1, character: 9 }, end: { line: 1, character: 9 } }, text: "l" },
],
version: 11,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 10 }, end: { line: 3, character: 10 } }, text: "o" },
{ range: { start: { line: 1, character: 10 }, end: { line: 1, character: 10 } }, text: "o" },
],
version: 12,
)
document.push_edits(
[
{ range: { start: { line: 3, character: 11 }, end: { line: 3, character: 11 } }, text: "'" },
{ range: { start: { line: 1, character: 11 }, end: { line: 1, character: 11 } }, text: "'" },
],
version: 13,
)
assert_equal(<<~RUBY, document.source)
# frozen_string_literal: true
puts 'hello'
puts 'hello'
RUBY
end
def test_pushing_edits_to_document_with_unicode
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
chars = ["億"]
RUBY
# Write puts 'a' in incremental edits
document.push_edits(
[{ range: { start: { line: 0, character: 13 }, end: { line: 0, character: 13 } }, text: "\n" }],
version: 2,
)
document.push_edits(
[{ range: { start: { line: 1, character: 0 }, end: { line: 1, character: 0 } }, text: "p" }],
version: 3,
)
document.push_edits(
[{ range: { start: { line: 1, character: 1 }, end: { line: 1, character: 1 } }, text: "u" }],
version: 4,
)
document.push_edits(
[{ range: { start: { line: 1, character: 2 }, end: { line: 1, character: 2 } }, text: "t" }],
version: 5,
)
document.push_edits(
[{ range: { start: { line: 1, character: 3 }, end: { line: 1, character: 3 } }, text: "s" }],
version: 6,
)
document.push_edits(
[{ range: { start: { line: 1, character: 4 }, end: { line: 1, character: 4 } }, text: " " }],
version: 7,
)
document.push_edits(
[{ range: { start: { line: 1, character: 5 }, end: { line: 1, character: 5 } }, text: "'" }],
version: 8,
)
document.push_edits(
[{ range: { start: { line: 1, character: 6 }, end: { line: 1, character: 6 } }, text: "a" }],
version: 9,
)
document.push_edits(
[{ range: { start: { line: 1, character: 7 }, end: { line: 1, character: 7 } }, text: "'" }],
version: 10,
)
assert_equal(<<~RUBY, document.source)
chars = ["億"]
puts 'a'
RUBY
end
def test_parsed_returns_true_when_parsed_successfully
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
# frozen_string_literal: true
puts 'hello'
RUBY
assert_predicate(document, :parsed?)
end
def test_parsed_returns_false_when_parsing_fails
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
class Foo
RUBY
refute_predicate(document, :parsed?)
end
def test_document_handle_4_byte_unicode_characters
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb", encoding: "utf-16")
class Foo
a = "👋"
end
RUBY
document.push_edits(
[
{ range: { start: { line: 1, character: 9 }, end: { line: 1, character: 9 } }, text: "s" },
],
version: 2,
)
document.parse
assert_predicate(document, :parsed?)
assert_equal(<<~RUBY, document.source)
class Foo
a = "👋s"
end
RUBY
document.push_edits(
[{ range: { start: { line: 1, character: 11 }, end: { line: 1, character: 11 } }, text: "\n" }],
version: 3,
)
document.push_edits(
[{ range: { start: { line: 2, character: 0 }, end: { line: 2, character: 0 } }, text: " p" }],
version: 4,
)
document.push_edits(
[{ range: { start: { line: 2, character: 3 }, end: { line: 2, character: 3 } }, text: "u" }],
version: 5,
)
document.push_edits(
[{ range: { start: { line: 2, character: 4 }, end: { line: 2, character: 4 } }, text: "t" }],
version: 6,
)
document.push_edits(
[{ range: { start: { line: 2, character: 5 }, end: { line: 2, character: 5 } }, text: "s" }],
version: 7,
)
document.push_edits(
[{ range: { start: { line: 2, character: 6 }, end: { line: 2, character: 6 } }, text: " " }],
version: 8,
)
document.push_edits(
[{ range: { start: { line: 2, character: 7 }, end: { line: 2, character: 7 } }, text: "'" }],
version: 9,
)
document.push_edits(
[{ range: { start: { line: 2, character: 8 }, end: { line: 2, character: 8 } }, text: "a" }],
version: 10,
)
document.push_edits(
[{ range: { start: { line: 2, character: 9 }, end: { line: 2, character: 9 } }, text: "'" }],
version: 11,
)
document.parse
assert_predicate(document, :parsed?)
assert_equal(<<~RUBY, document.source)
class Foo
a = "👋s"
puts 'a'
end
RUBY
end
def test_failing_to_parse_indicates_syntax_error
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
def foo
end
RUBY
refute_predicate(document, :syntax_error?)
document.push_edits(
[{
range: { start: { line: 0, character: 7 }, end: { line: 0, character: 7 } },
text: "\n def",
}],
version: 2,
)
document.parse
assert_predicate(document, :syntax_error?)
end
def test_files_opened_with_syntax_errors_are_properly_marked
document = RubyLsp::Document.new(source: +<<~RUBY, version: 1, uri: "file:///foo.rb")
def foo
RUBY
assert_predicate(document, :syntax_error?)
end
def test_locate
document = RubyLsp::Document.new(source: <<~RUBY, version: 1, uri: "file:///foo/bar.rb")
class Post < ActiveRecord::Base
scope :published do
# find posts that are published
where(published: true)
end
end
RUBY
# Locate the `ActiveRecord` module
found, parent = document.locate_node({ line: 0, character: 19 })
assert_instance_of(SyntaxTree::Const, found)
assert_equal("ActiveRecord", T.cast(found, SyntaxTree::Const).value)
assert_instance_of(SyntaxTree::VarRef, parent)
assert_equal("ActiveRecord", T.cast(parent, SyntaxTree::VarRef).value.value)
# Locate the `Base` class
found, parent = T.cast(
document.locate_node({ line: 0, character: 27 }),
[SyntaxTree::Const, SyntaxTree::ConstPathRef],
)
assert_instance_of(SyntaxTree::Const, found)
assert_equal("Base", found.value)
assert_instance_of(SyntaxTree::ConstPathRef, parent)
assert_equal("Base", parent.constant.value)
assert_equal("ActiveRecord", T.cast(parent.parent, SyntaxTree::VarRef).value.value)
# Locate the `where` invocation
found, parent = T.cast(document.locate_node({ line: 3, character: 4 }), [SyntaxTree::Ident, SyntaxTree::CallNode])
assert_instance_of(SyntaxTree::Ident, found)
assert_equal("where", found.value)
assert_instance_of(SyntaxTree::CallNode, parent)
end
def test_reparsing_without_new_edits_does_nothing
document = RubyLsp::Document.new(source: +"", version: 1, uri: "file:///foo/bar.rb")
document.push_edits(
[{ range: { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } }, text: "def foo" }],
version: 2,
)
document.parse
assert_predicate(document, :syntax_error?)
assert_empty(document.instance_variable_get(:@unparsed_edits))
end
def test_cache_set_and_get
document = RubyLsp::Document.new(source: +"", version: 1, uri: "file:///foo/bar.rb")
value = [1, 2, 3]
assert_equal(value, document.cache_set("textDocument/semanticHighlighting", value))
assert_equal(value, document.cache_get("textDocument/semanticHighlighting"))
end
private
def assert_error_edit(actual, error_range)
assert_equal([error_range].to_json, actual.to_json)
end
end