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

feat: provide builtin types and intrinsics #7

Merged
merged 15 commits into from
Aug 9, 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
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PROJECT_NAME = gnopls
BUILD_FLAGS = -mod=readonly -ldflags='$(LD_FLAGS)'
BUILD_FOLDER = ./build

.PHONY: install build clean
.PHONY: install build clean gen

## install: Install the binary.
install:
Expand All @@ -22,3 +22,7 @@ clean:
@echo Cleaning build cache...
@-rm -rf $(BUILD_FOLDER) 2> /dev/null
@go clean ./...

## gen: runs "go:generate" across all Go files
gen:
@find . -name '*.go' -print0 | xargs -0 grep -l '//go:generate' | xargs -I {} go generate {}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/dave/jennifer v1.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/badger/v3 v3.2103.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dave/jennifer v1.7.0 h1:uRbSBH9UTS64yXbh4FrMHfgfY762RD+C7bUPKODpSJE=
github.com/dave/jennifer v1.7.0/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
323 changes: 323 additions & 0 deletions internal/builtin/builtin_gen.go

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions internal/builtin/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package builtin

import (
"strings"

"go.lsp.dev/protocol"
)

//go:generate go run ../../tools/codegen-builtins -src ../../tools/gendata/builtin.go.txt -dest ./builtin_gen.go -omit Type,Type1,IntegerType,FloatType,ComplexType

// GetCompletions provides list of builtin symbols that has passed prefix in a name.
func GetCompletions(prefix string) []protocol.CompletionItem {
prefix = strings.TrimSpace(prefix)
if prefix == "" {
return nil
}

key, remainder := getBucketKey(prefix)
bucket, ok := buckets[key]
if !ok {
return nil
}

if remainder == 0 {
return bucket
}

// most buckets contain only single value
if len(bucket) == 1 && strings.HasPrefix(bucket[0].Label, prefix) {
return bucket
}

var items []protocol.CompletionItem
for _, item := range bucket {
// TODO: use fuzzy find?
if strings.HasPrefix(item.Label, prefix) {
items = append(items, item)
}
}

return items
}

func getBucketKey(str string) (rune, int) {
runes := []rune(str)
return runes[0], len(runes) - 1
}
6 changes: 3 additions & 3 deletions internal/lsp/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (tcr *TypeCheckResult) Errors() []ErrorInfo {

// Prints types.Info in a tabular form
// Kept only for debugging purpose.
func formatTypeInfo(fset token.FileSet, info *types.Info) string {
func formatTypeInfo(fset *token.FileSet, info *types.Info) string {
var items []string = nil
for expr, tv := range info.Types {
var buf strings.Builder
Expand All @@ -215,7 +215,7 @@ func formatTypeInfo(fset token.FileSet, info *types.Info) string {
// Prints types.Info in a tabular form
// Kept only for debugging purpose.
func getTypeAndValue(
fset token.FileSet,
fset *token.FileSet,
info *types.Info,
tok string,
line, offset int,
Expand Down Expand Up @@ -248,7 +248,7 @@ func getTypeAndValue(
// Use getTypeAndValue instead
// TODO: should be removed
func getTypeAndValueLight(
fset token.FileSet,
fset *token.FileSet,
info *types.Info,
tok string,
line int,
Expand Down
14 changes: 9 additions & 5 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"unicode"

"github.com/gnolang/gno/gnovm/pkg/gnomod"
"github.com/gnolang/gnopls/internal/builtin"
cmap "github.com/orcaman/concurrent-map/v2"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
Expand Down Expand Up @@ -199,7 +200,7 @@ func (s *server) Completion(ctx context.Context, reply jsonrpc2.Replier, req jso
switch n := paths[0].(type) {
case *ast.Ident:
_, tv := getTypeAndValue(
*pgf.Fset,
pgf.Fset,
pkg.TypeCheckResult.info, n.Name,
int(line),
offset,
Expand Down Expand Up @@ -267,7 +268,7 @@ func (s *server) Completion(ctx context.Context, reply jsonrpc2.Replier, req jso
return reply(ctx, nil, nil)
case *ast.CallExpr:
_, tv := getTypeAndValue(
*pgf.Fset,
pgf.Fset,
pkg.TypeCheckResult.info, types.ExprString(n),
int(line),
offset,
Expand Down Expand Up @@ -330,14 +331,17 @@ func (s *server) Completion(ctx context.Context, reply jsonrpc2.Replier, req jso
}

func completionPackageIdent(ctx context.Context, s *server, reply jsonrpc2.Replier, params protocol.CompletionParams, pgf *ParsedGnoFile, i *ast.Ident, includeFuncs bool) error {
// This function is called not just for packages but also as fallback for unresolved cases.
// So, let's also propose builtins first.
items := builtin.GetCompletions(i.Name)

for _, spec := range pgf.File.Imports {
path := spec.Path.Value[1 : len(spec.Path.Value)-1]
parts := strings.Split(path, "/")
last := parts[len(parts)-1]
if last == i.Name {
pkg := s.completionStore.lookupPkg(last)
if pkg != nil {
items := []protocol.CompletionItem{}
if includeFuncs {
for _, f := range pkg.Functions {
if !f.IsExported() {
Expand Down Expand Up @@ -367,12 +371,12 @@ func completionPackageIdent(ctx context.Context, s *server, reply jsonrpc2.Repli
Documentation: s.Doc,
})
}
return reply(ctx, items, nil)
break
}
}
}

return reply(ctx, nil, nil)
return reply(ctx, items, nil)
}

// End
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *server) Definition(ctx context.Context, reply jsonrpc2.Replier, req jso
switch n := paths[0].(type) {
case *ast.Ident:
_, tv := getTypeAndValue(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
info, n.Name,
int(line),
offset,
Expand Down Expand Up @@ -138,7 +138,7 @@ func definitionSelectorExpr(ctx context.Context, s *server, reply jsonrpc2.Repli
parentStr := types.ExprString(parent)

_, tv := getTypeAndValueLight(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.info,
exprStr,
int(line),
Expand All @@ -149,7 +149,7 @@ func definitionSelectorExpr(ctx context.Context, s *server, reply jsonrpc2.Repli
tvStr := tv.Type.String()

_, tvParent := getTypeAndValueLight(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.info,
parentStr,
int(line),
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (s *server) Hover(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2
switch n := paths[0].(type) {
case *ast.Ident:
_, tv := getTypeAndValue(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
info, n.Name,
int(line),
offset,
Expand Down Expand Up @@ -164,7 +164,7 @@ func hoverSelectorExpr(ctx context.Context, s *server, reply jsonrpc2.Replier, p
parentStr := types.ExprString(parent)

_, tv := getTypeAndValueLight(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.info,
exprStr,
int(line),
Expand All @@ -175,7 +175,7 @@ func hoverSelectorExpr(ctx context.Context, s *server, reply jsonrpc2.Replier, p
tvStr := tv.Type.String()

_, tvParent := getTypeAndValueLight(
*pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.fset,
pkg.TypeCheckResult.info,
parentStr,
int(line),
Expand Down
Loading
Loading