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

Use lsp_types directly in sway-lsp tests instead of simulating a client with tower-lsp #4976

Merged
merged 25 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions sway-lsp/src/handlers/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lsp_types::{
DidSaveTextDocumentParams, FileChangeType,
};

pub(crate) async fn handle_did_open_text_document(
pub async fn handle_did_open_text_document(
state: &ServerState,
params: DidOpenTextDocumentParams,
) -> Result<(), LanguageServerError> {
Expand All @@ -21,7 +21,7 @@ pub(crate) async fn handle_did_open_text_document(
Ok(())
}

pub(crate) async fn handle_did_change_text_document(
pub async fn handle_did_change_text_document(
state: &ServerState,
params: DidChangeTextDocumentParams,
) -> Result<(), LanguageServerError> {
Expand Down
31 changes: 15 additions & 16 deletions sway-lsp/src/handlers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sway_types::{Ident, Spanned};
use tower_lsp::jsonrpc::Result;
use tracing::metadata::LevelFilter;

pub(crate) fn handle_initialize(
pub fn handle_initialize(
state: &ServerState,
params: lsp_types::InitializeParams,
) -> Result<InitializeResult> {
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(crate) fn handle_initialize(
})
}

pub(crate) fn handle_document_symbol(
pub fn handle_document_symbol(
state: &ServerState,
params: lsp_types::DocumentSymbolParams,
) -> Result<Option<lsp_types::DocumentSymbolResponse>> {
Expand All @@ -66,7 +66,7 @@ pub(crate) fn handle_document_symbol(
}
}

pub(crate) fn handle_goto_definition(
pub fn handle_goto_definition(
state: &ServerState,
params: lsp_types::GotoDefinitionParams,
) -> Result<Option<lsp_types::GotoDefinitionResponse>> {
Expand All @@ -85,7 +85,7 @@ pub(crate) fn handle_goto_definition(
}
}

pub(crate) fn handle_completion(
pub fn handle_completion(
state: &ServerState,
params: lsp_types::CompletionParams,
) -> Result<Option<lsp_types::CompletionResponse>> {
Expand All @@ -109,7 +109,7 @@ pub(crate) fn handle_completion(
}
}

pub(crate) fn handle_hover(
pub fn handle_hover(
state: &ServerState,
params: lsp_types::HoverParams,
) -> Result<Option<lsp_types::Hover>> {
Expand All @@ -133,7 +133,7 @@ pub(crate) fn handle_hover(
}
}

pub(crate) fn handle_prepare_rename(
pub fn handle_prepare_rename(
state: &ServerState,
params: lsp_types::TextDocumentPositionParams,
) -> Result<Option<PrepareRenameResponse>> {
Expand All @@ -157,10 +157,7 @@ pub(crate) fn handle_prepare_rename(
}
}

pub(crate) fn handle_rename(
state: &ServerState,
params: RenameParams,
) -> Result<Option<WorkspaceEdit>> {
pub fn handle_rename(state: &ServerState, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
match state
.sessions
.uri_and_session_from_workspace(&params.text_document_position.text_document.uri)
Expand All @@ -183,7 +180,7 @@ pub(crate) fn handle_rename(
}
}

pub(crate) fn handle_document_highlight(
pub fn handle_document_highlight(
state: &ServerState,
params: lsp_types::DocumentHighlightParams,
) -> Result<Option<Vec<lsp_types::DocumentHighlight>>> {
Expand All @@ -204,7 +201,7 @@ pub(crate) fn handle_document_highlight(
}
}

pub(crate) fn handle_formatting(
pub fn handle_formatting(
state: &ServerState,
params: DocumentFormattingParams,
) -> Result<Option<Vec<lsp_types::TextEdit>>> {
Expand All @@ -218,7 +215,7 @@ pub(crate) fn handle_formatting(
})
}

pub(crate) fn handle_code_action(
pub fn handle_code_action(
state: &ServerState,
params: lsp_types::CodeActionParams,
) -> Result<Option<lsp_types::CodeActionResponse>> {
Expand All @@ -239,7 +236,7 @@ pub(crate) fn handle_code_action(
}
}

pub(crate) fn handle_code_lens(
pub fn handle_code_lens(
state: &ServerState,
params: lsp_types::CodeLensParams,
) -> Result<Option<Vec<CodeLens>>> {
Expand All @@ -258,6 +255,8 @@ pub(crate) fn handle_code_lens(
data: None,
});
});
// Sort the results
result.sort_by(|a, b| a.range.start.line.cmp(&b.range.start.line));
Ok(Some(result))
}
Err(err) => {
Expand All @@ -267,7 +266,7 @@ pub(crate) fn handle_code_lens(
}
}

pub(crate) fn handle_semantic_tokens_full(
pub fn handle_semantic_tokens_full(
state: &ServerState,
params: SemanticTokensParams,
) -> Result<Option<SemanticTokensResult>> {
Expand Down Expand Up @@ -325,7 +324,7 @@ pub(crate) fn handle_inlay_hints(
/// A formatted AST is written to a temporary file and the URI is
/// returned to the client so it can be opened and displayed in a
/// seperate side panel.
pub(crate) fn handle_show_ast(
pub fn handle_show_ast(
state: &ServerState,
params: lsp_ext::ShowAstParams,
) -> Result<Option<TextDocumentIdentifier>> {
Expand Down
2 changes: 1 addition & 1 deletion sway-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod config;
pub mod core;
pub mod error;
pub mod server_state;
mod handlers {
pub mod handlers {
pub mod notification;
pub mod request;
}
Expand Down
40 changes: 24 additions & 16 deletions sway-lsp/src/server_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,32 @@ use tower_lsp::{jsonrpc, Client};

/// `ServerState` is the primary mutable state of the language server
pub struct ServerState {
pub(crate) client: Client,
pub(crate) client: Option<Client>,
pub(crate) config: Arc<RwLock<Config>>,
pub(crate) keyword_docs: Arc<KeywordDocs>,
pub(crate) sessions: Arc<Sessions>,
}

impl Default for ServerState {
fn default() -> Self {
ServerState {
client: None,
config: Arc::new(RwLock::new(Default::default())),
keyword_docs: Arc::new(KeywordDocs::new()),
sessions: Arc::new(Sessions(DashMap::new())),
}
}
}

impl ServerState {
pub fn new(client: Client) -> ServerState {
let sessions = Arc::new(Sessions(DashMap::new()));
let config = Arc::new(RwLock::new(Default::default()));
let keyword_docs = Arc::new(KeywordDocs::new());
ServerState {
client,
config,
keyword_docs,
sessions,
client: Some(client),
..Default::default()
}
}

pub(crate) fn shutdown_server(&self) -> jsonrpc::Result<()> {
pub fn shutdown_server(&self) -> jsonrpc::Result<()> {
tracing::info!("Shutting Down the Sway Language Server");
let _ = self.sessions.iter().map(|item| {
let session = item.value();
Expand Down Expand Up @@ -77,13 +83,15 @@ impl ServerState {
Ok(_) => {
// Note: Even if the computed diagnostics vec is empty, we still have to push the empty Vec
// in order to clear former diagnostics. Newly pushed diagnostics always replace previously pushed diagnostics.
self.client
.publish_diagnostics(
workspace_uri.clone(),
self.diagnostics(&uri, session),
None,
)
.await;
if let Some(client) = self.client.as_ref() {
client
.publish_diagnostics(
workspace_uri.clone(),
self.diagnostics(&uri, session),
None,
)
.await;
}
}
Err(err) => {
if matches!(err, LanguageServerError::FailedToParse) {
Expand Down
Loading