This repository has been archived by the owner on Nov 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
Add a rename provider #39
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -631,3 +631,48 @@ export class FormatOnTypeAdapter extends FormatHelper implements monaco.language | |
}); | ||
} | ||
} | ||
|
||
// --- rename ---- | ||
|
||
export class RenameAdapter extends Adapter implements monaco.languages.RenameProvider { | ||
|
||
async provideRenameEdits(model: monaco.editor.ITextModel, position: Position, newName: string, token: CancellationToken): Promise<monaco.languages.WorkspaceEdit & monaco.languages.Rejection> { | ||
const resource = model.uri; | ||
const fileName = resource.toString(); | ||
const offset = this._positionToOffset(resource, position); | ||
const worker = await this._worker(resource); | ||
|
||
const renameInfo = await worker.getRenameInfo(fileName, offset, { allowRenameOfImportPath: false }); | ||
if (renameInfo.canRename === false) { // use explicit comparison so that the discriminated union gets resolved properly | ||
return { | ||
edits: [], | ||
rejectReason: renameInfo.localizedErrorMessage | ||
}; | ||
} | ||
if (renameInfo.fileToRename !== undefined) { | ||
throw new Error("Renaming files is not supported."); | ||
} | ||
|
||
const renameLocations = await worker.findRenameLocations(fileName, offset, /*strings*/ false, /*comments*/ false, /*prefixAndSuffix*/ false); | ||
const fileNameToResourceTextEditMap: { [fileName: string]: monaco.languages.ResourceTextEdit } = {}; | ||
|
||
const edits: monaco.languages.ResourceTextEdit[] = []; | ||
for (const renameLocation of renameLocations) { | ||
if (!(renameLocation.fileName in fileNameToResourceTextEditMap)) { | ||
const resourceTextEdit = { | ||
edits: [], | ||
resource: monaco.Uri.parse(renameLocation.fileName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You cannot use |
||
}; | ||
fileNameToResourceTextEditMap[renameLocation.fileName] = resourceTextEdit; | ||
edits.push(resourceTextEdit); | ||
} | ||
|
||
fileNameToResourceTextEditMap[renameLocation.fileName].edits.push({ | ||
range: this._textSpanToRange(resource, renameLocation.textSpan), | ||
text: newName | ||
}); | ||
} | ||
|
||
return { edits }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another optional method
resolveRenameLocation
in the interface which I didn't implement. What is the purpose of this method and do we need that?