From 1438ba7bd1999b9bffa04ffc26e40da1622a4ad8 Mon Sep 17 00:00:00 2001 From: sofia Date: Sun, 3 Aug 2025 19:39:40 +0300 Subject: [PATCH] Add semantic highlighting for binop params --- reid-lsp/src/analysis.rs | 14 ++++++++++++++ reid/src/ast/mod.rs | 4 ++-- reid/src/ast/parse.rs | 6 ++++-- reid/src/ast/process.rs | 4 ++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/reid-lsp/src/analysis.rs b/reid-lsp/src/analysis.rs index 94c5f65..787946a 100644 --- a/reid-lsp/src/analysis.rs +++ b/reid-lsp/src/analysis.rs @@ -333,6 +333,20 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti } for binop in &module.binop_defs { + if binop.meta.source_module_id == module.module_id { + for param in [&binop.lhs, &binop.rhs] { + dbg!(param); + scope.state.init_types(¶m.meta, Some(param.ty.clone())); + let idx = scope + .token_idx(¶m.meta, |t| matches!(t, Token::Identifier(_))) + .unwrap_or(param.meta.range.end); + dbg!(idx, scope.tokens.get(idx)); + let symbol = scope.state.new_symbol(idx, SemanticKind::Variable); + scope.state.set_symbol(idx, symbol); + scope.variables.insert(param.name.clone(), symbol); + } + } + match &binop.fn_kind { mir::FunctionDefinitionKind::Local(block, _) => analyze_block(context, module, block, &mut scope), mir::FunctionDefinitionKind::Extern(_) => {} diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index f18f02a..0e84492 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -272,9 +272,9 @@ pub enum TopLevelStatement { #[derive(Debug)] pub struct BinopDefinition { - pub lhs: (String, Type), + pub lhs: (String, Type, TokenRange), pub op: BinaryOperator, - pub rhs: (String, Type), + pub rhs: (String, Type, TokenRange), pub return_ty: Type, pub block: Block, pub signature_range: TokenRange, diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index f04e36b..43fd7f7 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -1126,6 +1126,7 @@ impl Parse for BinopDefinition { let Some(Token::Identifier(lhs_name)) = stream.next() else { return Err(stream.expected_err("lhs name")?); }; + let lhs_range = stream.get_range_prev_curr().unwrap(); stream.expect(Token::Colon)?; let lhs_type = stream.parse()?; stream.expect(Token::ParenClose)?; @@ -1136,6 +1137,7 @@ impl Parse for BinopDefinition { let Some(Token::Identifier(rhs_name)) = stream.next() else { return Err(stream.expected_err("rhs name")?); }; + let rhs_range = stream.get_range_prev_curr().unwrap(); stream.expect(Token::Colon)?; let rhs_type = stream.parse()?; stream.expect(Token::ParenClose)?; @@ -1145,9 +1147,9 @@ impl Parse for BinopDefinition { stream.expect(Token::Arrow)?; Ok(BinopDefinition { - lhs: (lhs_name, lhs_type), + lhs: (lhs_name, lhs_type, lhs_range), op: operator, - rhs: (rhs_name, rhs_type), + rhs: (rhs_name, rhs_type, rhs_range), return_ty: stream.parse()?, block: stream.parse()?, signature_range, diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 156ccf2..067db82 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -104,13 +104,13 @@ impl ast::Module { lhs: mir::FunctionParam { name: lhs.0.clone(), ty: lhs.1 .0.into_mir(module_id), - meta: lhs.1 .1.as_meta(module_id), + meta: lhs.2.as_meta(module_id), }, op: op.mir(), rhs: mir::FunctionParam { name: rhs.0.clone(), ty: rhs.1 .0.into_mir(module_id), - meta: rhs.1 .1.as_meta(module_id), + meta: rhs.2.as_meta(module_id), }, return_type: return_ty.0.into_mir(module_id), fn_kind: mir::FunctionDefinitionKind::Local(