Skip to content

Commit c705a17

Browse files
committed
fix tag source invalid
1 parent 276fd0e commit c705a17

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

crates/emmylua_ls/src/handlers/defination/goto_def_defination.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
use std::str::FromStr;
2+
13
use code_analysis::{LuaPropertyOwnerId, SemanticModel};
2-
use lsp_types::GotoDefinitionResponse;
4+
use lsp_types::{GotoDefinitionResponse, Location, Position, Range, Uri};
35

46
pub fn goto_def_defination(
57
semantic_model: &SemanticModel,
68
property_owner: LuaPropertyOwnerId,
79
) -> Option<GotoDefinitionResponse> {
10+
if let Some(property) = semantic_model
11+
.get_db()
12+
.get_property_index()
13+
.get_property(property_owner.clone())
14+
{
15+
if let Some(source) = &property.source {
16+
if let Some(location) = goto_source_location(source) {
17+
return Some(GotoDefinitionResponse::Scalar(location));
18+
}
19+
}
20+
}
21+
822
match property_owner {
923
LuaPropertyOwnerId::LuaDecl(decl_id) => {
1024
let decl = semantic_model
@@ -43,3 +57,30 @@ pub fn goto_def_defination(
4357

4458
None
4559
}
60+
61+
fn goto_source_location(source: &str) -> Option<Location> {
62+
let source_parts = source.split('#').collect::<Vec<_>>();
63+
if source_parts.len() == 2 {
64+
let uri = source_parts[0];
65+
let range = source_parts[1];
66+
let range_parts = range.split(':').collect::<Vec<_>>();
67+
if range_parts.len() == 2 {
68+
let mut line_str = range_parts[0];
69+
if line_str.to_ascii_lowercase().starts_with("l") {
70+
line_str = &line_str[1..];
71+
}
72+
let line = line_str.parse::<u32>().ok()?;
73+
let col = range_parts[1].parse::<u32>().ok()?;
74+
let range = Range {
75+
start: Position::new(line, col),
76+
end: Position::new(line, col),
77+
};
78+
return Some(Location {
79+
uri: Uri::from_str(uri).ok()?,
80+
range,
81+
});
82+
}
83+
}
84+
85+
None
86+
}

crates/emmylua_parser/src/syntax/node/token/tokens.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,18 +683,22 @@ impl LuaAstToken for LuaPathToken {
683683
&self.token
684684
}
685685

686-
fn can_cast(_: LuaTokenKind) -> bool
686+
fn can_cast(kind: LuaTokenKind) -> bool
687687
where
688688
Self: Sized,
689689
{
690-
true
690+
kind == LuaTokenKind::TKDocPath
691691
}
692692

693693
fn cast(syntax: LuaSyntaxToken) -> Option<Self>
694694
where
695695
Self: Sized,
696696
{
697-
Some(LuaPathToken { token: syntax })
697+
if Self::can_cast(syntax.kind().into()) {
698+
Some(LuaPathToken { token: syntax })
699+
} else {
700+
None
701+
}
698702
}
699703
}
700704

0 commit comments

Comments
 (0)