| 12 | ) |
| 13 | |
| 14 | func 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 | } |