Skip to content

Commit

Permalink
Add SyntaxNode::lookup_position (#5562)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaput authored May 16, 2024
1 parent 3dc4ebf commit d143119
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 1 addition & 4 deletions crates/cairo-lang-language-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,8 @@ fn get_node_and_lookup_items(
file: FileId,
position: TextPosition,
) -> Option<(SyntaxNode, Vec<LookupItemId>)> {
// Find offset for position.
let offset = position.offset_in_file(db.upcast(), file)?;

// Find the most specific syntax node at the offset.
let node = db.file_syntax(file).to_option()?.lookup_offset(db.upcast(), offset);
let node = db.file_syntax(file).to_option()?.lookup_position(db.upcast(), position);

// Find module.
let module_id = find_node_module(db, file, node.clone()).on_none(|| {
Expand Down
10 changes: 9 additions & 1 deletion crates/cairo-lang-syntax/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use std::sync::Arc;

use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::{TextOffset, TextSpan, TextWidth};
use cairo_lang_filesystem::span::{TextOffset, TextPosition, TextSpan, TextWidth};
use cairo_lang_utils::{require, Intern, LookupIntern};
use smol_str::SmolStr;

Expand Down Expand Up @@ -158,6 +158,14 @@ impl SyntaxNode {
self.clone()
}

/// Lookups a syntax node using a position.
pub fn lookup_position(&self, db: &dyn SyntaxGroup, position: TextPosition) -> SyntaxNode {
match position.offset_in_file(db.upcast(), self.stable_ptr().file_id(db)) {
Some(offset) => self.lookup_offset(db, offset),
None => self.clone(),
}
}

/// Returns all the text under the syntax node.
/// Note that this traverses the syntax tree, and generates a new string, so use responsibly.
pub fn get_text(&self, db: &dyn SyntaxGroup) -> String {
Expand Down

0 comments on commit d143119

Please sign in to comment.