Skip to content

Commit 5d26f86

Browse files
committed
Only publish updated diagnostics
1 parent 21bb3d4 commit 5d26f86

File tree

8 files changed

+106
-36
lines changed

8 files changed

+106
-36
lines changed

lib/typeprof/core/env.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def initialize
88
@run_queue = []
99
@run_queue_set = Set[]
1010

11+
@pending_diagnostic_paths = Set[]
12+
1113
@mod_object = ModuleEntity.new([])
1214
@mod_object.inner_modules[:Object] = @mod_object
1315

@@ -179,6 +181,19 @@ def run_all
179181
@run_count += run_count
180182
end
181183

184+
def process_diagnostic_paths
185+
processed_paths = []
186+
@pending_diagnostic_paths.each do |path|
187+
processed = yield path
188+
processed_paths << path if processed
189+
end
190+
processed_paths.each {|path| @pending_diagnostic_paths.delete(path) }
191+
end
192+
193+
def add_diagnostic_path(path)
194+
@pending_diagnostic_paths << path unless @pending_diagnostic_paths.include?(path)
195+
end
196+
182197
# just for validation
183198
def get_vertexes(vtxs)
184199
@mod_object.get_vertexes(vtxs)

lib/typeprof/core/graph/box.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def run0(genv, changes)
4444
end
4545

4646
def to_s
47-
"#{ self.class.to_s.split("::").last[0] }#{ @id ||= $new_id += 1 }"
47+
"#{ self.class.to_s.split("::").last }#{ @id ||= $new_id += 1 }"
4848
end
4949

5050
alias inspect to_s

lib/typeprof/core/graph/change_set.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ def reinstall(genv)
172172
@new_boxes.clear
173173

174174
@diagnostics.each do |diag|
175+
genv.add_diagnostic_path(diag.node.lenv.path)
175176
diag.node.remove_diagnostic(diag)
176177
end
177178
@new_diagnostics.each do |diag|
179+
genv.add_diagnostic_path(diag.node.lenv.path)
178180
diag.node.add_diagnostic(diag)
179181
end
180182
@diagnostics, @new_diagnostics = @new_diagnostics, @diagnostics

lib/typeprof/core/service.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ def update_rbs_file(path, code)
138138
true
139139
end
140140

141+
def process_diagnostic_paths(&blk)
142+
@genv.process_diagnostic_paths(&blk)
143+
end
144+
141145
def diagnostics(path, &blk)
142146
@rb_text_nodes[path]&.each_diagnostic(@genv, &blk)
143147
end

lib/typeprof/lsp/messages.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def run
138138
@server.open_texts[uri] = text
139139
@server.update_file(text.path, text.string)
140140
@server.send_request("workspace/codeLens/refresh")
141-
@server.publish_diagnostics(uri)
141+
@server.publish_updated_diagnostics
142142
end
143143
end
144144

@@ -153,7 +153,7 @@ def run
153153
text.apply_changes(changes, version)
154154
@server.update_file(text.path, text.string)
155155
@server.send_request("workspace/codeLens/refresh")
156-
@server.publish_diagnostics(uri)
156+
@server.publish_updated_diagnostics
157157
end
158158
end
159159

lib/typeprof/lsp/server.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def self.start_socket(core_options)
4747
end
4848
end
4949

50-
def initialize(core_options, reader, writer, url_schema: nil, publish_all_diagnostics: false)
50+
def initialize(core_options, reader, writer, url_schema: nil)
5151
@core_options = core_options
5252
@cores = {}
5353
@reader = reader
@@ -59,7 +59,6 @@ def initialize(core_options, reader, writer, url_schema: nil, publish_all_diagno
5959
@exit = false
6060
@signature_enabled = true
6161
@url_schema = url_schema || (File::ALT_SEPARATOR != "\\" ? "file://" : "file:///")
62-
@publish_all_diagnostics = publish_all_diagnostics # TODO: implement more dedicated publish feature
6362
@diagnostic_severity = :error
6463
end
6564

@@ -238,21 +237,22 @@ def exit
238237
@exit = true
239238
end
240239

241-
def publish_diagnostics(uri)
242-
(@publish_all_diagnostics ? @open_texts : [[uri, @open_texts[uri]]]).each do |uri, text|
240+
def publish_updated_diagnostics
241+
@cores.each do |_, core|
243242
diags = []
244-
if text
245-
@cores.each do |_, core|
246-
core.diagnostics(text.path) do |diag|
247-
diags << diag.to_lsp(severity: @diagnostic_severity)
248-
end
243+
core.process_diagnostic_paths do |path|
244+
uri = path_to_uri(path)
245+
next false unless @open_texts[uri]
246+
core.diagnostics(path) do |diag|
247+
diags << diag.to_lsp(severity: @diagnostic_severity)
249248
end
249+
send_notification(
250+
"textDocument/publishDiagnostics",
251+
uri: uri,
252+
diagnostics: diags
253+
)
254+
true
250255
end
251-
send_notification(
252-
"textDocument/publishDiagnostics",
253-
uri: uri,
254-
diagnostics: diags
255-
)
256256
end
257257
end
258258
end

sig/typeprof.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module TypeProf
2121
def completion: (String, untyped, CodePosition) { (untyped) -> untyped } -> Array[untyped]
2222
def dump_declarations: (String) -> String
2323
def rename: (String, CodePosition) -> void
24+
def process_diagnostic_paths: { (String) -> bool } -> void
2425
end
2526
end
2627
end

test/lsp/lsp_test.rb

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ def foo(nnn)
9595
)
9696

9797
expect_request("workspace/codeLens/refresh") {|json| }
98-
expect_notification("textDocument/publishDiagnostics") do |json|
99-
assert_equal([], json[:diagnostics])
100-
end
10198

10299
notify(
103100
"textDocument/didClose",
@@ -120,9 +117,6 @@ def foo(nnn)
120117
)
121118

122119
expect_request("workspace/codeLens/refresh") {|json| }
123-
expect_notification("textDocument/publishDiagnostics") do |json|
124-
assert_equal([], json[:diagnostics])
125-
end
126120

127121
id = request(
128122
"textDocument/hover",
@@ -146,9 +140,6 @@ def foo(nnn)
146140
)
147141

148142
expect_request("workspace/codeLens/refresh") {|json| }
149-
expect_notification("textDocument/publishDiagnostics") do |json|
150-
assert_equal([], json[:diagnostics])
151-
end
152143

153144
id = request(
154145
"textDocument/hover",
@@ -176,17 +167,75 @@ def foo(nnn)
176167

177168
expect_request("workspace/codeLens/refresh") {|json| }
178169
expect_notification("textDocument/publishDiagnostics") do |json|
179-
assert_equal([
180-
{
181-
message: "wrong number of arguments (2 for 1)",
182-
range: {
183-
start: { line: 4, character: 0 },
184-
end: { line: 4, character: 3 },
170+
assert_equal({
171+
uri: @folder + "basic.rb",
172+
diagnostics: [
173+
{
174+
message: "wrong number of arguments (2 for 1)",
175+
range: {
176+
start: { line: 4, character: 0 },
177+
end: { line: 4, character: 3 },
178+
},
179+
severity: 1,
180+
source: "TypeProf",
181+
}
182+
],
183+
}, json)
184+
end
185+
end
186+
187+
def test_diagnostics2
188+
init("basic")
189+
190+
notify(
191+
"textDocument/didOpen",
192+
textDocument: { uri: @folder + "basic1.rb", version: 0, text: <<-END },
193+
def check(nnn)
194+
nnn
195+
end
196+
END
197+
)
198+
expect_request("workspace/codeLens/refresh") {|json| }
199+
200+
notify(
201+
"textDocument/didOpen",
202+
textDocument: { uri: @folder + "basic2.rb", version: 0, text: <<-END },
203+
check(1, 2)
204+
END
205+
)
206+
207+
expect_request("workspace/codeLens/refresh") {|json| }
208+
expect_notification("textDocument/publishDiagnostics") do |json|
209+
assert_equal({
210+
uri: @folder + "basic2.rb",
211+
diagnostics: [
212+
{
213+
message: "wrong number of arguments (2 for 1)",
214+
range: {
215+
start: { line: 0, character: 0 },
216+
end: { line: 0, character: 5 },
217+
},
218+
severity: 1,
219+
source: "TypeProf",
185220
},
186-
severity: 1,
187-
source: "TypeProf",
221+
],
222+
}, json)
223+
end
224+
225+
notify(
226+
"textDocument/didChange",
227+
textDocument: { uri: @folder + "basic1.rb", version: 1 },
228+
contentChanges: [
229+
{
230+
range: { start: { line: 0, character: 13 }, end: { line: 0, character: 13 }},
231+
text: ", mmm", # def check(nnn) => def check(nnn, mmm)
188232
}
189-
], json[:diagnostics])
233+
]
234+
)
235+
236+
expect_request("workspace/codeLens/refresh") {|json| }
237+
expect_notification("textDocument/publishDiagnostics") do |json|
238+
assert_equal({ uri: @folder + "basic2.rb", diagnostics: [] }, json)
190239
end
191240
end
192241

@@ -215,7 +264,6 @@ def test(x)
215264
)
216265

217266
expect_request("workspace/codeLens/refresh") {|json| }
218-
expect_notification("textDocument/publishDiagnostics") {|json| }
219267

220268
id = request(
221269
"textDocument/completion",

0 commit comments

Comments
 (0)