From 5bb99e5aa7b8a801259c5eebe0476a1ac16f8d38 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Thu, 7 Nov 2024 15:34:24 -0500 Subject: [PATCH] Add LSP hover support for global vars/constants --- bootstrap/stage0.c | 84 ++++++++++++++++++++++++++++----- compiler/lsp/finder.oc | 16 +++++++ tests/lsp/hover/global_const.oc | 5 ++ tests/lsp/hover/global_var.oc | 5 ++ 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 tests/lsp/hover/global_const.oc create mode 100644 tests/lsp/hover/global_var.oc diff --git a/bootstrap/stage0.c b/bootstrap/stage0.c index a1abb90..9588939 100644 --- a/bootstrap/stage0.c +++ b/bootstrap/stage0.c @@ -29510,45 +29510,107 @@ bool compiler_lsp_finder_Finder_find_in_program(compiler_lsp_finder_Finder *this } #line 463 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" - std_vector_Vector__1_pop(this->scopes); + for (std_vector_Iterator__16 __iter = std_vector_Vector__16_iter(ns->variables); std_vector_Iterator__16_has_value(&__iter); std_vector_Iterator__16_next(&__iter)) { + +#line 463 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_AST *vardecl = std_vector_Iterator__16_cur(&__iter); + +#line 463 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + { + +#line 464 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_Variable *var = vardecl->u.var_decl.var; #line 465 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" - for (std_map_ValueIterator__3 __iter = std_map_Map__3_iter_values(ns->namespaces); std_map_ValueIterator__3_has_value(&__iter); std_map_ValueIterator__3_next(&__iter)) { + if (std_span_Span_contains_loc(var->sym->span, this->loc)) { #line 465 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + return compiler_lsp_finder_Finder_set_usage(this, var->sym, NULL); + } + +#line 467 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_AST *init = vardecl->u.var_decl.init; + +#line 468 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + if (((bool)init) && compiler_lsp_finder_Finder_find_in_expression(this, init)) { + +#line 468 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + return true; + } + } + } + +#line 471 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + for (std_vector_Iterator__16 __iter = std_vector_Vector__16_iter(ns->constants); std_vector_Iterator__16_has_value(&__iter); std_vector_Iterator__16_next(&__iter)) { + +#line 471 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_AST *vardecl = std_vector_Iterator__16_cur(&__iter); + +#line 471 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + { + +#line 472 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_Variable *var = vardecl->u.var_decl.var; + +#line 473 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + if (std_span_Span_contains_loc(var->sym->span, this->loc)) { + +#line 473 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + return compiler_lsp_finder_Finder_set_usage(this, var->sym, NULL); + } + +#line 475 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + compiler_ast_nodes_AST *init = vardecl->u.var_decl.init; + +#line 476 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + if (((bool)init) && compiler_lsp_finder_Finder_find_in_expression(this, init)) { + +#line 476 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + return true; + } + } + } + +#line 479 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + std_vector_Vector__1_pop(this->scopes); + +#line 481 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" + for (std_map_ValueIterator__3 __iter = std_map_Map__3_iter_values(ns->namespaces); std_map_ValueIterator__3_has_value(&__iter); std_map_ValueIterator__3_next(&__iter)) { + +#line 481 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" compiler_ast_program_Namespace *child = std_map_ValueIterator__3_cur(&__iter); -#line 465 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 481 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" { -#line 466 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 482 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" if (std_span_Span_contains_loc(child->sym->span, this->loc)) { -#line 466 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 482 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" return compiler_lsp_finder_Finder_set_usage(this, child->sym, NULL); } -#line 467 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 483 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" if (compiler_lsp_finder_Finder_find_in_program(this, child)) { -#line 467 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 483 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" return true; } } } -#line 470 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 486 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" return false; } -#line 473 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 489 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" bool compiler_lsp_finder_Finder_find(compiler_lsp_finder_Finder *this, compiler_ast_program_Program *program) { -#line 474 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 490 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" std_vector_Vector__1_push(this->scopes, program->global->scope); -#line 475 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" +#line 491 "/Users/mustafa/ocen-lang/ocen/compiler/lsp/finder.oc" return compiler_lsp_finder_Finder_find_in_program(this, program->global); } diff --git a/compiler/lsp/finder.oc b/compiler/lsp/finder.oc index ed21e55..e22f2b9 100644 --- a/compiler/lsp/finder.oc +++ b/compiler/lsp/finder.oc @@ -460,6 +460,22 @@ def Finder::find_in_program(&this, ns: &Namespace): bool { if .find_in_statement(import_) return true } + for vardecl in ns.variables.iter() { + let var = vardecl.u.var_decl.var + if var.sym.span.contains_loc(.loc) return .set_usage(var.sym, node: null) + + let init = vardecl.u.var_decl.init + if init? and .find_in_expression(init) return true + } + + for vardecl in ns.constants.iter() { + let var = vardecl.u.var_decl.var + if var.sym.span.contains_loc(.loc) return .set_usage(var.sym, node: null) + + let init = vardecl.u.var_decl.init + if init? and .find_in_expression(init) return true + } + .scopes.pop() for child : ns.namespaces.iter_values() { diff --git a/tests/lsp/hover/global_const.oc b/tests/lsp/hover/global_const.oc new file mode 100644 index 0000000..5326833 --- /dev/null +++ b/tests/lsp/hover/global_const.oc @@ -0,0 +1,5 @@ +/// lsp: -h 4 9 +/// {"hover": "const foo: i32"} + +const foo: i32 = 4 +def main() {} diff --git a/tests/lsp/hover/global_var.oc b/tests/lsp/hover/global_var.oc new file mode 100644 index 0000000..1a43246 --- /dev/null +++ b/tests/lsp/hover/global_var.oc @@ -0,0 +1,5 @@ +/// lsp: -h 4 6 +/// {"hover": "foo: i32"} + +let foo: i32 = 4 +def main() {}