Skip to content

Commit b838eba

Browse files
author
jordanbreen28
committed
(CAT-1595) - Remove diagnostic on textDoucment onDidClose
Prior to this commit, diagnostics (or "problems") in the vscode problems pane would not be removed on the closing of a file. This is bad practice, and quickly leads to a polluted problems pane with linting issues etc showing for all files despite being open or not. This commit aims to fix this issue, by calling publishDiagnostics in the vscode API on the close of a text document with an empty array of diagnostics as per the vscode documentation. This has been verified as working in local vscode setup.
1 parent 0e156e2 commit b838eba

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

lib/puppet-languageserver/global_queues/validation_queue.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@ def execute_job(job_object)
3434
super(job_object)
3535
session_state = session_state_from_connection_id(job_object.connection_id)
3636
document_store = session_state.nil? ? nil : session_state.documents
37-
raise "Document store is not available for connection id #{job_object.connection_id}" if document_store.nil?
38-
37+
raise "Document store is not available for connection id #{job_object.connection_id}" unless document_store
38+
39+
# Check if the document still exists
40+
doc = document_store.get_document(job_object.file_uri)
41+
unless doc
42+
# Send an empty diagnostics message to clear the diagnostics
43+
send_remove_diagnostic(job_object.connection_id, job_object.file_uri, nil)
44+
PuppetLanguageServer.log_message(:debug, "#{self.class.name}: Ignoring #{job_object.file_uri} as it is has been removed.")
45+
return
46+
end
47+
3948
# Check if the document is the latest version
4049
content = document_store.document_content(job_object.file_uri, job_object.doc_version)
41-
if content.nil?
42-
PuppetLanguageServer.log_message(:debug, "#{self.class.name}: Ignoring #{job_object.file_uri} as it is not the latest version or has been removed")
50+
unless content
51+
PuppetLanguageServer.log_message(:debug, "#{self.class.name}: Ignoring #{job_object.file_uri} as it is not the latest version.")
4352
return
4453
end
4554

@@ -88,6 +97,15 @@ def send_diagnostics(connection_id, file_uri, diagnostics)
8897
::PuppetEditorServices::Protocol::JsonRPCMessages.new_notification('textDocument/publishDiagnostics', 'uri' => file_uri, 'diagnostics' => diagnostics)
8998
)
9099
end
100+
101+
def send_remove_diagnostic(connection_id, file_uri, diagnostics)
102+
connection = PuppetEditorServices::Server.current_server.connection(connection_id)
103+
return if connection.nil?
104+
105+
connection.protocol.encode_and_send(
106+
::PuppetEditorServices::Protocol::JsonRPCMessages.new_notification('textDocument/publishDiagnostics', 'uri' => file_uri, 'diagnostics' => [])
107+
)
108+
end
91109
end
92110
end
93111
end

lib/puppet-languageserver/message_handler.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,12 @@ def notification_textdocument_didopen(client_handler_id, json_rpc_message)
328328
enqueue_validation(file_uri, doc_version, client_handler_id)
329329
end
330330

331-
def notification_textdocument_didclose(_, json_rpc_message)
331+
def notification_textdocument_didclose(client_handler_id, json_rpc_message)
332332
PuppetLanguageServer.log_message(:info, 'Received textDocument/didClose notification.')
333333
file_uri = json_rpc_message.params['textDocument']['uri']
334+
doc_version = json_rpc_message.params['textDocument']['version']
334335
documents.remove_document(file_uri)
336+
enqueue_validation(file_uri, doc_version, client_handler_id)
335337
end
336338

337339
def notification_textdocument_didchange(client_handler_id, json_rpc_message)

lib/puppet-languageserver/session_state/document_store.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ def document_content(uri, doc_version = nil)
8686
doc.nil? ? nil : doc.content.clone
8787
end
8888

89+
def get_document(uri)
90+
doc = @documents[uri]
91+
doc.nil? ? nil : doc.content.clone
92+
end
93+
8994
def document_tokens(uri, doc_version = nil)
9095
@doc_mutex.synchronize do
9196
return nil if @documents[uri].nil?

spec/languageserver/unit/puppet-languageserver/global_queues/validation_queue_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ def job(file_uri, document_version, connection_id, job_options = {})
176176
allow(PuppetLanguageServer::Puppetfile::ValidationProvider).to receive(:validate).and_raise("PuppetLanguageServer::Puppetfile::ValidationProvider.validate mock should not be called")
177177
end
178178

179-
it 'should not send validation results for documents that do not exist' do
180-
expect(subject).to_not receive(:send_diagnostics)
179+
it 'should send empty validation results for documents that do not exist' do
180+
expect(subject).to receive(:send_diagnostics).with(connection_id, VALIDATE_MISSING_FILENAME, [])
181181

182182
subject.execute(VALIDATE_MISSING_FILENAME, 1, connection_id)
183183
end

0 commit comments

Comments
 (0)