| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"crypto/sha256" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"strings" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/file" |
| 14 |
"golang.org/x/tools/gopls/internal/protocol" |
| 15 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type InitializationError struct { |
| 25 |
|
| 26 |
MainError error |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
Diagnostics map[protocol.DocumentURI][]*Diagnostic |
| 31 |
} |
| 32 |
|
| 33 |
func byURI(d *Diagnostic) protocol.DocumentURI { return d.URI } |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
type Diagnostic struct { |
| 40 |
URI protocol.DocumentURI |
| 41 |
Range protocol.Range |
| 42 |
Severity protocol.DiagnosticSeverity |
| 43 |
Code string |
| 44 |
CodeHref string |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Source DiagnosticSource |
| 49 |
|
| 50 |
Message string |
| 51 |
|
| 52 |
Tags []protocol.DiagnosticTag |
| 53 |
Related []protocol.DiagnosticRelatedInformation |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
BundledFixes *json.RawMessage |
| 68 |
SuggestedFixes []SuggestedFix |
| 69 |
} |
| 70 |
|
| 71 |
func (d *Diagnostic) String() string { |
| 72 |
return fmt.Sprintf("%v: %s", d.Range, d.Message) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
func (d *Diagnostic) Hash() file.Hash { |
| 78 |
h := sha256.New() |
| 79 |
for _, t := range d.Tags { |
| 80 |
fmt.Fprintf(h, "tag: %s\n", t) |
| 81 |
} |
| 82 |
for _, r := range d.Related { |
| 83 |
fmt.Fprintf(h, "related: %s %s %s\n", r.Location.URI, r.Message, r.Location.Range) |
| 84 |
} |
| 85 |
fmt.Fprintf(h, "code: %s\n", d.Code) |
| 86 |
fmt.Fprintf(h, "codeHref: %s\n", d.CodeHref) |
| 87 |
fmt.Fprintf(h, "message: %s\n", d.Message) |
| 88 |
fmt.Fprintf(h, "range: %s\n", d.Range) |
| 89 |
fmt.Fprintf(h, "severity: %s\n", d.Severity) |
| 90 |
fmt.Fprintf(h, "source: %s\n", d.Source) |
| 91 |
if d.BundledFixes != nil { |
| 92 |
fmt.Fprintf(h, "fixes: %s\n", *d.BundledFixes) |
| 93 |
} |
| 94 |
var hash [sha256.Size]byte |
| 95 |
h.Sum(hash[:0]) |
| 96 |
return hash |
| 97 |
} |
| 98 |
|
| 99 |
func ToProtocolDiagnostics(diagnostics ...*Diagnostic) []protocol.Diagnostic { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
reports := []protocol.Diagnostic{} |
| 104 |
for _, diag := range diagnostics { |
| 105 |
pdiag := protocol.Diagnostic{ |
| 106 |
|
| 107 |
Message: strings.TrimSpace(diag.Message), |
| 108 |
Range: diag.Range, |
| 109 |
Severity: diag.Severity, |
| 110 |
Source: string(diag.Source), |
| 111 |
Tags: protocol.NonNilSlice(diag.Tags), |
| 112 |
RelatedInformation: diag.Related, |
| 113 |
Data: diag.BundledFixes, |
| 114 |
} |
| 115 |
if diag.Code != "" { |
| 116 |
pdiag.Code = diag.Code |
| 117 |
} |
| 118 |
if diag.CodeHref != "" { |
| 119 |
pdiag.CodeDescription = &protocol.CodeDescription{Href: diag.CodeHref} |
| 120 |
} |
| 121 |
reports = append(reports, pdiag) |
| 122 |
} |
| 123 |
return reports |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
type DiagnosticSource string |
| 131 |
|
| 132 |
const ( |
| 133 |
UnknownError DiagnosticSource = "<Unknown source>" |
| 134 |
ListError DiagnosticSource = "go list" |
| 135 |
ParseError DiagnosticSource = "syntax" |
| 136 |
TypeError DiagnosticSource = "compiler" |
| 137 |
ModTidyError DiagnosticSource = "go mod tidy" |
| 138 |
CompilerOptDetailsInfo DiagnosticSource = "optimizer details" |
| 139 |
UpgradeNotification DiagnosticSource = "upgrade available" |
| 140 |
Vulncheck DiagnosticSource = "vulncheck imports" |
| 141 |
Govulncheck DiagnosticSource = "govulncheck" |
| 142 |
TemplateError DiagnosticSource = "template" |
| 143 |
WorkFileError DiagnosticSource = "go.work file" |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
type SuggestedFix struct { |
| 156 |
Title string |
| 157 |
Edits map[protocol.DocumentURI][]protocol.TextEdit |
| 158 |
Command *protocol.Command |
| 159 |
ActionKind protocol.CodeActionKind |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func SuggestedFixFromCommand(cmd *protocol.Command, kind protocol.CodeActionKind) SuggestedFix { |
| 164 |
return SuggestedFix{ |
| 165 |
Title: cmd.Title, |
| 166 |
Command: cmd, |
| 167 |
ActionKind: kind, |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
type lazyFixesJSON struct { |
| 176 |
|
| 177 |
|
| 178 |
Actions []protocol.CodeAction |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
func bundleLazyFixes(sd *Diagnostic) bool { |
| 185 |
if len(sd.SuggestedFixes) == 0 { |
| 186 |
return true |
| 187 |
} |
| 188 |
var actions []protocol.CodeAction |
| 189 |
for _, fix := range sd.SuggestedFixes { |
| 190 |
if fix.Edits != nil { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
return false |
| 202 |
} |
| 203 |
action := protocol.CodeAction{ |
| 204 |
Title: fix.Title, |
| 205 |
Kind: fix.ActionKind, |
| 206 |
Command: fix.Command, |
| 207 |
} |
| 208 |
actions = append(actions, action) |
| 209 |
} |
| 210 |
fixes := lazyFixesJSON{ |
| 211 |
Actions: actions, |
| 212 |
} |
| 213 |
data, err := json.Marshal(fixes) |
| 214 |
if err != nil { |
| 215 |
bug.Reportf("marshalling lazy fixes: %v", err) |
| 216 |
return false |
| 217 |
} |
| 218 |
msg := json.RawMessage(data) |
| 219 |
sd.BundledFixes = &msg |
| 220 |
return true |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
func BundledLazyFixes(diag protocol.Diagnostic) ([]protocol.CodeAction, error) { |
| 226 |
var fix lazyFixesJSON |
| 227 |
if diag.Data != nil { |
| 228 |
err := protocol.UnmarshalJSON(*diag.Data, &fix) |
| 229 |
if err != nil { |
| 230 |
return nil, fmt.Errorf("unmarshalling fix from diagnostic data: %v", err) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
var actions []protocol.CodeAction |
| 235 |
for _, action := range fix.Actions { |
| 236 |
|
| 237 |
if action.Edit != nil { |
| 238 |
return nil, fmt.Errorf("bundled fix %q includes workspace edits", action.Title) |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
action.Diagnostics = []protocol.Diagnostic{diag} |
| 243 |
actions = append(actions, action) |
| 244 |
} |
| 245 |
|
| 246 |
return actions, nil |
| 247 |
} |