-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement generic code lens interface
- Loading branch information
1 parent
6e8883e
commit f66b386
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package decoder | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/hcl-lang/lang" | ||
) | ||
|
||
func (d *PathDecoder) CodeLensesForFile(ctx context.Context, file string) ([]lang.CodeLens, error) { | ||
lenses := make([]lang.CodeLens, 0) | ||
|
||
// TODO: multierror | ||
|
||
for _, clFunc := range d.decoderCtx.CodeLenses { | ||
ctx = withPathContext(ctx, d.pathCtx) | ||
|
||
cls, err := clFunc(ctx, d.path, file) | ||
if err != nil { | ||
return lenses, err | ||
} | ||
lenses = append(lenses, cls...) | ||
} | ||
|
||
return lenses, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lang | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
) | ||
|
||
type CodeLensFunc func(ctx context.Context, path Path, file string) ([]CodeLens, error) | ||
|
||
type CodeLens struct { | ||
Range hcl.Range | ||
Command Command | ||
} | ||
|
||
type Command struct { | ||
Title string | ||
ID string | ||
Arguments []CommandArgument | ||
} | ||
|
||
type CommandArgument interface { | ||
AsJSON() (json.RawMessage, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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" | ||
) | ||
|
||
func ReferenceCount(showReferencesCmdId string, refCtx ReferenceContext) 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{ | ||
hclAsJsonPos(hclPos), | ||
refCtx, | ||
}, | ||
}, | ||
}) | ||
} | ||
return lenses, nil | ||
} | ||
} | ||
|
||
func hclAsJsonPos(pos hcl.Pos) jsonPos { | ||
return jsonPos{ | ||
Line: pos.Line, | ||
Column: pos.Column, | ||
Byte: pos.Byte, | ||
} | ||
} | ||
|
||
type jsonPos struct { | ||
Line int `json:"line"` | ||
Column int `json:"column"` | ||
Byte int `json:"byte"` | ||
} | ||
|
||
func (p jsonPos) AsJSON() (json.RawMessage, error) { | ||
b, err := json.Marshal(p) | ||
return json.RawMessage(b), err | ||
} | ||
|
||
type ReferenceContext struct { | ||
IncludeDeclaration bool `json:"includeDeclaration"` | ||
} | ||
|
||
func (refCtx ReferenceContext) AsJSON() (json.RawMessage, error) { | ||
b, err := json.Marshal(refCtx) | ||
return json.RawMessage(b), err | ||
} | ||
|
||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package codelens | ||
|
||
import "testing" | ||
|
||
func TestReferenceCount(t *testing.T) { | ||
t.Fatal("TODO") | ||
} |