From 263e3d817667211375686ae8853348017f0657ae Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 30 Mar 2022 17:47:25 -0700 Subject: [PATCH] Start work on interpreting 'label/insertText' completions These completions don't supply a range that should be overwritten, so the client needs to infer it via substring matching. Co-authored-by: Keith Simmons --- crates/project/src/project.rs | 5 ++++- crates/text/src/tests.rs | 9 +++++++++ crates/text/src/text.rs | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index e859e756f8..77dfc741c1 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -2363,7 +2363,10 @@ impl Project { Some(lsp::CompletionTextEdit::Edit(edit)) => { (range_from_lsp(edit.range), edit.new_text.clone()) } - None => (position..position, lsp_completion.label.clone()), + None => ( + this.common_prefix_at_position(position, &lsp_completion.label), + lsp_completion.label.clone(), + ), Some(lsp::CompletionTextEdit::InsertAndReplace(_)) => { log::info!("unsupported insert/replace completion"); return None; diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rs index 7961dccd56..0faf8e19de 100644 --- a/crates/text/src/tests.rs +++ b/crates/text/src/tests.rs @@ -164,6 +164,15 @@ fn test_line_len() { assert_eq!(buffer.line_len(5), 0); } +#[test] +fn test_common_prefix_at_positionn() { + let buffer = Buffer::new(0, 0, History::new("a = (bcd)".into())); + assert_eq!( + buffer.common_prefix_at_position(Point::new(0, 8), "bcdef"), + Point::new(0, 5)..Point::new(0, 8) + ) +} + #[test] fn test_text_summary_for_range() { let buffer = Buffer::new(0, 0, History::new("ab\nefg\nhklm\nnopqrs\ntuvwxyz".into())); diff --git a/crates/text/src/text.rs b/crates/text/src/text.rs index b811d08c04..0e742f8a8b 100644 --- a/crates/text/src/text.rs +++ b/crates/text/src/text.rs @@ -1508,6 +1508,13 @@ impl BufferSnapshot { .eq(needle.bytes()) } + pub fn common_prefix_at_position(&self, position: T, needle: &str) -> Range + where + T: TextDimension + ToOffset, + { + todo!() + } + pub fn text(&self) -> String { self.visible_text.to_string() }