MCPcopy Create free account
hub / github.com/docker/docker-language-server / DocumentHighlight

Function DocumentHighlight

internal/bake/hcl/documentHighlight.go:14–93  ·  view source on GitHub ↗
(document document.BakeHCLDocument, position protocol.Position)

Source from the content-addressed store, hash-verified

12)
13
14func DocumentHighlight(document document.BakeHCLDocument, position protocol.Position) ([]protocol.DocumentHighlight, error) {
15 body, ok := document.File().Body.(*hclsyntax.Body)
16 if !ok {
17 return nil, errors.New("unrecognized body in HCL document")
18 }
19
20 bytes := document.Input()
21 target := ""
22 for _, block := range body.Blocks {
23 if block.Type == "group" {
24 if targets, ok := block.Body.Attributes["targets"]; ok {
25 if expr, ok := targets.Expr.(*hclsyntax.TupleConsExpr); ok {
26 for _, item := range expr.Exprs {
27 if template, ok := item.(*hclsyntax.TemplateExpr); ok && len(template.Parts) == 1 && isInsideRange(template.Parts[0].Range(), position) {
28 value, _ := template.Parts[0].Value(&hcl.EvalContext{})
29 target = value.AsString()
30 break
31 }
32 }
33 }
34 }
35 } else if block.Type == "target" && len(block.LabelRanges) > 0 && isInsideRange(block.LabelRanges[0], position) {
36 label := string(bytes[block.LabelRanges[0].Start.Byte:block.LabelRanges[0].End.Byte])
37 if Quoted(label) {
38 unquotedRange := hcl.Range{
39 Start: hcl.Pos{
40 Line: block.LabelRanges[0].Start.Line,
41 Column: block.LabelRanges[0].Start.Column + 1,
42 },
43 End: hcl.Pos{
44 Line: block.LabelRanges[0].End.Line,
45 Column: block.LabelRanges[0].End.Column - 1,
46 },
47 }
48 if isInsideRange(unquotedRange, position) {
49 target = label[1 : len(label)-1]
50 }
51 } else {
52 target = label
53 }
54 }
55 }
56
57 if target != "" {
58 ranges := []protocol.DocumentHighlight{}
59 for _, block := range body.Blocks {
60 if block.Type == "group" {
61 if targets, ok := block.Body.Attributes["targets"]; ok {
62 if expr, ok := targets.Expr.(*hclsyntax.TupleConsExpr); ok {
63 for _, item := range expr.Exprs {
64 if template, ok := item.(*hclsyntax.TemplateExpr); ok && len(template.Parts) == 1 {
65 value, _ := template.Parts[0].Value(&hcl.EvalContext{})
66 if target == value.AsString() {
67 ranges = append(ranges, protocol.DocumentHighlight{
68 Kind: types.CreateDocumentHighlightKindPointer(protocol.DocumentHighlightKindRead),
69 Range: createProtocolRange(template.Parts[0].Range(), false),
70 })
71 }

Callers 2

TestDocumentHighlightFunction · 0.70

Calls 6

isInsideRangeFunction · 0.85
QuotedFunction · 0.85
createProtocolRangeFunction · 0.85
FileMethod · 0.65
InputMethod · 0.65

Tested by 1

TestDocumentHighlightFunction · 0.56