Skip to content

Commit f4ad0bf

Browse files
committed
more language server reorganization
1 parent eb62371 commit f4ad0bf

File tree

11 files changed

+125
-112
lines changed

11 files changed

+125
-112
lines changed

crates/language-server/src/backend/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use hir_analysis::{
1212
};
1313
use salsa::{ParallelDatabase, Snapshot};
1414

15-
use crate::goto::Cursor;
15+
use crate::functionality::goto::Cursor;
1616

1717
#[salsa::jar(db = LanguageServerDb)]
18-
pub struct Jar(crate::diagnostics::file_line_starts);
18+
pub struct Jar(crate::functionality::diagnostics::file_line_starts);
1919

2020
pub trait LanguageServerDb:
2121
salsa::DbWithJar<Jar> + HirAnalysisDb + HirDb + LowerHirDb + SpannedHirDb + InputDb

crates/language-server/src/backend.rs renamed to crates/language-server/src/backend/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
pub(crate) mod db;
2-
mod handlers;
3-
mod helpers;
4-
pub(crate) mod streams;
52
pub(crate) mod workspace;
63
use db::LanguageServerDatabase;
74
use std::sync::Arc;
@@ -11,10 +8,10 @@ use workspace::Workspace;
118
use tower_lsp::Client;
129

1310
pub struct Backend {
14-
client: Client,
15-
db: LanguageServerDatabase,
16-
workspace: Arc<RwLock<Workspace>>,
17-
workers: tokio::runtime::Runtime,
11+
pub(super) client: Client,
12+
pub(super) db: LanguageServerDatabase,
13+
pub(super) workspace: Arc<RwLock<Workspace>>,
14+
pub(super) workers: tokio::runtime::Runtime,
1815
}
1916

2017
impl Backend {

crates/language-server/src/config.rs

Whitespace-only changes.

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::Range;
1+
use std::{ops::Range, sync::Arc};
22

33
use camino::Utf8Path;
44
use clap::Error;
@@ -12,6 +12,9 @@ use common::{
1212
use fxhash::FxHashMap;
1313
use hir::{diagnostics::DiagnosticVoucher, LowerHirDb};
1414
use salsa::Snapshot;
15+
use tokio::sync::RwLock;
16+
use tower_lsp::Client;
17+
use tracing::info;
1518

1619
use crate::{
1720
backend::db::{LanguageServerDatabase, LanguageServerDb},
@@ -165,3 +168,23 @@ pub fn get_diagnostics(
165168

166169
Ok(result)
167170
}
171+
172+
pub(super) async fn diagnostics_workload(
173+
client: Client,
174+
workspace: Arc<RwLock<Workspace>>,
175+
db: Snapshot<LanguageServerDatabase>,
176+
url: lsp_types::Url,
177+
) {
178+
info!("handling diagnostics for {:?}", url);
179+
let workspace = &workspace.read().await;
180+
let diagnostics = get_diagnostics(&db, workspace, url.clone());
181+
182+
let client = client.clone();
183+
let diagnostics = diagnostics
184+
.unwrap()
185+
.into_iter()
186+
.map(|(uri, diags)| async { client.publish_diagnostics(uri, diags, None).await })
187+
.collect::<Vec<_>>();
188+
189+
futures::future::join_all(diagnostics).await;
190+
}

crates/language-server/src/goto.rs renamed to crates/language-server/src/functionality/goto.rs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ use fxhash::FxHashMap;
22
use hir::{
33
hir_def::{scope_graph::ScopeId, ItemKind, PathId, TopLevelMod},
44
visitor::{prelude::LazyPathSpan, Visitor, VisitorCtxt},
5-
HirDb,
5+
HirDb, LowerHirDb, SpannedHirDb,
6+
};
7+
use hir_analysis::{
8+
name_resolution::{EarlyResolvedPath, NameRes},
9+
HirAnalysisDb,
610
};
7-
use hir_analysis::{name_resolution::EarlyResolvedPath, HirAnalysisDb};
811
use salsa::Snapshot;
912

10-
use crate::backend::db::{LanguageServerDatabase, LanguageServerDb};
13+
use crate::{
14+
backend::db::{LanguageServerDatabase, LanguageServerDb},
15+
util::{to_lsp_location_from_scope, to_offset_from_position},
16+
};
1117
use common::diagnostics::Span;
1218
use hir::span::LazySpan;
1319

@@ -91,6 +97,71 @@ pub fn goto_enclosing_path(
9197
Some(resolved_path)
9298
}
9399

100+
use lsp_types::{GotoDefinitionParams, GotoDefinitionResponse};
101+
102+
use crate::backend::workspace::{IngotFileContext, Workspace};
103+
// use crate::diagnostics::get_diagnostics;
104+
105+
use std::sync::Arc;
106+
use tokio::sync::RwLock;
107+
108+
use tower_lsp::jsonrpc::Result;
109+
110+
pub async fn goto_helper(
111+
db: Snapshot<LanguageServerDatabase>,
112+
workspace: Arc<RwLock<Workspace>>,
113+
params: GotoDefinitionParams,
114+
) -> Result<Option<GotoDefinitionResponse>> {
115+
let workspace = workspace.read().await;
116+
// Convert the position to an offset in the file
117+
let params = params.text_document_position_params;
118+
let file_text = std::fs::read_to_string(params.text_document.uri.path()).ok();
119+
let cursor: Cursor = to_offset_from_position(params.position, file_text.unwrap().as_str());
120+
121+
// Get the module and the goto info
122+
let file_path = params.text_document.uri.path();
123+
let top_mod = workspace
124+
.top_mod_from_file_path(db.as_lower_hir_db(), file_path)
125+
.unwrap();
126+
let goto_info = goto_enclosing_path(&db, top_mod, cursor);
127+
128+
// Convert the goto info to a Location
129+
let scopes = match goto_info {
130+
Some(EarlyResolvedPath::Full(bucket)) => {
131+
bucket.iter().map(NameRes::scope).collect::<Vec<_>>()
132+
}
133+
Some(EarlyResolvedPath::Partial {
134+
res,
135+
unresolved_from: _,
136+
}) => {
137+
vec![res.scope()]
138+
}
139+
None => return Ok(None),
140+
};
141+
142+
let locations = scopes
143+
.iter()
144+
.filter_map(|scope| *scope)
145+
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
146+
.collect::<Vec<_>>();
147+
148+
let _errors = scopes
149+
.iter()
150+
.filter_map(|scope| *scope)
151+
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
152+
.filter_map(std::result::Result::err)
153+
.map(|err| err.to_string())
154+
.collect::<Vec<_>>()
155+
.join("\n");
156+
157+
Ok(Some(lsp_types::GotoDefinitionResponse::Array(
158+
locations
159+
.into_iter()
160+
.filter_map(std::result::Result::ok)
161+
.collect(),
162+
)))
163+
}
164+
94165
#[cfg(test)]
95166
mod tests {
96167
use crate::backend::workspace::{IngotFileContext, Workspace};

crates/language-server/src/backend/handlers.rs renamed to crates/language-server/src/functionality/handlers.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
use super::helpers::{goto_helper, hover_helper};
1+
use super::goto::goto_helper;
2+
use super::hover::hover_helper;
23
use crate::backend::Backend;
34

45
use crate::backend::workspace::SyncableIngotFileContext;
6+
use crate::functionality::diagnostics::diagnostics_workload;
57

68
use common::InputDb;
79
use fxhash::FxHashSet;
810

911
use lsp_types::TextDocumentItem;
10-
use salsa::{ParallelDatabase, Snapshot};
12+
use salsa::ParallelDatabase;
1113

12-
use std::sync::Arc;
13-
use tokio::sync::RwLock;
14+
use super::capabilities::server_capabilities;
1415

15-
use crate::backend::db::LanguageServerDatabase;
16-
use crate::capabilities::server_capabilities;
17-
18-
use crate::backend::workspace::{IngotFileContext, SyncableInputFile, Workspace};
19-
use crate::diagnostics::get_diagnostics;
16+
use crate::backend::workspace::{IngotFileContext, SyncableInputFile};
17+
// use crate::diagnostics::get_diagnostics;
2018

2119
use tracing::info;
2220

23-
use tower_lsp::Client;
24-
2521
impl Backend {
2622
pub(super) async fn handle_initialized(
2723
&mut self,
@@ -161,23 +157,3 @@ impl Backend {
161157
let _ = responder.send(Ok(response));
162158
}
163159
}
164-
165-
pub(super) async fn diagnostics_workload(
166-
client: Client,
167-
workspace: Arc<RwLock<Workspace>>,
168-
db: Snapshot<LanguageServerDatabase>,
169-
url: lsp_types::Url,
170-
) {
171-
info!("handling diagnostics for {:?}", url);
172-
let workspace = &workspace.read().await;
173-
let diagnostics = get_diagnostics(&db, workspace, url.clone());
174-
175-
let client = client.clone();
176-
let diagnostics = diagnostics
177-
.unwrap()
178-
.into_iter()
179-
.map(|(uri, diags)| async { client.publish_diagnostics(uri, diags, None).await })
180-
.collect::<Vec<_>>();
181-
182-
futures::future::join_all(diagnostics).await;
183-
}

crates/language-server/src/backend/helpers.rs renamed to crates/language-server/src/functionality/hover.rs

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use std::sync::Arc;
22

33
use common::{input::IngotKind, InputDb};
4-
use hir::{LowerHirDb, SpannedHirDb};
5-
use hir_analysis::{
6-
name_resolution::{EarlyResolvedPath, NameRes},
7-
HirAnalysisDb,
8-
};
4+
use hir::LowerHirDb;
5+
use hir_analysis::{name_resolution::EarlyResolvedPath, HirAnalysisDb};
6+
use lsp_types::Hover;
97
use tracing::info;
108

119
use salsa::Snapshot;
@@ -15,10 +13,11 @@ use tower_lsp::jsonrpc::Result;
1513
use crate::{
1614
backend::db::LanguageServerDatabase,
1715
backend::workspace::{IngotFileContext, Workspace},
18-
goto::{goto_enclosing_path, Cursor},
19-
util::{to_lsp_location_from_scope, to_offset_from_position},
16+
util::to_offset_from_position,
2017
};
2118

19+
use super::goto::{goto_enclosing_path, Cursor};
20+
2221
pub async fn hover_helper(
2322
db: Snapshot<LanguageServerDatabase>,
2423
workspace: Arc<RwLock<Workspace>>,
@@ -101,60 +100,3 @@ pub async fn hover_helper(
101100
};
102101
Ok(Some(result))
103102
}
104-
105-
use lsp_types::{GotoDefinitionParams, GotoDefinitionResponse, Hover};
106-
107-
pub async fn goto_helper(
108-
db: Snapshot<LanguageServerDatabase>,
109-
workspace: Arc<RwLock<Workspace>>,
110-
params: GotoDefinitionParams,
111-
) -> Result<Option<GotoDefinitionResponse>> {
112-
let workspace = workspace.read().await;
113-
// Convert the position to an offset in the file
114-
let params = params.text_document_position_params;
115-
let file_text = std::fs::read_to_string(params.text_document.uri.path()).ok();
116-
let cursor: Cursor = to_offset_from_position(params.position, file_text.unwrap().as_str());
117-
118-
// Get the module and the goto info
119-
let file_path = params.text_document.uri.path();
120-
let top_mod = workspace
121-
.top_mod_from_file_path(db.as_lower_hir_db(), file_path)
122-
.unwrap();
123-
let goto_info = goto_enclosing_path(&db, top_mod, cursor);
124-
125-
// Convert the goto info to a Location
126-
let scopes = match goto_info {
127-
Some(EarlyResolvedPath::Full(bucket)) => {
128-
bucket.iter().map(NameRes::scope).collect::<Vec<_>>()
129-
}
130-
Some(EarlyResolvedPath::Partial {
131-
res,
132-
unresolved_from: _,
133-
}) => {
134-
vec![res.scope()]
135-
}
136-
None => return Ok(None),
137-
};
138-
139-
let locations = scopes
140-
.iter()
141-
.filter_map(|scope| *scope)
142-
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
143-
.collect::<Vec<_>>();
144-
145-
let _errors = scopes
146-
.iter()
147-
.filter_map(|scope| *scope)
148-
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
149-
.filter_map(std::result::Result::err)
150-
.map(|err| err.to_string())
151-
.collect::<Vec<_>>()
152-
.join("\n");
153-
154-
Ok(Some(lsp_types::GotoDefinitionResponse::Array(
155-
locations
156-
.into_iter()
157-
.filter_map(std::result::Result::ok)
158-
.collect(),
159-
)))
160-
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mod capabilities;
2+
pub(super) mod diagnostics;
3+
pub(super) mod goto;
4+
pub(super) mod handlers;
5+
pub(super) mod hover;
6+
pub(crate) mod streams;

0 commit comments

Comments
 (0)