lsp: Remove explicit rowan dependency from LSP

... by implementing more helpers on `parser::SyntaxNode` and
`parser::SyntaxToken`.
This commit is contained in:
Tobias Hunger 2024-08-26 10:29:30 +00:00 committed by Tobias Hunger
parent 06e2144535
commit d28b13cbe5
6 changed files with 28 additions and 10 deletions

View file

@ -37,7 +37,7 @@ i-slint-common = { workspace = true, features = ["default"] }
num_enum = "0.7"
strum = { workspace = true }
rowan = { workspace = true }
rowan = { version = "0.15" }
smol_str = "0.2.0"
derive_more = { workspace = true }
codemap-diagnostic = { version = "0.1.1", optional = true }

View file

@ -762,6 +762,11 @@ impl SyntaxToken {
pub fn parent(&self) -> SyntaxNode {
SyntaxNode { node: self.token.parent().unwrap(), source_file: self.source_file.clone() }
}
pub fn parent_ancestors(&self) -> impl Iterator<Item = SyntaxNode> + '_ {
self.token
.parent_ancestors()
.map(|node| SyntaxNode { node, source_file: self.source_file.clone() })
}
pub fn next_token(&self) -> Option<SyntaxToken> {
// Due to a bug (as of rowan 0.15.3), rowan::SyntaxToken::next_token doesn't work if a
// sibling don't have tokens.
@ -787,6 +792,10 @@ impl SyntaxToken {
})?;
Some(SyntaxToken { token, source_file: self.source_file.clone() })
}
pub fn prev_token(&self) -> Option<SyntaxToken> {
let token = self.token.prev_token()?;
Some(SyntaxToken { token, source_file: self.source_file.clone() })
}
pub fn text(&self) -> &str {
self.token.text()
}
@ -847,6 +856,16 @@ impl SyntaxNode {
.first_token()
.map(|token| SyntaxToken { token, source_file: self.source_file.clone() })
}
pub fn last_token(&self) -> Option<SyntaxToken> {
self.node
.last_token()
.map(|token| SyntaxToken { token, source_file: self.source_file.clone() })
}
pub fn token_at_offset(&self, offset: TextSize) -> rowan::TokenAtOffset<SyntaxToken> {
self.node
.token_at_offset(offset)
.map(|token| SyntaxToken { token, source_file: self.source_file.clone() })
}
}
#[derive(Debug, Clone, derive_more::From)]

View file

@ -85,7 +85,6 @@ dissimilar = "1.0.7"
euclid = { workspace = true }
itertools = { workspace = true }
lsp-types = { version = "0.95.0", features = ["proposed"] }
rowan = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

View file

@ -589,7 +589,7 @@ pub fn token_at_offset(doc: &syntax_nodes::Document, offset: u32) -> Option<Synt
_ => l,
},
};
Some(SyntaxToken { token, source_file: doc.source_file.clone() })
Some(token)
}
fn has_experimental_client_capability(capabilities: &ClientCapabilities, name: &str) -> bool {

View file

@ -6,7 +6,9 @@ use std::num::NonZeroUsize;
use i_slint_compiler::diagnostics::{BuildDiagnostics, SourceFile};
use i_slint_compiler::object_tree;
use i_slint_compiler::parser::{syntax_nodes, SyntaxKind, SyntaxNode, TextRange, TextSize};
use i_slint_compiler::parser::{
syntax_nodes, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
};
use i_slint_core::lengths::{LogicalPoint, LogicalRect, LogicalSize};
use slint_interpreter::ComponentInstance;
@ -464,9 +466,7 @@ fn insert_position_before_first_component(
first_exported_component
};
fn find_pre_indent_and_replacement(
token: &rowan::SyntaxToken<i_slint_compiler::parser::Language>,
) -> (String, u32) {
fn find_pre_indent_and_replacement(token: &SyntaxToken) -> (String, u32) {
match token.kind() {
SyntaxKind::Whitespace => {
if token.prev_token().is_some() {

View file

@ -11,7 +11,7 @@ use i_slint_compiler::diagnostics::Spanned;
use i_slint_compiler::langtype::{ElementType, Type};
use i_slint_compiler::object_tree::{Element, PropertyDeclaration, PropertyVisibility};
use i_slint_compiler::parser::{
syntax_nodes, Language, SyntaxKind, SyntaxNode, TextRange, TextSize,
syntax_nodes, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize,
};
use lsp_types::Url;
@ -140,7 +140,7 @@ fn add_element_properties(
}
/// Move left from the start of a `token` to include white-space and comments that go with it.
fn left_extend(token: rowan::SyntaxToken<Language>) -> rowan::SyntaxToken<Language> {
fn left_extend(token: SyntaxToken) -> SyntaxToken {
let mut current_token = token.prev_token();
let mut start_token = token.clone();
let mut last_comment = token;
@ -170,7 +170,7 @@ fn left_extend(token: rowan::SyntaxToken<Language>) -> rowan::SyntaxToken<Langua
}
/// Move right from the end of the `token` to include white-space and comments that go with it.
fn right_extend(token: rowan::SyntaxToken<Language>) -> rowan::SyntaxToken<Language> {
fn right_extend(token: SyntaxToken) -> SyntaxToken {
let mut current_token = token.next_token();
let mut end_token = token.clone();
let mut last_comment = token;