當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript workspace.applyEdit方法代碼示例

本文整理匯總了TypeScript中vscode.workspace.applyEdit方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript workspace.applyEdit方法的具體用法?TypeScript workspace.applyEdit怎麽用?TypeScript workspace.applyEdit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在vscode.workspace的用法示例。


在下文中一共展示了workspace.applyEdit方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: onCodeAction

	private async onCodeAction(source: Source, workspaceEdit: WorkspaceEdit): Promise<boolean> {
		const success = workspace.applyEdit(workspaceEdit);
		if (!success) {
			return false;
		}

		let firstEdit: TextEdit | undefined = undefined;
		for (const [uri, edits] of workspaceEdit.entries()) {
			if (uri.fsPath === source.uri.fsPath) {
				firstEdit = edits[0];
				break;
			}
		}

		if (!firstEdit) {
			return true;
		}

		const newLines = firstEdit.newText.match(/\n/g);
		const editedRange = new Range(
			firstEdit.range.start.line, 0,
			firstEdit.range.end.line + 1 + (newLines ? newLines.length : 0), 0);
		// TODO: Workaround for https://github.com/Microsoft/TypeScript/issues/12249
		// apply formatting to the source range until TS returns formatted results
		const edits = (await commands.executeCommand('vscode.executeFormatRangeProvider', source.uri, editedRange, source.formattingOptions || {})) as TextEdit[];
		if (!edits || !edits.length) {
			return false;
		}
		const formattingEdit = new WorkspaceEdit();
		formattingEdit.set(source.uri, edits);
		return workspace.applyEdit(formattingEdit);
	}
開發者ID:Chan-PH,項目名稱:vscode,代碼行數:32,代碼來源:codeActionProvider.ts

示例2: doRefactoring

	private async doRefactoring(file: string, refactor: string, action: string, range: Range): Promise<boolean> {
		const args: Proto.GetEditsForRefactorRequestArgs = {
			file,
			refactor,
			action,
			startLine: range.start.line + 1,
			startOffset: range.start.character + 1,
			endLine: range.end.line + 1,
			endOffset: range.end.character + 1
		};

		const response = await this.client.execute('getEditsForRefactor', args);
		if (!response || !response.body || !response.body.edits.length) {
			return false;
		}

		const edit = this.toWorkspaceEdit(response.body.edits);
		if (!(await workspace.applyEdit(edit))) {
			return false;
		}

		const renameLocation = response.body.renameLocation;
		if (renameLocation) {
			if (window.activeTextEditor && window.activeTextEditor.document.uri.fsPath === file) {
				const pos = new Position(renameLocation.line - 1, renameLocation.offset - 1);
				window.activeTextEditor.selection = new Selection(pos, pos);
				await commands.executeCommand('editor.action.rename');
			}
		}
		return true;
	}
開發者ID:armanio123,項目名稱:vscode,代碼行數:31,代碼來源:refactorProvider.ts

示例3: async

		saveText: async (text: string) => {
			const filename = await utils.createFile('', getFixturePath([
				rule,
				value,
				'test'
			]));
			const doc = await workspace.openTextDocument(filename);
			await window.showTextDocument(doc);
			const edit = new WorkspaceEdit();
			edit.insert(doc.uri, new Position(0, 0), text);
			assert.strictEqual(
				await workspace.applyEdit(edit),
				true,
				'applies edit'
			);
			return await new Promise(resolve => {
				let saveCount = 0;
				workspace.onDidSaveTextDocument(savedDoc => {
					if (++saveCount === (options.saves || 1)) {
						resolve(savedDoc.getText());
					}
				});
				doc.save();
			});
		}
開發者ID:SamVerschueren,項目名稱:editorconfig-vscode,代碼行數:25,代碼來源:index.test.ts

示例4: onCodeAction

	private onCodeAction(source: Source, workspaceEdit: WorkspaceEdit) {
		workspace.applyEdit(workspaceEdit).then(success => {
			if (!success) {
				return Promise.reject<boolean>(false);
			}

			let firstEdit: TextEdit | null = null;
			for (const [uri, edits] of workspaceEdit.entries()) {
				if (uri.fsPath === source.uri.fsPath) {
					firstEdit = edits[0];
					break;
				}
			}

			if (!firstEdit) {
				return true;
			}

			const newLines = firstEdit.newText.match(/\n/g);
			const editedRange = new Range(
				new Position(firstEdit.range.start.line, 0),
				new Position(firstEdit.range.end.line + 1 + (newLines ? newLines.length : 0), 0));

			// TODO: Workaround for https://github.com/Microsoft/TypeScript/issues/12249
			// apply formatting to the source range until TS returns formatted results
			return commands.executeCommand('vscode.executeFormatRangeProvider', source.uri, editedRange, source.formattingOptions || {}).then((edits: TextEdit[]) => {
				if (!edits || !edits.length) {
					return false;
				}
				const workspaceEdit = new WorkspaceEdit();
				workspaceEdit.set(source.uri, edits);
				return workspace.applyEdit(workspaceEdit);
			});
		});
	}
開發者ID:diarmaidm,項目名稱:vscode,代碼行數:35,代碼來源:codeActionProvider.ts

示例5: applyCodeAction

export async function applyCodeAction(
	client: ITypescriptServiceClient,
	action: Proto.CodeAction,
	file: string
): Promise<boolean> {
	if (action.changes && action.changes.length) {
		const workspaceEdit = new WorkspaceEdit();
		for (const change of action.changes) {
			for (const textChange of change.textChanges) {
				workspaceEdit.replace(client.asUrl(change.fileName),
					tsTextSpanToVsRange(textChange),
					textChange.newText);
			}
		}

		if (!(await workspace.applyEdit(workspaceEdit))) {
			return false;
		}
	}

	if (action.commands && action.commands.length) {
		for (const command of action.commands) {
			const response = await client.execute('applyCodeActionCommand', { file, command });
			if (!response || !response.body) {
				return false;
			}
		}
	}
	return true;
}
開發者ID:gokulakrishna9,項目名稱:vscode,代碼行數:30,代碼來源:codeAction.ts

示例6: _revertChanges

	private async _revertChanges(textEditor: TextEditor, changes: LineChange[]): Promise<void> {
		const modifiedDocument = textEditor.document;
		const modifiedUri = modifiedDocument.uri;

		if (modifiedUri.scheme !== 'file') {
			return;
		}

		const originalUri = toGitUri(modifiedUri, '~');
		const originalDocument = await workspace.openTextDocument(originalUri);
		const basename = path.basename(modifiedUri.fsPath);
		const message = localize('confirm revert', "Are you sure you want to revert the selected changes in {0}?", basename);
		const yes = localize('revert', "Revert Changes");
		const pick = await window.showWarningMessage(message, { modal: true }, yes);

		if (pick !== yes) {
			return;
		}

		const result = applyLineChanges(originalDocument, modifiedDocument, changes);
		const edit = new WorkspaceEdit();
		edit.replace(modifiedUri, new Range(new Position(0, 0), modifiedDocument.lineAt(modifiedDocument.lineCount - 1).range.end), result);
		workspace.applyEdit(edit);
		await modifiedDocument.save();
	}
開發者ID:golf1052,項目名稱:vscode,代碼行數:25,代碼來源:commands.ts

示例7: it

	it("renames all exact references but not other items with same name", async () => {
		const doc = currentDoc();
		await setTestContent(`
			class Danny {
				static int myField = 1;
			}
			class Other {
				static int Danny = 2;
			}
			var a = new Danny();
			print(Danny.myField);
		`);
		const renameResult = await (vs.commands.executeCommand("vscode.executeDocumentRenameProvider", doc.uri, positionOf("D^anny"), "NewDanny") as Thenable<vs.WorkspaceEdit>);
		await vs.workspace.applyEdit(renameResult);
		await ensureTestContent(`
			class NewDanny {
				static int myField = 1;
			}
			class Other {
				static int Danny = 2;
			}
			var a = new NewDanny();
			print(NewDanny.myField);
		`);
	});
開發者ID:DanTup,項目名稱:Dart-Code,代碼行數:25,代碼來源:dart_rename_provider.test.ts

示例8: doRefactoring

	private async doRefactoring(document: TextDocument, file: string, refactor: string, action: string, range: Range): Promise<boolean> {
		await this.formattingOptionsManager.ensureFormatOptionsForDocument(document, undefined);

		const args: Proto.GetEditsForRefactorRequestArgs = {
			...vsRangeToTsFileRange(file, range),
			refactor,
			action
		};

		const response = await this.client.execute('getEditsForRefactor', args);
		if (!response || !response.body || !response.body.edits.length) {
			return false;
		}

		const edit = this.toWorkspaceEdit(response.body.edits);
		if (!(await workspace.applyEdit(edit))) {
			return false;
		}

		const renameLocation = response.body.renameLocation;
		if (renameLocation) {
			if (window.activeTextEditor && window.activeTextEditor.document.uri.fsPath === file) {
				const pos = tsLocationToVsPosition(renameLocation);
				window.activeTextEditor.selection = new Selection(pos, pos);
				await commands.executeCommand('editor.action.rename');
			}
		}
		return true;
	}
開發者ID:elibarzilay,項目名稱:vscode,代碼行數:29,代碼來源:refactorProvider.ts

示例9: WorkspaceEdit

			return commands.executeCommand('vscode.executeFormatRangeProvider', source.uri, editedRange, source.formattingOptions || {}).then((edits: TextEdit[]) => {
				if (!edits || !edits.length) {
					return false;
				}
				const workspaceEdit = new WorkspaceEdit();
				workspaceEdit.set(source.uri, edits);
				return workspace.applyEdit(workspaceEdit);
			});
開發者ID:jhasse,項目名稱:vscode,代碼行數:8,代碼來源:codeActionProvider.ts

示例10: applyTextEdit

function applyTextEdit(we) {
    telemetry.traceEvent('command-applytextedit');

    let wse = new vscode.WorkspaceEdit()
    for (let edit of we.documentChanges[0].edits) {
        wse.replace(we.documentChanges[0].textDocument.uri, new vscode.Range(edit.range.start.line, edit.range.start.character, edit.range.end.line, edit.range.end.character), edit.newText)
    }
    vscode.workspace.applyEdit(wse)
}
開發者ID:JuliaEditorSupport,項目名稱:julia-vscode,代碼行數:9,代碼來源:smallcommands.ts


注:本文中的vscode.workspace.applyEdit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。