feat: create query crate

This commit is contained in:
Myriad-Dreamin 2024-03-07 13:35:45 +08:00
parent 7e9bddb763
commit 9af8eb4b52
29 changed files with 1312 additions and 1341 deletions

View 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))),
}
}