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

refactor: Reflect upstream hcl-lang changes #689

Merged
merged 2 commits into from
Nov 1, 2021
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.3.0
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902
github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419
github.com/hashicorp/hcl/v2 v2.10.1
github.com/hashicorp/terraform-exec v0.15.0
github.com/hashicorp/terraform-json v0.13.0
Expand All @@ -34,5 +34,5 @@ require (
github.com/vektra/mockery/v2 v2.9.4
github.com/zclconf/go-cty v1.9.1
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e
golang.org/x/tools v0.1.7
)
21 changes: 14 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uG
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20210803155453-7c098e4940bc/go.mod h1:xzXU6Fn+TWVaZUFxV8CyAsObi2oMgSEFAmLvCx2ArzM=
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902 h1:FxmNMZrjISkvGoXdoySct4dnk40KwhspPj9LvAaPPJo=
github.com/hashicorp/hcl-lang v0.0.0-20211014152429-0bfbdcca0902/go.mod h1:D7lBT7dekCcgbxzIHHBFvaRm42u5jY0pDoiC2J6A2KM=
github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419 h1:4coi4Rt0qgJMrkVxMa/NcwmvX6CnFS5gluVbZElhsWI=
github.com/hashicorp/hcl-lang v0.0.0-20211029211837-70678d4f0419/go.mod h1:NQq9vfyCPpRTPS4L5xeJGxp32qqp83UkDAO37NyBGF8=
github.com/hashicorp/hcl/v2 v2.10.1 h1:h4Xx4fsrRE26ohAk/1iGF/JBqRQbyUqu5Lvj60U54ys=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down Expand Up @@ -379,6 +379,7 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
github.com/zclconf/go-cty v1.8.0/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
Expand Down Expand Up @@ -428,8 +429,9 @@ golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -451,8 +453,9 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
Expand Down Expand Up @@ -484,17 +487,20 @@ golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -516,8 +522,9 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e h1:ssd5ulOvVWlh4kDSUF2SqzmMeWfjmwDXM+uGw/aQjRE=
golang.org/x/tools v0.0.0-20200323144430-8dcfad9e016e/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
29 changes: 14 additions & 15 deletions internal/cmd/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/decoder"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/logging"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (c *CompletionCommand) Run(args []string) int {
fs.SetLogger(logger)
fs.CreateAndOpenDocument(fh, "terraform", content)

file, err := fs.GetDocument(fh)
doc, err := fs.GetDocument(fh)
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand All @@ -98,7 +99,7 @@ func (c *CompletionCommand) Run(args []string) int {
URI: fh.DocumentURI(),
},
Position: lspPos,
}, file)
}, doc)
if err != nil {
c.Ui.Error(err.Error())
return 1
Expand All @@ -112,29 +113,27 @@ func (c *CompletionCommand) Run(args []string) int {
}
modMgr := module.NewSyncModuleManager(ctx, fs, ss.Modules, ss.ProviderSchemas)

mod, err := modMgr.AddModule(fh.Dir())
_, err = modMgr.AddModule(fh.Dir())
if err != nil {
c.Ui.Error(err.Error())
return 1
}

schema, err := modMgr.SchemaForModule(file.Dir())

if err != nil {
c.Ui.Error(fmt.Sprintf("failed to find schema: %s", err.Error()))
return 1
}
pos := fPos.Position()

d, err := decoder.DecoderForModule(ctx, mod)
d, err := decoder.NewDecoder(ctx, &decoder.PathReader{
ModuleReader: ss.Modules,
SchemaReader: ss.ProviderSchemas,
}).Path(lang.Path{
Path: doc.Dir(),
LanguageID: doc.LanguageID(),
})
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to find decoder: %s", err.Error()))
c.Ui.Error(err.Error())
return 1
}
d.SetSchema(schema)

pos := fPos.Position()

candidates, err := d.CandidatesAtPos(file.Filename(), pos)
candidates, err := d.CandidatesAtPos(doc.Filename(), pos)
if err != nil {
c.Ui.Error(fmt.Sprintf("failed to find candidates: %s", err.Error()))
return 1
Expand Down
115 changes: 115 additions & 0 deletions internal/codelens/reference_count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package codelens

import (
"context"
"encoding/json"
"fmt"

"github.com/hashicorp/hcl-lang/decoder"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func ReferenceCount(showReferencesCmdId string) lang.CodeLensFunc {
return func(ctx context.Context, path lang.Path, file string) ([]lang.CodeLens, error) {
lenses := make([]lang.CodeLens, 0)

pathCtx, err := decoder.PathCtx(ctx)
if err != nil {
return nil, err
}

refTargets := pathCtx.ReferenceTargets.OutermostInFile(file)
if err != nil {
return nil, err
}

// There can be two targets pointing to the same range
// e.g. when a block is targetable as type-less reference
// and as an object, which is important in most contexts
// but not here, where we present it to the user.
dedupedTargets := make(map[hcl.Range]reference.Targets, 0)
for _, refTarget := range refTargets {
rng := *refTarget.RangePtr
if _, ok := dedupedTargets[rng]; !ok {
dedupedTargets[rng] = make(reference.Targets, 0)
}
dedupedTargets[rng] = append(dedupedTargets[rng], refTarget)
}

for rng, refTargets := range dedupedTargets {
originCount := 0
var defRange *hcl.Range
for _, refTarget := range refTargets {
if refTarget.DefRangePtr != nil {
defRange = refTarget.DefRangePtr
}

originCount += len(pathCtx.ReferenceOrigins.Targeting(refTarget))
}

if originCount == 0 {
continue
}

var hclPos hcl.Pos
if defRange != nil {
hclPos = posMiddleOfRange(defRange)
} else {
hclPos = posMiddleOfRange(&rng)
}

lenses = append(lenses, lang.CodeLens{
Range: rng,
Command: lang.Command{
Title: getTitle("reference", "references", originCount),
ID: showReferencesCmdId,
Arguments: []lang.CommandArgument{
Position(ilsp.HCLPosToLSP(hclPos)),
ReferenceContext(lsp.ReferenceContext{}),
},
},
})
}
return lenses, nil
}
}

type Position lsp.Position

func (p Position) MarshalJSON() ([]byte, error) {
return json.Marshal(lsp.Position(p))
}

type ReferenceContext lsp.ReferenceContext

func (rc ReferenceContext) MarshalJSON() ([]byte, error) {
return json.Marshal(lsp.ReferenceContext(rc))
}

func posMiddleOfRange(rng *hcl.Range) hcl.Pos {
col := rng.Start.Column
byte := rng.Start.Byte

if rng.Start.Line == rng.End.Line && rng.End.Column > rng.Start.Column {
charsFromStart := (rng.End.Column - rng.Start.Column) / 2
col += charsFromStart
byte += charsFromStart
}

return hcl.Pos{
Line: rng.Start.Line,
Column: col,
Byte: byte,
}
}

func getTitle(singular, plural string, n int) string {
if n > 1 || n == 0 {
return fmt.Sprintf("%d %s", n, plural)
}
return fmt.Sprintf("%d %s", n, singular)
}
52 changes: 0 additions & 52 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ func (k *contextKey) String() string {

var (
ctxDs = &contextKey{"document storage"}
ctxClientCapsSetter = &contextKey{"client capabilities setter"}
ctxClientCaps = &contextKey{"client capabilities"}
ctxClientName = &contextKey{"client name"}
ctxTfExecPath = &contextKey{"terraform executable path"}
ctxTfExecLogPath = &contextKey{"terraform executor log path"}
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
Expand Down Expand Up @@ -56,55 +53,6 @@ func DocumentStorage(ctx context.Context) (filesystem.DocumentStorage, error) {
return fs, nil
}

func WithClientCapabilitiesSetter(ctx context.Context, caps *lsp.ClientCapabilities) context.Context {
return context.WithValue(ctx, ctxClientCapsSetter, caps)
}

func SetClientCapabilities(ctx context.Context, caps *lsp.ClientCapabilities) error {
cc, ok := ctx.Value(ctxClientCapsSetter).(*lsp.ClientCapabilities)
if !ok {
return missingContextErr(ctxClientCapsSetter)
}

*cc = *caps
return nil
}

func WithClientCapabilities(ctx context.Context, caps *lsp.ClientCapabilities) context.Context {
return context.WithValue(ctx, ctxClientCaps, caps)
}

func ClientCapabilities(ctx context.Context) (lsp.ClientCapabilities, error) {
caps, ok := ctx.Value(ctxClientCaps).(*lsp.ClientCapabilities)
if !ok {
return lsp.ClientCapabilities{}, missingContextErr(ctxClientCaps)
}

return *caps, nil
}

func WithClientName(ctx context.Context, namePtr *string) context.Context {
return context.WithValue(ctx, ctxClientName, namePtr)
}

func ClientName(ctx context.Context) (string, bool) {
name, ok := ctx.Value(ctxClientName).(*string)
if !ok {
return "", false
}
return *name, true
}

func SetClientName(ctx context.Context, name string) error {
namePtr, ok := ctx.Value(ctxClientName).(*string)
if !ok {
return missingContextErr(ctxClientName)
}

*namePtr = name
return nil
}

func WithTerraformExecLogPath(ctx context.Context, path string) context.Context {
return context.WithValue(ctx, ctxTfExecLogPath, path)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/decoder/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package decoder

import (
"context"

ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
)

type languageIdCtxKey struct{}

func WithLanguageId(ctx context.Context, langId ilsp.LanguageID) context.Context {
return context.WithValue(ctx, languageIdCtxKey{}, langId)
}

func LanguageId(ctx context.Context) (ilsp.LanguageID, bool) {
id, ok := ctx.Value(languageIdCtxKey{}).(ilsp.LanguageID)
if !ok {
return "", false
}
return id, true
}
Loading