Skip to content

Commit 8be906a

Browse files
committed
fix hint
1 parent 56246c4 commit 8be906a

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

crates/emmylua_code_analysis/src/compilation/analyzer/lua/stats.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use emmylua_parser::{
2-
BinaryOperator, LuaAssignStat, LuaAstNode, LuaExpr, LuaFuncStat, LuaIndexExpr,
2+
BinaryOperator, LuaAssignStat, LuaAstNode, LuaAstToken, LuaExpr, LuaFuncStat, LuaIndexExpr,
33
LuaLocalFuncStat, LuaLocalStat, LuaTableField, LuaVarExpr, PathTrait,
44
};
55

@@ -542,7 +542,8 @@ pub fn try_add_class_default_call(
542542
decl_id.into(),
543543
LuaOperatorMetaMethod::Call,
544544
analyzer.file_id,
545-
index_expr.get_range(),
545+
// 必须指向名称, 使用 index_expr 的完整范围不会跳转到函数上
546+
index_expr.get_name_token()?.syntax().text_range(),
546547
OperatorFunction::DefaultCall(signature_id),
547548
);
548549
analyzer.db.get_operator_index_mut().add_operator(operator);

crates/emmylua_ls/src/handlers/test/inlay_hint_test.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(test)]
22
mod tests {
3+
use std::{ops::Deref, sync::Arc};
4+
35
use lsp_types::{InlayHint, InlayHintLabel, Location, Position, Range};
46

57
use crate::handlers::test_lib::ProviderVirtualWorkspace;
@@ -123,4 +125,38 @@ mod tests {
123125
.unwrap();
124126
assert!(result.len() == 1);
125127
}
128+
129+
#[test]
130+
fn test_class_call_hint() {
131+
let mut ws = ProviderVirtualWorkspace::new();
132+
let mut emmyrc = ws.analysis.get_emmyrc().deref().clone();
133+
emmyrc.runtime.class_default_call.function_name = "__init".to_string();
134+
emmyrc.runtime.class_default_call.force_non_colon = true;
135+
emmyrc.runtime.class_default_call.force_return_self = true;
136+
ws.analysis.update_config(Arc::new(emmyrc));
137+
138+
let result = ws
139+
.check_inlay_hint(
140+
r#"
141+
---@class MyClass
142+
local A
143+
144+
function A:__init(a)
145+
end
146+
147+
A()
148+
"#,
149+
)
150+
.unwrap();
151+
assert!(result.len() == 2);
152+
153+
let location = match &result.get(1).unwrap().label {
154+
InlayHintLabel::LabelParts(parts) => parts.first().unwrap().location.as_ref().unwrap(),
155+
InlayHintLabel::String(_) => panic!(),
156+
};
157+
assert_eq!(
158+
location.range,
159+
Range::new(Position::new(4, 27), Position::new(4, 33))
160+
);
161+
}
126162
}

0 commit comments

Comments
 (0)