Skip to content

Commit

Permalink
CORE: Allow calling fields which contain function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Jan 5, 2024
1 parent cf59b93 commit f37adb7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
23 changes: 15 additions & 8 deletions cmd/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,32 @@ func getDefinitionToken(resolver fuckoff.Resolver, token *ast.Token) []protocol.
method = resolver.GetMethod(s, token.String(), true)
}

if method != nil {
return newDefinition(method)
if method == nil {
_, field := s.GetStaticField(token.String())
if field == nil {
_, field = s.GetField(token.String())
}

if field != nil {
if _, ok := ast.As[*ast.Func](field.Type); ok {
return newDefinition(field)
}
}
}

return newDefinition(method)
} else {
_, field := s.GetField(token.String())
if field == nil {
_, field = s.GetStaticField(token.String())
}

if field != nil {
return newDefinition(field)
}
return newDefinition(field)
}
} else if e, ok := ast.As[*ast.Enum](parent.Value.Result().Type); ok {
case_ := e.GetCase(token.String())

if case_ != nil {
return newDefinition(case_)
}
return newDefinition(case_)
}

case *ast.InitField:
Expand Down
6 changes: 5 additions & 1 deletion cmd/lsp/highlighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ func (h *highlighter) VisitMember(expr *ast.Member) {
} else if i, ok := expr.Value.(*ast.Identifier); ok && i.Kind == ast.EnumKind {
h.add(expr.Name, enumMemberKind)
} else {
h.add(expr.Name, propertyKind)
if _, ok := ast.As[*ast.Func](expr.Result().Type); ok {
h.add(expr.Name, functionKind)
} else {
h.add(expr.Name, propertyKind)
}
}

expr.AcceptChildren(h)
Expand Down
11 changes: 11 additions & 0 deletions cmd/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ func getHoverToken(resolver fuckoff.Resolver, token *ast.Token) *protocol.Hover
method = resolver.GetMethod(s, token.String(), true)
}

if method == nil {
_, field := s.GetStaticField(token.String())
if field == nil {
_, field = s.GetField(token.String())
}

if f, ok := ast.As[*ast.Func](field.Type); ok {
method = f
}
}

if method != nil {
return newHover(token, method.Signature(true))
}
Expand Down
31 changes: 23 additions & 8 deletions core/checker/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,17 @@ func (c *checker) VisitMember(expr *ast.Member) {
function := c.resolver.GetMethod(v, expr.Name.String(), true)

if function == nil {
c.error(expr.Name, "Struct '%s' does not contain static method with the name '%s'", ast.PrintType(v), expr.Name)
expr.Result().SetInvalid()
_, field := v.GetStaticField(expr.Name.String())

return
if f, ok := ast.As[*ast.Func](field.Type); ok {
expr.Result().SetValue(f, ast.AssignableFlag|ast.AddressableFlag)
return
} else {
c.error(expr.Name, "Struct '%s' does not contain static method with the name '%s'", ast.PrintType(v), expr.Name)
expr.Result().SetInvalid()

return
}
}

expr.Result().SetFunction(function)
Expand Down Expand Up @@ -856,13 +863,21 @@ func (c *checker) VisitMember(expr *ast.Member) {
if parentWantsFunction(expr) {
function := c.resolver.GetMethod(s, expr.Name.String(), false)

if function != nil {
expr.Result().SetFunction(function)
} else {
c.error(expr.Name, "Struct '%s' does not contain method '%s'", ast.PrintType(s), expr.Name)
expr.Result().SetInvalid()
if function == nil {
_, field := s.GetField(expr.Name.String())

if f, ok := ast.As[*ast.Func](field.Type); ok {
expr.Result().SetValue(f, ast.AssignableFlag|ast.AddressableFlag)
return
} else {
c.error(expr.Name, "Struct '%s' does not contain method '%s'", ast.PrintType(s), expr.Name)
expr.Result().SetInvalid()

return
}
}

expr.Result().SetFunction(function)
return
}

Expand Down

0 comments on commit f37adb7

Please sign in to comment.