Skip to content

Commit afcb47a

Browse files
authored
Merge pull request #444 from onflow/jribbink/crypto-fix
[LS] Add Crypto identifier import back
2 parents 90339bb + abb7831 commit afcb47a

File tree

7 files changed

+687
-536
lines changed

7 files changed

+687
-536
lines changed

languageserver/cmd/languageserver/main_wasm.go

+11
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ import (
2929
"syscall/js"
3030

3131
"github.com/onflow/cadence/common"
32+
"github.com/onflow/cadence/stdlib"
3233

3334
"github.com/onflow/cadence-tools/languageserver/server"
35+
36+
coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts"
3437
)
3538

3639
const globalFunctionNamePrefix = "CADENCE_LANGUAGE_SERVER"
@@ -185,6 +188,13 @@ func start(id int) {
185188
return res.String(), nil
186189
}
187190

191+
identifierImportResolver := func(location common.IdentifierLocation) (code string, err error) {
192+
if location == stdlib.CryptoContractLocation {
193+
return string(coreContracts.Crypto()), nil
194+
}
195+
return "", fmt.Errorf("CLS %d: unknown identifier location: %s", id, location)
196+
}
197+
188198
languageServer, err := server.NewServer()
189199
if err != nil {
190200
panic(err)
@@ -193,6 +203,7 @@ func start(id int) {
193203
err = languageServer.SetOptions(
194204
server.WithAddressImportResolver(addressImportResolver),
195205
server.WithStringImportResolver(stringImportResolver),
206+
server.WithIdentifierImportResolver(identifierImportResolver),
196207
)
197208
if err != nil {
198209
panic(err)

languageserver/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
)
2424

2525
require (
26-
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0 // indirect
26+
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0
2727
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0 // indirect
2828
)
2929

languageserver/integration/integration.go

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func NewFlowIntegration(s *server.Server, enableFlowClient bool) (*FlowIntegrati
5353
server.WithStringImportResolver(resolve.stringImport),
5454
server.WithInitializationOptionsHandler(integration.initialize),
5555
server.WithExtendedStandardLibraryValues(FVMStandardLibraryValues()...),
56+
server.WithIdentifierImportResolver(resolve.identifierImport),
5657
}
5758

5859
if enableFlowClient {

languageserver/integration/resolvers.go

+12
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ package integration
2020

2121
import (
2222
"errors"
23+
"fmt"
2324
"path/filepath"
2425
"strings"
2526

2627
"github.com/onflow/cadence/common"
2728
"github.com/onflow/cadence/sema"
29+
"github.com/onflow/cadence/stdlib"
2830
"github.com/onflow/flow-go-sdk"
2931
"github.com/onflow/flowkit/v2"
3032
"github.com/onflow/flowkit/v2/config"
33+
34+
coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts"
3135
)
3236

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

73+
// identifierImport resolves the code for an identifier location.
74+
func (r *resolvers) identifierImport(location common.IdentifierLocation) (string, error) {
75+
if location == stdlib.CryptoContractLocation {
76+
return string(coreContracts.Crypto()), nil
77+
}
78+
return "", fmt.Errorf("unknown identifier location: %s", location)
79+
}
80+
6981
// addressContractNames returns a slice of all the contract names on the address location.
7082
func (r *resolvers) addressContractNames(address common.Address) ([]string, error) {
7183
if r.client == nil {

languageserver/server/server.go

+17
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ type Server struct {
175175
resolveAddressContractNames AddressContractNamesResolver
176176
// resolveStringImport is the optional function that is used to resolve string imports
177177
resolveStringImport StringImportResolver
178+
// resolveIdentifierImport is the optional function that is used to resolve identifier imports
179+
resolveIdentifierImport func(location common.IdentifierLocation) (string, error)
178180
// codeLensProviders are the functions that are used to provide code lenses for a checker
179181
codeLensProviders []CodeLensProvider
180182
// diagnosticProviders are the functions that are used to provide diagnostics for a checker
@@ -242,6 +244,15 @@ func WithStringImportResolver(resolver StringImportResolver) Option {
242244
}
243245
}
244246

247+
// WithIdentifierImportResolver returns a server option that sets the given function
248+
// as the function that is used to resolve identifier imports
249+
func WithIdentifierImportResolver(resolver func(location common.IdentifierLocation) (string, error)) Option {
250+
return func(s *Server) error {
251+
s.resolveIdentifierImport = resolver
252+
return nil
253+
}
254+
}
255+
245256
// WithCodeLensProvider returns a server option that adds the given function
246257
// as a function that is used to generate code lenses
247258
func WithCodeLensProvider(provider CodeLensProvider) Option {
@@ -2048,6 +2059,12 @@ func (s *Server) resolveImport(location common.Location) (program *ast.Program,
20482059
}
20492060
code, err = s.resolveAddressImport(loc)
20502061

2062+
case common.IdentifierLocation:
2063+
if s.resolveIdentifierImport == nil {
2064+
return nil, nil
2065+
}
2066+
code, err = s.resolveIdentifierImport(loc)
2067+
20512068
default:
20522069
return nil, nil
20532070
}

0 commit comments

Comments
 (0)