Simple scope completion

This commit is contained in:
Aleksey Kladov 2018-08-26 12:51:45 +03:00
parent ac226021cf
commit 71722c047f
5 changed files with 34 additions and 2 deletions

View file

@ -14,7 +14,7 @@ use {
#[derive(Debug)] #[derive(Debug)]
pub struct CompletionItem { pub struct CompletionItem {
name: String, pub name: String,
} }
pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionItem>> { pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionItem>> {

View file

@ -4,6 +4,7 @@ use languageserver_types::{
TextDocumentSyncOptions, TextDocumentSyncOptions,
TextDocumentSyncKind, TextDocumentSyncKind,
ExecuteCommandOptions, ExecuteCommandOptions,
CompletionOptions,
}; };
pub fn server_capabilities() -> ServerCapabilities { pub fn server_capabilities() -> ServerCapabilities {
@ -18,7 +19,10 @@ pub fn server_capabilities() -> ServerCapabilities {
} }
)), )),
hover_provider: None, hover_provider: None,
completion_provider: None, completion_provider: Some(CompletionOptions {
resolve_provider: None,
trigger_characters: None,
}),
signature_help_provider: None, signature_help_provider: None,
definition_provider: Some(true), definition_provider: Some(true),
type_definition_provider: None, type_definition_provider: None,

View file

@ -4,6 +4,7 @@ use languageserver_types::{
Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, Diagnostic, DiagnosticSeverity, Url, DocumentSymbol,
Command, TextDocumentIdentifier, WorkspaceEdit, Command, TextDocumentIdentifier, WorkspaceEdit,
SymbolInformation, Position, Location, TextEdit, SymbolInformation, Position, Location, TextEdit,
CompletionItem,
}; };
use serde_json::{to_value, from_value}; use serde_json::{to_value, from_value};
use libanalysis::{Query}; use libanalysis::{Query};
@ -259,6 +260,28 @@ pub fn handle_parent_module(
Ok(res) Ok(res)
} }
pub fn handle_completion(
world: ServerWorld,
params: req::CompletionParams,
) -> Result<Option<req::CompletionResponse>> {
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( pub fn handle_execute_command(
world: ServerWorld, world: ServerWorld,
mut params: req::ExecuteCommandParams, mut params: req::ExecuteCommandParams,

View file

@ -28,6 +28,7 @@ use {
handle_find_matching_brace, handle_find_matching_brace,
handle_parent_module, handle_parent_module,
handle_join_lines, handle_join_lines,
handle_completion,
}, },
}; };
@ -143,6 +144,9 @@ fn on_request(
handle_request_on_threadpool::<req::GotoDefinition>( handle_request_on_threadpool::<req::GotoDefinition>(
&mut req, pool, world, sender, handle_goto_definition, &mut req, pool, world, sender, handle_goto_definition,
)?; )?;
handle_request_on_threadpool::<req::Completion>(
&mut req, pool, world, sender, handle_completion,
)?;
handle_request_on_threadpool::<req::ParentModule>( handle_request_on_threadpool::<req::ParentModule>(
&mut req, pool, world, sender, handle_parent_module, &mut req, pool, world, sender, handle_parent_module,
)?; )?;

View file

@ -11,6 +11,7 @@ pub use languageserver_types::{
WorkspaceSymbolParams, WorkspaceSymbolParams,
TextDocumentPositionParams, TextDocumentPositionParams,
TextEdit, TextEdit,
CompletionParams, CompletionResponse,
}; };