From 78509f7ae55c5719b179a2b12eac85bbb5ae376e Mon Sep 17 00:00:00 2001 From: ef3d0c3e Date: Tue, 19 Nov 2024 12:44:29 +0100 Subject: [PATCH] Add import definition & fix bug --- src/elements/import.rs | 14 ++++++++++---- src/lsp/definition.rs | 8 ++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/elements/import.rs b/src/elements/import.rs index c63a3bb..45dc9f7 100644 --- a/src/elements/import.rs +++ b/src/elements/import.rs @@ -10,6 +10,7 @@ use crate::parser::rule::RegexRule; use crate::parser::source::SourceFile; use crate::parser::source::Token; use ariadne::Fmt; +use lsp::definition; use regex::Captures; use regex::Regex; use std::rc::Rc; @@ -160,10 +161,12 @@ impl RegexRule for ImportRule { }; state.with_state(|new_state| { - let (import_doc, _) = - new_state - .parser - .parse(new_state, import, Some(document), ParseMode::default()); + let (import_doc, _) = new_state.parser.parse( + new_state, + import.clone(), + Some(document), + ParseMode::default(), + ); document.merge(import_doc.content(), import_doc.scope(), Some(&import_as)); }); @@ -202,6 +205,9 @@ impl RegexRule for ImportRule { sems.add(path, tokens.import_path); } + // Definition point to start of imported document + definition::from_source(token, &Token::new(0..0, import), &state.shared.lsp); + reports } } diff --git a/src/lsp/definition.rs b/src/lsp/definition.rs index 5efeb4b..5e2a871 100644 --- a/src/lsp/definition.rs +++ b/src/lsp/definition.rs @@ -86,8 +86,12 @@ fn from_source_impl( }; // Add definition - let target_path = std::fs::canonicalize(orignal_target.0.name().as_str()).unwrap(); - let uri = Url::from_file_path(target_path).unwrap(); + let uri = if orignal_target.0.name().starts_with("file://") { + Url::try_from(orignal_target.0.name().as_str()).unwrap() + } else { + let target_path = std::fs::canonicalize(orignal_target.0.name().as_str()).unwrap(); + Url::from_file_path(target_path).unwrap() + }; db.push(( Location { uri,