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

[LS] Add Crypto identifier import back #444

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions languageserver/cmd/languageserver/main_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ import (
"syscall/js"

"github.com/onflow/cadence/common"
"github.com/onflow/cadence/stdlib"

"github.com/onflow/cadence-tools/languageserver/server"

coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts"
)

const globalFunctionNamePrefix = "CADENCE_LANGUAGE_SERVER"
Expand Down Expand Up @@ -185,6 +188,13 @@ func start(id int) {
return res.String(), nil
}

identifierImportResolver := func(location common.IdentifierLocation) (code string, err error) {
if location == stdlib.CryptoContractLocation {
return string(coreContracts.Crypto()), nil
}
return "", fmt.Errorf("CLS %d: unknown identifier location: %s", id, location)
}

languageServer, err := server.NewServer()
if err != nil {
panic(err)
Expand All @@ -193,6 +203,7 @@ func start(id int) {
err = languageServer.SetOptions(
server.WithAddressImportResolver(addressImportResolver),
server.WithStringImportResolver(stringImportResolver),
server.WithIdentifierImportResolver(identifierImportResolver),
)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion languageserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
)

require (
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0 // indirect
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0 // indirect
)

Expand Down
1 change: 1 addition & 0 deletions languageserver/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewFlowIntegration(s *server.Server, enableFlowClient bool) (*FlowIntegrati
server.WithStringImportResolver(resolve.stringImport),
server.WithInitializationOptionsHandler(integration.initialize),
server.WithExtendedStandardLibraryValues(FVMStandardLibraryValues()...),
server.WithIdentifierImportResolver(resolve.identifierImport),
}

if enableFlowClient {
Expand Down
12 changes: 12 additions & 0 deletions languageserver/integration/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ package integration

import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/onflow/cadence/common"
"github.com/onflow/cadence/sema"
"github.com/onflow/cadence/stdlib"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/config"

coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts"
)

type resolvers struct {
Expand Down Expand Up @@ -66,6 +70,14 @@ func (r *resolvers) addressImport(location common.AddressLocation) (string, erro
return string(account.Contracts[location.Name]), nil
}

// identifierImport resolves the code for an identifier location.
func (r *resolvers) identifierImport(location common.IdentifierLocation) (string, error) {
if location == stdlib.CryptoContractLocation {
return string(coreContracts.Crypto()), nil
}
return "", fmt.Errorf("unknown identifier location: %s", location)
}

// addressContractNames returns a slice of all the contract names on the address location.
func (r *resolvers) addressContractNames(address common.Address) ([]string, error) {
if r.client == nil {
Expand Down
17 changes: 17 additions & 0 deletions languageserver/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ type Server struct {
resolveAddressContractNames AddressContractNamesResolver
// resolveStringImport is the optional function that is used to resolve string imports
resolveStringImport StringImportResolver
// resolveIdentifierImport is the optional function that is used to resolve identifier imports
resolveIdentifierImport func(location common.IdentifierLocation) (string, error)
// codeLensProviders are the functions that are used to provide code lenses for a checker
codeLensProviders []CodeLensProvider
// diagnosticProviders are the functions that are used to provide diagnostics for a checker
Expand Down Expand Up @@ -242,6 +244,15 @@ func WithStringImportResolver(resolver StringImportResolver) Option {
}
}

// WithIdentifierImportResolver returns a server option that sets the given function
// as the function that is used to resolve identifier imports
func WithIdentifierImportResolver(resolver func(location common.IdentifierLocation) (string, error)) Option {
return func(s *Server) error {
s.resolveIdentifierImport = resolver
return nil
}
}

// WithCodeLensProvider returns a server option that adds the given function
// as a function that is used to generate code lenses
func WithCodeLensProvider(provider CodeLensProvider) Option {
Expand Down Expand Up @@ -2052,6 +2063,12 @@ func (s *Server) resolveImport(location common.Location) (program *ast.Program,
}
code, err = s.resolveAddressImport(loc)

case common.IdentifierLocation:
if s.resolveIdentifierImport == nil {
return nil, nil
}
code, err = s.resolveIdentifierImport(loc)

default:
return nil, nil
}
Expand Down
Loading
Loading