refactor: introduce uri_for_id and deref_syntax_at (#267)

* dev: add documentation

* refactor: introduce uri_for_id and deref_syntax_at
This commit is contained in:
Myriad-Dreamin 2024-05-09 12:32:20 +08:00 committed by GitHub
parent 6d669461f9
commit e2b7bc4b56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 236 additions and 174 deletions

View file

@ -2,11 +2,7 @@ use std::ops::Range;
use log::debug;
use crate::{
prelude::*,
syntax::{get_deref_target, DerefTarget},
SemanticRequest,
};
use crate::{prelude::*, syntax::DerefTarget, SemanticRequest};
/// The [`textDocument/declaration`] request asks the server for the declaration
/// location of a symbol at a given text document position.
@ -38,28 +34,16 @@ impl SemanticRequest for GotoDeclarationRequest {
fn request(self, ctx: &mut AnalysisContext) -> Option<Self::Response> {
let source = ctx.source_by_path(&self.path).ok()?;
let offset = ctx.to_typst_pos(self.position, &source)?;
let cursor = offset + 1;
let ast_node = LinkedNode::new(source.root()).leaf_at(cursor)?;
debug!("ast_node: {ast_node:?}", ast_node = ast_node);
let deref_target = get_deref_target(ast_node, cursor)?;
let use_site = deref_target.node();
let origin_selection_range = ctx.to_lsp_range(use_site.range(), &source);
let deref_target = ctx.deref_syntax_at(&source, self.position, 1)?;
let origin_selection_range = ctx.to_lsp_range(deref_target.node().range(), &source);
let def_use = ctx.def_use(source.clone())?;
let ref_spans = find_declarations(ctx, def_use, deref_target)?;
let mut links = vec![];
for ref_range in ref_spans {
let ref_id = source.id();
let ref_source = &source;
let span_path = ctx.path_for_id(ref_id).ok()?;
let range = ctx.to_lsp_range(ref_range, ref_source);
let uri = path_to_url(&span_path).ok()?;
let uri = ctx.uri_for_id(source.id()).ok()?;
let range = ctx.to_lsp_range(ref_range, &source);
links.push(LocationLink {
origin_selection_range: Some(origin_selection_range),