|
| 1 | +/* --------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | +import { URI } from "vscode-uri" |
| 6 | +import { Position } from "./position" |
| 7 | +import { Range } from "./range" |
| 8 | +import { TextEdit } from "./textEdit" |
| 9 | + |
| 10 | +/** |
| 11 | + * Remove all falsy values from `array`. The original array IS modified. |
| 12 | + */ |
| 13 | +function coalesceInPlace<T>(array: Array<T | undefined | null>): void { |
| 14 | + let to = 0 |
| 15 | + for (let i = 0; i < array.length; i++) { |
| 16 | + if (array[i]) { |
| 17 | + array[to] = array[i] |
| 18 | + to += 1 |
| 19 | + } |
| 20 | + } |
| 21 | + array.length = to |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Additional data for entries of a workspace edit. Supports to label entries and marks entries |
| 26 | + * as needing confirmation by the user. The editor groups edits with equal labels into tree nodes, |
| 27 | + * for instance all edits labelled with "Changes in Strings" would be a tree node. |
| 28 | + */ |
| 29 | +export interface WorkspaceEditEntryMetadata { |
| 30 | + |
| 31 | + /** |
| 32 | + * A flag which indicates that user confirmation is needed. |
| 33 | + */ |
| 34 | + needsConfirmation: boolean; |
| 35 | + |
| 36 | + /** |
| 37 | + * A human-readable string which is rendered prominent. |
| 38 | + */ |
| 39 | + label: string; |
| 40 | + |
| 41 | + /** |
| 42 | + * A human-readable string which is rendered less prominent on the same line. |
| 43 | + */ |
| 44 | + description?: string; |
| 45 | + |
| 46 | + /** |
| 47 | + * The icon path or {@link ThemeIcon} for the edit. |
| 48 | + */ |
| 49 | + iconPath?: URI | { light: URI; dark: URI }; |
| 50 | +} |
| 51 | + |
| 52 | +export interface IFileOperationOptions { |
| 53 | + overwrite?: boolean; |
| 54 | + ignoreIfExists?: boolean; |
| 55 | + ignoreIfNotExists?: boolean; |
| 56 | + recursive?: boolean; |
| 57 | +} |
| 58 | + |
| 59 | +export const enum FileEditType { |
| 60 | + File = 1, |
| 61 | + Text = 2, |
| 62 | + Cell = 3, |
| 63 | + CellReplace = 5, |
| 64 | +} |
| 65 | + |
| 66 | +export interface IFileOperation { |
| 67 | + _type: FileEditType.File; |
| 68 | + from?: URI; |
| 69 | + to?: URI; |
| 70 | + options?: IFileOperationOptions; |
| 71 | + metadata?: WorkspaceEditEntryMetadata; |
| 72 | +} |
| 73 | + |
| 74 | +export interface IFileTextEdit { |
| 75 | + _type: FileEditType.Text; |
| 76 | + uri: URI; |
| 77 | + edit: TextEdit; |
| 78 | + metadata?: WorkspaceEditEntryMetadata; |
| 79 | +} |
| 80 | + |
| 81 | +type WorkspaceEditEntry = IFileOperation | IFileTextEdit |
| 82 | + |
| 83 | +export class WorkspaceEdit { |
| 84 | + |
| 85 | + private readonly _edits: WorkspaceEditEntry[] = [] |
| 86 | + |
| 87 | + public _allEntries(): ReadonlyArray<WorkspaceEditEntry> { |
| 88 | + return this._edits |
| 89 | + } |
| 90 | + |
| 91 | + // --- file |
| 92 | + |
| 93 | + public renameFile(from: URI, to: URI, options?: { overwrite?: boolean; ignoreIfExists?: boolean }, metadata?: WorkspaceEditEntryMetadata): void { |
| 94 | + this._edits.push({ _type: FileEditType.File, from, to, options, metadata }) |
| 95 | + } |
| 96 | + |
| 97 | + public createFile(uri: URI, options?: { overwrite?: boolean; ignoreIfExists?: boolean }, metadata?: WorkspaceEditEntryMetadata): void { |
| 98 | + this._edits.push({ _type: FileEditType.File, from: undefined, to: uri, options, metadata }) |
| 99 | + } |
| 100 | + |
| 101 | + public deleteFile(uri: URI, options?: { recursive?: boolean; ignoreIfNotExists?: boolean }, metadata?: WorkspaceEditEntryMetadata): void { |
| 102 | + this._edits.push({ _type: FileEditType.File, from: uri, to: undefined, options, metadata }) |
| 103 | + } |
| 104 | + |
| 105 | + // --- text |
| 106 | + |
| 107 | + public replace(uri: URI, range: Range, newText: string, metadata?: WorkspaceEditEntryMetadata): void { |
| 108 | + this._edits.push({ _type: FileEditType.Text, uri, edit: new TextEdit(range, newText), metadata }) |
| 109 | + } |
| 110 | + |
| 111 | + public insert(resource: URI, position: Position, newText: string, metadata?: WorkspaceEditEntryMetadata): void { |
| 112 | + this.replace(resource, new Range(position, position), newText, metadata) |
| 113 | + } |
| 114 | + |
| 115 | + public delete(resource: URI, range: Range, metadata?: WorkspaceEditEntryMetadata): void { |
| 116 | + this.replace(resource, range, '', metadata) |
| 117 | + } |
| 118 | + |
| 119 | + // --- text (Maplike) |
| 120 | + |
| 121 | + public has(uri: URI): boolean { |
| 122 | + return this._edits.some(edit => edit._type === FileEditType.Text && edit.uri.toString() === uri.toString()) |
| 123 | + } |
| 124 | + |
| 125 | + public set(uri: URI, edits: TextEdit[]): void { |
| 126 | + if (!edits) { |
| 127 | + // remove all text edits for `uri` |
| 128 | + for (let i = 0; i < this._edits.length; i++) { |
| 129 | + const element = this._edits[i] |
| 130 | + if (element._type === FileEditType.Text && element.uri.toString() === uri.toString()) { |
| 131 | + this._edits[i] = undefined! // will be coalesced down below |
| 132 | + } |
| 133 | + } |
| 134 | + coalesceInPlace(this._edits) |
| 135 | + } else { |
| 136 | + // append edit to the end |
| 137 | + for (const edit of edits) { |
| 138 | + if (edit) { |
| 139 | + this._edits.push({ _type: FileEditType.Text, uri, edit }) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public get(uri: URI): TextEdit[] { |
| 146 | + const res: TextEdit[] = [] |
| 147 | + for (let candidate of this._edits) { |
| 148 | + if (candidate._type === FileEditType.Text && candidate.uri.toString() === uri.toString()) { |
| 149 | + res.push(candidate.edit) |
| 150 | + } |
| 151 | + } |
| 152 | + return res |
| 153 | + } |
| 154 | + |
| 155 | + public entries(): [URI, TextEdit[]][] { |
| 156 | + const textEdits = new ResourceMap<[URI, TextEdit[]]>() |
| 157 | + for (let candidate of this._edits) { |
| 158 | + if (candidate._type === FileEditType.Text) { |
| 159 | + let textEdit = textEdits.get(candidate.uri) |
| 160 | + if (!textEdit) { |
| 161 | + textEdit = [candidate.uri, []] |
| 162 | + textEdits.set(candidate.uri, textEdit) |
| 163 | + } |
| 164 | + textEdit[1].push(candidate.edit) |
| 165 | + } |
| 166 | + } |
| 167 | + return [...textEdits.values()] |
| 168 | + } |
| 169 | + |
| 170 | + public get size(): number { |
| 171 | + return this.entries().length |
| 172 | + } |
| 173 | + |
| 174 | + public toJSON(): any { |
| 175 | + return this.entries() |
| 176 | + } |
| 177 | +} |
0 commit comments