Skip to content

Commit

Permalink
fix: wrong semantic delta after file switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Chronostasys committed Sep 28, 2023
1 parent 86d2435 commit e39e77a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 23 deletions.
19 changes: 11 additions & 8 deletions src/ast/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub type GenericCache = Arc<RefCell<GenericCacheMap>>;
/// Context for code generation
pub struct Ctx<'a> {
pub generic_types: FxHashMap<String, Arc<RefCell<PLType>>>,
pub need_highlight: usize,
pub need_highlight: Arc<RefCell<usize>>,
pub plmod: Mod,
pub father: Option<&'a Ctx<'a>>, // father context, for symbol lookup
pub root: Option<&'a Ctx<'a>>, // root context, for symbol lookup
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<'a, 'ctx> Ctx<'a> {
) -> Ctx<'a> {
let generic_infer: GenericCache = Default::default();
Ctx {
need_highlight: 0,
need_highlight: Default::default(),
generic_types: FxHashMap::default(),
plmod: Mod::new(src_file_path.to_string(), generic_infer.clone()),
father: None,
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<'a, 'ctx> Ctx<'a> {
root = Some(self);
}
let mut ctx = Ctx {
need_highlight: self.need_highlight,
need_highlight: self.need_highlight.clone(),
generic_types: FxHashMap::default(),
plmod: self.plmod.new_child(),
father: Some(self),
Expand Down Expand Up @@ -1221,6 +1221,9 @@ impl<'a, 'ctx> Ctx<'a> {
}

pub fn send_if_go_to_def(&self, range: Range, destrange: Range, file: String) {
if self.need_highlight.borrow().ne(&0) {
return;
}
if range == Default::default() {
return;
}
Expand Down Expand Up @@ -1384,7 +1387,7 @@ impl<'a, 'ctx> Ctx<'a> {
}

pub fn push_semantic_token(&self, range: Range, tp: SemanticTokenType, modifiers: u32) {
if self.need_highlight != 0 || self.in_macro {
if self.need_highlight.borrow().ne(&0) || self.in_macro {
return;
}
self.get_root_ctx()
Expand All @@ -1394,7 +1397,7 @@ impl<'a, 'ctx> Ctx<'a> {
.push(range.to_diag_range(), type_index(tp), modifiers)
}
pub fn push_type_hints(&self, range: Range, pltype: Arc<RefCell<PLType>>) {
if self.need_highlight != 0 || self.in_macro {
if self.need_highlight.borrow().ne(&0) || self.in_macro {
return;
}
let hint = InlayHint {
Expand All @@ -1412,7 +1415,7 @@ impl<'a, 'ctx> Ctx<'a> {
self.plmod.hints.borrow_mut().push(hint);
}
pub fn push_param_hint(&self, range: Range, name: String) {
if self.need_highlight != 0 || self.in_macro {
if self.need_highlight.borrow().ne(&0) || self.in_macro {
return;
}
let hint = InlayHint {
Expand Down Expand Up @@ -1482,7 +1485,7 @@ impl<'a, 'ctx> Ctx<'a> {
}

pub fn save_if_comment_doc_hover(&self, range: Range, docs: Option<Vec<Box<NodeEnum>>>) {
if self.need_highlight != 0 {
if self.need_highlight.borrow().ne(&0) {
return;
}
let mut content = vec![];
Expand All @@ -1500,7 +1503,7 @@ impl<'a, 'ctx> Ctx<'a> {
}

pub fn save_if_hover(&self, range: Range, value: HoverContents) {
if self.need_highlight != 0 {
if self.need_highlight.borrow().ne(&0) {
return;
}
self.plmod.hovers.borrow_mut().insert(
Expand Down
6 changes: 6 additions & 0 deletions src/ast/ctx/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl<'a> Ctx<'a> {
///
/// For details, see the module documentation.
pub fn set_glob_refs(&self, name: &str, range: Range) {
if self.need_highlight.borrow().ne(&0) {
return;
}
let root = self.get_root_ctx();
root.plmod
.glob_refs
Expand Down Expand Up @@ -118,6 +121,9 @@ impl<'a> Ctx<'a> {
///
/// For details, see the module documentation.
pub fn set_local_refs(&self, refs: Arc<MutVec<Location>>, range: Range) {
if self.need_highlight.borrow().ne(&0) {
return;
}
refs.borrow_mut().push(self.get_location(range));
self.plmod.local_refs.borrow_mut().insert(range, refs);
}
Expand Down
28 changes: 15 additions & 13 deletions src/ast/node/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,19 +600,21 @@ impl Node for StatementsNode {
}
terminator = re.unwrap().get_term();
}
for (v, symbol) in &ctx.table {
if let Some(refs) = &symbol.refs {
if refs.borrow().len() <= 1 && v != "self" && !v.starts_with('_') {
symbol
.range
.new_warn(WarnCode::UNUSED_VARIABLE)
.set_source(&ctx.get_file())
.add_label(
symbol.range,
ctx.get_file(),
format_label!("Unused variable `{}`", v),
)
.add_to_ctx(ctx);
if ctx.need_highlight.borrow().eq(&0) {
for (v, symbol) in &ctx.table {
if let Some(refs) = &symbol.refs {
if refs.borrow().len() <= 1 && v != "self" && !v.starts_with('_') {
symbol
.range
.new_warn(WarnCode::UNUSED_VARIABLE)
.set_source(&ctx.get_file())
.add_label(
symbol.range,
ctx.get_file(),
format_label!("Unused variable `{}`", v),
)
.add_to_ctx(ctx);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ast/pltype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ impl FNValue {
.insert(name, Arc::new(RefCell::new(PLType::Fn(res.clone()))));

let block = ctx.block;
ctx.need_highlight += 1;
*ctx.need_highlight.borrow_mut() += 1;
let f = self.clone();
if let Some(n) = &mut self.node {
builder.rm_curr_debug_location();
Expand All @@ -914,7 +914,7 @@ impl FNValue {
} else {
unreachable!()
}
ctx.need_highlight -= 1;
*ctx.need_highlight.borrow_mut() -= 1;
ctx.position_at_end(block.unwrap(), builder);

res.fntype.ret_pltype = self
Expand Down
17 changes: 17 additions & 0 deletions src/lsp/lspserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ fn main_loop(
);
let last_tokens = Cell::new(Default::default());
let mut completions: Vec<Vec<lsp_types::CompletionItem>> = vec![];
let mut last_semantic_file = "".to_string();

log::info!("starting main loop");
for msg in &connection.receiver {
Expand Down Expand Up @@ -233,6 +234,7 @@ fn main_loop(
})
.on::<SemanticTokensFullRequest, _>(|id, params| {
let uri = url_to_path(params.text_document.uri);
last_semantic_file = uri.clone();
docin.set_file(&mut db).to(uri);
docin.set_action(&mut db).to(ActionType::SemanticTokensFull);
docin
Expand All @@ -251,6 +253,21 @@ fn main_loop(
})
.on::<SemanticTokensFullDeltaRequest, _>(|id, params| {
let uri = url_to_path(params.text_document.uri);
if last_semantic_file != uri {
let sender = connection.sender.clone();
pool.execute(move || {
send_semantic_tokens_edit(
&sender,
id,
SemanticTokensDelta {
result_id: None,
edits: vec![],
},
);
});
return;
}
last_semantic_file = uri.clone();
docin.set_file(&mut db).to(uri);
docin.set_action(&mut db).to(ActionType::SemanticTokensFull);
docin
Expand Down

0 comments on commit e39e77a

Please sign in to comment.