Skip to content

Commit

Permalink
Revert "Support LSP filterText in completions"
Browse files Browse the repository at this point in the history
This reverts commit 1177691.

Commit 25076db (Fake filterText support by adding insertText as
snippet, 2022-09-24) already does this (with small UI differences)
and does not rely on an out-of-tree Kakoune feature. Let's remove
this deprecated feature to make way for another proposed completion
extension - see next commit - which is incompatible with this one.
  • Loading branch information
krobelus committed Jun 24, 2023
1 parent d272455 commit e779226
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 68 deletions.
9 changes: 0 additions & 9 deletions rc/lsp.kak
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,6 @@ ${lsp_draft}\"\"\"
}
}}

declare-option -hidden bool lsp_have_kakoune_feature_filtertext
declare-option -hidden completions lsp_have_kakoune_feature_filtertext_tmp
try %{
set-option global lsp_have_kakoune_feature_filtertext_tmp 1.1@0 insert_text|filter_text|on_select|menu
set-option global lsp_have_kakoune_feature_filtertext true
}

define-command -hidden lsp-completion -docstring "Request completions for the main cursor position" %{
try %{
# Fail if preceding character is a whitespace (by default; the trigger could be customized).
Expand Down Expand Up @@ -397,8 +390,6 @@ line = ${kak_cursor_line}
column = ${kak_cursor_column}
[params.completion]
offset = ${kak_opt_lsp_completion_offset}
[params]
have_kakoune_feature_filtertext = ${kak_opt_lsp_have_kakoune_feature_filtertext}
" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}}

Expand Down
91 changes: 33 additions & 58 deletions src/language_features/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,64 +152,15 @@ fn editor_completion(
.or_else(|| x.insert_text.clone())
.unwrap_or_else(|| x.label.clone());

fn completion_entry(
insert_text: &str,
maybe_filter_text: &Option<String>,
on_select: &str,
menu: &str,
) -> String {
if let Some(filter_text) = maybe_filter_text {
editor_quote(&format!(
"{}|{}|{}|{}",
escape_tuple_element(insert_text),
escape_tuple_element(filter_text),
escape_tuple_element(on_select),
escape_tuple_element(menu),
))
} else {
editor_quote(&format!(
"{}|{}|{}",
escape_tuple_element(insert_text),
escape_tuple_element(on_select),
escape_tuple_element(menu),
))
}
fn completion_entry(insert_text: &str, on_select: &str, menu: &str) -> String {
editor_quote(&format!(
"{}|{}|{}",
escape_tuple_element(insert_text),
escape_tuple_element(on_select),
escape_tuple_element(menu),
))
}

let maybe_filter_text = {
let specified_filter_text = x.filter_text.as_ref().unwrap_or(&x.label);
let specified_insert_text = x
.text_edit
.as_ref()
.map(|cte| match cte {
CompletionTextEdit::Edit(text_edit) => &text_edit.new_text,
CompletionTextEdit::InsertAndReplace(text_edit) => &text_edit.new_text,
})
.or(x.insert_text.as_ref())
.unwrap_or(&x.label);
if !params.have_kakoune_feature_filtertext
&& specified_filter_text != specified_insert_text
&& x.insert_text_format != Some(InsertTextFormat::SNIPPET)
{
// Simulate filter-text support by giving the filter-text to Kakoune
// but expand to the insert-text when the completion is accepted.
let command = formatdoc!(
"{on_select}
lsp-snippets-insert-completion {}",
editor_quote(&(insert_text + "$0"))
);
let insert_text = specified_filter_text;
return completion_entry(insert_text, &None, &command, &entry);
}
if params.have_kakoune_feature_filtertext
&& specified_filter_text != specified_insert_text
{
Some(specified_filter_text.clone())
} else {
None
}
};

// If snippet support is both enabled and provided by the server,
// we'll need to perform some transformations on the completion commands.
if ctx.config.snippet_support && x.insert_text_format == Some(InsertTextFormat::SNIPPET)
Expand All @@ -234,9 +185,33 @@ fn editor_completion(
editor_quote(&snippet)
);

completion_entry(&insert_text, &maybe_filter_text, &command, &entry)
completion_entry(&insert_text, &command, &entry)
} else {
completion_entry(&insert_text, &maybe_filter_text, &on_select, &entry)
// Due to implementation reasons, we currently do not support filter text
// with snippets.
let specified_filter_text = x.filter_text.as_ref().unwrap_or(&x.label);
let specified_insert_text = x
.text_edit
.as_ref()
.map(|cte| match cte {
CompletionTextEdit::Edit(text_edit) => &text_edit.new_text,
CompletionTextEdit::InsertAndReplace(text_edit) => &text_edit.new_text,
})
.or(x.insert_text.as_ref())
.unwrap_or(&x.label);
let (insert_text, on_select) = if specified_filter_text != specified_insert_text {
// Simulate filter-text support by giving the filter-text to Kakoune
// but expand to the insert-text when the completion is accepted.
let on_select = formatdoc!(
"{on_select}
lsp-snippets-insert-completion {}",
editor_quote(&(insert_text + "$0"))
);
(specified_filter_text, on_select)
} else {
(&insert_text, on_select)
};
completion_entry(insert_text, &on_select, &entry)
}
})
.join(" ");
Expand Down
1 change: 0 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ pub struct TextDocumentDidChangeParams {
pub struct TextDocumentCompletionParams {
pub position: KakounePosition,
pub completion: EditorCompletion,
pub have_kakoune_feature_filtertext: bool,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down

0 comments on commit e779226

Please sign in to comment.