Skip to content

Commit 37cddf3

Browse files
committed
Update graph on edits
1 parent 1208c48 commit 37cddf3

4 files changed

Lines changed: 31 additions & 78 deletions

File tree

lib/ruby_lsp/server.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,16 @@ def text_document_did_change(message)
415415
params = message[:params]
416416
text_document = params[:textDocument]
417417

418-
@store.push_edits(uri: text_document[:uri], edits: params[:contentChanges], version: text_document[:version])
418+
document = @store.get(text_document[:uri])
419+
document.push_edits(params[:contentChanges], version: text_document[:version])
420+
421+
language_id = document.language_id
422+
423+
if [:ruby, :rbs].include?(language_id)
424+
graph = @global_state.graph
425+
graph.index_source(text_document[:uri].to_s, document.source, language_id.to_s)
426+
graph.resolve
427+
end
419428
end
420429

421430
#: (Hash[Symbol, untyped] message) -> void
@@ -1033,6 +1042,23 @@ def workspace_did_change_watched_files(message)
10331042
# is fine, but we shouldn't process the same file changes more than once
10341043
changes.uniq!
10351044

1045+
graph = @global_state.graph
1046+
1047+
# Handle deletions and accumulate additions and changes for indexing
1048+
additions_and_changes = changes.each_with_object([]) do |change, acc|
1049+
if change[:type] == Constant::FileChangeType::DELETED
1050+
graph.delete_document(change[:uri])
1051+
else
1052+
path = URI(change[:uri]).to_standardized_path
1053+
next if path.nil?
1054+
next unless File.directory?(path) || [".rb", ".rbs"].include?(File.extname(path))
1055+
1056+
acc << path
1057+
end
1058+
end
1059+
graph.index_all(additions_and_changes)
1060+
graph.resolve
1061+
10361062
index = @global_state.index
10371063
changes.each do |change|
10381064
# File change events include folders, but we're only interested in files

lib/ruby_lsp/store.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ def set(uri:, source:, version:, language_id:)
5353
end
5454
end
5555

56-
#: (uri: URI::Generic, edits: Array[Hash[Symbol, untyped]], version: Integer) -> void
57-
def push_edits(uri:, edits:, version:)
58-
@state[uri.to_s] #: as !nil
59-
.push_edits(edits, version: version)
60-
end
61-
6256
#: -> void
6357
def clear
6458
@state.clear

test/server_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ def test_changed_file_only_indexes_ruby
465465
type: RubyLsp::Constant::FileChangeType::CREATED,
466466
},
467467
{
468-
uri: URI("file:///.rubocop.yml"),
468+
uri: URI("file:///.rubocop.yml").to_s,
469469
type: RubyLsp::Constant::FileChangeType::CREATED,
470470
},
471471
],
@@ -505,7 +505,7 @@ def test_did_change_watched_files_handles_deletions
505505
uri.full_path == path
506506
end
507507

508-
uri = URI::Generic.from_path(path: path)
508+
uri = URI::Generic.from_path(path: path).to_s
509509

510510
@server.global_state.index.index_all(uris: [])
511511
@server.process_message({
@@ -1175,7 +1175,7 @@ def test_rubocop_config_changes_trigger_workspace_diagnostic_refresh
11751175
@server.global_state.index.index_all(uris: [])
11761176

11771177
[".rubocop.yml", ".rubocop", ".rubocop_todo.yml"].each do |config_file|
1178-
uri = URI::Generic.from_path(path: File.join(Dir.pwd, config_file))
1178+
uri = URI::Generic.from_path(path: File.join(Dir.pwd, config_file)).to_s
11791179

11801180
@server.process_message({
11811181
method: "workspace/didChangeWatchedFiles",
@@ -1324,7 +1324,7 @@ class Foo
13241324
end
13251325
RUBY
13261326
File.write(path, source)
1327-
uri = URI::Generic.from_path(path: path)
1327+
uri = URI::Generic.from_path(path: path).to_s
13281328

13291329
begin
13301330
@server.process_message({

test/store_test.rb

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -168,73 +168,6 @@ def test_cache
168168
assert_equal(2, counter)
169169
end
170170

171-
def test_push_edits
172-
uri = URI("file:///foo/bar.rb")
173-
@store.set(uri: uri, source: +"def bar; end", version: 1, language_id: :ruby)
174-
175-
# Write puts 'a' in incremental edits
176-
@store.push_edits(
177-
uri: uri,
178-
edits: [{ range: { start: { line: 0, character: 8 }, end: { line: 0, character: 8 } }, text: " " }],
179-
version: 2,
180-
)
181-
@store.push_edits(
182-
uri: uri,
183-
edits: [{ range: { start: { line: 0, character: 9 }, end: { line: 0, character: 9 } }, text: "p" }],
184-
version: 3,
185-
)
186-
@store.push_edits(
187-
uri: uri,
188-
edits: [{ range: { start: { line: 0, character: 10 }, end: { line: 0, character: 10 } }, text: "u" }],
189-
version: 4,
190-
)
191-
@store.push_edits(
192-
uri: uri,
193-
edits: [{ range: { start: { line: 0, character: 11 }, end: { line: 0, character: 11 } }, text: "t" }],
194-
version: 5,
195-
)
196-
@store.push_edits(
197-
uri: uri,
198-
edits: [{ range: { start: { line: 0, character: 12 }, end: { line: 0, character: 12 } }, text: "s" }],
199-
version: 6,
200-
)
201-
@store.push_edits(
202-
uri: uri,
203-
edits: [{ range: { start: { line: 0, character: 13 }, end: { line: 0, character: 13 } }, text: " " }],
204-
version: 7,
205-
)
206-
@store.push_edits(
207-
uri: uri,
208-
edits: [{ range: { start: { line: 0, character: 14 }, end: { line: 0, character: 14 } }, text: "'" }],
209-
version: 8,
210-
)
211-
@store.push_edits(
212-
uri: uri,
213-
edits: [{ range: { start: { line: 0, character: 15 }, end: { line: 0, character: 15 } }, text: "a" }],
214-
version: 9,
215-
)
216-
@store.push_edits(
217-
uri: uri,
218-
edits: [{ range: { start: { line: 0, character: 16 }, end: { line: 0, character: 16 } }, text: "'" }],
219-
version: 10,
220-
)
221-
@store.push_edits(
222-
uri: uri,
223-
edits: [{ range: { start: { line: 0, character: 17 }, end: { line: 0, character: 17 } }, text: ";" }],
224-
version: 11,
225-
)
226-
227-
assert_equal(
228-
RubyLsp::RubyDocument.new(
229-
source: "def bar; puts 'a'; end",
230-
version: 1,
231-
uri: uri,
232-
global_state: @global_state,
233-
),
234-
@store.get(uri),
235-
)
236-
end
237-
238171
def test_raises_non_existing_document_error_on_unknown_unsaved_files
239172
assert_raises(RubyLsp::Store::NonExistingDocumentError) do
240173
@store.get(URI("untitled:Untitled-1"))

0 commit comments

Comments
 (0)