Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions src/server/analysis.odin
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,13 @@ resolve_function_overload :: proc(ast_context: ^AstContext, group: ^ast.Proc_Gro
resolve_call_arg_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Expr) -> (Symbol, bool) {
old_current_package := ast_context.current_package
ast_context.current_package = ast_context.document_package

old_use_locals := ast_context.use_locals
ast_context.use_locals = true

defer {
ast_context.current_package = old_current_package
ast_context.use_locals = old_use_locals
}

return resolve_type_expression(ast_context, node)
Expand Down Expand Up @@ -1124,6 +1129,12 @@ resolve_location_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Ex
}

resolve_type_expression :: proc(ast_context: ^AstContext, node: ^ast.Expr) -> (Symbol, bool) {
if node != nil {
if _, ok := node.derived.(^ast.Bad_Expr); ok {
return {}, false
}
}

clear(&ast_context.recursion_map)
symbol := Symbol{}
ok := internal_resolve_type_expression(ast_context, node, &symbol)
Expand Down Expand Up @@ -1385,10 +1396,14 @@ resolve_call_directive :: proc(ast_context: ^AstContext, call: ^ast.Call_Expr) -
if len(call.args) == 1 {
ident := new_type(ast.Ident, call.pos, call.end, ast_context.allocator)
ident.name = "u8"
value := SymbolSliceValue{
expr = ident
value := SymbolSliceValue {
expr = ident,
}
symbol := Symbol {
name = "#load",
pkg = ast_context.current_package,
value = value,
}
symbol := Symbol{name = "#load", pkg = ast_context.current_package, value = value}
return symbol, true
} else if len(call.args) == 2 {
return resolve_type_expression(ast_context, call.args[1])
Expand All @@ -1407,10 +1422,14 @@ resolve_call_directive :: proc(ast_context: ^AstContext, call: ^ast.Call_Expr) -
selector := new_type(ast.Selector_Expr, call.pos, call.end, ast_context.allocator)
selector.expr = pkg
selector.field = field
value := SymbolSliceValue{
expr = selector
value := SymbolSliceValue {
expr = selector,
}
symbol := Symbol {
name = "#load_directory",
pkg = ast_context.current_package,
value = value,
}
symbol := Symbol{name = "#load_directory", pkg = ast_context.current_package, value = value}
return symbol, true
}

Expand Down
71 changes: 70 additions & 1 deletion src/server/ast.odin
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#+feature dynamic-literals
package server

import "base:runtime"
import "core:fmt"
import "core:mem"
import "core:odin/ast"
Expand Down Expand Up @@ -1453,32 +1454,96 @@ construct_bit_field_field_docs :: proc(file: ast.File, v: ^ast.Bit_Field_Type, a
}
}

Comment_Index :: struct {
line_to_idx: map[int][dynamic]int,
allocator: runtime.Allocator,
}

build_comment_index :: proc(file: ast.File, allocator := context.allocator) -> Comment_Index {
index := Comment_Index {
line_to_idx = make(map[int][dynamic]int, allocator),
allocator = allocator,
}

for c, i in file.comments {
line := c.pos.line

if line not_in index.line_to_idx {
index.line_to_idx[line] = make([dynamic]int, 0, 1, allocator)
}

append(&index.line_to_idx[line], i)
}

return index
}

destroy_comment_index :: proc(index: ^Comment_Index) {
for _, &indices in index.line_to_idx {
delete(indices)
}

delete(index.line_to_idx)
}

// Retrives the comment group from the specified line of the file
// Returns the index where the comment was found
get_file_comment :: proc(
file: ast.File,
line: int,
start_index := 0,
index: ^Comment_Index = nil,
allocator := context.temp_allocator,
) -> (
^ast.Comment_Group,
int,
) {
// TODO: linear scan might be a bit slow for files with lots of comments?
if index != nil {
indices, ok := index.line_to_idx[line]

if !ok {
return nil, -1
}

for idx in indices {
if idx >= start_index {
c := file.comments[idx]

for item, j in c.list {
comment := new_type(ast.Comment_Group, item.pos, parser.end_pos(item), allocator)

if j == len(c.list) - 1 {
comment.list = c.list[j:]
} else {
comment.list = c.list[j:j + 1]
}

return comment, idx
}
}
}

return nil, -1
}

for i := start_index; i < len(file.comments); i += 1 {
c := file.comments[i]

if c.pos.line == line {
for item, j in c.list {
comment := new_type(ast.Comment_Group, item.pos, parser.end_pos(item), allocator)

if j == len(c.list) - 1 {
comment.list = c.list[j:]
} else {
comment.list = c.list[j:j + 1]
}

return comment, i
}
}
}

return nil, -1
}

Expand Down Expand Up @@ -1524,6 +1589,9 @@ get_field_docs_and_comments :: proc(
[dynamic]^ast.Comment_Group,
[dynamic]^ast.Comment_Group,
) {
index := build_comment_index(file)
Copy link
Collaborator

@BradLewis BradLewis Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment applies with this index as before.

defer destroy_comment_index(&index)

docs := make([dynamic]^ast.Comment_Group, allocator)
comments := make([dynamic]^ast.Comment_Group, allocator)
prev_line := -1
Expand Down Expand Up @@ -1554,6 +1622,7 @@ get_field_docs_and_comments :: proc(
n.pos.line,
start_index = last_comment + 1,
allocator = allocator,
index = &index,
)
}

Expand Down
144 changes: 142 additions & 2 deletions src/server/completion.odin
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,152 @@ get_directive_completion :: proc(
results: ^[dynamic]CompletionResult,
) -> bool {
is_incomplete := false
context_directives := make([dynamic]CompletionResult, 0, context.temp_allocator)

if position_context.value_decl != nil {
for value in position_context.value_decl.values {
if _, ok := value.derived.(^ast.Struct_Type); ok {
append_struct_directives(&context_directives)
break
} else if _, ok := value.derived.(^ast.Bit_Field_Type); ok {
append_bitfield_directives(&context_directives)
break
} else if _, ok := value.derived.(^ast.Union_Type); ok {
append_union_directives(&context_directives)
break
}
}
}

if position_context.function != nil {
append_procedure_directives(&context_directives)
}

if position_context.switch_stmt != nil || position_context.switch_type_stmt != nil {
append_switch_directives(&context_directives)
}

if len(context_directives) > 0 {
append(results, ..context_directives[:])
} else {
append(results, ..completion_items_directives[:])
}

// Right now just return all the possible completions, but later on I should give the context specific ones
append(results, ..completion_items_directives[:])
return is_incomplete
}

@(private = "file")
append_struct_directives :: proc(results: ^[dynamic]CompletionResult) {
struct_directives := []string{"align", "packed", "raw_union"}

for directive in struct_directives {
if doc, ok := directive_docs[directive]; ok {
documentation := MarkupContent {
kind = "markdown",
value = doc,
}
append(
results,
CompletionResult {
completion_item = CompletionItem {
label = directive,
kind = .Constant,
documentation = documentation,
},
},
)
}
}
}

@(private = "file")
append_bitfield_directives :: proc(results: ^[dynamic]CompletionResult) {
if doc, ok := directive_docs["align"]; ok {
documentation := MarkupContent {
kind = "markdown",
value = doc,
}
append(
results,
CompletionResult {
completion_item = CompletionItem{label = "align", kind = .Constant, documentation = documentation},
},
)
}
}

@(private = "file")
append_union_directives :: proc(results: ^[dynamic]CompletionResult) {
union_directives := []string{"no_nil", "shared_nil", "align"}

for directive in union_directives {
if doc, ok := directive_docs[directive]; ok {
documentation := MarkupContent {
kind = "markdown",
value = doc,
}
append(
results,
CompletionResult {
completion_item = CompletionItem {
label = directive,
kind = .Constant,
documentation = documentation,
},
},
)
}
}
}

@(private = "file")
append_procedure_directives :: proc(results: ^[dynamic]CompletionResult) {
proc_directives := []string{"optional_ok", "require_results", "deprecated", "cold", "test"}

for directive in proc_directives {
if doc, ok := directive_docs[directive]; ok {
documentation := MarkupContent {
kind = "markdown",
value = doc,
}
append(
results,
CompletionResult {
completion_item = CompletionItem {
label = directive,
kind = .Constant,
documentation = documentation,
},
},
)
}
}
}

@(private = "file")
append_switch_directives :: proc(results: ^[dynamic]CompletionResult) {
switch_directives := []string{"partial"}

for directive in switch_directives {
if doc, ok := directive_docs[directive]; ok {
documentation := MarkupContent {
kind = "markdown",
value = doc,
}
append(
results,
CompletionResult {
completion_item = CompletionItem {
label = directive,
kind = .Constant,
documentation = documentation,
},
},
)
}
}
}

get_comp_lit_completion :: proc(
ast_context: ^AstContext,
position_context: ^DocumentPositionContext,
Expand Down
Loading