diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-10-02 17:27:24 +0200 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-10-02 17:27:24 +0200 |
| commit | fcb535b92278c22596441d9a881c4f1cd4c7fc95 (patch) | |
| tree | 1856c3ffef4da8b0142d1216527aee556d6ffb20 /src | |
| parent | 8f6b082e67784c693d51ce8f9013cfff7ca643f6 (diff) | |
Fix signature print issue, and switch stmt in a switch type stmt.
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/ast.odin | 3 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 23 | ||||
| -rw-r--r-- | src/server/analysis.odin | 117 | ||||
| -rw-r--r-- | src/server/completion.odin | 159 |
4 files changed, 162 insertions, 140 deletions
diff --git a/src/common/ast.odin b/src/common/ast.odin index 93bc52a..44bd643 100644 --- a/src/common/ast.odin +++ b/src/common/ast.odin @@ -863,6 +863,9 @@ build_string_node :: proc( build_string(n.expr, builder, remove_pointers) case ^Binary_Expr: build_string(n.left, builder, remove_pointers) + strings.write_string(builder, " ") + strings.write_string(builder, n.op.text) + strings.write_string(builder, " ") build_string(n.right, builder, remove_pointers) case ^Paren_Expr: strings.write_string(builder, "(") diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index 717bec4..32c6eb8 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -460,7 +460,6 @@ is_foreign_block_only_procedures :: proc(stmt: ^ast.Stmt) -> bool { @(private) is_value_decl_statement_ending_with_call :: proc(stmt: ^ast.Stmt) -> bool { - if value_decl, ok := stmt.derived.(^ast.Value_Decl); ok { if len(value_decl.values) == 0 { return false @@ -477,6 +476,22 @@ is_value_decl_statement_ending_with_call :: proc(stmt: ^ast.Stmt) -> bool { } @(private) +is_value_expression_call :: proc(expr: ^ast.Expr) -> bool { + #partial switch v in expr.derived { + case ^ast.Call_Expr, ^ast.Selector_Call_Expr: + return true + case ^ast.Unary_Expr: + #partial switch v2 in v.expr.derived { + case ^ast.Call_Expr, ^ast.Selector_Call_Expr: + return true + } + } + + return false +} + + +@(private) is_values_nestable_assign :: proc(list: []^ast.Expr) -> bool { if len(list) > 1 { return true @@ -992,6 +1007,10 @@ visit_stmt :: proc( if v.init != nil && is_value_decl_statement_ending_with_call(v.init) { document = cons(document, group(if_document)) + } else if v.cond != nil && + v.init == nil && + is_value_expression_call(v.cond) { + document = cons(document, group(if_document)) } else { document = cons(document, group(hang(3, if_document))) } @@ -2600,7 +2619,7 @@ visit_signature_field :: proc( document = cons(document, text("=")) document = cons_with_nopl(document, visit_expr(p, field.default_value)) } - return document + return group(document) } @(private) diff --git a/src/server/analysis.odin b/src/server/analysis.odin index 9362d03..c7081c5 100644 --- a/src/server/analysis.odin +++ b/src/server/analysis.odin @@ -385,9 +385,9 @@ resolve_type_comp_literal :: proc( if name == field_value.field.derived.(^ast.Ident).name { if symbol, ok := resolve_type_expression( - ast_context, - s.types[i], - ); ok { + ast_context, + s.types[i], + ); ok { //Stop at bitset, because we don't want to enter a comp_lit of a bitset if _, ok := symbol.value.(SymbolBitSetValue); ok { @@ -413,9 +413,9 @@ resolve_type_comp_literal :: proc( } if symbol, ok := resolve_type_expression( - ast_context, - s.types[element_index], - ); ok { + ast_context, + s.types[element_index], + ); ok { //Stop at bitset, because we don't want to enter a comp_lit of a bitset if _, ok := symbol.value.(SymbolBitSetValue); ok { return current_symbol, current_comp_lit, true @@ -616,8 +616,7 @@ resolve_generic_function_ast :: proc( is_symbol_same_typed :: proc( ast_context: ^AstContext, - a, - b: Symbol, + a, b: Symbol, flags: ast.Field_Flags = {}, ) -> bool { //relying on the fact that a is the call argument to avoid checking both sides for untyped. @@ -863,7 +862,7 @@ resolve_function_overload :: proc( for arg_expr in group.args { next_fn: if f, ok := resolve_type_expression(ast_context, arg_expr); - ok { + ok { if call_expr == nil || len(call_expr.args) == 0 { append(&candidates, f) break next_fn @@ -930,9 +929,9 @@ resolve_function_overload :: proc( break next_fn } if s, ok := resolve_type_expression( - ast_context, - p.return_types[0].type, - ); ok { + ast_context, + p.return_types[0].type, + ); ok { call_symbol = s } } @@ -954,11 +953,11 @@ resolve_function_overload :: proc( } if !is_symbol_same_typed( - ast_context, - call_symbol, - arg_symbol, - procedure.arg_types[i].flags, - ) { + ast_context, + call_symbol, + arg_symbol, + procedure.arg_types[i].flags, + ) { break next_fn } } @@ -1186,7 +1185,7 @@ resolve_type_expression :: proc( if unary, ok := v.type.derived.(^ast.Unary_Expr); ok { if unary.op.kind == .Question { if symbol, ok := resolve_type_expression(ast_context, v.expr); - ok { + ok { if union_value, ok := symbol.value.(SymbolUnionValue); ok { if len(union_value.types) != 1 { return {}, false @@ -1539,10 +1538,10 @@ resolve_type_identifier :: proc( } if local, is_global_space := get_local( - ast_context, - node.pos.offset, - node.name, - ); local != nil && ast_context.use_locals { + ast_context, + node.pos.offset, + node.name, + ); local != nil && ast_context.use_locals { is_distinct := false //Sometimes the locals are semi resolved and can no longer use the locals @@ -1868,9 +1867,9 @@ resolve_symbol_return :: proc( #partial switch v in &symbol.value { case SymbolProcedureGroupValue: if symbol, ok := resolve_function_overload( - ast_context, - v.group.derived.(^ast.Proc_Group)^, - ); ok { + ast_context, + v.group.derived.(^ast.Proc_Group)^, + ); ok { return symbol, true } else { return symbol, false @@ -1878,10 +1877,10 @@ resolve_symbol_return :: proc( case SymbolProcedureValue: if v.generic { if resolved_symbol, ok := resolve_generic_function( - ast_context, - v.arg_types, - v.return_types, - ); ok { + ast_context, + v.arg_types, + v.return_types, + ); ok { return resolved_symbol, ok } else { return symbol, true @@ -1972,10 +1971,10 @@ resolve_location_identifier :: proc( symbol: Symbol if local, _, _ := get_local_lhs_and_rhs( - ast_context, - node.pos.offset, - node.name, - ); local != nil { + ast_context, + node.pos.offset, + node.name, + ); local != nil { symbol.range = common.get_token_range(local, ast_context.file.src) uri := common.create_uri(local.pos.file, ast_context.allocator) symbol.pkg = ast_context.document_package @@ -2070,9 +2069,9 @@ resolve_first_symbol_from_binary_expression :: proc( } } else if _, ok := binary.left.derived.(^ast.Binary_Expr); ok { if s, ok := resolve_first_symbol_from_binary_expression( - ast_context, - cast(^ast.Binary_Expr)binary.left, - ); ok { + ast_context, + cast(^ast.Binary_Expr)binary.left, + ); ok { return s, ok } } @@ -2085,9 +2084,9 @@ resolve_first_symbol_from_binary_expression :: proc( } } else if _, ok := binary.right.derived.(^ast.Binary_Expr); ok { if s, ok := resolve_first_symbol_from_binary_expression( - ast_context, - cast(^ast.Binary_Expr)binary.right, - ); ok { + ast_context, + cast(^ast.Binary_Expr)binary.right, + ); ok { return s, ok } } @@ -2865,9 +2864,9 @@ get_locals_block_stmt :: proc( get_locals_using :: proc(expr: ^ast.Expr, ast_context: ^AstContext) { if symbol, expr, ok := unwrap_procedure_until_struct_or_package( - ast_context, - expr, - ); ok { + ast_context, + expr, + ); ok { #partial switch v in symbol.value { case SymbolPackageValue: if ident, ok := expr.derived.(^ast.Ident); ok { @@ -3510,7 +3509,7 @@ resolve_entire_decl :: proc( } case ^ast.Selector_Expr: if symbol, ok := resolve_type_expression(ast_context, &v.node); - ok { + ok { data.symbols[cast(uintptr)node] = SymbolAndNode { node = v, symbol = symbol, @@ -3523,7 +3522,7 @@ resolve_entire_decl :: proc( } case ^ast.Call_Expr: if symbol, ok := resolve_type_expression(ast_context, &v.node); - ok { + ok { data.symbols[cast(uintptr)node] = SymbolAndNode { node = v, symbol = symbol, @@ -3550,7 +3549,7 @@ resolve_entire_decl :: proc( ) if symbol, ok := resolve_location_selector(ast_context, v); - ok { + ok { data.symbols[cast(uintptr)node] = SymbolAndNode { node = v.field, symbol = symbol, @@ -3594,7 +3593,7 @@ resolve_entire_decl :: proc( } if symbol, ok := resolve_location_identifier(ast_context, v^); - ok { + ok { data.symbols[cast(uintptr)node] = SymbolAndNode { node = v, symbol = symbol, @@ -3750,9 +3749,9 @@ unwrap_bitset :: proc( ) { if bitset_value, ok := bitset_symbol.value.(SymbolBitSetValue); ok { if enum_symbol, ok := resolve_type_expression( - ast_context, - bitset_value.expr, - ); ok { + ast_context, + bitset_value.expr, + ); ok { if enum_value, ok := enum_symbol.value.(SymbolEnumValue); ok { return enum_value, true } @@ -4055,16 +4054,16 @@ get_document_position_context :: proc( } if !position_in_node( - position_context.comp_lit, - position_context.position, - ) { + position_context.comp_lit, + position_context.position, + ) { position_context.comp_lit = nil } if !position_in_node( - position_context.parent_comp_lit, - position_context.position, - ) { + position_context.parent_comp_lit, + position_context.position, + ) { position_context.parent_comp_lit = nil } @@ -4077,9 +4076,9 @@ get_document_position_context :: proc( } if !position_in_node( - position_context.parent_binary, - position_context.position, - ) { + position_context.parent_binary, + position_context.position, + ) { position_context.parent_binary = nil } @@ -4179,6 +4178,7 @@ fallback_position_context_completion :: proc( c == ':' || c == '\n' || c == '\r' || + c == '\t' || c == '=' || c == '<' || c == '-' || @@ -4234,7 +4234,6 @@ fallback_position_context_completion :: proc( s := string(position_context.file.src[begin_offset:end_offset]) if !partial_arrow { - only_whitespaces := true for r in s { diff --git a/src/server/completion.odin b/src/server/completion.odin index 2f695af..bb29bfb 100644 --- a/src/server/completion.odin +++ b/src/server/completion.odin @@ -102,16 +102,18 @@ get_completion_list :: proc( } if position_context.switch_type_stmt != nil && - position_context.case_clause != nil { + position_context.case_clause != nil && + position_context.switch_type_stmt.pos.offset > + position_context.switch_stmt.pos.offset { if assign, ok := position_context.switch_type_stmt.tag.derived.(^ast.Assign_Stmt); ok && assign.rhs != nil && len(assign.rhs) == 1 { ast_context.use_globals = true ast_context.use_locals = true if symbol, ok := resolve_type_expression( - &ast_context, - assign.rhs[0], - ); ok { + &ast_context, + assign.rhs[0], + ); ok { if union_value, ok := symbol.value.(SymbolUnionValue); ok { completion_type = .Switch_Type } @@ -208,15 +210,15 @@ get_comp_lit_completion :: proc( } if symbol, ok := resolve_type_expression( - ast_context, - position_context.parent_comp_lit.type, - ); ok { + ast_context, + position_context.parent_comp_lit.type, + ); ok { if comp_symbol, _, ok := resolve_type_comp_literal( - ast_context, - position_context, - symbol, - position_context.parent_comp_lit, - ); ok { + ast_context, + position_context, + symbol, + position_context.parent_comp_lit, + ); ok { ast_context.current_package = comp_symbol.pkg #partial switch v in comp_symbol.value { case SymbolStructValue: @@ -224,13 +226,13 @@ get_comp_lit_completion :: proc( ast_context.current_package = comp_symbol.pkg if resolved, ok := resolve_type_expression( - ast_context, - v.types[i], - ); ok { + ast_context, + v.types[i], + ); ok { if field_exists_in_comp_lit( - position_context.comp_lit, - name, - ) { + position_context.comp_lit, + name, + ) { continue } @@ -493,7 +495,7 @@ get_selector_completion :: proc( } if symbol, ok := resolve_type_expression(ast_context, v.types[i]); - ok { + ok { if expr, ok := position_context.selector.derived.(^ast.Selector_Expr); ok { if expr.op.text == "->" && symbol.type != .Function { @@ -615,9 +617,9 @@ get_implicit_completion :: proc( if position_context.value_decl != nil && position_context.value_decl.type != nil { if enum_value, ok := unwrap_enum( - ast_context, - position_context.value_decl.type, - ); ok { + ast_context, + position_context.value_decl.type, + ); ok { for name in enum_value.names { item := CompletionItem { label = name, @@ -653,9 +655,9 @@ get_implicit_completion :: proc( } if enum_value, ok := unwrap_enum( - ast_context, - position_context.switch_stmt.cond, - ); ok { + ast_context, + position_context.switch_stmt.cond, + ); ok { for name in enum_value.names { if name in used_enums { continue @@ -681,9 +683,9 @@ get_implicit_completion :: proc( is_bitset_assignment_operator(position_context.assign.op.text) { //bitsets if symbol, ok := resolve_type_expression( - ast_context, - position_context.assign.lhs[0], - ); ok { + ast_context, + position_context.assign.lhs[0], + ); ok { if value, ok := unwrap_bitset(ast_context, symbol); ok { for name in value.names { @@ -707,9 +709,9 @@ get_implicit_completion :: proc( is_bitset_binary_operator(position_context.binary.op.text) { //bitsets if symbol, ok := resolve_first_symbol_from_binary_expression( - ast_context, - position_context.parent_binary, - ); ok { + ast_context, + position_context.parent_binary, + ); ok { if value, ok := unwrap_bitset(ast_context, symbol); ok { for name in value.names { item := CompletionItem { @@ -745,15 +747,15 @@ get_implicit_completion :: proc( } if symbol, ok := resolve_type_expression( - ast_context, - position_context.parent_comp_lit.type, - ); ok { + ast_context, + position_context.parent_comp_lit.type, + ); ok { if comp_symbol, comp_lit, ok := resolve_type_comp_literal( - ast_context, - position_context, - symbol, - position_context.parent_comp_lit, - ); ok { + ast_context, + position_context, + symbol, + position_context.parent_comp_lit, + ); ok { if s, ok := comp_symbol.value.(SymbolStructValue); ok { ast_context.current_package = comp_symbol.pkg @@ -795,13 +797,13 @@ get_implicit_completion :: proc( list.items = items[:] return } else if bitset_symbol, ok := resolve_type_expression( - ast_context, - type, - ); ok { + ast_context, + type, + ); ok { if value, ok := unwrap_bitset( - ast_context, - bitset_symbol, - ); ok { + ast_context, + bitset_symbol, + ); ok { for name in value.names { item := CompletionItem { @@ -817,7 +819,7 @@ get_implicit_completion :: proc( } } } else if s, ok := unwrap_bitset(ast_context, comp_symbol); - ok { + ok { for enum_name in s.names { item := CompletionItem { label = enum_name, @@ -842,15 +844,15 @@ get_implicit_completion :: proc( enum_node: ^ast.Expr if position_in_node( - position_context.binary.right, - position_context.position, - ) { + position_context.binary.right, + position_context.position, + ) { context_node = position_context.binary.right enum_node = position_context.binary.left } else if position_in_node( - position_context.binary.left, - position_context.position, - ) { + position_context.binary.left, + position_context.position, + ) { context_node = position_context.binary.left enum_node = position_context.binary.right } @@ -884,7 +886,7 @@ get_implicit_completion :: proc( } else { //procedures are the only types that can return more than one value if symbol, ok := resolve_type_expression(ast_context, elem); - ok { + ok { if procedure, ok := symbol.value.(SymbolProcedureValue); ok { if procedure.return_types == nil { @@ -901,9 +903,9 @@ get_implicit_completion :: proc( if len(position_context.assign.lhs) > rhs_index { if enum_value, ok := unwrap_enum( - ast_context, - position_context.assign.lhs[rhs_index], - ); ok { + ast_context, + position_context.assign.lhs[rhs_index], + ); ok { for name in enum_value.names { item := CompletionItem { label = name, @@ -944,11 +946,9 @@ get_implicit_completion :: proc( if len(position_context.function.type.results.list) > return_index { if enum_value, ok := unwrap_enum( - ast_context, - position_context.function.type.results.list[ - return_index \ - ].type, - ); ok { + ast_context, + position_context.function.type.results.list[return_index].type, + ); ok { for name in enum_value.names { item := CompletionItem { label = name, @@ -972,16 +972,16 @@ get_implicit_completion :: proc( call^, ) if symbol, ok := resolve_type_expression(ast_context, call.expr); - ok && parameter_ok { + ok && parameter_ok { if proc_value, ok := symbol.value.(SymbolProcedureValue); ok { if len(proc_value.arg_types) <= parameter_index { return } if enum_value, ok := unwrap_enum( - ast_context, - proc_value.arg_types[parameter_index].type, - ); ok { + ast_context, + proc_value.arg_types[parameter_index].type, + ); ok { for name in enum_value.names { item := CompletionItem { label = name, @@ -1144,7 +1144,7 @@ get_identifier_completion :: proc( build_procedure_symbol_signature(&symbol) if score, ok := common.fuzzy_match(matcher, ident.name); - ok == 1 { + ok == 1 { append( &combined, CombinedResult{ @@ -1446,9 +1446,9 @@ get_type_switch_completion :: proc( if union_value, ok := unwrap_union(ast_context, assign.rhs[0]); ok { for type, i in union_value.types { if symbol, ok := resolve_type_expression( - ast_context, - union_value.types[i], - ); ok { + ast_context, + union_value.types[i], + ); ok { name := symbol.name if name in used_unions { continue @@ -1512,18 +1512,19 @@ get_core_insert_package_if_non_existent :: proc( strings.write_string(&builder, fmt.tprintf("import \"core:%v\"", pkg)) return { - newText = strings.to_string(builder), - range = { - start = { - line = ast_context.file.pkg_decl.end.line + 1, - character = 0, - }, - end = { - line = ast_context.file.pkg_decl.end.line + 1, - character = 0, + newText = strings.to_string(builder), + range = { + start = { + line = ast_context.file.pkg_decl.end.line + 1, + character = 0, + }, + end = { + line = ast_context.file.pkg_decl.end.line + 1, + character = 0, + }, }, }, - }, true + true } get_range_from_selection_start_to_dot :: proc( |