| 14 | ) |
| 15 | |
| 16 | func Definition(ctx context.Context, definitionLinkSupport bool, manager *document.Manager, documentURI uri.URI, doc document.BakeHCLDocument, position protocol.Position) (any, error) { |
| 17 | body, ok := doc.File().Body.(*hclsyntax.Body) |
| 18 | if !ok { |
| 19 | return nil, errors.New("unrecognized body in HCL document") |
| 20 | } |
| 21 | |
| 22 | for _, b := range body.Blocks { |
| 23 | if isInsideBodyRangeLines(b.Body, int(position.Line+1)) { |
| 24 | for _, attribute := range b.Body.Attributes { |
| 25 | if isInsideRange(attribute.Expr.Range(), position) { |
| 26 | return ResolveAttributeValue(ctx, definitionLinkSupport, manager, doc, body, documentURI, position, b, attribute), nil |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | for _, attribute := range body.Attributes { |
| 33 | if isInsideRange(attribute.NameRange, position) { |
| 34 | return types.CreateDefinitionResult( |
| 35 | definitionLinkSupport, |
| 36 | protocol.Range{ |
| 37 | Start: protocol.Position{ |
| 38 | Line: uint32(attribute.NameRange.Start.Line) - 1, |
| 39 | Character: uint32(attribute.NameRange.Start.Column) - 1, |
| 40 | }, |
| 41 | End: protocol.Position{ |
| 42 | Line: uint32(attribute.NameRange.End.Line) - 1, |
| 43 | Character: uint32(attribute.NameRange.End.Column) - 1, |
| 44 | }, |
| 45 | }, |
| 46 | &protocol.Range{ |
| 47 | Start: protocol.Position{ |
| 48 | Line: uint32(attribute.NameRange.Start.Line) - 1, |
| 49 | Character: uint32(attribute.NameRange.Start.Column) - 1, |
| 50 | }, |
| 51 | End: protocol.Position{ |
| 52 | Line: uint32(attribute.NameRange.End.Line) - 1, |
| 53 | Character: uint32(attribute.NameRange.End.Column) - 1, |
| 54 | }, |
| 55 | }, |
| 56 | string(documentURI), |
| 57 | ), nil |
| 58 | } |
| 59 | |
| 60 | if isInsideRange(attribute.SrcRange, position) { |
| 61 | return ResolveAttributeValue(ctx, definitionLinkSupport, manager, doc, body, documentURI, position, nil, attribute), nil |
| 62 | } |
| 63 | } |
| 64 | return nil, nil |
| 65 | } |
| 66 | |
| 67 | func ResolveAttributeValue(ctx context.Context, definitionLinkSupport bool, manager *document.Manager, doc document.BakeHCLDocument, body *hclsyntax.Body, documentURI uri.URI, position protocol.Position, sourceBlock *hclsyntax.Block, attribute *hclsyntax.Attribute) any { |
| 68 | if tupleConsExpr, ok := attribute.Expr.(*hclsyntax.TupleConsExpr); ok { |