mirror of
https://github.com/Myriad-Dreamin/tinymist.git
synced 2025-11-23 20:56:42 +00:00
feat: create query crate
This commit is contained in:
parent
7e9bddb763
commit
9af8eb4b52
29 changed files with 1312 additions and 1341 deletions
40
crates/tinymist-query/src/selection_range.rs
Normal file
40
crates/tinymist-query/src/selection_range.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SelectionRangeRequest {
|
||||
pub path: PathBuf,
|
||||
pub positions: Vec<LspPosition>,
|
||||
pub position_encoding: PositionEncoding,
|
||||
}
|
||||
|
||||
pub fn selection_range(
|
||||
world: &TypstSystemWorld,
|
||||
req: SelectionRangeRequest,
|
||||
) -> Option<Vec<SelectionRange>> {
|
||||
let source = get_suitable_source_in_workspace(world, &req.path).ok()?;
|
||||
|
||||
let mut ranges = Vec::new();
|
||||
for position in req.positions {
|
||||
let typst_offset =
|
||||
lsp_to_typst::position_to_offset(position, req.position_encoding, &source);
|
||||
let tree = LinkedNode::new(source.root());
|
||||
let leaf = tree.leaf_at(typst_offset)?;
|
||||
ranges.push(range_for_node(&source, req.position_encoding, &leaf));
|
||||
}
|
||||
|
||||
Some(ranges)
|
||||
}
|
||||
|
||||
fn range_for_node(
|
||||
source: &Source,
|
||||
position_encoding: PositionEncoding,
|
||||
node: &LinkedNode,
|
||||
) -> SelectionRange {
|
||||
let range = typst_to_lsp::range(node.range(), source, position_encoding);
|
||||
SelectionRange {
|
||||
range: range.raw_range,
|
||||
parent: node
|
||||
.parent()
|
||||
.map(|node| Box::new(range_for_node(source, position_encoding, node))),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue