aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui
diff options
context:
space:
mode:
authorMichael Davis2022-10-22 14:52:25 +0000
committerBlaž Hrastnik2023-03-08 01:48:35 +0000
commitb9b1ec22084cfe82ced7e34412dab0351828fc53 (patch)
treec54c521bd08aa0cd47179b22fb28e69e703d6ec4 /helix-term/src/ui
parent3f90dafa3c4875cb33f404392552edc2381e6bf7 (diff)
Apply snippets as transactions
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r--helix-term/src/ui/completion.rs46
1 files changed, 36 insertions, 10 deletions
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs
index a24da20a..6897305d 100644
--- a/helix-term/src/ui/completion.rs
+++ b/helix-term/src/ui/completion.rs
@@ -119,7 +119,9 @@ impl Completion {
start_offset: usize,
trigger_offset: usize,
) -> Transaction {
- let transaction = if let Some(edit) = &item.text_edit {
+ use helix_lsp::snippet;
+
+ if let Some(edit) = &item.text_edit {
let edit = match edit {
lsp::CompletionTextEdit::Edit(edit) => edit.clone(),
lsp::CompletionTextEdit::InsertAndReplace(item) => {
@@ -128,12 +130,38 @@ impl Completion {
}
};
- util::generate_transaction_from_completion_edit(
- doc.text(),
- doc.selection(view_id),
- edit,
- offset_encoding, // TODO: should probably transcode in Client
- )
+ if matches!(item.kind, Some(lsp::CompletionItemKind::SNIPPET))
+ || matches!(
+ item.insert_text_format,
+ Some(lsp::InsertTextFormat::SNIPPET)
+ )
+ {
+ match snippet::parse(&edit.new_text) {
+ Ok(snippet) => snippet::into_transaction(
+ snippet,
+ doc.text(),
+ doc.selection(view_id),
+ &edit,
+ doc.line_ending.as_str(),
+ offset_encoding,
+ ),
+ Err(err) => {
+ log::error!(
+ "Failed to parse snippet: {:?}, remaining output: {}",
+ &edit.new_text,
+ err
+ );
+ Transaction::new(doc.text())
+ }
+ }
+ } else {
+ util::generate_transaction_from_completion_edit(
+ doc.text(),
+ doc.selection(view_id),
+ edit,
+ offset_encoding, // TODO: should probably transcode in Client
+ )
+ }
} else {
let text = item.insert_text.as_ref().unwrap_or(&item.label);
// Some LSPs just give you an insertText with no offset ¯\_(ツ)_/¯
@@ -157,9 +185,7 @@ impl Completion {
(cursor, cursor, Some(text.into()))
})
- };
-
- transaction
+ }
}
fn completion_changes(transaction: &Transaction, trigger_offset: usize) -> Vec<Change> {