Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'src/text.rs')
-rw-r--r--src/text.rs58
1 files changed, 33 insertions, 25 deletions
diff --git a/src/text.rs b/src/text.rs
index 923a23d..59dd5ae 100644
--- a/src/text.rs
+++ b/src/text.rs
@@ -16,14 +16,14 @@ use helix_core::syntax::{HighlightEvent, Loader};
use implicit_fn::implicit_fn;
use itertools::Itertools;
use lsp_types::{
- DocumentSymbol, Location, SemanticTokensLegend, SnippetTextEdit,
- TextEdit,
+ DocumentSymbol, Edit, Location, SemanticTokensLegend, TextEdit,
};
pub use manipulations::Manip;
use rootcause::option_ext::OptionExt;
use rootcause::prelude::{IteratorExt, ResultExt};
use rootcause::report;
use ropey::{Rope, RopeSlice};
+use rust_analyzer::lsp::ext::SnippetTextEdit;
use serde::{Deserialize, Serialize};
use tree_house::Language;
pub mod cursor;
@@ -275,16 +275,14 @@ impl TextArea {
self.changes
.inner
.push(Action::Inserted { at: c, insert: with.to_string() });
+ let cc = with.chars().count();
let manip = |x| {
- if x < c {
- Manip::Unmoved(x)
- } else {
- Manip::Moved(x + with.chars().count())
- }
+ if x < c { Manip::Unmoved(x) } else { Manip::Moved(x + cc) }
};
self.tabstops.as_mut().map(|x| x.manipulate(manip));
self.cursor.manipulate(manip);
self.bookmarks.manipulate(manip);
+
for m in self
.inlays
.range(Marking::idx(c as _)..)
@@ -297,7 +295,7 @@ impl TextArea {
std::collections::btree_set::Entry::Vacant(_) =>
unreachable!(),
};
- m.position += with.chars().count() as u32;
+ m.position += cc as u32;
self.inlays.insert(m);
}
self.tokens.iter_mut().for_each(|d| d.manip(manip));
@@ -360,7 +358,7 @@ impl TextArea {
text: &'b mut Rope,
) -> rootcause::Result<()> {
match insert_text_format {
- Some(lsp_types::InsertTextFormat::SNIPPET) => {
+ Some(lsp_types::InsertTextFormat::Snippet) => {
let begin = text.l_position(range.start).ok_or_report()?;
let end = text.l_position(range.end).ok_or_report()?;
text.try_remove(begin..end)?;
@@ -382,7 +380,7 @@ impl TextArea {
SnippetTextEdit { range,new_text, insert_text_format, .. }: &'b SnippetTextEdit,
) -> rootcause::Result<()> {
match insert_text_format {
- Some(lsp_types::InsertTextFormat::SNIPPET) => self
+ Some(lsp_types::InsertTextFormat::Snippet) => self
.apply_snippet(&TextEdit {
range: range.clone(),
new_text: new_text.clone(),
@@ -525,13 +523,11 @@ impl TextArea {
Some(x) => {
self.cursor.one(Cursor::new(x.r().end, &self.rope));
}
- None => {
- self.cursor.one(Cursor::new(
- x.last.clone().unwrap().r().end,
- &self.rope,
- ));
+ None if let Some(x) = &x.last => {
+ self.cursor.one(Cursor::new(x.r().end, &self.rope));
self.tabstops = None;
}
+ None => self.tabstops = None,
},
}
}
@@ -806,9 +802,9 @@ impl TextArea {
.take(c)
.zip(0..)
{
- if e.c() != '\n' {
+ if e.c() != '\n' && e.c() != '\r' {
cells.get((x + self.ho, y)).unwrap().letter =
- Some(e.c());
+ Some(if e.c() == '\t' { ' ' } else { e.c() });
let s =
&mut cells.get((x + self.ho, y)).unwrap().style;
@@ -1353,7 +1349,7 @@ fn apply() {
}
"#,
);
-
+ use lsp_types::*;
t.apply_snippet(&TextEdit {
range: lsp_types::Range {
start: Position { line: 0, character: 8 },
@@ -1386,7 +1382,7 @@ fn apply2() {
}",
);
- use lsp_types::Range;
+ use lsp_types::*;
let mut th = [
TextEdit {
range: Range {
@@ -1451,12 +1447,21 @@ fn apply2() {
pub trait SortTedits {
fn sort_tedits(&mut self);
}
+impl SortTedits for [Edit] {
+ fn sort_tedits(&mut self) {
+ self.as_mut().sort_by_key(|t| match t {
+ Edit::TextEdit(t) => Reverse(t.range.start),
+ Edit::AnnotatedTextEdit(t) => Reverse(t.text_edit.range.start),
+ Edit::SnippetTextEdit(t) => Reverse(t.range.start),
+ });
+ }
+}
impl SortTedits for [TextEdit] {
fn sort_tedits(&mut self) {
self.as_mut().sort_by_key(|t| Reverse(t.range.start));
}
}
-impl SortTedits for [SnippetTextEdit] {
+impl SortTedits for [rust_analyzer::lsp::ext::SnippetTextEdit] {
fn sort_tedits(&mut self) {
self.as_mut().sort_by_key(|t| Reverse(t.range.start));
}
@@ -1464,13 +1469,13 @@ impl SortTedits for [SnippetTextEdit] {
#[test]
fn inlays() {
- use lsp_types::{InlayHint, InlayHintLabel};
+ use lsp_types::InlayHint;
let mut t = TextArea::default();
_ = t.insert("let x = 4;");
t.set_inlay(&[InlayHint {
- position: Position { line: 0, character: 4 },
- label: InlayHintLabel::String("u".into()),
- kind: Some(lsp_types::InlayHintKind::TYPE),
+ position: lsp_types::Position { line: 0, character: 4 },
+ label: lsp_types::Label::String("u".into()),
+ kind: Some(lsp_types::InlayHintKind::Type),
text_edits: None,
tooltip: None,
padding_left: None,
@@ -1486,7 +1491,10 @@ fn inlays() {
Char('t', 2, 2),
Char(' ', 3, 3),
Fake(
- &Marking { position: 4, data: Box::new([('u', None)]) },
+ Cow::Owned(Marking {
+ position: 4,
+ data: vec![('u', None)].into_boxed_slice(),
+ }),
0,
4,
'u'