Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LS: Rewrite from tower_lsp to lsp_server #6431

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 57 additions & 73 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
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.2" }
cairo-lang-syntax = { path = "../cairo-lang-syntax", version = "~2.8.2" }
cairo-lang-test-plugin = { path = "../cairo-lang-test-plugin", version = "~2.8.2" }
cairo-lang-utils = { path = "../cairo-lang-utils", version = "~2.8.2" }
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::lsp_types::{ClientCapabilities, ConfigurationItem};
use tower_lsp::Client;
use tracing::{debug, error, warn};

use crate::lsp::capabilities::client::ClientCapabilitiesExt;
use crate::server::api::{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::{ast, SyntaxNode, TypedStablePtr, TypedSyntaxNode};
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::kind::SyntaxKind;
use cairo_lang_syntax::node::SyntaxNode;
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use cairo_lang_semantic::{ConcreteTypeId, Pattern, TypeLongId};
use cairo_lang_syntax::node::ast::PathSegment;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::{LookupIntern, Upcast};
use tower_lsp::lsp_types::{CompletionItem, CompletionItemKind, Position, Range, TextEdit};
use lsp_types::{CompletionItem, CompletionItemKind, InsertTextFormat, Position, Range, TextEdit};
use tracing::debug;

use crate::ide::utils::find_methods_for_type;
Expand Down Expand Up @@ -282,7 +282,7 @@ pub fn completion_for_method(
let completion = CompletionItem {
label: format!("{}()", name),
insert_text: Some(format!("{}($0)", name)),
insert_text_format: Some(tower_lsp::lsp_types::InsertTextFormat::SNIPPET),
insert_text_format: Some(InsertTextFormat::SNIPPET),
detail: Some(detail),
kind: Some(CompletionItemKind::METHOD),
additional_text_edits: Some(additional_text_edits),
Expand Down
Loading