Skip to content

Commit

Permalink
test(lsp): adds functionality for testing language server
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Jan 16, 2021
1 parent f50e363 commit 7119fe0
Show file tree
Hide file tree
Showing 16 changed files with 813 additions and 567 deletions.
4 changes: 2 additions & 2 deletions crates/mun_language_server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ anyhow = "1.0"
thiserror = "1.0"
salsa = "0.15.0"
hir = { version = "=0.2.0", path="../mun_hir", package="mun_hir" }
rayon = "1.3"
num_cpus = "1.13.0"
threadpool="1.8.1"
vfs = { path = "../mun_vfs", package="mun_vfs" }
project = { path = "../mun_project", package="mun_project" }
mun_target = { version = "=0.2.0", path = "../mun_target" }
Expand All @@ -39,3 +38,4 @@ paths = {path="../mun_paths", package="mun_paths"}

[dev-dependencies]
tempdir = "0.3.7"
mun_test = { path = "../mun_test"}
5 changes: 5 additions & 0 deletions crates/mun_language_server/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl Analysis {
db: self.db.snapshot(),
}
}

/// Requests any outstanding snapshot to cancel computations.
pub fn request_cancellation(&mut self) {
self.db.request_cancelation();
}
}

/// The `AnalysisSnapshot` is a snapshot of the state of the source, it enables querying for
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_language_server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::project_manifest::ProjectManifest;
use paths::AbsPathBuf;
use project::ProjectManifest;

/// The configuration used by the language server.
#[derive(Debug, Clone)]
Expand Down
7 changes: 6 additions & 1 deletion crates/mun_language_server/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::cancelation::Canceled;
use hir::{HirDatabase, Upcast};
use mun_target::spec::Target;
use salsa::{Database, Snapshot};
use salsa::{Database, Durability, Snapshot};
use std::panic;

/// The `AnalysisDatabase` provides the database for all analyses. A database is given input and
Expand Down Expand Up @@ -38,6 +38,11 @@ impl AnalysisDatabase {

db
}

/// Triggers a simple write on the database which will cancell all outstanding snapshots.
pub fn request_cancelation(&mut self) {
self.salsa_runtime_mut().synthetic_write(Durability::LOW);
}
}

impl salsa::Database for AnalysisDatabase {
Expand Down
33 changes: 17 additions & 16 deletions crates/mun_language_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
use std::convert::TryFrom;

use serde::{de::DeserializeOwned, Serialize};

pub use config::{Config, FilesWatcher};
pub use main_loop::main_loop;
use paths::AbsPathBuf;
use project::ProjectManifest;
pub(crate) use state::LanguageServerState;

mod analysis;
mod cancelation;
mod capabilities;
Expand All @@ -6,34 +16,25 @@ mod config;
mod conversion;
mod db;
mod diagnostics;
mod dispatcher;
mod main_loop;
mod project_manifest;
mod workspace;

pub use config::Config;
pub use main_loop::main_loop;

use crate::{config::FilesWatcher, project_manifest::ProjectManifest};
use paths::AbsPathBuf;
use serde::{de::DeserializeOwned, Serialize};
use std::convert::TryFrom;

pub type Result<T> = anyhow::Result<T>;
mod state;

/// Deserializes a `T` from a json value.
pub fn from_json<T: DeserializeOwned>(what: &'static str, json: serde_json::Value) -> Result<T> {
pub fn from_json<T: DeserializeOwned>(
what: &'static str,
json: serde_json::Value,
) -> anyhow::Result<T> {
T::deserialize(&json)
.map_err(|e| anyhow::anyhow!("could not deserialize {}: {}: {}", what, e, json))
}

/// Converts the `T` to a json value
pub fn to_json<T: Serialize>(value: T) -> Result<serde_json::Value> {
pub fn to_json<T: Serialize>(value: T) -> anyhow::Result<serde_json::Value> {
serde_json::to_value(value).map_err(|e| anyhow::anyhow!("could not serialize to json: {}", e))
}

/// Main entry point for the language server
pub fn run_server() -> Result<()> {
pub fn run_server() -> anyhow::Result<()> {
log::info!("language server started");

// Setup IO connections
Expand Down
Loading

0 comments on commit 7119fe0

Please sign in to comment.