| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"context" |
| 13 |
"fmt" |
| 14 |
"path/filepath" |
| 15 |
"slices" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"golang.org/x/tools/gopls/internal/cache" |
| 19 |
"golang.org/x/tools/gopls/internal/golang" |
| 20 |
"golang.org/x/tools/gopls/internal/protocol" |
| 21 |
"golang.org/x/tools/internal/diff" |
| 22 |
|
| 23 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 24 |
) |
| 25 |
|
| 26 |
type diagnosticsParams struct { |
| 27 |
File string `json:"file" jsonschema:"the absolute path to the file to diagnose"` |
| 28 |
} |
| 29 |
|
| 30 |
func (h *handler) fileDiagnosticsHandler(ctx context.Context, req *mcp.CallToolRequest, params diagnosticsParams) (*mcp.CallToolResult, any, error) { |
| 31 |
countGoFileDiagnosticsMCP.Inc() |
| 32 |
fh, snapshot, release, err := h.fileOf(ctx, params.File) |
| 33 |
if err != nil { |
| 34 |
return nil, nil, err |
| 35 |
} |
| 36 |
defer release() |
| 37 |
|
| 38 |
diagnostics, fixes, err := h.diagnoseFile(ctx, snapshot, fh.URI()) |
| 39 |
if err != nil { |
| 40 |
return nil, nil, err |
| 41 |
} |
| 42 |
|
| 43 |
var builder strings.Builder |
| 44 |
if len(diagnostics) == 0 { |
| 45 |
return textResult("No diagnostics"), nil, nil |
| 46 |
} |
| 47 |
|
| 48 |
if err := summarizeDiagnostics(ctx, snapshot, &builder, diagnostics, fixes); err != nil { |
| 49 |
return nil, nil, err |
| 50 |
} |
| 51 |
|
| 52 |
return textResult(builder.String()), nil, nil |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func (h *handler) diagnoseFile(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) ([]*cache.Diagnostic, map[*cache.Diagnostic]*protocol.CodeAction, error) { |
| 57 |
diagnostics, err := golang.DiagnoseFile(ctx, snapshot, uri) |
| 58 |
if err != nil { |
| 59 |
return nil, nil, err |
| 60 |
} |
| 61 |
if len(diagnostics) == 0 { |
| 62 |
return nil, nil, nil |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
actions, _ := h.lspServer.CodeAction(ctx, &protocol.CodeActionParams{ |
| 72 |
TextDocument: protocol.TextDocumentIdentifier{ |
| 73 |
URI: uri, |
| 74 |
}, |
| 75 |
Context: protocol.CodeActionContext{ |
| 76 |
Only: []protocol.CodeActionKind{protocol.QuickFix}, |
| 77 |
Diagnostics: cache.ToProtocolDiagnostics(diagnostics...), |
| 78 |
}, |
| 79 |
}) |
| 80 |
|
| 81 |
type key struct { |
| 82 |
Message string |
| 83 |
Range protocol.Range |
| 84 |
} |
| 85 |
|
| 86 |
actionMap := make(map[key]*protocol.CodeAction) |
| 87 |
for _, action := range actions { |
| 88 |
for _, d := range action.Diagnostics { |
| 89 |
k := key{d.Message, d.Range} |
| 90 |
if alt, ok := actionMap[k]; !ok || !alt.IsPreferred && action.IsPreferred { |
| 91 |
actionMap[k] = &action |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
fixes := make(map[*cache.Diagnostic]*protocol.CodeAction) |
| 97 |
for _, d := range diagnostics { |
| 98 |
if fix, ok := actionMap[key{d.Message, d.Range}]; ok { |
| 99 |
fixes[d] = fix |
| 100 |
} |
| 101 |
} |
| 102 |
return diagnostics, fixes, nil |
| 103 |
} |
| 104 |
|
| 105 |
func summarizeDiagnostics(ctx context.Context, snapshot *cache.Snapshot, w *strings.Builder, diagnostics []*cache.Diagnostic, fixes map[*cache.Diagnostic]*protocol.CodeAction) error { |
| 106 |
for _, d := range diagnostics { |
| 107 |
fmt.Fprintf(w, "%d:%d-%d:%d: [%s] %s\n", d.Range.Start.Line, d.Range.Start.Character, d.Range.End.Line, d.Range.End.Character, d.Severity, d.Message) |
| 108 |
|
| 109 |
fix, ok := fixes[d] |
| 110 |
if ok && fix.Edit != nil { |
| 111 |
w.WriteString("Fix:\n") |
| 112 |
if err := writeUnifiedDiff(ctx, snapshot, w, fix.Edit.DocumentChanges); err != nil { |
| 113 |
return err |
| 114 |
} |
| 115 |
w.WriteString("\n") |
| 116 |
} |
| 117 |
} |
| 118 |
return nil |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
func writeUnifiedDiff(ctx context.Context, snapshot *cache.Snapshot, w *strings.Builder, changes []protocol.DocumentChange) error { |
| 130 |
for _, change := range changes { |
| 131 |
|
| 132 |
var ( |
| 133 |
oldFile, newFile string |
| 134 |
oldContent, newContent string |
| 135 |
) |
| 136 |
switch { |
| 137 |
case change.CreateFile != nil: |
| 138 |
oldFile, newFile = "/dev/null", filepath.ToSlash(change.CreateFile.URI.Path()) |
| 139 |
oldContent, newContent = "", "" |
| 140 |
case change.DeleteFile != nil: |
| 141 |
fh, err := snapshot.ReadFile(ctx, change.DeleteFile.URI) |
| 142 |
if err != nil { |
| 143 |
return err |
| 144 |
} |
| 145 |
content, err := fh.Content() |
| 146 |
if err != nil { |
| 147 |
return err |
| 148 |
} |
| 149 |
oldFile, newFile = filepath.ToSlash(change.DeleteFile.URI.Path()), "/dev/null" |
| 150 |
oldContent, newContent = string(content), "" |
| 151 |
case change.RenameFile != nil: |
| 152 |
fh, err := snapshot.ReadFile(ctx, change.RenameFile.OldURI) |
| 153 |
if err != nil { |
| 154 |
return err |
| 155 |
} |
| 156 |
content, err := fh.Content() |
| 157 |
if err != nil { |
| 158 |
return err |
| 159 |
} |
| 160 |
oldFile = filepath.ToSlash(change.RenameFile.OldURI.Path()) |
| 161 |
newFile = filepath.ToSlash(change.RenameFile.NewURI.Path()) |
| 162 |
oldContent, newContent = string(content), string(content) |
| 163 |
case change.TextDocumentEdit != nil: |
| 164 |
fh, err := snapshot.ReadFile(ctx, change.TextDocumentEdit.TextDocument.URI) |
| 165 |
if err != nil { |
| 166 |
return err |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
sorted := protocol.AsTextEdits(change.TextDocumentEdit.Edits) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
slices.SortStableFunc(sorted, func(a, b protocol.TextEdit) int { |
| 175 |
return protocol.CompareRange(a.Range, b.Range) |
| 176 |
}) |
| 177 |
|
| 178 |
content, err := fh.Content() |
| 179 |
if err != nil { |
| 180 |
return err |
| 181 |
} |
| 182 |
|
| 183 |
var newSrc bytes.Buffer |
| 184 |
{ |
| 185 |
mapper := protocol.NewMapper(fh.URI(), content) |
| 186 |
|
| 187 |
start := 0 |
| 188 |
for _, edit := range sorted { |
| 189 |
l, r, err := mapper.RangeOffsets(edit.Range) |
| 190 |
if err != nil { |
| 191 |
return err |
| 192 |
} |
| 193 |
|
| 194 |
newSrc.Write(content[start:l]) |
| 195 |
newSrc.WriteString(edit.NewText) |
| 196 |
|
| 197 |
start = r |
| 198 |
} |
| 199 |
newSrc.Write(content[start:]) |
| 200 |
} |
| 201 |
|
| 202 |
oldFile, newFile = filepath.ToSlash(fh.URI().Path()), filepath.ToSlash(fh.URI().Path()) |
| 203 |
oldContent, newContent = string(content), newSrc.String() |
| 204 |
default: |
| 205 |
continue |
| 206 |
} |
| 207 |
w.WriteString(diff.Unified(oldFile, newFile, oldContent, newContent)) |
| 208 |
w.WriteString("\n") |
| 209 |
} |
| 210 |
return nil |
| 211 |
} |