From e8c50f091581637058838ee75ceb33ee9ef99a81 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 21 Nov 2018 19:04:33 +0300 Subject: Use `:` as a trigger character for completion Note that VSCode asks for completion after *first* `:` as well: use crate: we use hacks to protect against that, and to give completions only after the second `:`. --- crates/ra_lsp_server/src/caps.rs | 2 +- crates/ra_lsp_server/src/main_loop/handlers.rs | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index bcf857fce..560f64989 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -19,7 +19,7 @@ pub fn server_capabilities() -> ServerCapabilities { hover_provider: Some(true), completion_provider: Some(CompletionOptions { resolve_provider: None, - trigger_characters: None, + trigger_characters: Some(vec![":".to_string()]), }), signature_help_provider: Some(SignatureHelpOptions { trigger_characters: Some(vec!["(".to_string(), ",".to_string(), ")".to_string()]), diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index c872b0dc4..27933a7ae 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -9,7 +9,7 @@ use languageserver_types::{ WorkspaceEdit, ParameterInformation, SignatureInformation, Hover, HoverContents, }; use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition}; -use ra_syntax::text_utils::contains_offset_nonstrict; +use ra_syntax::{TextUnit, text_utils::contains_offset_nonstrict}; use rustc_hash::FxHashMap; use serde_json::to_value; @@ -381,6 +381,28 @@ pub fn handle_completion( let offset = params.position.conv_with(&line_index); FilePosition { file_id, offset } }; + let completion_triggered_after_single_colon = { + let mut res = false; + if let Some(ctx) = params.context { + if ctx.trigger_character.unwrap_or(String::new()) == ":" { + let source_file = world.analysis().file_syntax(position.file_id); + let syntax = source_file.syntax(); + let text = syntax.text(); + if let Some(next_char) = text.char_at(position.offset) { + let diff = TextUnit::of_char(next_char) + TextUnit::of_char(':'); + let prev_char = position.offset - diff; + if text.char_at(prev_char) != Some(':') { + res = true; + } + } + } + } + res + }; + if completion_triggered_after_single_colon { + return Ok(None); + } + let items = match world.analysis().completions(position)? { None => return Ok(None), Some(items) => items, -- cgit v1.2.3