提交 5e021a4f 编写于 作者: J Johannes Rieken

insertSnippet with Position or Range, #19116

上级 8630aef3
......@@ -946,12 +946,12 @@ declare module 'vscode' {
* or accept the snippet.
*
* @param snippet The snippet to insert in this edit.
* @param selection One or many selections at which to insert the snippets. Defaults to the current editor selection.
* @param location Ranges or position at which to insert the snippet, defaults to the current editor selection.
* @param options The undo/redo behaviour around this edit. By default, undo stops will be created before and after this edit.
* @return A promise that resolves with a value indicating if the snippet could be inserted. Note that the promise does not signal
* that the snippet is completely filled-in or accepted.
*/
insertSnippet(snippet: SnippetString, selection?: Selection | Selection[], options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
insertSnippet(snippet: SnippetString, location?: Position | Position[] | Range | Range[], options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
/**
* Adds a set of decorations to the text editor. If a set of decorations already exists with
......
......@@ -138,7 +138,7 @@ export abstract class MainThreadEditorsShape {
$tryRevealRange(id: string, range: editorCommon.IRange, revealType: TextEditorRevealType): TPromise<any> { throw ni(); }
$trySetSelections(id: string, selections: editorCommon.ISelection[]): TPromise<any> { throw ni(); }
$tryApplyEdits(id: string, modelVersionId: number, edits: editorCommon.ISingleEditOperation[], opts: IApplyEditsOptions): TPromise<boolean> { throw ni(); }
$tryInsertSnippet(id: string, template: string, selections: editorCommon.ISelection[], opts: IUndoStopOptions): TPromise<any> { throw ni(); }
$tryInsertSnippet(id: string, template: string, selections: editorCommon.IRange[], opts: IUndoStopOptions): TPromise<any> { throw ni(); }
}
export abstract class MainThreadTreeExplorersShape {
......
......@@ -13,7 +13,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
import { ExtHostDocuments, ExtHostDocumentData } from 'vs/workbench/api/node/extHostDocuments';
import { Selection, Range, Position, EndOfLine, TextEditorRevealType, TextEditorSelectionChangeKind, TextEditorLineNumbersStyle, SnippetString } from './extHostTypes';
import { ISingleEditOperation, TextEditorCursorStyle } from 'vs/editor/common/editorCommon';
import { ISingleEditOperation, TextEditorCursorStyle, IRange } from 'vs/editor/common/editorCommon';
import { IResolvedTextEditorConfiguration, ISelectionChangeEvent, ITextEditorConfigurationUpdate } from 'vs/workbench/api/node/mainThreadEditorsTracker';
import * as TypeConverters from './extHostTypeConverters';
import { MainContext, MainThreadEditorsShape, ExtHostEditorsShape, ITextEditorAddData, ITextEditorPositionData } from './extHost.protocol';
......@@ -620,17 +620,32 @@ class ExtHostTextEditor implements vscode.TextEditor {
});
}
insertSnippet(snippet: SnippetString, where?: Selection | Selection[], options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> {
insertSnippet(snippet: SnippetString, where?: Position | Position[] | Range | Range[], options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> {
let ranges: IRange[];
if (!where || (Array.isArray(where) && where.length === 0)) {
where = this._selections;
}
ranges = this._selections.map(TypeConverters.fromRange);
} else if (where instanceof Position) {
const {lineNumber, column} = TypeConverters.fromPosition(where);
ranges = [{ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column }];
const selections = Array.isArray(where)
? where.map(TypeConverters.fromSelection)
: [TypeConverters.fromSelection(where)];
} else if (where instanceof Range) {
ranges = [TypeConverters.fromRange(where)];
} else {
ranges = [];
for (const posOrRange of where) {
if (posOrRange instanceof Range) {
ranges.push(TypeConverters.fromRange(posOrRange));
} else {
const {lineNumber, column} = TypeConverters.fromPosition(posOrRange);
ranges.push({ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column });
}
}
}
return this._proxy.$tryInsertSnippet(this._id, snippet.value, selections, options);
return this._proxy.$tryInsertSnippet(this._id, snippet.value, ranges, options);
}
// ---- util
......
......@@ -293,11 +293,11 @@ export class MainThreadEditors extends MainThreadEditorsShape {
return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits, opts));
}
$tryInsertSnippet(id: string, template: string, selections: ISelection[], opts: IUndoStopOptions): TPromise<boolean> {
$tryInsertSnippet(id: string, template: string, ranges: IRange[], opts: IUndoStopOptions): TPromise<boolean> {
if (!this._textEditorsMap[id]) {
return TPromise.wrapError('TextEditor disposed');
}
return TPromise.as(this._textEditorsMap[id].insertSnippet(template, selections, opts));
return TPromise.as(this._textEditorsMap[id].insertSnippet(template, ranges, opts));
}
$registerTextEditorDecorationType(key: string, options: IDecorationRenderOptions): void {
......
......@@ -397,14 +397,15 @@ export class MainThreadTextEditor {
return false;
}
insertSnippet(template: string, selections: EditorCommon.ISelection[], opts: IUndoStopOptions) {
insertSnippet(template: string, ranges: EditorCommon.IRange[], opts: IUndoStopOptions) {
if (!this._codeEditor) {
return false;
}
this._codeEditor.focus();
const selections = ranges.map(r => new Selection(r.startLineNumber, r.startColumn, r.endLineNumber, r.endColumn));
this._codeEditor.setSelections(selections);
this._codeEditor.focus();
if (opts.undoStopBefore) {
this._codeEditor.pushUndoStop();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册