Skip to content

Commit

Permalink
Merge pull request #444 from onflow/jribbink/crypto-fix
Browse files Browse the repository at this point in the history
[LS] Add Crypto identifier import back
  • Loading branch information
turbolent authored Feb 12, 2025
2 parents 90339bb + abb7831 commit afcb47a
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 536 deletions.
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 @@ -23,7 +23,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 @@ -2048,6 +2059,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

0 comments on commit afcb47a

Please sign in to comment.