Skip to content

Commit

Permalink
WIP displaying type size on hover.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Sep 17, 2024
1 parent 85889e2 commit df7f7ea
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
89 changes: 88 additions & 1 deletion server/internal/lsp/server/TextDocumentHover.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"fmt"

"github.com/pherrymason/c3-lsp/pkg/symbols"
"github.com/pherrymason/c3-lsp/pkg/utils"
"github.com/tliron/glsp"
Expand Down Expand Up @@ -30,13 +32,98 @@ func (h *Server) TextDocumentHover(context *glsp.Context, params *protocol.Hover
extraLine += "\n\nIn module **[" + foundSymbol.GetModuleString() + "]**"
}

sizeInfo := ""
if utils.IsFeatureEnabled("SIZE_ON_HOVER") {
if hasSize(foundSymbol) {
sizeInfo = "// size = " + calculateSize(foundSymbol) + ", align = " + calculateAlignment(foundSymbol) + "\n"
}
}

hover := protocol.Hover{
Contents: protocol.MarkupContent{
Kind: protocol.MarkupKindMarkdown,
Value: "```c3" + "\n" + foundSymbol.GetHoverInfo() + "\n```" +
Value: "```c3" + "\n" +
sizeInfo +
foundSymbol.GetHoverInfo() + "\n```" +
extraLine,
},
}

return &hover, nil
}

func hasSize(symbol symbols.Indexable) bool {
_, isVariable := symbol.(*symbols.Variable)
if isVariable {
return true
}

_, isMember := symbol.(*symbols.StructMember)
if isMember {
return true
}

_, isStruct := symbol.(*symbols.Struct)
if isStruct {
return true
}

_, isBitStruct := symbol.(*symbols.Bitstruct)
if isBitStruct {
return true
}

return false
}

func calculateSize(symbol symbols.Indexable) string {
variable, isVariable := symbol.(*symbols.Variable)
if isVariable {
if variable.Type.IsPointer() {
return fmt.Sprintf("%d", utils.PointerSize())
}

if variable.Type.IsBaseTypeLanguage() {
return fmt.Sprintf("%d", getLanguageTypeSize(variable.Type.GetName()))
}
}

member, isMember := symbol.(*symbols.StructMember)
if isMember {
if member.GetType().IsPointer() {
return fmt.Sprintf("%d", utils.PointerSize())
}

if member.GetType().IsBaseTypeLanguage() {
return fmt.Sprintf("%d", getLanguageTypeSize(member.GetType().GetName()))
}
}

return "?"
}

func calculateAlignment(symbol symbols.Indexable) string {
return ""
}

func getLanguageTypeSize(typeName string) uint {
size := uint(0)
switch typeName {
case "bool":
size = 1
case "ichar", "char":
size = 8
case "short", "ushort":
size = 16
case "int", "uint":
size = 32
case "long", "ulong":
size = 64
case "int128", "uint128":
size = 128
case "iptr", "uptr", "isz", "usz":
size = 0
}

return size
}
4 changes: 4 additions & 0 deletions server/pkg/symbols/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (t *Type) SetModule(module string) {
t.module = module
}

func (t *Type) IsPointer() bool {
return t.pointer > 0
}

func (t Type) String() string {
pointerStr := strings.Repeat("*", t.pointer)
optionalStr := ""
Expand Down
31 changes: 31 additions & 0 deletions server/pkg/utils/arch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"runtime"
)

const (
CPU_UNKNOWN = iota
CPU_64 = 64
CPU_32 = 32
)

func CpuArchitecture() uint {
if runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64" {
return CPU_64
} else if runtime.GOARCH == "386" || runtime.GOARCH == "arm" {
return CPU_32
}

return CPU_UNKNOWN
}

func PointerSize() uint {
arch := CpuArchitecture()

if arch != CPU_UNKNOWN {
return arch / 8
}

return 0
}
17 changes: 17 additions & 0 deletions server/pkg/utils/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

func getFeatureFlags() map[string]bool {
return map[string]bool{
"SIZE_ON_HOVER": false,
}
}

// Function to check if a specific feature is enabled
func IsFeatureEnabled(feature string) bool {
flags := getFeatureFlags()
if enabled, exists := flags[feature]; exists {
return enabled
}
// Default behavior if feature flag is not found
return false
}

0 comments on commit df7f7ea

Please sign in to comment.