-
Notifications
You must be signed in to change notification settings - Fork 2
/
codeaction.go
98 lines (82 loc) · 3.13 KB
/
codeaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* © 2023 Khulnasoft Limited All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package vulnmap
import (
"errors"
"github.com/google/uuid"
)
// CodeAction represents a code action that can be executed by the client using an in-document menu.
// This type should be created by the NewCodeAction or NewDeferredCodeAction functions.
//
// There are 3 types of code actions:
// - No Edit + No CommandData - Deferred code action, which means that either DeferredEdit or DeferredCommand must be set.
// - Only edit/Only command - Resolved immediately to run the edit/command.
// - Both edit and command - Resolved immediately to run edit first and then command.
type CodeAction struct {
// Title is a short, human-readable, title for this code action.
Title string
IsPreferred *bool
// Edit is an optional WorkspaceEdit literal that can be executed by the client.
Edit *WorkspaceEdit
// DeferredEdit is a function that returns a WorkspaceEdit.
// Used for heavy calculations that shouldn't be done ahead of time.
// A CodeAction cannot have both Edit and DeferredEdit.
DeferredEdit *func() *WorkspaceEdit
// Command that will be executed after the Edit (if present).
Command *CommandData
// DeferredCommand is a function that returns a Command.
// Used for heavy calculations that shouldn't be done ahead of time.
// A CodeAction cannot have both Command and DeferredCommand.
DeferredCommand *func() *CommandData
// UUID is a unique identifier for this code action. This is used for deferred resolution of a command or edit.
Uuid *uuid.UUID
}
func NewCodeAction(title string, edit *WorkspaceEdit, command *CommandData) (CodeAction, error) {
if edit == nil && command == nil {
return CodeAction{}, errors.New("a non-deferred action must have either an edit or a command")
}
action := CodeAction{
Title: title,
Edit: edit,
Command: command,
}
return action, nil
}
func NewDeferredCodeAction(title string,
deferredEdit *func() *WorkspaceEdit,
deferredCommand *func() *CommandData,
) (CodeAction, error) {
if deferredEdit == nil && deferredCommand == nil {
return CodeAction{}, errors.New("deferredEdit and deferredCommand cannot both be nil")
}
id := uuid.New()
action := CodeAction{
Title: title,
DeferredEdit: deferredEdit,
DeferredCommand: deferredCommand,
Uuid: &id,
}
return action, nil
}
func NewPreferredCodeAction(title string, edit *WorkspaceEdit, command *CommandData) (CodeAction, error) {
action, err := NewCodeAction(title, edit, command)
if err != nil {
return CodeAction{}, err
}
action.IsPreferred = new(bool)
*action.IsPreferred = true
return action, nil
}