From 71722c047f96cb754c958c52591ca53f2a9c4d3c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 26 Aug 2018 12:51:45 +0300 Subject: [PATCH] Simple scope completion --- crates/libeditor/src/completion.rs | 2 +- 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 + 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/crates/libeditor/src/completion.rs b/crates/libeditor/src/completion.rs index 16c9ead741..aeed195593 100644 --- a/crates/libeditor/src/completion.rs +++ b/crates/libeditor/src/completion.rs @@ -14,7 +14,7 @@ use { #[derive(Debug)] pub struct CompletionItem { - name: String, + pub name: String, } pub fn scope_completion(file: &File, offset: TextUnit) -> Option> { diff --git a/crates/server/src/caps.rs b/crates/server/src/caps.rs index ffebd9b47d..98499bb05e 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 b68e93e46b..e7ac53028a 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 1e65041e2f..5213ecc048 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 c431deeb45..6d3466b660 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, };