(ctx context.Context, definitionLinkSupport bool, manager *document.Manager, doc document.BakeHCLDocument, body *hclsyntax.Body, documentURI uri.URI, position protocol.Position, sourceBlock *hclsyntax.Block, attributeName string, expression hclsyntax.Expression)
| 102 | } |
| 103 | |
| 104 | func ResolveExpression(ctx context.Context, definitionLinkSupport bool, manager *document.Manager, doc document.BakeHCLDocument, body *hclsyntax.Body, documentURI uri.URI, position protocol.Position, sourceBlock *hclsyntax.Block, attributeName string, expression hclsyntax.Expression) any { |
| 105 | if templateExpr, ok := expression.(*hclsyntax.TemplateExpr); ok { |
| 106 | for _, part := range templateExpr.Parts { |
| 107 | if isInsideRange(part.Range(), position) { |
| 108 | return ResolveExpression(ctx, definitionLinkSupport, manager, doc, body, documentURI, position, sourceBlock, attributeName, part) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if literalValueExpr, ok := expression.(*hclsyntax.LiteralValueExpr); ok && sourceBlock != nil && sourceBlock.Type == "target" { |
| 114 | if attributeName == "no-cache-filter" || attributeName == "target" { |
| 115 | dockerfileURI, dockerfilePath, err := doc.DockerfileForTarget(sourceBlock) |
| 116 | if dockerfilePath == "" || err != nil { |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | value, _ := literalValueExpr.Value(&hcl.EvalContext{}) |
| 121 | target := value.AsString() |
| 122 | |
| 123 | bytes, nodes := document.OpenDockerfile(ctx, manager, dockerfileURI, dockerfilePath) |
| 124 | lines := strings.Split(string(bytes), "\n") |
| 125 | for _, child := range nodes { |
| 126 | if strings.EqualFold(child.Value, "FROM") { |
| 127 | if child.Next != nil && child.Next.Next != nil && strings.EqualFold(child.Next.Next.Value, "AS") && child.Next.Next.Next != nil && child.Next.Next.Next.Value == target { |
| 128 | endLineLength := len(lines[child.EndLine-1]) |
| 129 | // 13 is ASCII for \r |
| 130 | if lines[child.EndLine-1][endLineLength-1] == 13 { |
| 131 | endLineLength-- |
| 132 | } |
| 133 | return types.CreateDefinitionResult( |
| 134 | definitionLinkSupport, |
| 135 | protocol.Range{ |
| 136 | Start: protocol.Position{ |
| 137 | Line: uint32(child.StartLine) - 1, |
| 138 | Character: 0, |
| 139 | }, |
| 140 | End: protocol.Position{ |
| 141 | Line: uint32(child.EndLine) - 1, |
| 142 | Character: uint32(endLineLength), |
| 143 | }, |
| 144 | }, |
| 145 | &protocol.Range{ |
| 146 | Start: protocol.Position{ |
| 147 | Line: uint32(literalValueExpr.Range().Start.Line) - 1, |
| 148 | Character: uint32(literalValueExpr.Range().Start.Column) - 1, |
| 149 | }, |
| 150 | End: protocol.Position{ |
| 151 | Line: uint32(literalValueExpr.Range().End.Line) - 1, |
| 152 | Character: uint32(uint32(literalValueExpr.Range().End.Column) - 1), |
| 153 | }, |
| 154 | }, |
| 155 | protocol.URI(dockerfileURI), |
| 156 | ) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
no test coverage detected