Skip to content

Commit

Permalink
LS: Rewrite from tower_lsp to lsp_server (#6431)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Magiera <piotrmagiera150@gmail.com>
Co-authored-by: Marek Kaput <marek.kaput@swmansion.com>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent 737cc88 commit ed19125
Show file tree
Hide file tree
Showing 59 changed files with 2,205 additions and 1,106 deletions.
131 changes: 57 additions & 74 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# Don't correct the shorthand for ByteArray.
ba = "ba"
compilability = "compilability"
# jod_thread crate in LS
jod = "jod"

[files]
extend-exclude = [
Expand Down
4 changes: 3 additions & 1 deletion crates/bin/cairo-language-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fn main() {
use std::process::ExitCode;

fn main() -> ExitCode {
cairo_lang_language_server::start()
}
10 changes: 8 additions & 2 deletions crates/cairo-lang-language-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,27 @@ cairo-lang-starknet = { path = "../cairo-lang-starknet", version = "~2.8.4" }
cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.8.4" }
cairo-lang-test-plugin = { path = "../cairo-lang-test-plugin", version = "~2.8.4" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.8.4" }
crossbeam = "0.8.4"
indent.workspace = true
indoc.workspace = true
itertools.workspace = true
jod-thread = "0.1.2"
lsp-server = "0.7.7"
lsp-types = "=0.95.0"
rustc-hash = "1.1.0"
salsa.workspace = true
scarb-metadata = "1.12"
serde = { workspace = true, default-features = true }
serde_json.workspace = true
smol_str.workspace = true
tempfile = "3"
tokio.workspace = true
tower-lsp = "0.20.0"
tracing = "0.1"
tracing-chrome = "0.7.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[target.'cfg(target_vendor = "apple")'.dependencies]
libc = "0.2.155"

[dev-dependencies]
assert_fs = "1.1"
cairo-lang-language-server = { path = ".", features = ["testing"] }
Expand Down
58 changes: 37 additions & 21 deletions crates/cairo-lang-language-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ use std::collections::VecDeque;
use std::path::PathBuf;

use anyhow::Context;
use lsp_server::ErrorCode;
use lsp_types::request::WorkspaceConfiguration;
use lsp_types::{ClientCapabilities, ConfigurationItem, ConfigurationParams};
use serde_json::Value;
use tower_lsp::Client;
use tower_lsp::lsp_types::{ClientCapabilities, ConfigurationItem};
use tracing::{debug, error, warn};

use crate::lsp::capabilities::client::ClientCapabilitiesExt;
use crate::lsp::result::{LSPResult, LSPResultEx};
use crate::server::client::{Notifier, Requester};
use crate::server::schedule::Task;
use crate::state::State;

// TODO(mkaput): Write a macro that will auto-generate this struct and the `reload` logic.
// TODO(mkaput): Write a test that checks that fields in this struct are sorted alphabetically.
Expand Down Expand Up @@ -39,13 +44,18 @@ pub struct Config {
impl Config {
/// Reloads the configuration from the language client.
#[tracing::instrument(name = "reload_config", level = "trace", skip_all)]
pub async fn reload(&mut self, client: &Client, client_capabilities: &ClientCapabilities) {
pub fn reload(
&mut self,
requester: &mut Requester<'_>,
client_capabilities: &ClientCapabilities,
on_reloaded: fn(&mut State, &Notifier),
) -> LSPResult<()> {
if !client_capabilities.workspace_configuration_support() {
warn!(
"client does not support `workspace/configuration` requests, config will not be \
reloaded"
);
return;
return Ok(());
}

let items = vec![
Expand All @@ -56,34 +66,40 @@ impl Config {
},
];
let expected_len = items.len();
if let Ok(response) = client
.configuration(items)
.await
.context("failed to query language client for configuration items")
.inspect_err(|e| warn!("{e:?}"))
{

let handler = move |response: Vec<Value>| {
let response_len = response.len();
if response_len != expected_len {
error!(
"server returned unexpected number of configuration items, expected: \
{expected_len}, got: {response_len}"
);
return;
return Task::nothing();
}

// This conversion is O(1), and makes popping from front also O(1).
let mut response = VecDeque::from(response);

self.unmanaged_core_path = response
.pop_front()
.as_ref()
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(Into::into);
self.trace_macro_diagnostics =
response.pop_front().as_ref().and_then(Value::as_bool).unwrap_or_default();
Task::local(move |state, notifier, _, _| {
state.config.unmanaged_core_path = response
.pop_front()
.as_ref()
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(Into::into);
state.config.trace_macro_diagnostics =
response.pop_front().as_ref().and_then(Value::as_bool).unwrap_or_default();

debug!("reloaded configuration: {self:#?}");
}
debug!("reloaded configuration: {:#?}", state.config);

on_reloaded(state, &notifier);
})
};

requester
.request::<WorkspaceConfiguration>(ConfigurationParams { items }, handler)
.context("failed to query language client for configuration items")
.with_failure_code(ErrorCode::RequestFailed)
.inspect_err(|e| warn!("{e:?}"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cairo_lang_semantic::resolve::Resolver;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{SyntaxNode, TypedStablePtr, TypedSyntaxNode, ast};
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::{CodeAction, CodeActionKind, Range, TextEdit, Url, WorkspaceEdit};
use lsp_types::{CodeAction, CodeActionKind, Range, TextEdit, Url, WorkspaceEdit};
use tracing::debug;

use crate::ide::utils::find_methods_for_type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cairo_lang_syntax::node::SyntaxNode;
use cairo_lang_syntax::node::kind::SyntaxKind;
use tower_lsp::lsp_types::{CodeAction, Command};
use lsp_types::{CodeAction, Command};

use crate::lang::db::{AnalysisDatabase, LsSyntaxGroup};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_lang_syntax::node::SyntaxNode;
use tower_lsp::lsp_types::{
use lsp_types::{
CodeAction, CodeActionOrCommand, CodeActionParams, CodeActionResponse, Diagnostic,
NumberOrString,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use cairo_lang_syntax::node::SyntaxNode;
use cairo_lang_utils::Upcast;
use tower_lsp::lsp_types::{CodeAction, Diagnostic, TextEdit, Url, WorkspaceEdit};
use lsp_types::{CodeAction, Diagnostic, TextEdit, Url, WorkspaceEdit};

use crate::lang::db::AnalysisDatabase;

Expand Down
Loading

0 comments on commit ed19125

Please sign in to comment.