Skip to content

Commit bbef0ed

Browse files
authored
Merge pull request #1024 from micahscopes/additional-lsp-diagnostics
additional LSP diagnostics + fixups
2 parents 89c9961 + 065ee22 commit bbef0ed

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

crates/language-server/src/functionality/diagnostics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use hir::{
1212
analysis_pass::AnalysisPassManager, diagnostics::DiagnosticVoucher, lower::map_file_to_mod,
1313
ParsingPass,
1414
};
15-
use hir_analysis::name_resolution::{
16-
DefConflictAnalysisPass, ImportAnalysisPass, PathAnalysisPass,
15+
use hir_analysis::{
16+
name_resolution::{DefConflictAnalysisPass, ImportAnalysisPass, PathAnalysisPass},
17+
ty::{BodyAnalysisPass, FuncAnalysisPass, ImplTraitAnalysisPass, TraitAnalysisPass},
1718
};
1819
use url::Url;
1920

@@ -130,8 +131,10 @@ fn initialize_analysis_pass(db: &LanguageServerDatabase) -> AnalysisPassManager<
130131
pass_manager.add_module_pass(Box::new(DefConflictAnalysisPass::new(db)));
131132
pass_manager.add_module_pass(Box::new(ImportAnalysisPass::new(db)));
132133
pass_manager.add_module_pass(Box::new(PathAnalysisPass::new(db)));
133-
// pass_manager.add_module_pass(Box::new(FuncAnalysisPass::new(db)));
134-
// pass_manager.add_module_pass(Box::new(TraitAnalysisPass::new(db)));
134+
pass_manager.add_module_pass(Box::new(TraitAnalysisPass::new(db)));
135+
pass_manager.add_module_pass(Box::new(ImplTraitAnalysisPass::new(db)));
136+
pass_manager.add_module_pass(Box::new(FuncAnalysisPass::new(db)));
137+
pass_manager.add_module_pass(Box::new(BodyAnalysisPass::new(db)));
135138

136139
pass_manager
137140
}

crates/language-server/src/server.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,18 @@ use async_lsp::ClientSocket;
1313
use async_std::stream::StreamExt;
1414
use futures_batch::ChunksTimeoutStreamExt;
1515
// use serde_json::Value;
16-
use tracing::info;
1716
use tracing::instrument::WithSubscriber;
17+
use tracing::{info, warn};
1818

1919
use crate::backend::Backend;
2020
use crate::functionality::{goto, handlers};
21+
use async_lsp::lsp_types::request::Initialize;
2122
use async_lsp::router::Router;
22-
use async_lsp::{
23-
lsp_types::request::Initialize,
24-
// steer::{FirstComeFirstServe, LspSteer},
25-
// util::BoxLspService,
26-
// ResponseError,
27-
};
2823

2924
pub(crate) fn setup(
3025
client: ClientSocket,
3126
name: String,
32-
// ) -> LspActorService<Backend> {
3327
) -> WithFallbackService<LspActorService<Backend>, Router<()>> {
34-
// BoxLspService<Value, ResponseError> {
3528
info!("Setting up server");
3629
let client_for_actor = client.clone();
3730
let client_for_logging = client.clone();
@@ -60,12 +53,12 @@ pub(crate) fn setup(
6053

6154
let mut streaming_router = Router::new(());
6255
setup_streams(client.clone(), &mut streaming_router);
56+
setup_unhandled(&mut streaming_router);
6357

64-
// lsp_actor_service
6558
WithFallbackService::new(lsp_actor_service, streaming_router)
6659
}
6760

68-
pub fn setup_streams(client: ClientSocket, router: &mut Router<()>) {
61+
fn setup_streams(client: ClientSocket, router: &mut Router<()>) {
6962
info!("setting up streams");
7063

7164
let mut diagnostics_stream = router
@@ -83,3 +76,15 @@ pub fn setup_streams(client: ClientSocket, router: &mut Router<()>) {
8376
.with_current_subscriber(),
8477
);
8578
}
79+
80+
fn setup_unhandled(router: &mut Router<()>) {
81+
router
82+
.unhandled_notification(|_, params| {
83+
warn!("Unhandled notification: {:?}", params);
84+
std::ops::ControlFlow::Continue(())
85+
})
86+
.unhandled_event(|_, params| {
87+
warn!("Unhandled event: {:?}", params);
88+
std::ops::ControlFlow::Continue(())
89+
});
90+
}

crates/language-server/src/util.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use common::{
55
};
66
use fxhash::FxHashMap;
77
use hir::{hir_def::scope_graph::ScopeId, span::LazySpan, SpannedHirDb};
8-
use tracing::error;
8+
use tracing::{error, warn};
99
use url::Url;
1010

1111
pub fn calculate_line_offsets(text: &str) -> Vec<usize> {
@@ -83,14 +83,26 @@ pub fn diag_to_lsp(
8383
) -> FxHashMap<async_lsp::lsp_types::Url, Vec<async_lsp::lsp_types::Diagnostic>> {
8484
let mut result =
8585
FxHashMap::<async_lsp::lsp_types::Url, Vec<async_lsp::lsp_types::Diagnostic>>::default();
86-
diag.sub_diagnostics.into_iter().for_each(|sub| {
87-
let uri = sub.span.as_ref().unwrap().file.abs_path(db);
88-
let lsp_range = to_lsp_range_from_span(sub.span.unwrap(), db);
8986

90-
// todo: generalize this to handle other kinds of URLs besides file URLs
91-
let uri = Url::from_file_path(uri).unwrap();
87+
diag.sub_diagnostics.into_iter().for_each(|sub| {
88+
let span = match sub.span {
89+
Some(span) => span,
90+
None => {
91+
warn!("Diagnostic span is missing");
92+
return;
93+
}
94+
};
95+
96+
let uri = span.file.abs_path(db);
97+
let uri = match Url::from_file_path(uri) {
98+
Ok(uri) => uri,
99+
Err(()) => {
100+
error!("Failed to convert path to URL");
101+
return;
102+
}
103+
};
92104

93-
match lsp_range {
105+
match to_lsp_range_from_span(span, db) {
94106
Ok(range) => {
95107
let diags = result.entry(uri).or_insert_with(Vec::new);
96108
diags.push(async_lsp::lsp_types::Diagnostic {
@@ -102,14 +114,15 @@ pub fn diag_to_lsp(
102114
related_information: None,
103115
tags: None,
104116
code_description: None,
105-
data: None, // for code actions
117+
data: None,
106118
});
107119
}
108-
Err(_) => {
109-
error!("Failed to convert span to range");
120+
Err(e) => {
121+
error!("Error getting diagnostic: {} (ignoring diagnostic)", e);
110122
}
111123
}
112124
});
125+
113126
result
114127
}
115128

0 commit comments

Comments
 (0)