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

Rework server to use experimental mutable tower-lsp branch #5057

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion forc-plugins/forc-fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn format_pkg_at_dir(app: &App, dir: &Path, formatter: &mut Formatter) -> Result
Some(path) => {
let manifest_path = path.clone();
let manifest_file = manifest_path.join(constants::MANIFEST_FILE_NAME);
let files = get_sway_files(path);
let files = get_sway_files(&path);
let mut contains_edits = false;

for file in files {
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tempfile = "3"
thiserror = "1.0.30"
tokio = { version = "1.3", features = ["io-std", "io-util", "macros", "net", "rt-multi-thread", "sync", "time"] }
toml_edit = "0.19"
tower-lsp = { version = "0.19", features = ["proposed"] }
tower-lsp = { git = "https://github.com/ebkalderon/tower-lsp", branch = "support-mutable-methods" }
tracing = "0.1"
urlencoding = "2.1.2"

Expand Down
8 changes: 4 additions & 4 deletions sway-lsp/benches/lsp_benchmarks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ pub mod requests;
pub mod token_map;

use lsp_types::Url;
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;
use sway_lsp::core::session::{self, Session};

pub fn compile_test_project() -> (Url, Arc<Session>) {
let session = Session::new();
pub fn compile_test_project() -> (Url, Session) {
let mut session = Session::new();
// Load the test project
let uri = Url::from_file_path(benchmark_dir().join("src/main.sw")).unwrap();
session.handle_open_file(&uri);
// Compile the project and write the parse result to the session
let parse_result = session::parse_project(&uri).unwrap();
session.write_parse_result(parse_result);
(uri, Arc::new(session))
(uri, session)
}

pub fn sway_workspace_dir() -> PathBuf {
Expand Down
27 changes: 10 additions & 17 deletions sway-lsp/benches/lsp_benchmarks/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use lsp_types::{
use sway_lsp::{capabilities, lsp_ext::OnEnterParams, utils::keyword_docs::KeywordDocs};

fn benchmarks(c: &mut Criterion) {
let (uri, session) = black_box(super::compile_test_project());
let (uri, ref session) = black_box(super::compile_test_project());
let config = sway_lsp::config::Config::default();
let keyword_docs = KeywordDocs::new();
let position = Position::new(1717, 24);
let range = Range::new(Position::new(1628, 0), Position::new(1728, 0));

c.bench_function("semantic_tokens", |b| {
b.iter(|| capabilities::semantic_tokens::semantic_tokens_full(session.clone(), &uri))
b.iter(|| capabilities::semantic_tokens::semantic_tokens_full(session, &uri))
});

c.bench_function("document_symbol", |b| {
Expand All @@ -34,13 +34,11 @@ fn benchmarks(c: &mut Criterion) {
});

c.bench_function("hover", |b| {
b.iter(|| {
capabilities::hover::hover_data(session.clone(), &keyword_docs, uri.clone(), position)
})
b.iter(|| capabilities::hover::hover_data(session, &keyword_docs, uri.clone(), position))
});

c.bench_function("highlight", |b| {
b.iter(|| capabilities::highlight::get_highlights(session.clone(), uri.clone(), position))
b.iter(|| capabilities::highlight::get_highlights(session, uri.clone(), position))
});

c.bench_function("goto_definition", |b| {
Expand All @@ -49,23 +47,18 @@ fn benchmarks(c: &mut Criterion) {

c.bench_function("inlay_hints", |b| {
b.iter(|| {
capabilities::inlay_hints::inlay_hints(
session.clone(),
&uri,
&range,
&config.inlay_hints,
)
capabilities::inlay_hints::inlay_hints(session, &uri, &range, &config.inlay_hints)
})
});

c.bench_function("prepare_rename", |b| {
b.iter(|| capabilities::rename::prepare_rename(session.clone(), uri.clone(), position))
b.iter(|| capabilities::rename::prepare_rename(session, uri.clone(), position))
});

c.bench_function("rename", |b| {
b.iter(|| {
capabilities::rename::rename(
session.clone(),
session,
"new_token_name".to_string(),
uri.clone(),
position,
Expand All @@ -75,11 +68,11 @@ fn benchmarks(c: &mut Criterion) {

c.bench_function("code_action", |b| {
let range = Range::new(Position::new(4, 10), Position::new(4, 10));
b.iter(|| capabilities::code_actions::code_actions(session.clone(), &range, &uri, &uri))
b.iter(|| capabilities::code_actions::code_actions(session, &range, &uri, &uri))
});

c.bench_function("code_lens", |b| {
b.iter(|| capabilities::code_lens::code_lens(&session, &uri.clone()))
b.iter(|| capabilities::code_lens::code_lens(session, &uri.clone()))
});

c.bench_function("on_enter", |b| {
Expand All @@ -91,7 +84,7 @@ fn benchmarks(c: &mut Criterion) {
text: "\n".to_string(),
}],
};
b.iter(|| capabilities::on_enter::on_enter(&config.on_enter, &session, &uri, &params))
b.iter(|| capabilities::on_enter::on_enter(&config.on_enter, session, &uri, &params))
});
}

Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/benches/lsp_benchmarks/token_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use lsp_types::Position;

fn benchmarks(c: &mut Criterion) {
let (uri, session) = black_box(super::compile_test_project());
let engines = session.engines.read();
let engines = &session.engines;
let position = Position::new(1716, 24);

c.bench_function("tokens_for_file", |b| {
Expand Down
7 changes: 3 additions & 4 deletions sway-lsp/src/capabilities/code_actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use lsp_types::{
CodeActionResponse, Position, Range, TextEdit, Url, WorkspaceEdit,
};
use serde_json::Value;
use std::{collections::HashMap, sync::Arc};
use std::collections::HashMap;
use sway_core::{language::ty, Engines};
use sway_types::Spanned;

Expand All @@ -37,18 +37,17 @@ pub(crate) struct CodeActionContext<'a> {
}

pub fn code_actions(
session: Arc<Session>,
session: &Session,
range: &Range,
uri: &Url,
temp_uri: &Url,
) -> Option<CodeActionResponse> {
let engines = session.engines.read();
let (_, token) = session
.token_map()
.token_at_position(temp_uri, range.start)?;

let ctx = CodeActionContext {
engines: &engines,
engines: &session.engines,
tokens: session.token_map(),
token: &token,
uri,
Expand Down
4 changes: 2 additions & 2 deletions sway-lsp/src/capabilities/code_lens.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{path::PathBuf, sync::Arc};
use std::path::PathBuf;

use lsp_types::{CodeLens, Url};

use crate::core::session::Session;

pub fn code_lens(session: &Arc<Session>, url: &Url) -> Vec<CodeLens> {
pub fn code_lens(session: &Session, url: &Url) -> Vec<CodeLens> {
let url_path = PathBuf::from(url.path());

// Construct code lenses for runnable functions
Expand Down
1 change: 0 additions & 1 deletion sway-lsp/src/capabilities/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub fn get_diagnostics(
.push(diagnostic);
}
}

diagnostics
}

Expand Down
3 changes: 1 addition & 2 deletions sway-lsp/src/capabilities/highlight.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::core::session::Session;
use lsp_types::{DocumentHighlight, Position, Url};
use std::sync::Arc;

pub fn get_highlights(
session: Arc<Session>,
session: &Session,
url: Url,
position: Position,
) -> Option<Vec<DocumentHighlight>> {
Expand Down
7 changes: 3 additions & 4 deletions sway-lsp/src/capabilities/hover/hover_link_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::{
core::{session::Session, token::get_range_from_span},
utils::document::get_url_from_span,
};
use std::sync::Arc;
use sway_core::{
language::{
ty::{TyDecl, TyTraitDecl},
Expand All @@ -26,12 +25,12 @@ pub struct RelatedType {
pub struct HoverLinkContents<'a> {
pub related_types: Vec<RelatedType>,
pub implementations: Vec<Span>,
session: Arc<Session>,
session: &'a Session,
engines: &'a Engines,
}

impl<'a> HoverLinkContents<'a> {
pub fn new(session: Arc<Session>, engines: &'a Engines) -> Self {
pub fn new(session: &'a Session, engines: &'a Engines) -> Self {
Self {
related_types: Vec::new(),
implementations: Vec::new(),
Expand Down Expand Up @@ -82,7 +81,7 @@ impl<'a> HoverLinkContents<'a> {
/// Adds all implementations of the given [TyTraitDecl] to the list of implementations.
pub fn add_implementations_for_trait(&mut self, trait_decl: &TyTraitDecl) {
if let Some(namespace) = self.session.namespace() {
let call_path = CallPath::from(trait_decl.name.clone()).to_fullpath(&namespace);
let call_path = CallPath::from(trait_decl.name.clone()).to_fullpath(namespace);
let impl_spans = namespace.get_impl_spans_for_trait_name(&call_path);
self.add_implementations(&trait_decl.span(), impl_spans);
}
Expand Down
15 changes: 9 additions & 6 deletions sway-lsp/src/capabilities/hover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
attributes::doc_comment_attributes, keyword_docs::KeywordDocs, markdown, markup::Markup,
},
};
use std::sync::Arc;
use sway_core::{
language::{ty, Visibility},
Engines, TypeId,
Expand All @@ -22,7 +21,7 @@ use self::hover_link_contents::HoverLinkContents;

/// Extracts the hover information for a token at the current position.
pub fn hover_data(
session: Arc<Session>,
session: &Session,
keyword_docs: &KeywordDocs,
url: Url,
position: Position,
Expand All @@ -47,8 +46,7 @@ pub fn hover_data(
});
}

let engines = session.engines.read();
let (decl_ident, decl_token) = match token.declared_token_ident(&engines) {
let (decl_ident, decl_token) = match token.declared_token_ident(&session.engines) {
Some(decl_ident) => {
let decl_token = session
.token_map()
Expand All @@ -62,7 +60,12 @@ pub fn hover_data(
None => (ident.clone(), token),
};

let contents = hover_format(session.clone(), &engines, &decl_token, &decl_ident.name);
let contents = hover_format(
session.clone(),
&session.engines,
&decl_token,
&decl_ident.name,
);
Some(lsp_types::Hover {
contents,
range: Some(range),
Expand Down Expand Up @@ -120,7 +123,7 @@ fn markup_content(markup: Markup) -> lsp_types::MarkupContent {
}

fn hover_format(
session: Arc<Session>,
session: &Session,
engines: &Engines,
token: &Token,
ident_name: &str,
Expand Down
10 changes: 3 additions & 7 deletions sway-lsp/src/capabilities/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
},
};
use lsp_types::{self, Range, Url};
use std::sync::Arc;
use sway_core::{language::ty::TyDecl, type_system::TypeInfo};
use sway_types::Spanned;

Expand All @@ -24,7 +23,7 @@ pub struct InlayHint {
}

pub fn inlay_hints(
session: Arc<Session>,
session: &Session,
uri: &Url,
range: &Range,
config: &InlayHintsConfig,
Expand All @@ -38,9 +37,6 @@ pub fn inlay_hints(
return None;
}

let engines = session.engines.read();
let type_engine = engines.te();

let hints: Vec<lsp_types::InlayHint> = session
.token_map()
.tokens_for_file(uri)
Expand All @@ -63,7 +59,7 @@ pub fn inlay_hints(
})
})
.filter_map(|var| {
let type_info = type_engine.get(var.type_ascription.type_id);
let type_info = session.engines.te().get(var.type_ascription.type_id);
match type_info {
TypeInfo::Unknown | TypeInfo::UnknownGeneric { .. } => None,
_ => Some(var),
Expand All @@ -72,7 +68,7 @@ pub fn inlay_hints(
.map(|var| {
let range = get_range_from_span(&var.name.span());
let kind = InlayKind::TypeHint;
let label = format!("{}", engines.help_out(var.type_ascription));
let label = format!("{}", session.engines.help_out(var.type_ascription));
let inlay_hint = InlayHint { range, kind, label };
self::inlay_hint(config.render_colons, inlay_hint)
})
Expand Down
7 changes: 3 additions & 4 deletions sway-lsp/src/capabilities/on_enter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
core::{document::TextDocument, session::Session},
lsp_ext::OnEnterParams,
};
use std::sync::Arc;
use tower_lsp::lsp_types::{
DocumentChanges, OneOf, OptionalVersionedTextDocumentIdentifier, Position, Range,
TextDocumentEdit, TextEdit, Url, WorkspaceEdit,
Expand All @@ -17,7 +16,7 @@ const DOC_COMMENT_START: &str = "///";
/// with the appropriate comment start pattern (// or ///).
pub fn on_enter(
config: &OnEnterConfig,
session: &Arc<Session>,
session: &Session,
temp_uri: &Url,
params: &OnEnterParams,
) -> Option<WorkspaceEdit> {
Expand All @@ -31,11 +30,11 @@ pub fn on_enter(
.expect("could not get text document");

if config.continue_doc_comments.unwrap_or(false) {
workspace_edit = get_comment_workspace_edit(DOC_COMMENT_START, params, &text_document);
workspace_edit = get_comment_workspace_edit(DOC_COMMENT_START, params, text_document);
}

if config.continue_comments.unwrap_or(false) && workspace_edit.is_none() {
workspace_edit = get_comment_workspace_edit(COMMENT_START, params, &text_document);
workspace_edit = get_comment_workspace_edit(COMMENT_START, params, text_document);
}

workspace_edit
Expand Down