Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
Retrieve more information from types.Info
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Mar 27, 2024
1 parent 6694b28 commit f092c0b
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions internal/lsp/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -129,9 +130,13 @@ func (tc *TypeCheck) ImportFrom(path, _ string, _ types.ImportMode) (*types.Pack
func (pi *PackageInfo) TypeCheck(tc *TypeCheck) *TypeCheckResult {
fset := token.NewFileSet()
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Instances: make(map[*ast.Ident]types.Instance),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
files := make([]*ast.File, 0, len(pi.Files))
var errs error
Expand Down Expand Up @@ -185,3 +190,46 @@ func (tcr *TypeCheckResult) Errors() []ErrorInfo {
}
return res
}

// Prints types.Info in a tabular form
// Kept only for debugging purpose.
func formatTypeInfo(fset token.FileSet, info *types.Info) string {
var items []string = nil
for expr, tv := range info.Types {
var buf strings.Builder
posn := fset.Position(expr.Pos())
tvstr := tv.Type.String()
if tv.Value != nil {
tvstr += " = " + tv.Value.String()
}
// line:col | expr | mode : type = value
fmt.Fprintf(&buf, "%2d:%2d | %-19s | %-7s : %s",
posn.Line, posn.Column, types.ExprString(expr),
mode(tv), tvstr)
items = append(items, buf.String())
}
sort.Strings(items)
return strings.Join(items, "\n")
}

func mode(tv types.TypeAndValue) string {
switch {
case tv.IsVoid():
return "void"
case tv.IsType():
return "type"
case tv.IsBuiltin():
return "builtin"
case tv.IsNil():
return "nil"
case tv.Assignable():
if tv.Addressable() {
return "var"
}
return "mapindex"
case tv.IsValue():
return "value"
default:
return "unknown"
}
}

0 comments on commit f092c0b

Please sign in to comment.