Skip to content

Commit 5520ce6

Browse files
authored
Merge pull request #11 from secondcircle/fix-gopls-rename-documentchanges
Fix rename operations with modern LSP servers like gopls
2 parents 30f1bd2 + 5050e0e commit 5520ce6

File tree

1 file changed

+56
-12
lines changed

1 file changed

+56
-12
lines changed

src/lsp-client.ts

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ export class LSPClient {
225225
},
226226
},
227227
workspace: {
228+
workspaceEdit: {
229+
documentChanges: true,
230+
},
228231
workspaceFolders: true,
229232
},
230233
},
@@ -750,23 +753,64 @@ export class LSPClient {
750753
});
751754

752755
process.stderr.write(
753-
`[DEBUG renameSymbol] Result type: ${typeof result}, hasChanges: ${result && typeof result === 'object' && 'changes' in result}\n`
756+
`[DEBUG renameSymbol] Result type: ${typeof result}, hasChanges: ${result && typeof result === 'object' && 'changes' in result}, hasDocumentChanges: ${result && typeof result === 'object' && 'documentChanges' in result}\n`
754757
);
755758

756-
if (result && typeof result === 'object' && 'changes' in result) {
757-
const workspaceEdit = result as {
758-
changes: Record<
759+
if (result && typeof result === 'object') {
760+
// Handle the 'changes' format (older LSP servers)
761+
if ('changes' in result) {
762+
const workspaceEdit = result as {
763+
changes: Record<
764+
string,
765+
Array<{ range: { start: Position; end: Position }; newText: string }>
766+
>;
767+
};
768+
769+
const changeCount = Object.keys(workspaceEdit.changes || {}).length;
770+
process.stderr.write(
771+
`[DEBUG renameSymbol] WorkspaceEdit has changes for ${changeCount} files\n`
772+
);
773+
774+
return workspaceEdit;
775+
}
776+
777+
// Handle the 'documentChanges' format (modern LSP servers like gopls)
778+
if ('documentChanges' in result) {
779+
const workspaceEdit = result as {
780+
documentChanges?: Array<{
781+
textDocument: { uri: string; version?: number };
782+
edits: Array<{ range: { start: Position; end: Position }; newText: string }>;
783+
}>;
784+
};
785+
786+
process.stderr.write(
787+
`[DEBUG renameSymbol] WorkspaceEdit has documentChanges with ${workspaceEdit.documentChanges?.length || 0} entries\n`
788+
);
789+
790+
// Convert documentChanges to changes format for compatibility
791+
const changes: Record<
759792
string,
760793
Array<{ range: { start: Position; end: Position }; newText: string }>
761-
>;
762-
};
763-
764-
const changeCount = Object.keys(workspaceEdit.changes || {}).length;
765-
process.stderr.write(
766-
`[DEBUG renameSymbol] WorkspaceEdit has changes for ${changeCount} files\n`
767-
);
794+
> = {};
795+
796+
if (workspaceEdit.documentChanges) {
797+
for (const change of workspaceEdit.documentChanges) {
798+
// Handle TextDocumentEdit (the only type needed for symbol renames)
799+
if (change.textDocument && change.edits) {
800+
const uri = change.textDocument.uri;
801+
if (!changes[uri]) {
802+
changes[uri] = [];
803+
}
804+
changes[uri].push(...change.edits);
805+
process.stderr.write(
806+
`[DEBUG renameSymbol] Added ${change.edits.length} edits for ${uri}\n`
807+
);
808+
}
809+
}
810+
}
768811

769-
return workspaceEdit;
812+
return { changes };
813+
}
770814
}
771815

772816
process.stderr.write('[DEBUG renameSymbol] No rename changes available\n');

0 commit comments

Comments
 (0)