|
| 1 | +import { StateEffect, StateField } from '@codemirror/state'; |
| 2 | +import { EditorView, showPanel, Panel } from '@codemirror/view'; |
| 3 | +import type * as LSP from 'vscode-languageserver-protocol'; |
| 4 | + |
| 5 | +import { languageServerPlugin } from './plugin'; |
| 6 | +import { offsetToPos, posToOffset } from './pos'; |
| 7 | + |
| 8 | +const openRenamePanel = StateEffect.define<string>(); |
| 9 | +const closeRenamePanel = StateEffect.define<null>(); |
| 10 | + |
| 11 | +const renamePanelState = StateField.define<string | null>({ |
| 12 | + create() { |
| 13 | + return null; |
| 14 | + }, |
| 15 | + update(value, tr) { |
| 16 | + for (const effect of tr.effects) { |
| 17 | + if (effect.is(openRenamePanel)) return effect.value; |
| 18 | + if (effect.is(closeRenamePanel)) return null; |
| 19 | + } |
| 20 | + return value; |
| 21 | + }, |
| 22 | + provide: (field) => |
| 23 | + showPanel.from(field, (value) => |
| 24 | + value != null ? (view) => createRenamePanel(view, value) : null, |
| 25 | + ), |
| 26 | +}); |
| 27 | + |
| 28 | +function applyWorkspaceEdit(view: EditorView, edit: LSP.WorkspaceEdit) { |
| 29 | + const plugin = view.plugin(languageServerPlugin); |
| 30 | + if (!plugin) return; |
| 31 | + |
| 32 | + const changes: { from: number; to: number; insert: string }[] = []; |
| 33 | + |
| 34 | + if (edit.changes) { |
| 35 | + const edits = edit.changes[plugin.documentUri]; |
| 36 | + if (edits) { |
| 37 | + for (const e of edits) { |
| 38 | + changes.push({ |
| 39 | + from: posToOffset(view.state.doc, e.range.start), |
| 40 | + to: posToOffset(view.state.doc, e.range.end), |
| 41 | + insert: e.newText, |
| 42 | + }); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (edit.documentChanges) { |
| 48 | + for (const change of edit.documentChanges) { |
| 49 | + if ('textDocument' in change) { |
| 50 | + if (change.textDocument.uri !== plugin.documentUri) continue; |
| 51 | + for (const e of change.edits) { |
| 52 | + if ('range' in e) { |
| 53 | + changes.push({ |
| 54 | + from: posToOffset(view.state.doc, e.range.start), |
| 55 | + to: posToOffset(view.state.doc, e.range.end), |
| 56 | + insert: e.newText, |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (changes.length > 0) { |
| 65 | + view.dispatch(view.state.update({ changes })); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function createRenamePanel(view: EditorView, placeholder: string): Panel { |
| 70 | + const dom = document.createElement('div'); |
| 71 | + dom.className = 'cm-lsp-rename-panel'; |
| 72 | + |
| 73 | + const input = document.createElement('input'); |
| 74 | + input.className = 'cm-lsp-rename-input'; |
| 75 | + input.value = placeholder; |
| 76 | + input.setAttribute('aria-label', 'New name'); |
| 77 | + |
| 78 | + const submit = () => { |
| 79 | + const newName = input.value.trim(); |
| 80 | + if (!newName || newName === placeholder) { |
| 81 | + view.dispatch({ effects: closeRenamePanel.of(null) }); |
| 82 | + view.focus(); |
| 83 | + return; |
| 84 | + } |
| 85 | + performRename(view, newName); |
| 86 | + }; |
| 87 | + |
| 88 | + input.addEventListener('keydown', (e) => { |
| 89 | + if (e.key === 'Enter') { |
| 90 | + e.preventDefault(); |
| 91 | + submit(); |
| 92 | + } else if (e.key === 'Escape') { |
| 93 | + e.preventDefault(); |
| 94 | + view.dispatch({ effects: closeRenamePanel.of(null) }); |
| 95 | + view.focus(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + dom.appendChild(input); |
| 100 | + |
| 101 | + return { |
| 102 | + dom, |
| 103 | + top: true, |
| 104 | + mount() { |
| 105 | + input.select(); |
| 106 | + }, |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +async function performRename(view: EditorView, newName: string) { |
| 111 | + const plugin = view.plugin(languageServerPlugin); |
| 112 | + if (!plugin) return; |
| 113 | + |
| 114 | + const pos = view.state.selection.main.head; |
| 115 | + |
| 116 | + view.dispatch({ effects: closeRenamePanel.of(null) }); |
| 117 | + view.focus(); |
| 118 | + |
| 119 | + const result = await plugin.client.textDocumentRename({ |
| 120 | + textDocument: { uri: plugin.documentUri }, |
| 121 | + position: offsetToPos(view.state.doc, pos), |
| 122 | + newName, |
| 123 | + }); |
| 124 | + |
| 125 | + if (result) { |
| 126 | + applyWorkspaceEdit(view, result); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +export function renameSymbol(view: EditorView): boolean { |
| 131 | + const plugin = view.plugin(languageServerPlugin); |
| 132 | + if (!plugin) return false; |
| 133 | + |
| 134 | + if ( |
| 135 | + !plugin.client.ready || |
| 136 | + !plugin.client.capabilities?.renameProvider |
| 137 | + ) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + const pos = view.state.selection.main.head; |
| 142 | + const capabilities = plugin.client.capabilities; |
| 143 | + const renameProvider = capabilities.renameProvider; |
| 144 | + const supportsPrepare = |
| 145 | + typeof renameProvider === 'object' && renameProvider.prepareProvider; |
| 146 | + |
| 147 | + if (supportsPrepare) { |
| 148 | + plugin.client |
| 149 | + .textDocumentPrepareRename({ |
| 150 | + textDocument: { uri: plugin.documentUri }, |
| 151 | + position: offsetToPos(view.state.doc, pos), |
| 152 | + }) |
| 153 | + .then((result) => { |
| 154 | + if (!result) return; |
| 155 | + const placeholder = |
| 156 | + 'placeholder' in result |
| 157 | + ? result.placeholder |
| 158 | + : view.state.doc.sliceString( |
| 159 | + posToOffset(view.state.doc, result.start), |
| 160 | + posToOffset(view.state.doc, result.end), |
| 161 | + ); |
| 162 | + view.dispatch({ |
| 163 | + effects: openRenamePanel.of(placeholder), |
| 164 | + }); |
| 165 | + }); |
| 166 | + } else { |
| 167 | + const word = view.state.wordAt(pos); |
| 168 | + const placeholder = word |
| 169 | + ? view.state.doc.sliceString(word.from, word.to) |
| 170 | + : ''; |
| 171 | + view.dispatch({ effects: openRenamePanel.of(placeholder) }); |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | +} |
| 176 | + |
| 177 | +export const renameExtension = () => renamePanelState; |
0 commit comments