Skip to content

Commit 7ed9a83

Browse files
committed
refactor annotator
1 parent 21aee41 commit 7ed9a83

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

crates/emmylua_ls/src/handlers/emmy_annotator/build_annotator.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::collections::HashSet;
22

33
use code_analysis::{DbIndex, LuaDeclId, LuaDocument, SemanticModel};
44
use emmylua_parser::{
5-
LuaAst, LuaAstNode, LuaAstToken, LuaForRangeStat, LuaForStat, LuaLocalFuncStat, LuaLocalStat, LuaNameExpr, LuaParamList
5+
LuaAst, LuaAstNode, LuaAstToken, LuaForRangeStat, LuaForStat, LuaLocalFuncStat, LuaLocalStat,
6+
LuaNameExpr, LuaParamList,
67
};
78
use rowan::TextRange;
89

@@ -76,7 +77,7 @@ fn build_local_stat_annotator(
7677
let locals = local_stat.get_local_name_list();
7778
for local_name in locals {
7879
let mut annotator = EmmyAnnotator {
79-
typ: EmmyAnnotatorType::Local,
80+
typ: EmmyAnnotatorType::ReadOnlyLocal,
8081
ranges: vec![],
8182
};
8283
let name_token = local_name.get_name_token()?;
@@ -87,12 +88,15 @@ fn build_local_stat_annotator(
8788
.push(document.to_lsp_range(name_token_range)?);
8889

8990
let decl_id = LuaDeclId::new(file_id, local_name.get_position());
90-
let ref_ranges = db
91-
.get_reference_index()
92-
.get_local_references(&file_id, &decl_id);
91+
let reference_index = db.get_reference_index();
92+
let ref_ranges = reference_index.get_local_references(&file_id, &decl_id);
9393
if let Some(ref_ranges) = ref_ranges {
9494
for range in ref_ranges {
9595
use_range_set.insert(*range);
96+
if reference_index.is_write_range(file_id, *range) {
97+
annotator.typ = EmmyAnnotatorType::MutLocal
98+
}
99+
96100
annotator.ranges.push(document.to_lsp_range(*range)?);
97101
}
98102
}
@@ -113,7 +117,7 @@ fn build_params_annotator(
113117
let file_id = document.get_file_id();
114118
for param_name in param_list.get_params() {
115119
let mut annotator = EmmyAnnotator {
116-
typ: EmmyAnnotatorType::Param,
120+
typ: EmmyAnnotatorType::ReadonlyParam,
117121
ranges: vec![],
118122
};
119123
let name_token = param_name.get_name_token()?;
@@ -124,12 +128,15 @@ fn build_params_annotator(
124128
.push(document.to_lsp_range(name_token_range)?);
125129

126130
let decl_id = LuaDeclId::new(file_id, param_name.get_position());
127-
let ref_ranges = db
128-
.get_reference_index()
129-
.get_local_references(&file_id, &decl_id);
131+
let reference_index = db.get_reference_index();
132+
let ref_ranges = reference_index.get_local_references(&file_id, &decl_id);
130133
if let Some(ref_ranges) = ref_ranges {
131134
for range in ref_ranges {
132135
use_range_set.insert(*range);
136+
if reference_index.is_write_range(file_id, *range) {
137+
annotator.typ = EmmyAnnotatorType::MutParam
138+
}
139+
133140
annotator.ranges.push(document.to_lsp_range(*range)?);
134141
}
135142
}
@@ -181,7 +188,7 @@ fn build_for_stat_annotator(
181188
let name_range = name_token.get_range();
182189

183190
let mut annotator = EmmyAnnotator {
184-
typ: EmmyAnnotatorType::Param,
191+
typ: EmmyAnnotatorType::ReadonlyParam,
185192
ranges: vec![],
186193
};
187194

@@ -216,7 +223,7 @@ fn build_for_range_annotator(
216223
let name_range = name_token.get_range();
217224

218225
let mut annotator = EmmyAnnotator {
219-
typ: EmmyAnnotatorType::Param,
226+
typ: EmmyAnnotatorType::ReadonlyParam,
220227
ranges: vec![],
221228
};
222229

@@ -252,7 +259,7 @@ fn build_local_func_stat_annotator(
252259
let name_range = name_token.get_range();
253260

254261
let mut annotator = EmmyAnnotator {
255-
typ: EmmyAnnotatorType::Local,
262+
typ: EmmyAnnotatorType::ReadOnlyLocal,
256263
ranges: vec![],
257264
};
258265

@@ -273,4 +280,4 @@ fn build_local_func_stat_annotator(
273280
result.push(annotator);
274281

275282
Some(())
276-
}
283+
}

crates/emmylua_ls/src/handlers/emmy_annotator/emmy_annotator_request.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ pub struct EmmyAnnotator {
2525
#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
2626
#[serde(into = "u8", try_from = "u8")]
2727
pub enum EmmyAnnotatorType {
28-
Param = 0,
28+
ReadonlyParam = 0,
2929
Global = 1,
30-
Local = 2,
31-
Upvalue = 3,
30+
ReadOnlyLocal = 2,
31+
MutLocal = 3,
32+
MutParam = 4,
3233
}
3334

3435
impl From<EmmyAnnotatorType> for u8 {
@@ -40,11 +41,12 @@ impl From<EmmyAnnotatorType> for u8 {
4041
impl From<u8> for EmmyAnnotatorType {
4142
fn from(value: u8) -> Self {
4243
match value {
43-
0 => EmmyAnnotatorType::Param,
44+
0 => EmmyAnnotatorType::ReadonlyParam,
4445
1 => EmmyAnnotatorType::Global,
45-
2 => EmmyAnnotatorType::Local,
46-
3 => EmmyAnnotatorType::Upvalue,
47-
_ => EmmyAnnotatorType::Param,
46+
2 => EmmyAnnotatorType::ReadOnlyLocal,
47+
3 => EmmyAnnotatorType::MutLocal,
48+
4 => EmmyAnnotatorType::MutParam,
49+
_ => EmmyAnnotatorType::ReadOnlyLocal,
4850
}
4951
}
5052
}

0 commit comments

Comments
 (0)