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/main_loop/handlers.rs | 23 +++++++++++++++++++++++ crates/server/src/main_loop/mod.rs | 4 ++++ 2 files changed, 27 insertions(+) (limited to 'crates/server/src/main_loop') 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, )?; -- cgit v1.2.3