Skip to content

Commit 34e695d

Browse files
committed
Try extract completion word from text_edit
Close #857
1 parent 2fe9c98 commit 34e695d

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/types.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,30 @@ impl VimCompleteItem {
532532
lspitem, complete_position
533533
);
534534
let abbr = lspitem.label.clone();
535-
let word = lspitem
536-
.insert_text
537-
.clone()
538-
.unwrap_or_else(|| lspitem.label.clone());
535+
536+
let word = lspitem.insert_text.clone().unwrap_or_else(|| {
537+
match (lspitem.text_edit.clone(), complete_position) {
538+
(Some(ref text_edit), Some(complete_position))
539+
if !text_edit.new_text.is_empty() =>
540+
{
541+
// TextEdit range start might be different from vim expected completion start.
542+
// From spec, TextEdit can only span one line, i.e., the current line.
543+
if text_edit.range.start.character != complete_position {
544+
text_edit
545+
.new_text
546+
.get((complete_position as usize)..)
547+
.and_then(|line| line.split_whitespace().next())
548+
.map_or_else(String::new, ToOwned::to_owned)
549+
} else {
550+
text_edit.new_text.clone()
551+
}
552+
}
553+
(Some(ref text_edit), _) if !text_edit.new_text.is_empty() => {
554+
text_edit.new_text.clone()
555+
}
556+
(_, _) => lspitem.label.clone(),
557+
}
558+
});
539559

540560
let snippet;
541561
if lspitem.insert_text_format == Some(InsertTextFormat::Snippet) {

tests/data/sample-cpp/completion.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
struct Point {
2+
int x;
3+
};
4+
5+
int main() {
6+
struct Point point;
7+
point.
8+
}

0 commit comments

Comments
 (0)