-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.go
More file actions
132 lines (106 loc) · 3.48 KB
/
actions.go
File metadata and controls
132 lines (106 loc) · 3.48 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
// commentStyle represents comment syntax for a language
type commentStyle struct {
prefix string
suffix string // empty if single-line comment
}
func textDocumentCodeAction(context *glsp.Context, params *protocol.CodeActionParams) (any, error) {
uri := params.TextDocument.URI
var actions []protocol.CodeAction
// Check each diagnostic in the range
for _, diag := range params.Context.Diagnostics {
if diag.Source != nil && *diag.Source == "gitleaks" {
// Get document content to determine indentation
doc, ok := globalServer.documents.Get(uri)
if !ok {
continue
}
// Add action to ignore this finding
actions = append(actions, createIgnoreAction(uri, diag, doc.Content))
}
}
return actions, nil
}
// createIgnoreAction creates a code action to add gitleaks:allow comment
func createIgnoreAction(uri protocol.DocumentUri, diag protocol.Diagnostic, content string) protocol.CodeAction {
// Calculate the line of the diagnostic
line := diag.Range.Start.Line
// Get the content of the line
lines := strings.Split(content, "\n")
var lineContent string
if int(line) < len(lines) {
lineContent = lines[line]
}
// Get comment syntax for the file type
style := getCommentSyntax(uriToPath(uri))
// Create the comment to append
var comment string
if style.suffix == "" {
// Single-line comment
comment = fmt.Sprintf(" %s gitleaks:allow", style.prefix)
} else {
// Block comment
comment = fmt.Sprintf(" %s gitleaks:allow %s", style.prefix, style.suffix)
}
// Create text edit to append comment at end of line
edit := protocol.TextEdit{
Range: protocol.Range{
Start: protocol.Position{Line: line, Character: uint32(len(lineContent))},
End: protocol.Position{Line: line, Character: uint32(len(lineContent))},
},
NewText: comment,
}
changes := make(map[protocol.DocumentUri][]protocol.TextEdit)
changes[uri] = []protocol.TextEdit{edit}
title := "Ignore this secret (add gitleaks:allow comment)"
kind := protocol.CodeActionKindQuickFix
return protocol.CodeAction{
Title: title,
Kind: &kind,
Edit: &protocol.WorkspaceEdit{
Changes: changes,
},
Diagnostics: []protocol.Diagnostic{diag},
}
}
// getCommentSyntax returns the appropriate comment syntax for a file
func getCommentSyntax(filename string) commentStyle {
// Extract extension
ext := strings.ToLower(filepath.Ext(filename))
// Map extensions to comment styles
switch ext {
// C-style comments: //
case ".go", ".rs", ".java", ".js", ".ts", ".jsx", ".tsx", ".c", ".cpp", ".cc", ".h", ".hpp",
".cs", ".swift", ".kt", ".scala", ".dart", ".php":
return commentStyle{prefix: "//"}
// Hash comments: #
case ".py", ".rb", ".sh", ".bash", ".zsh", ".fish", ".pl", ".yaml", ".yml", ".toml",
".conf", ".cfg", ".ini", ".env", ".r", ".jl":
return commentStyle{prefix: "#"}
// Lua/SQL comments: --
case ".lua", ".sql", ".hs", ".elm":
return commentStyle{prefix: "--"}
// Lisp-style: ;
case ".lisp", ".el", ".clj", ".scm":
return commentStyle{prefix: ";"}
// Vim script: "
case ".vim":
return commentStyle{prefix: "\""}
// HTML/XML comments: <!-- -->
case ".html", ".htm", ".xml", ".svg":
return commentStyle{prefix: "<!--", suffix: "-->"}
// CSS/C block comments: /* */
case ".css", ".scss", ".sass", ".less":
return commentStyle{prefix: "/*", suffix: "*/"}
// Default to // for unknown types
default:
return commentStyle{prefix: "//"}
}
}