Skip to content

Commit

Permalink
Simplify Language.FindHoverInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Jan 14, 2024
1 parent 6700bca commit 5ee17d3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
4 changes: 4 additions & 0 deletions server/lsp/indexables/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ func (e Enum) GetEnumerator(identifier string) Enumerator {

panic(fmt.Sprint(identifier, " enumerator not found"))
}

func (e Enum) GetHoverInfo() string {
return e.name
}
9 changes: 8 additions & 1 deletion server/lsp/indexables/enumerator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package indexables

import "github.com/tliron/glsp/protocol_3_16"
import (
"fmt"
"github.com/tliron/glsp/protocol_3_16"
)

type Enumerator struct {
name string
Expand Down Expand Up @@ -39,3 +42,7 @@ func (e Enumerator) GetDeclarationRange() Range {
func (e Enumerator) GetDocumentRange() Range {
return e.documentRange
}

func (e Enumerator) GetHoverInfo() string {
return fmt.Sprintf("%s: %s", e.name, e.value)
}
9 changes: 8 additions & 1 deletion server/lsp/indexables/function.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package indexables

import protocol "github.com/tliron/glsp/protocol_3_16"
import (
"fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
)

type FunctionType int

Expand Down Expand Up @@ -99,3 +102,7 @@ func (f Function) AddFunction(f2 *Function) {
func (f Function) AddStruct(s Struct) {
f.Structs[s.name] = s
}

func (f Function) GetHoverInfo() string {
return fmt.Sprintf("%s %s()", f.GetReturnType(), f.GetName())
}
1 change: 1 addition & 0 deletions server/lsp/indexables/indexable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Indexable interface {
GetDocumentURI() string
GetDeclarationRange() Range
GetDocumentRange() Range
GetHoverInfo() string
}

type IndexableCollection []Indexable
Expand Down
9 changes: 8 additions & 1 deletion server/lsp/indexables/struct.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package indexables

import protocol "github.com/tliron/glsp/protocol_3_16"
import (
"fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
)

type Struct struct {
name string
Expand Down Expand Up @@ -51,3 +54,7 @@ func NewStructMember(name string, baseType string, posRange Range) StructMember
posRange: posRange,
}
}

func (s Struct) GetHoverInfo() string {
return fmt.Sprintf("%s", s.name)
}
9 changes: 8 additions & 1 deletion server/lsp/indexables/variable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package indexables

import protocol "github.com/tliron/glsp/protocol_3_16"
import (
"fmt"
protocol "github.com/tliron/glsp/protocol_3_16"
)

type Variable struct {
Name string
Expand Down Expand Up @@ -43,3 +46,7 @@ func (v Variable) GetDeclarationRange() Range {
func (v Variable) GetDocumentRange() Range {
return v.documentRange
}

func (v Variable) GetHoverInfo() string {
return fmt.Sprintf("%s %s", v.GetType(), v.GetName())
}
26 changes: 5 additions & 21 deletions server/lsp/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lsp
import "C"
import (
"errors"
"fmt"
"github.com/pherrymason/c3-lsp/lsp/indexables"
protocol "github.com/tliron/glsp/protocol_3_16"
)
Expand Down Expand Up @@ -67,30 +66,15 @@ func (l *Language) FindHoverInformation(doc *Document, params *protocol.HoverPar

identifier := l.findClosestSymbolDeclaration(word, params.TextDocument.URI, params.Position)

// TODO: Move below code to a factory, and to be called from server so Language has less knowledge about LSP protocol.
// expected behaviour:
// hovering on variables: display variable type + any description
// hovering on functions: display function signature
// hovering on members: same as variable
var hover protocol.Hover
switch v := identifier.(type) {
case indexables.Variable:
hover = protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: fmt.Sprintf("%s %s", v.GetType(), v.GetName()),
},
}

case *indexables.Function:
hover = protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: fmt.Sprintf("%s %s()", v.GetReturnType(), v.GetName()),
},
}
case *indexables.Struct:
default:
hover := protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: identifier.GetHoverInfo(),
},
}

return hover, nil
Expand Down

0 comments on commit 5ee17d3

Please sign in to comment.