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)]
pub struct CompletionItem {
name: String,
pub name: String,
}
pub fn scope_completion(file: &File, offset: TextUnit) -> Option<Vec<CompletionItem>> {

View File

@ -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,

View File

@ -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<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(
world: ServerWorld,
mut params: req::ExecuteCommandParams,

View File

@ -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::<req::GotoDefinition>(
&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>(
&mut req, pool, world, sender, handle_parent_module,
)?;

View File

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