Skip to content

Commit

Permalink
Group diagnostics & track file not removed from cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
antaalt committed Oct 26, 2024
1 parent 41358b7 commit baf2e37
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
66 changes: 42 additions & 24 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use lsp_types::{
CompletionOptionsCompletionItem, CompletionParams, CompletionResponse, ConfigurationParams,
DidChangeConfigurationParams, DidChangeTextDocumentParams, DidCloseTextDocumentParams,
DidOpenTextDocumentParams, DidSaveTextDocumentParams, DocumentDiagnosticParams,
DocumentDiagnosticReport, DocumentDiagnosticReportResult, FullDocumentDiagnosticReport,
GotoDefinitionParams, HoverParams, HoverProviderCapability, MessageType,
RelatedFullDocumentDiagnosticReport, ShowMessageParams, SignatureHelpOptions,
DocumentDiagnosticReport, DocumentDiagnosticReportKind, DocumentDiagnosticReportResult,
FullDocumentDiagnosticReport, GotoDefinitionParams, HoverParams, HoverProviderCapability,
MessageType, RelatedFullDocumentDiagnosticReport, ShowMessageParams, SignatureHelpOptions,
SignatureHelpParams, TextDocumentSyncKind, Url, WorkDoneProgressOptions,
};
use lsp_types::{InitializeParams, ServerCapabilities};
Expand Down Expand Up @@ -241,26 +241,43 @@ impl ServerLanguage {
match self.get_watched_file(&uri) {
Some(cached_file) => {
match self.recolt_diagnostic(&uri, Rc::clone(&cached_file)) {
Ok(diagnostics) => {
for diagnostic in diagnostics {
if diagnostic.0 == uri {
self.send_response::<DocumentDiagnosticRequest>(
req.id.clone(),
DocumentDiagnosticReportResult::Report(
DocumentDiagnosticReport::Full(
RelatedFullDocumentDiagnosticReport {
related_documents: None, // TODO: data of other files.
full_document_diagnostic_report:
FullDocumentDiagnosticReport {
result_id: Some(req.id.to_string()),
items: diagnostic.1,
},
},
Ok(mut diagnostics) => {
let main_diagnostic = match diagnostics.remove(&uri) {
Some(diag) => diag,
None => vec![],
};
self.send_response::<DocumentDiagnosticRequest>(
req.id.clone(),
DocumentDiagnosticReportResult::Report(
DocumentDiagnosticReport::Full(
RelatedFullDocumentDiagnosticReport {
related_documents: Some(
diagnostics
.into_iter()
.map(|diagnostic| {
(
diagnostic.0,
DocumentDiagnosticReportKind::Full(
FullDocumentDiagnosticReport {
result_id: Some(
req.id.to_string(),
),
items: diagnostic.1,
},
),
)
})
.collect(),
),
),
)
}
}
full_document_diagnostic_report:
FullDocumentDiagnosticReport {
result_id: Some(req.id.to_string()),
items: main_diagnostic,
},
},
),
),
)
}
// Send empty report.
Err(error) => self.send_response_error(
Expand Down Expand Up @@ -688,10 +705,11 @@ impl ServerLanguage {
// Remove AST
let file_path = Self::to_file_path(&uri);
let lang = RefCell::borrow(rc).shading_language;
let count = Rc::strong_count(&rc) == 1;
let is_last_ref = Rc::strong_count(rc) == 1;
debug!("Removing watched file {} with ref count {}", file_path.display(), Rc::strong_count(rc));

// Check if deps depends on it && if its open for edit
if count && !is_open_in_editor {
if is_last_ref && !is_open_in_editor {
match self.get_symbol_provider_mut(lang).remove_ast(&file_path) {
Ok(_) => {}
Err(err) => self.send_notification_error(format!(
Expand Down
22 changes: 12 additions & 10 deletions src/server/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,14 @@ impl ServerLanguage {
}
// Clear diagnostic if no errors.
if diagnostics.get(&uri).is_none() {
info!(
"Clearing diagnostic for main file {} (diags:{:?})",
uri, diagnostics
);
info!("Clearing diagnostic for main file {}", uri);
diagnostics.insert(uri.clone(), vec![]);
}
// Add empty diagnostics to dependencies without errors to clear them.
dependencies.visit_dependencies(&mut |dep| {
let uri = Url::from_file_path(&dep).unwrap();
if diagnostics.get(&uri).is_none() {
info!(
"Clearing diagnostic for deps file {} (diags:{:?})",
uri, diagnostics
);
info!("Clearing diagnostic for deps file {}", uri);
diagnostics.insert(uri, vec![]);
}
});
Expand Down Expand Up @@ -118,7 +112,10 @@ impl ServerLanguage {
debug!("Removed deps: {:?}", removed_deps);
for removed_dep in removed_deps {
let url = Url::from_file_path(&removed_dep).unwrap();
// File might have been removed as dependent on another file...
let mut cached_file_mut = RefCell::borrow_mut(&cached_file);
// Remove ref in deps.
cached_file_mut.dependencies.remove(&removed_dep);
// File might have been removed already as dependent on another file...
match self.watched_files.get(&url) {
Some(_) => self.remove_watched_file(&url, false),
None => {}
Expand Down Expand Up @@ -170,9 +167,14 @@ impl ServerLanguage {
if self.config.validate {
match self.recolt_diagnostic(uri, cached_file) {
Ok(diagnostics) => {
info!(
"Publishing diagnostic for file {} ({} diags)",
uri.path(),
diagnostics.len()
);
for diagnostic in diagnostics {
let publish_diagnostics_params = PublishDiagnosticsParams {
uri: diagnostic.0.clone(),
uri: diagnostic.0,
diagnostics: diagnostic.1,
version: version,
};
Expand Down

0 comments on commit baf2e37

Please sign in to comment.