Infer completions old ranges based on the syntax tree

This commit is contained in:
Max Brunsfeld
2022-04-25 13:14:05 -07:00
parent 8ddc7e6458
commit 45922603f8
4 changed files with 148 additions and 30 deletions
+3 -1
View File
@@ -15,6 +15,7 @@ test-support = [
"lsp/test-support",
"text/test-support",
"tree-sitter-rust",
"tree-sitter-typescript",
"util/test-support",
]
@@ -45,7 +46,8 @@ similar = "1.3"
smallvec = { version = "1.6", features = ["union"] }
smol = "1.2"
tree-sitter = "0.20"
tree-sitter-rust = { version = "0.20.0", optional = true }
tree-sitter-rust = { version = "*", optional = true }
tree-sitter-typescript = { version = "*", optional = true }
[dev-dependencies]
client = { path = "../client", features = ["test-support"] }
+29 -1
View File
@@ -38,7 +38,7 @@ use tree_sitter::{InputEdit, QueryCursor, Tree};
use util::TryFutureExt as _;
#[cfg(any(test, feature = "test-support"))]
pub use tree_sitter_rust;
pub use {tree_sitter_rust, tree_sitter_typescript};
pub use lsp::DiagnosticSeverity;
@@ -1638,6 +1638,34 @@ impl BufferSnapshot {
.and_then(|language| language.grammar.as_ref())
}
pub fn range_for_word_token_at<T: ToOffset + ToPoint>(
&self,
position: T,
) -> Option<Range<usize>> {
let offset = position.to_offset(self);
// Find the first leaf node that touches the position.
let tree = self.tree.as_ref()?;
let mut cursor = tree.root_node().walk();
while cursor.goto_first_child_for_byte(offset).is_some() {}
let node = cursor.node();
if node.child_count() > 0 {
return None;
}
// Check that the leaf node contains word characters.
let range = node.byte_range();
if self
.text_for_range(range.clone())
.flat_map(str::chars)
.any(|c| c.is_alphanumeric())
{
return Some(range);
} else {
None
}
}
pub fn range_for_syntax_ancestor<T: ToOffset>(&self, range: Range<T>) -> Option<Range<usize>> {
let tree = self.tree.as_ref()?;
let range = range.start.to_offset(self)..range.end.to_offset(self);