Skip to content

Commit a155b62

Browse files
authored
Add popup prompt for creating input.json if it doesnt exist when Evaluate is run (#1929)
Signed-off-by: Sean Ledford <s_ledford@apple.com>
1 parent 820d3f0 commit a155b62

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

internal/lsp/server.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ type LanguageServer struct {
164164

165165
workspaceRootURI string
166166
workspaceDiagnosticsPoll time.Duration
167+
168+
// Flag used to suppress input.json prompt if user chooses to ignore it
169+
supressInputPrompt bool
167170
}
168171

169172
// lintFileJob is sent to the lintFileJobs channel to trigger a
@@ -2578,6 +2581,8 @@ func (l *LanguageServer) handleEvalCommand(ctx context.Context, args types.Comma
25782581

25792582
var inputMap map[string]any
25802583

2584+
var inputPath string
2585+
25812586
// When the first comment in the file is `regal eval: use-as-input`, the AST of that module is
25822587
// used as the input rather than the contents of input.json/yaml. This is a development feature for
25832588
// working on rules (built-in or custom), allowing querying the AST of the module directly.
@@ -2593,7 +2598,33 @@ func (l *LanguageServer) handleEvalCommand(ctx context.Context, args types.Comma
25932598
// Normal mode — try to find the input.json/yaml file in the workspace and use as input
25942599
// NOTE that we don't break on missing input, as some rules don't depend on that, and should
25952600
// still be evaluable. We may consider returning some notice to the user though.
2596-
_, inputMap = rio.FindInput(uri.ToPath(args.Target), l.workspacePath())
2601+
inputPath, inputMap = rio.FindInput(uri.ToPath(args.Target), l.workspacePath())
2602+
if inputPath == "" && !l.supressInputPrompt {
2603+
var action types.MessageActionItem
2604+
2605+
reqParams := types.ShowMessageRequestParams{
2606+
Type: 3, // info
2607+
Message: "No input.json/yaml file was found. " +
2608+
"This file is used to provide input data to your rules. " +
2609+
"Would you like to create one?",
2610+
Actions: []types.MessageActionItem{
2611+
{Title: "Yes"},
2612+
{Title: "No"},
2613+
{Title: "Ignore"},
2614+
},
2615+
}
2616+
2617+
if err = l.conn.Call(ctx, "window/showMessageRequest", reqParams, &action); err != nil {
2618+
l.log.Message("window/showMessageRequest failed: %v", err)
2619+
} else if action.Title == "Yes" {
2620+
inputFile := filepath.Join(l.workspacePath(), "input.json")
2621+
if err = os.WriteFile(inputFile, []byte("{}\n"), 0o600); err != nil {
2622+
l.log.Message("failed to create input.json: %v", err)
2623+
}
2624+
} else if action.Title == "Ignore" {
2625+
l.supressInputPrompt = true
2626+
}
2627+
}
25972628
}
25982629

25992630
var result EvalResult

internal/lsp/types/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,5 +631,15 @@ type (
631631
Success bool `json:"success"`
632632
}
633633

634+
MessageActionItem struct {
635+
Title string `json:"title"`
636+
}
637+
638+
ShowMessageRequestParams struct {
639+
Type uint `json:"type"`
640+
Message string `json:"message"`
641+
Actions []MessageActionItem `json:"actions"`
642+
}
643+
634644
iuint interface{ ~int | ~uint }
635645
)

0 commit comments

Comments
 (0)