Skip to content

Commit

Permalink
more language server reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
micahscopes committed Mar 28, 2024
1 parent eb62371 commit f4ad0bf
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 112 deletions.
4 changes: 2 additions & 2 deletions crates/language-server/src/backend/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use hir_analysis::{
};
use salsa::{ParallelDatabase, Snapshot};

use crate::goto::Cursor;
use crate::functionality::goto::Cursor;

#[salsa::jar(db = LanguageServerDb)]
pub struct Jar(crate::diagnostics::file_line_starts);
pub struct Jar(crate::functionality::diagnostics::file_line_starts);

pub trait LanguageServerDb:
salsa::DbWithJar<Jar> + HirAnalysisDb + HirDb + LowerHirDb + SpannedHirDb + InputDb
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pub(crate) mod db;
mod handlers;
mod helpers;
pub(crate) mod streams;
pub(crate) mod workspace;
use db::LanguageServerDatabase;
use std::sync::Arc;
Expand All @@ -11,10 +8,10 @@ use workspace::Workspace;
use tower_lsp::Client;

pub struct Backend {
client: Client,
db: LanguageServerDatabase,
workspace: Arc<RwLock<Workspace>>,
workers: tokio::runtime::Runtime,
pub(super) client: Client,
pub(super) db: LanguageServerDatabase,
pub(super) workspace: Arc<RwLock<Workspace>>,
pub(super) workers: tokio::runtime::Runtime,
}

impl Backend {
Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::Range;
use std::{ops::Range, sync::Arc};

use camino::Utf8Path;
use clap::Error;
Expand All @@ -12,6 +12,9 @@ use common::{
use fxhash::FxHashMap;
use hir::{diagnostics::DiagnosticVoucher, LowerHirDb};
use salsa::Snapshot;
use tokio::sync::RwLock;
use tower_lsp::Client;
use tracing::info;

use crate::{
backend::db::{LanguageServerDatabase, LanguageServerDb},
Expand Down Expand Up @@ -165,3 +168,23 @@ pub fn get_diagnostics(

Ok(result)
}

pub(super) async fn diagnostics_workload(
client: Client,
workspace: Arc<RwLock<Workspace>>,
db: Snapshot<LanguageServerDatabase>,
url: lsp_types::Url,
) {
info!("handling diagnostics for {:?}", url);
let workspace = &workspace.read().await;
let diagnostics = get_diagnostics(&db, workspace, url.clone());

let client = client.clone();
let diagnostics = diagnostics
.unwrap()
.into_iter()
.map(|(uri, diags)| async { client.publish_diagnostics(uri, diags, None).await })
.collect::<Vec<_>>();

futures::future::join_all(diagnostics).await;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ use fxhash::FxHashMap;
use hir::{
hir_def::{scope_graph::ScopeId, ItemKind, PathId, TopLevelMod},
visitor::{prelude::LazyPathSpan, Visitor, VisitorCtxt},
HirDb,
HirDb, LowerHirDb, SpannedHirDb,
};
use hir_analysis::{
name_resolution::{EarlyResolvedPath, NameRes},
HirAnalysisDb,
};
use hir_analysis::{name_resolution::EarlyResolvedPath, HirAnalysisDb};
use salsa::Snapshot;

use crate::backend::db::{LanguageServerDatabase, LanguageServerDb};
use crate::{
backend::db::{LanguageServerDatabase, LanguageServerDb},
util::{to_lsp_location_from_scope, to_offset_from_position},
};
use common::diagnostics::Span;
use hir::span::LazySpan;

Expand Down Expand Up @@ -91,6 +97,71 @@ pub fn goto_enclosing_path(
Some(resolved_path)
}

use lsp_types::{GotoDefinitionParams, GotoDefinitionResponse};

use crate::backend::workspace::{IngotFileContext, Workspace};
// use crate::diagnostics::get_diagnostics;

use std::sync::Arc;
use tokio::sync::RwLock;

use tower_lsp::jsonrpc::Result;

pub async fn goto_helper(
db: Snapshot<LanguageServerDatabase>,
workspace: Arc<RwLock<Workspace>>,
params: GotoDefinitionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let workspace = workspace.read().await;
// Convert the position to an offset in the file
let params = params.text_document_position_params;
let file_text = std::fs::read_to_string(params.text_document.uri.path()).ok();
let cursor: Cursor = to_offset_from_position(params.position, file_text.unwrap().as_str());

// Get the module and the goto info
let file_path = params.text_document.uri.path();
let top_mod = workspace
.top_mod_from_file_path(db.as_lower_hir_db(), file_path)
.unwrap();
let goto_info = goto_enclosing_path(&db, top_mod, cursor);

// Convert the goto info to a Location
let scopes = match goto_info {
Some(EarlyResolvedPath::Full(bucket)) => {
bucket.iter().map(NameRes::scope).collect::<Vec<_>>()
}
Some(EarlyResolvedPath::Partial {
res,
unresolved_from: _,
}) => {
vec![res.scope()]
}
None => return Ok(None),
};

let locations = scopes
.iter()
.filter_map(|scope| *scope)
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
.collect::<Vec<_>>();

let _errors = scopes
.iter()
.filter_map(|scope| *scope)
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
.filter_map(std::result::Result::err)
.map(|err| err.to_string())
.collect::<Vec<_>>()
.join("\n");

Ok(Some(lsp_types::GotoDefinitionResponse::Array(
locations
.into_iter()
.filter_map(std::result::Result::ok)
.collect(),
)))
}

#[cfg(test)]
mod tests {
use crate::backend::workspace::{IngotFileContext, Workspace};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
use super::helpers::{goto_helper, hover_helper};
use super::goto::goto_helper;
use super::hover::hover_helper;
use crate::backend::Backend;

use crate::backend::workspace::SyncableIngotFileContext;
use crate::functionality::diagnostics::diagnostics_workload;

use common::InputDb;
use fxhash::FxHashSet;

use lsp_types::TextDocumentItem;
use salsa::{ParallelDatabase, Snapshot};
use salsa::ParallelDatabase;

use std::sync::Arc;
use tokio::sync::RwLock;
use super::capabilities::server_capabilities;

use crate::backend::db::LanguageServerDatabase;
use crate::capabilities::server_capabilities;

use crate::backend::workspace::{IngotFileContext, SyncableInputFile, Workspace};
use crate::diagnostics::get_diagnostics;
use crate::backend::workspace::{IngotFileContext, SyncableInputFile};
// use crate::diagnostics::get_diagnostics;

use tracing::info;

use tower_lsp::Client;

impl Backend {
pub(super) async fn handle_initialized(
&mut self,
Expand Down Expand Up @@ -161,23 +157,3 @@ impl Backend {
let _ = responder.send(Ok(response));
}
}

pub(super) async fn diagnostics_workload(
client: Client,
workspace: Arc<RwLock<Workspace>>,
db: Snapshot<LanguageServerDatabase>,
url: lsp_types::Url,
) {
info!("handling diagnostics for {:?}", url);
let workspace = &workspace.read().await;
let diagnostics = get_diagnostics(&db, workspace, url.clone());

let client = client.clone();
let diagnostics = diagnostics
.unwrap()
.into_iter()
.map(|(uri, diags)| async { client.publish_diagnostics(uri, diags, None).await })
.collect::<Vec<_>>();

futures::future::join_all(diagnostics).await;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::sync::Arc;

use common::{input::IngotKind, InputDb};
use hir::{LowerHirDb, SpannedHirDb};
use hir_analysis::{
name_resolution::{EarlyResolvedPath, NameRes},
HirAnalysisDb,
};
use hir::LowerHirDb;
use hir_analysis::{name_resolution::EarlyResolvedPath, HirAnalysisDb};
use lsp_types::Hover;
use tracing::info;

use salsa::Snapshot;
Expand All @@ -15,10 +13,11 @@ use tower_lsp::jsonrpc::Result;
use crate::{
backend::db::LanguageServerDatabase,
backend::workspace::{IngotFileContext, Workspace},
goto::{goto_enclosing_path, Cursor},
util::{to_lsp_location_from_scope, to_offset_from_position},
util::to_offset_from_position,
};

use super::goto::{goto_enclosing_path, Cursor};

pub async fn hover_helper(
db: Snapshot<LanguageServerDatabase>,
workspace: Arc<RwLock<Workspace>>,
Expand Down Expand Up @@ -101,60 +100,3 @@ pub async fn hover_helper(
};
Ok(Some(result))
}

use lsp_types::{GotoDefinitionParams, GotoDefinitionResponse, Hover};

pub async fn goto_helper(
db: Snapshot<LanguageServerDatabase>,
workspace: Arc<RwLock<Workspace>>,
params: GotoDefinitionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let workspace = workspace.read().await;
// Convert the position to an offset in the file
let params = params.text_document_position_params;
let file_text = std::fs::read_to_string(params.text_document.uri.path()).ok();
let cursor: Cursor = to_offset_from_position(params.position, file_text.unwrap().as_str());

// Get the module and the goto info
let file_path = params.text_document.uri.path();
let top_mod = workspace
.top_mod_from_file_path(db.as_lower_hir_db(), file_path)
.unwrap();
let goto_info = goto_enclosing_path(&db, top_mod, cursor);

// Convert the goto info to a Location
let scopes = match goto_info {
Some(EarlyResolvedPath::Full(bucket)) => {
bucket.iter().map(NameRes::scope).collect::<Vec<_>>()
}
Some(EarlyResolvedPath::Partial {
res,
unresolved_from: _,
}) => {
vec![res.scope()]
}
None => return Ok(None),
};

let locations = scopes
.iter()
.filter_map(|scope| *scope)
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
.collect::<Vec<_>>();

let _errors = scopes
.iter()
.filter_map(|scope| *scope)
.map(|scope| to_lsp_location_from_scope(scope, db.as_spanned_hir_db()))
.filter_map(std::result::Result::err)
.map(|err| err.to_string())
.collect::<Vec<_>>()
.join("\n");

Ok(Some(lsp_types::GotoDefinitionResponse::Array(
locations
.into_iter()
.filter_map(std::result::Result::ok)
.collect(),
)))
}
6 changes: 6 additions & 0 deletions crates/language-server/src/functionality/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod capabilities;
pub(super) mod diagnostics;
pub(super) mod goto;
pub(super) mod handlers;
pub(super) mod hover;
pub(crate) mod streams;
6 changes: 2 additions & 4 deletions crates/language-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod backend;
mod capabilities;
mod diagnostics;
mod functionality;
mod globals;
mod goto;
mod logger;
mod server;
mod util;
Expand Down Expand Up @@ -45,6 +43,6 @@ async fn main() {
_ = tower_lsp::Server::new(stdin, stdout, socket)
.serve(service) => {}
// backend
_ = backend::streams::setup_streams(&mut backend, message_receivers) => {}
_ = functionality::streams::setup_streams(&mut backend, message_receivers) => {}
}
}

0 comments on commit f4ad0bf

Please sign in to comment.