Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objc fixes #337

Merged
merged 4 commits into from
Apr 8, 2024
Merged
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
5 changes: 5 additions & 0 deletions src/odin/printer/visit.odin
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ is_values_nestable_if_break_assign :: proc(list: []^ast.Expr) -> bool {
#partial switch v in expr.derived {
case ^ast.Call_Expr, ^ast.Comp_Lit, ^ast.Or_Return_Expr:
return true
case ^ast.Unary_Expr:
#partial switch v2 in v.expr.derived {
case ^ast.Call_Expr:
return true
}
}
}
return false
Expand Down
30 changes: 20 additions & 10 deletions src/server/analysis.odin
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,12 @@ internal_resolve_type_expression :: proc(
v.expr,
); ok {
ast_context.use_locals = false
ast_context.current_package = selector.pkg

if selector.pkg != "" {
ast_context.current_package = selector.pkg
} else {
ast_context.current_package = ast_context.document_package
}

#partial switch s in selector.value {
case SymbolProcedureValue:
Expand All @@ -982,6 +987,12 @@ internal_resolve_type_expression :: proc(
); ok {
ast_context.use_locals = false

if selector.pkg != "" {
ast_context.current_package = selector.pkg
} else {
ast_context.current_package = ast_context.document_package
}

#partial switch s in selector.value {
case SymbolFixedArrayValue:
components_count := 0
Expand Down Expand Up @@ -1039,18 +1050,13 @@ internal_resolve_type_expression :: proc(
)
selector_expr.expr = s.return_types[0].type
selector_expr.field = v.field

return internal_resolve_type_expression(
ast_context,
selector_expr,
)
}
case SymbolStructValue:
if selector.pkg != "" {
ast_context.current_package = selector.pkg
} else {
ast_context.current_package = ast_context.document_package
}

for name, i in s.names {
if v.field != nil && name == v.field.name {
ast_context.field_name = v.field^
Expand All @@ -1063,8 +1069,6 @@ internal_resolve_type_expression :: proc(
}
}
case SymbolPackageValue:
ast_context.current_package = selector.pkg

try_build_package(ast_context.current_package)

if v.field != nil {
Expand Down Expand Up @@ -4865,10 +4869,16 @@ get_document_position_node :: proc(
case ^Selector_Call_Expr:
if position_context.hint == .Definition ||
position_context.hint == .Hover ||
position_context.hint == .SignatureHelp {
position_context.hint == .SignatureHelp ||
position_context.hint == .Completion {
position_context.selector = n.expr
position_context.field = n.call
position_context.selector_expr = cast(^Selector_Expr)node

if _, ok := n.call.derived.(^ast.Call_Expr); ok {
position_context.call = n.call
}

get_document_position(n.expr, position_context)
get_document_position(n.call, position_context)

Expand Down
18 changes: 15 additions & 3 deletions src/server/completion.odin
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,11 @@ get_selector_completion :: proc(

expr_len := 0

if basic, ok := v.len.derived.(^ast.Basic_Lit); ok {
if expr_len, ok = strconv.parse_int(basic.tok.text); !ok {
expr_len = 0
if v.len != nil {
if basic, ok := v.len.derived.(^ast.Basic_Lit); ok {
if expr_len, ok = strconv.parse_int(basic.tok.text); !ok {
expr_len = 0
}
}
}

Expand Down Expand Up @@ -1099,6 +1101,16 @@ get_implicit_completion :: proc(
ok && parameter_ok {
ast_context.current_package = symbol.pkg

//Selector call expression always set the first argument to be the type of struct called, so increment it.
if position_context.selector_expr != nil {
if selector_call, ok := position_context.selector_expr.derived.(^ast.Selector_Call_Expr);
ok {
if selector_call.call == position_context.call {
parameter_index += 1
}
}
}

if proc_value, ok := symbol.value.(SymbolProcedureValue); ok {
if len(proc_value.arg_types) <= parameter_index {
return
Expand Down
20 changes: 19 additions & 1 deletion src/server/hover.odin
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ get_hover_information :: proc(
}
}

if position_context.selector != nil && position_context.identifier != nil {
if position_context.selector != nil &&
position_context.identifier != nil &&
position_context.field == position_context.identifier {
hover.range = common.get_token_range(
position_context.identifier^,
ast_context.file.src,
Expand Down Expand Up @@ -211,6 +213,7 @@ get_hover_information :: proc(
}

selector: Symbol

selector, ok = resolve_type_expression(
&ast_context,
position_context.selector,
Expand All @@ -229,6 +232,21 @@ get_hover_information :: proc(
}
}

if v, is_proc := selector.value.(SymbolProcedureValue); is_proc {
if len(v.return_types) == 0 || v.return_types[0].type == nil {
return {}, false, false
}

ast_context.current_package = selector.pkg

if selector, ok = resolve_type_expression(
&ast_context,
v.return_types[0].type,
); !ok {
return {}, false, false
}
}

ast_context.current_package = selector.pkg

#partial switch v in selector.value {
Expand Down
148 changes: 147 additions & 1 deletion tests/objc_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import test "src:testing"


@(test)
cobj_return_type_with_selector_expression :: proc(t: ^testing.T) {
objc_return_type_with_selector_expression :: proc(t: ^testing.T) {
packages := make([dynamic]test.Package)

append(
Expand Down Expand Up @@ -46,3 +46,149 @@ cobj_return_type_with_selector_expression :: proc(t: ^testing.T) {
{"Window.initWithContentRect: my_package.Window_initWithContentRect"},
)
}

@(test)
objc_return_type_with_selector_expression_2 :: proc(t: ^testing.T) {
packages := make([dynamic]test.Package)

append(
&packages,
test.Package {
pkg = "my_package",
source = `package my_package
@(objc_class="NSWindow")
Window :: struct { dummy: int}

@(objc_type=Window, objc_name="alloc", objc_is_class_method=true)
Window_alloc :: proc "c" () -> ^Window {
}
@(objc_type=Window, objc_name="initWithContentRect")
Window_initWithContentRect :: proc (self: ^Window, contentRect: Rect, styleMask: WindowStyleMask, backing: BackingStoreType, doDefer: BOOL) -> ^Window {
}
`,
},
)

source := test.Source {
main = `package test
import "my_package"

main :: proc() {
window := my_package.Window.alloc()->initWithContentRect(
{{0, 0}, {500, 400}},
{.Titled, .Closable, .Resizable},
.Buffered,
false,
)

window->{*}
}
`,
packages = packages[:],
}

test.expect_completion_details(
t,
&source,
"->",
{"Window.initWithContentRect: my_package.Window_initWithContentRect"},
)
}


@(test)
objc_hover_chained_selector :: proc(t: ^testing.T) {
packages := make([dynamic]test.Package)

append(
&packages,
test.Package {
pkg = "my_package",
source = `package my_package
@(objc_class="NSWindow")
Window :: struct { dummy: int}

@(objc_type=Window, objc_name="alloc", objc_is_class_method=true)
Window_alloc :: proc "c" () -> ^Window {
}
@(objc_type=Window, objc_name="initWithContentRect")
Window_initWithContentRect :: proc (self: ^Window, contentRect: Rect, styleMask: WindowStyleMask, backing: BackingStoreType, doDefer: BOOL) -> ^Window {
}

My_Struct :: struct {
dummy: int,
}
`,
},
)

source := test.Source {
main = `package test
import "my_package"

main :: proc() {
window := my_package.Window.alloc()->initWithConte{*}ntRect(
{{0, 0}, {500, 400}},
{.Titled, .Closable, .Resizable},
.Buffered,
false,
)
}
`,
packages = packages[:],
}

test.expect_hover(
t,
&source,
"Window.initWithContentRect: proc(self: ^Window, contentRect: Rect, styleMask: WindowStyleMask, backing: BackingStoreType, doDefer: BOOL)",
)
}

@(test)
objc_implicit_enum_completion :: proc(t: ^testing.T) {
packages := make([dynamic]test.Package)

append(
&packages,
test.Package {
pkg = "my_package",
source = `package my_package
My_Enum :: enum {
Regular = 0,
Accessory = 1,
Prohibited = 2,
}

@(objc_class="NSWindow")
Window :: struct { dummy: int}

@(objc_type=Window, objc_name="alloc", objc_is_class_method=true)
Window_alloc :: proc "c" () -> ^Window {
}
@(objc_type=Window, objc_name="initWithContentRect")
Window_initWithContentRect :: proc (self: ^Window, my_enum: My_Enum) -> ^Window {
}

My_Struct :: struct {
dummy: int,
}
`,
},
)

source := test.Source {
main = `package test
import "my_package"

main :: proc() {
window := my_package.Window.alloc()->initWithContentRect(
.{*}
)
}
`,
packages = packages[:],
}

test.expect_completion_labels(t, &source, ".", {"Accessory"})
}