| 12 | ) |
| 13 | |
| 14 | func CodeLens(ctx context.Context, documentURI string, doc document.BakeHCLDocument) ([]protocol.CodeLens, error) { |
| 15 | body, ok := doc.File().Body.(*hclsyntax.Body) |
| 16 | if !ok { |
| 17 | return nil, errors.New("unrecognized body in HCL document") |
| 18 | } |
| 19 | |
| 20 | dp, err := doc.DocumentPath() |
| 21 | if err != nil { |
| 22 | return nil, fmt.Errorf("could not parse URI (%v): %w", documentURI, err) |
| 23 | } |
| 24 | |
| 25 | _, cwd := types.Concatenate(dp.Folder, ".", dp.WSLDollarSignHost) |
| 26 | result := []protocol.CodeLens{} |
| 27 | for _, block := range body.Blocks { |
| 28 | if len(block.Labels) > 0 { |
| 29 | switch block.Type { |
| 30 | case "group": |
| 31 | rng := protocol.Range{ |
| 32 | Start: protocol.Position{Line: uint32(block.Range().Start.Line - 1)}, |
| 33 | End: protocol.Position{Line: uint32(block.Range().Start.Line - 1)}, |
| 34 | } |
| 35 | result = append(result, createCodeLens("Build", cwd, "build", block.Labels[0], rng)) |
| 36 | result = append(result, createCodeLens("Check", cwd, "check", block.Labels[0], rng)) |
| 37 | result = append(result, createCodeLens("Print", cwd, "print", block.Labels[0], rng)) |
| 38 | case "target": |
| 39 | rng := protocol.Range{ |
| 40 | Start: protocol.Position{Line: uint32(block.Range().Start.Line - 1)}, |
| 41 | End: protocol.Position{Line: uint32(block.Range().Start.Line - 1)}, |
| 42 | } |
| 43 | result = append(result, createCodeLens("Build", cwd, "build", block.Labels[0], rng)) |
| 44 | result = append(result, createCodeLens("Check", cwd, "check", block.Labels[0], rng)) |
| 45 | result = append(result, createCodeLens("Print", cwd, "print", block.Labels[0], rng)) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return result, nil |
| 50 | } |
| 51 | |
| 52 | func createCodeLens(title, cwd, call, target string, rng protocol.Range) protocol.CodeLens { |
| 53 | return protocol.CodeLens{ |