Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
28 changes: 20 additions & 8 deletions src/server/analysis.odin
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,6 @@ get_top_candiate :: proc(candidates: []Candidate) -> (Candidate, bool) {


should_resolve_all_proc_overload_possibilities :: proc(ast_context: ^AstContext, call_expr: ^ast.Call_Expr) -> bool {
// TODO: We need a better way to handle this
if ast_context.resolve_specific_overload {
return false
}
Expand All @@ -693,7 +692,12 @@ should_resolve_all_proc_overload_possibilities :: proc(ast_context: ^AstContext,
return false
}

return ast_context.position_hint == .Completion || ast_context.position_hint == .SignatureHelp || call_expr == nil
#partial switch ast_context.position_hint {
case .Completion, .SignatureHelp:
return true
case:
return call_expr == nil
}
}

/*
Expand Down Expand Up @@ -1385,10 +1389,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 +1415,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
84 changes: 80 additions & 4 deletions 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 @@ -388,12 +389,14 @@ collect_value_decl :: proc(
stmt: ^ast.Node,
foreign_attrs: []^ast.Attribute,
) {
index := build_comment_index(file)
defer destroy_comment_index(&index)
Comment thread
TheBearodactyl marked this conversation as resolved.
Outdated
value_decl, is_value_decl := stmt.derived.(^ast.Value_Decl)

if !is_value_decl {
return
}
comment, _ := get_file_comment(file, value_decl.pos.line)
comment, _ := get_file_comment(file, value_decl.pos.line, index = &index)

attributes := merge_attributes(value_decl.attributes[:], foreign_attrs)

Expand Down Expand Up @@ -1373,6 +1376,9 @@ repeat :: proc(value: string, count: int, allocator := context.allocator) -> str
// Corrects docs and comments on a Struct_Type. Creates new nodes and adds them to the provided struct
// using the provided allocator, so `v` should have the same lifetime as the allocator.
construct_struct_field_docs :: proc(file: ast.File, v: ^ast.Struct_Type, allocator := context.temp_allocator) {
index := build_comment_index(file)
defer destroy_comment_index(&index)

for field, i in v.fields.list {
// There is currently a bug in the odin parser where it adds line comments for a field to the
// docs of the following field, we address this problem here.
Expand Down Expand Up @@ -1411,14 +1417,16 @@ construct_struct_field_docs :: proc(file: ast.File, v: ^ast.Struct_Type, allocat
}
} else if field.comment == nil {
// We need to check the file to see if it contains a line comment as it might be skipped
field.comment, _ = get_file_comment(file, field.pos.line, allocator = allocator)
field.comment, _ = get_file_comment(file, field.pos.line, allocator = allocator, index = &index)
}
}
}

// Corrects docs and comments on a Bit_Field_Type. Creates new nodes and adds them to the provided bit_field
// using the provided allocator, so `v` should have the same lifetime as the allocator.
construct_bit_field_field_docs :: proc(file: ast.File, v: ^ast.Bit_Field_Type, allocator := context.temp_allocator) {
index := build_comment_index(file)
defer destroy_comment_index(&index)
for field, i in v.fields {
// There is currently a bug in the odin parser where it adds line comments for a field to the
// docs of the following field, we address this problem here.
Expand Down Expand Up @@ -1448,9 +1456,41 @@ construct_bit_field_field_docs :: proc(file: ast.File, v: ^ast.Bit_Field_Type, a
}
} else if field.comments == nil {
// We need to check the file to see if it contains a line comment as there is no next field
field.comments, _ = get_file_comment(file, field.pos.line, allocator = allocator)
field.comments, _ = get_file_comment(file, field.pos.line, allocator = allocator, index = &index)
}
}
}

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
Expand All @@ -1459,26 +1499,58 @@ 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 +1596,9 @@ get_field_docs_and_comments :: proc(
[dynamic]^ast.Comment_Group,
[dynamic]^ast.Comment_Group,
) {
index := build_comment_index(file)

@BradLewis BradLewis Dec 15, 2025

Copy link
Copy Markdown
Collaborator

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 +1629,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
Loading