From 3bec2be9de400dc044ae924cb0ae36faa454d111 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 10 May 2020 19:25:37 +0200 Subject: [PATCH] req -> lsp_ext --- crates/rust-analyzer/src/lib.rs | 3 +- .../rust-analyzer/src/{req.rs => lsp_ext.rs} | 6 +-- crates/rust-analyzer/src/main_loop.rs | 25 ++++++------ .../rust-analyzer/src/main_loop/handlers.rs | 38 +++++++++++-------- crates/rust-analyzer/src/to_proto.rs | 16 ++++---- .../rust-analyzer/tests/heavy_tests/main.rs | 17 +++++---- .../tests/heavy_tests/support.rs | 6 +-- 7 files changed, 58 insertions(+), 53 deletions(-) rename crates/rust-analyzer/src/{req.rs => lsp_ext.rs} (98%) diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index 6d719108c31..57d0e92188e 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -24,8 +24,7 @@ mod to_proto; mod from_proto; mod main_loop; mod markdown; -// TODO: rename to lsp_ext -pub mod req; +pub mod lsp_ext; pub mod config; mod world; mod diagnostics; diff --git a/crates/rust-analyzer/src/req.rs b/crates/rust-analyzer/src/lsp_ext.rs similarity index 98% rename from crates/rust-analyzer/src/req.rs rename to crates/rust-analyzer/src/lsp_ext.rs index aeb29370c0d..313a8c7697e 100644 --- a/crates/rust-analyzer/src/req.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -1,12 +1,12 @@ -//! Defines `rust-analyzer` specific custom messages. +//! rust-analyzer extensions to the LSP. + +use std::path::PathBuf; use lsp_types::request::Request; use lsp_types::{Location, Position, Range, TextDocumentIdentifier}; use rustc_hash::FxHashMap; use serde::{Deserialize, Serialize}; -use std::path::PathBuf; - pub enum AnalyzerStatus {} impl Request for AnalyzerStatus { diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 333ecc85984..fa72a9cc64d 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -38,12 +38,11 @@ use threadpool::ThreadPool; use crate::{ config::{Config, FilesWatcher}, diagnostics::DiagnosticTask, - from_proto, + from_proto, lsp_ext, main_loop::{ pending_requests::{PendingRequest, PendingRequests}, subscriptions::Subscriptions, }, - req, world::{WorldSnapshot, WorldState}, Result, }; @@ -502,26 +501,27 @@ fn on_request( request_received, }; pool_dispatcher - .on_sync::(|s, ()| Ok(s.collect_garbage()))? - .on_sync::(|s, p| handlers::handle_join_lines(s.snapshot(), p))? - .on_sync::(|s, p| handlers::handle_on_enter(s.snapshot(), p))? + .on_sync::(|s, ()| Ok(s.collect_garbage()))? + .on_sync::(|s, p| handlers::handle_join_lines(s.snapshot(), p))? + .on_sync::(|s, p| handlers::handle_on_enter(s.snapshot(), p))? .on_sync::(|s, p| { handlers::handle_selection_range(s.snapshot(), p) })? - .on_sync::(|s, p| { + .on_sync::(|s, p| { handlers::handle_find_matching_brace(s.snapshot(), p) })? - .on::(handlers::handle_analyzer_status)? - .on::(handlers::handle_syntax_tree)? - .on::(handlers::handle_expand_macro)? + .on::(handlers::handle_analyzer_status)? + .on::(handlers::handle_syntax_tree)? + .on::(handlers::handle_expand_macro)? + .on::(handlers::handle_parent_module)? + .on::(handlers::handle_runnables)? + .on::(handlers::handle_inlay_hints)? .on::(handlers::handle_on_type_formatting)? .on::(handlers::handle_document_symbol)? .on::(handlers::handle_workspace_symbol)? .on::(handlers::handle_goto_definition)? .on::(handlers::handle_goto_implementation)? .on::(handlers::handle_goto_type_definition)? - .on::(handlers::handle_parent_module)? - .on::(handlers::handle_runnables)? .on::(handlers::handle_completion)? .on::(handlers::handle_code_action)? .on::(handlers::handle_code_lens)? @@ -534,7 +534,6 @@ fn on_request( .on::(handlers::handle_references)? .on::(handlers::handle_formatting)? .on::(handlers::handle_document_highlight)? - .on::(handlers::handle_inlay_hints)? .on::(handlers::handle_call_hierarchy_prepare)? .on::( handlers::handle_call_hierarchy_incoming, @@ -546,7 +545,7 @@ fn on_request( .on::( handlers::handle_semantic_tokens_range, )? - .on::(handlers::handle_ssr)? + .on::(handlers::handle_ssr)? .finish(); Ok(()) } diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 7aa77d19a49..be8688bc395 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -34,7 +34,7 @@ use crate::{ config::RustfmtConfig, diagnostics::DiagnosticTask, from_json, from_proto, - req::{self, InlayHint, InlayHintsParams}, + lsp_ext::{self, InlayHint, InlayHintsParams}, to_proto, world::WorldSnapshot, LspError, Result, @@ -52,7 +52,10 @@ pub fn handle_analyzer_status(world: WorldSnapshot, _: ()) -> Result { Ok(buf) } -pub fn handle_syntax_tree(world: WorldSnapshot, params: req::SyntaxTreeParams) -> Result { +pub fn handle_syntax_tree( + world: WorldSnapshot, + params: lsp_ext::SyntaxTreeParams, +) -> Result { let _p = profile("handle_syntax_tree"); let id = from_proto::file_id(&world, ¶ms.text_document.uri)?; let line_index = world.analysis().file_line_index(id)?; @@ -63,8 +66,8 @@ pub fn handle_syntax_tree(world: WorldSnapshot, params: req::SyntaxTreeParams) - pub fn handle_expand_macro( world: WorldSnapshot, - params: req::ExpandMacroParams, -) -> Result> { + params: lsp_ext::ExpandMacroParams, +) -> Result> { let _p = profile("handle_expand_macro"); let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; let line_index = world.analysis().file_line_index(file_id)?; @@ -74,7 +77,7 @@ pub fn handle_expand_macro( None => Ok(None), Some(offset) => { let res = world.analysis().expand_macro(FilePosition { file_id, offset })?; - Ok(res.map(|it| req::ExpandedMacro { name: it.name, expansion: it.expansion })) + Ok(res.map(|it| lsp_ext::ExpandedMacro { name: it.name, expansion: it.expansion })) } } } @@ -124,7 +127,7 @@ pub fn handle_selection_range( pub fn handle_find_matching_brace( world: WorldSnapshot, - params: req::FindMatchingBraceParams, + params: lsp_ext::FindMatchingBraceParams, ) -> Result> { let _p = profile("handle_find_matching_brace"); let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; @@ -146,8 +149,8 @@ pub fn handle_find_matching_brace( pub fn handle_join_lines( world: WorldSnapshot, - params: req::JoinLinesParams, -) -> Result { + params: lsp_ext::JoinLinesParams, +) -> Result { let _p = profile("handle_join_lines"); let frange = from_proto::file_range(&world, params.text_document, params.range)?; let source_change = world.analysis().join_lines(frange)?; @@ -157,7 +160,7 @@ pub fn handle_join_lines( pub fn handle_on_enter( world: WorldSnapshot, params: lsp_types::TextDocumentPositionParams, -) -> Result> { +) -> Result> { let _p = profile("handle_on_enter"); let position = from_proto::file_position(&world, params)?; match world.analysis().on_enter(position)? { @@ -388,8 +391,8 @@ pub fn handle_parent_module( pub fn handle_runnables( world: WorldSnapshot, - params: req::RunnablesParams, -) -> Result> { + params: lsp_ext::RunnablesParams, +) -> Result> { let _p = profile("handle_runnables"); let file_id = from_proto::file_id(&world, ¶ms.text_document.uri)?; let line_index = world.analysis().file_line_index(file_id)?; @@ -419,7 +422,7 @@ pub fn handle_runnables( match cargo_spec { Some(spec) => { for &cmd in ["check", "test"].iter() { - res.push(req::Runnable { + res.push(lsp_ext::Runnable { range: Default::default(), label: format!("cargo {} -p {}", cmd, spec.package), bin: "cargo".to_string(), @@ -431,7 +434,7 @@ pub fn handle_runnables( } } None => { - res.push(req::Runnable { + res.push(lsp_ext::Runnable { range: Default::default(), label: "cargo check --workspace".to_string(), bin: "cargo".to_string(), @@ -972,7 +975,10 @@ pub fn handle_document_highlight( Ok(Some(res)) } -pub fn handle_ssr(world: WorldSnapshot, params: req::SsrParams) -> Result { +pub fn handle_ssr( + world: WorldSnapshot, + params: lsp_ext::SsrParams, +) -> Result { let _p = profile("handle_ssr"); let source_change = world.analysis().structural_search_replace(¶ms.query, params.parse_only)??; @@ -1003,7 +1009,7 @@ fn to_lsp_runnable( world: &WorldSnapshot, file_id: FileId, runnable: Runnable, -) -> Result { +) -> Result { let spec = CargoTargetSpec::for_file(world, file_id)?; let (args, extra_args) = CargoTargetSpec::runnable_args(spec, &runnable.kind)?; let line_index = world.analysis().file_line_index(file_id)?; @@ -1014,7 +1020,7 @@ fn to_lsp_runnable( RunnableKind::DocTest { test_id, .. } => format!("doctest {}", test_id), RunnableKind::Bin => "run binary".to_string(), }; - Ok(req::Runnable { + Ok(lsp_ext::Runnable { range: to_proto::range(&line_index, runnable.range), label, bin: "cargo".to_string(), diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 4157ce8ea17..4500d49827e 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -10,7 +10,7 @@ use ra_syntax::{SyntaxKind, TextRange, TextSize}; use ra_text_edit::{Indel, TextEdit}; use ra_vfs::LineEndings; -use crate::{req, semantic_tokens, world::WorldSnapshot, Result}; +use crate::{lsp_ext, semantic_tokens, world::WorldSnapshot, Result}; pub(crate) fn position(line_index: &LineIndex, offset: TextSize) -> lsp_types::Position { let line_col = line_index.line_col(offset); @@ -215,14 +215,14 @@ pub(crate) fn signature_information( lsp_types::SignatureInformation { label, documentation, parameters: Some(parameters) } } -pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> req::InlayHint { - req::InlayHint { +pub(crate) fn inlay_int(line_index: &LineIndex, inlay_hint: InlayHint) -> lsp_ext::InlayHint { + lsp_ext::InlayHint { label: inlay_hint.label.to_string(), range: range(line_index, inlay_hint.range), kind: match inlay_hint.kind { - InlayKind::ParameterHint => req::InlayKind::ParameterHint, - InlayKind::TypeHint => req::InlayKind::TypeHint, - InlayKind::ChainingHint => req::InlayKind::ChainingHint, + InlayKind::ParameterHint => lsp_ext::InlayKind::ParameterHint, + InlayKind::TypeHint => lsp_ext::InlayKind::TypeHint, + InlayKind::ChainingHint => lsp_ext::InlayKind::ChainingHint, }, } } @@ -478,7 +478,7 @@ pub(crate) fn resource_op( pub(crate) fn source_change( world: &WorldSnapshot, source_change: SourceChange, -) -> Result { +) -> Result { let cursor_position = match source_change.cursor_position { None => None, Some(pos) => { @@ -513,7 +513,7 @@ pub(crate) fn source_change( changes: None, document_changes: Some(lsp_types::DocumentChanges::Operations(document_changes)), }; - Ok(req::SourceChange { label: source_change.label, workspace_edit, cursor_position }) + Ok(lsp_ext::SourceChange { label: source_change.label, workspace_edit, cursor_position }) } pub fn call_hierarchy_item( diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs index e459e3a3cec..5011cc2734d 100644 --- a/crates/rust-analyzer/tests/heavy_tests/main.rs +++ b/crates/rust-analyzer/tests/heavy_tests/main.rs @@ -3,15 +3,16 @@ mod support; use std::{collections::HashMap, path::PathBuf, time::Instant}; use lsp_types::{ - CodeActionContext, DidOpenTextDocumentParams, DocumentFormattingParams, FormattingOptions, - GotoDefinitionParams, HoverParams, PartialResultParams, Position, Range, TextDocumentItem, - TextDocumentPositionParams, WorkDoneProgressParams, -}; -use rust_analyzer::req::{ - CodeActionParams, CodeActionRequest, Completion, CompletionParams, DidOpenTextDocument, - Formatting, GotoDefinition, GotoTypeDefinition, HoverRequest, OnEnter, Runnables, - RunnablesParams, + notification::DidOpenTextDocument, + request::{ + CodeActionRequest, Completion, Formatting, GotoDefinition, GotoTypeDefinition, HoverRequest, + }, + CodeActionContext, CodeActionParams, CompletionParams, DidOpenTextDocumentParams, + DocumentFormattingParams, FormattingOptions, GotoDefinitionParams, HoverParams, + PartialResultParams, Position, Range, TextDocumentItem, TextDocumentPositionParams, + WorkDoneProgressParams, }; +use rust_analyzer::lsp_ext::{OnEnter, Runnables, RunnablesParams}; use serde_json::json; use tempfile::TempDir; use test_utils::skip_slow_tests; diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index 8d47ee4f64c..8756ad4a3c8 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -13,15 +13,15 @@ use lsp_types::{ request::Shutdown, DidOpenTextDocumentParams, TextDocumentIdentifier, TextDocumentItem, Url, WorkDoneProgress, }; +use lsp_types::{ProgressParams, ProgressParamsValue}; use serde::Serialize; use serde_json::{to_string_pretty, Value}; use tempfile::TempDir; use test_utils::{find_mismatch, parse_fixture}; -use req::{ProgressParams, ProgressParamsValue}; use rust_analyzer::{ config::{ClientCapsConfig, Config}, - main_loop, req, + main_loop, }; pub struct Project<'a> { @@ -206,7 +206,7 @@ impl Server { Message::Notification(n) if n.method == "$/progress" => { match n.clone().extract::("$/progress").unwrap() { ProgressParams { - token: req::ProgressToken::String(ref token), + token: lsp_types::ProgressToken::String(ref token), value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(_)), } if token == "rustAnalyzer/startup" => true, _ => false,