( session: Session, params: lsp.RenameParams, )
| 11 | import {filePathToUri, lspPositionToTsPosition, tsTextSpanToLspRange} from '../utils'; |
| 12 | |
| 13 | export function onRenameRequest( |
| 14 | session: Session, |
| 15 | params: lsp.RenameParams, |
| 16 | ): lsp.WorkspaceEdit | null { |
| 17 | const lsInfo = session.getLSAndScriptInfo(params.textDocument); |
| 18 | if (lsInfo === null) { |
| 19 | return null; |
| 20 | } |
| 21 | const {languageService, scriptInfo} = lsInfo; |
| 22 | const project = session.getDefaultProjectForScriptInfo(scriptInfo); |
| 23 | if (project === null || session.renameDisabledProjects.has(project)) { |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | const offset = lspPositionToTsPosition(scriptInfo, params.position); |
| 28 | const renameLocations = languageService.findRenameLocations( |
| 29 | scriptInfo.fileName, |
| 30 | offset, |
| 31 | /*findInStrings*/ false, |
| 32 | /*findInComments*/ false, |
| 33 | ); |
| 34 | if (renameLocations === undefined) { |
| 35 | return null; |
| 36 | } |
| 37 | |
| 38 | const changes = renameLocations.reduce( |
| 39 | (changes, location) => { |
| 40 | let uri: lsp.URI = filePathToUri(location.fileName); |
| 41 | if (changes[uri] === undefined) { |
| 42 | changes[uri] = []; |
| 43 | } |
| 44 | const fileEdits = changes[uri]; |
| 45 | |
| 46 | const lsInfo = session.getLSAndScriptInfo(location.fileName); |
| 47 | if (lsInfo === null) { |
| 48 | return changes; |
| 49 | } |
| 50 | const range = tsTextSpanToLspRange(lsInfo.scriptInfo, location.textSpan); |
| 51 | fileEdits.push({range, newText: params.newName}); |
| 52 | return changes; |
| 53 | }, |
| 54 | {} as {[uri: string]: lsp.TextEdit[]}, |
| 55 | ); |
| 56 | |
| 57 | return {changes}; |
| 58 | } |
| 59 | |
| 60 | export function onPrepareRename( |
| 61 | session: Session, |
no test coverage detected