From 71722c047f96cb754c958c52591ca53f2a9c4d3c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Aug 2018 12:51:45 +0300 Subject: Simple scope completion --- crates/server/src/caps.rs | 6 +++++- crates/server/src/main_loop/handlers.rs | 23 +++++++++++++++++++++++ crates/server/src/main_loop/mod.rs | 4 ++++ crates/server/src/req.rs | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) (limited to 'crates/server/src') diff --git a/crates/server/src/caps.rs b/crates/server/src/caps.rs index ffebd9b47..98499bb05 100644 --- a/crates/server/src/caps.rs +++ b/crates/server/src/caps.rs @@ -4,6 +4,7 @@ use languageserver_types::{ TextDocumentSyncOptions, TextDocumentSyncKind, ExecuteCommandOptions, + CompletionOptions, }; pub fn server_capabilities() -> ServerCapabilities { @@ -18,7 +19,10 @@ pub fn server_capabilities() -> ServerCapabilities { } )), hover_provider: None, - completion_provider: None, + completion_provider: Some(CompletionOptions { + resolve_provider: None, + trigger_characters: None, + }), signature_help_provider: None, definition_provider: Some(true), type_definition_provider: None, diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index b68e93e46..e7ac53028 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -4,6 +4,7 @@ use languageserver_types::{ Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, Command, TextDocumentIdentifier, WorkspaceEdit, SymbolInformation, Position, Location, TextEdit, + CompletionItem, }; use serde_json::{to_value, from_value}; use libanalysis::{Query}; @@ -259,6 +260,28 @@ pub fn handle_parent_module( Ok(res) } +pub fn handle_completion( + world: ServerWorld, + params: req::CompletionParams, +) -> Result> { + let file_id = params.text_document.try_conv_with(&world)?; + let file = world.analysis().file_syntax(file_id)?; + let line_index = world.analysis().file_line_index(file_id)?; + let offset = params.position.conv_with(&line_index); + let items = match libeditor::scope_completion(&file, offset) { + None => return Ok(None), + Some(items) => items, + }; + let items = items.into_iter() + .map(|item| CompletionItem { + label: item.name, + .. Default::default() + }) + .collect(); + + Ok(Some(req::CompletionResponse::Array(items))) +} + pub fn handle_execute_command( world: ServerWorld, mut params: req::ExecuteCommandParams, diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index 1e65041e2..5213ecc04 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -28,6 +28,7 @@ use { handle_find_matching_brace, handle_parent_module, handle_join_lines, + handle_completion, }, }; @@ -143,6 +144,9 @@ fn on_request( handle_request_on_threadpool::( &mut req, pool, world, sender, handle_goto_definition, )?; + handle_request_on_threadpool::( + &mut req, pool, world, sender, handle_completion, + )?; handle_request_on_threadpool::( &mut req, pool, world, sender, handle_parent_module, )?; diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index c431deeb4..6d3466b66 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -11,6 +11,7 @@ pub use languageserver_types::{ WorkspaceSymbolParams, TextDocumentPositionParams, TextEdit, + CompletionParams, CompletionResponse, }; -- cgit v1.2.3