| 13 | ) |
| 14 | |
| 15 | func DocumentLink(ctx context.Context, documentURI protocol.URI, document document.BakeHCLDocument) ([]protocol.DocumentLink, error) { |
| 16 | body, ok := document.File().Body.(*hclsyntax.Body) |
| 17 | if !ok { |
| 18 | return nil, errors.New("unrecognized body in HCL document") |
| 19 | } |
| 20 | |
| 21 | d, err := document.DocumentPath() |
| 22 | if err != nil { |
| 23 | return nil, fmt.Errorf("LSP client sent invalid URI: %v", string(documentURI)) |
| 24 | } |
| 25 | |
| 26 | bytes := document.Input() |
| 27 | links := []protocol.DocumentLink{} |
| 28 | for _, b := range body.Blocks { |
| 29 | attributes := b.Body.Attributes |
| 30 | for _, v := range attributes { |
| 31 | if v.Name == "dockerfile" { |
| 32 | dockerfilePath := string(bytes[v.Expr.Range().Start.Byte:v.Expr.Range().End.Byte]) |
| 33 | if !Quoted(dockerfilePath) { |
| 34 | continue |
| 35 | } |
| 36 | |
| 37 | dockerfilePath = strings.TrimPrefix(dockerfilePath, "\"") |
| 38 | dockerfilePath = strings.TrimSuffix(dockerfilePath, "\"") |
| 39 | target, tooltip := types.Concatenate(d.Folder, dockerfilePath, d.WSLDollarSignHost) |
| 40 | links = append(links, protocol.DocumentLink{ |
| 41 | Range: protocol.Range{ |
| 42 | Start: protocol.Position{Line: uint32(v.SrcRange.Start.Line) - 1, Character: uint32(v.Expr.Range().Start.Column)}, |
| 43 | End: protocol.Position{Line: uint32(v.SrcRange.Start.Line) - 1, Character: uint32(v.Expr.Range().End.Column - 2)}, |
| 44 | }, |
| 45 | Target: types.CreateStringPointer(target), |
| 46 | Tooltip: types.CreateStringPointer(tooltip), |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return links, nil |
| 52 | } |