| 46 | } |
| 47 | |
| 48 | func InlineCompletion(ctx context.Context, params *protocol.InlineCompletionParams, manager *document.Manager, bakeDocument document.BakeHCLDocument) ([]protocol.InlineCompletionItem, error) { |
| 49 | documentPath, err := bakeDocument.DocumentPath() |
| 50 | if err != nil { |
| 51 | return nil, fmt.Errorf("LSP client sent invalid URI: %v", params.TextDocument.URI) |
| 52 | } |
| 53 | |
| 54 | body, ok := bakeDocument.File().Body.(*hclsyntax.Body) |
| 55 | if !ok { |
| 56 | return nil, errors.New("unrecognized body in HCL document") |
| 57 | } |
| 58 | |
| 59 | documentURI, dockerfilePath := types.Concatenate(documentPath.Folder, "Dockerfile", documentPath.WSLDollarSignHost) |
| 60 | if !shouldSuggest(bakeDocument.Input(), body, params.Position) { |
| 61 | return nil, nil |
| 62 | } |
| 63 | |
| 64 | preexistingTargets := []string{} |
| 65 | for _, block := range body.Blocks { |
| 66 | if block.Type == "target" && len(block.Labels) > 0 { |
| 67 | preexistingTargets = append(preexistingTargets, block.Labels[0]) |
| 68 | } |
| 69 | |
| 70 | if attribute, ok := block.Body.Attributes["target"]; ok { |
| 71 | if templateExpr, ok := attribute.Expr.(*hclsyntax.TemplateExpr); ok { |
| 72 | if len(templateExpr.Parts) == 1 { |
| 73 | if literalValueExpr, ok := templateExpr.Parts[0].(*hclsyntax.LiteralValueExpr); ok { |
| 74 | value, _ := literalValueExpr.Value(&hcl.EvalContext{}) |
| 75 | if value.Type() == cty.String { |
| 76 | target := value.AsString() |
| 77 | preexistingTargets = append(preexistingTargets, target) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | argNames := []string{} |
| 86 | args := map[string]string{} |
| 87 | targets := []string{} |
| 88 | _, nodes := document.OpenDockerfile(ctx, manager, documentURI, dockerfilePath) |
| 89 | before := true |
| 90 | for _, child := range nodes { |
| 91 | if strings.EqualFold(child.Value, "ARG") && before { |
| 92 | if child.Next != nil { |
| 93 | arg := child.Next.Value |
| 94 | idx := strings.Index(arg, "=") |
| 95 | if idx == -1 { |
| 96 | args[arg] = "" |
| 97 | argNames = append(argNames, arg) |
| 98 | } else { |
| 99 | args[arg[:idx]] = arg[idx+1:] |
| 100 | argNames = append(argNames, arg[:idx]) |
| 101 | } |
| 102 | } |
| 103 | } else if strings.EqualFold(child.Value, "FROM") { |
| 104 | before = false |
| 105 | if child.Next != nil && child.Next.Next != nil && strings.EqualFold(child.Next.Next.Value, "AS") && child.Next.Next.Next != nil { |