Skip to content

Commit 5e021a4

Browse files
committed
insertSnippet with Position or Range, microsoft#19116
1 parent 8630aef commit 5e021a4

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

src/vs/vscode.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,12 @@ declare module 'vscode' {
946946
* or accept the snippet.
947947
*
948948
* @param snippet The snippet to insert in this edit.
949-
* @param selection One or many selections at which to insert the snippets. Defaults to the current editor selection.
949+
* @param location Ranges or position at which to insert the snippet, defaults to the current editor selection.
950950
* @param options The undo/redo behaviour around this edit. By default, undo stops will be created before and after this edit.
951951
* @return A promise that resolves with a value indicating if the snippet could be inserted. Note that the promise does not signal
952952
* that the snippet is completely filled-in or accepted.
953953
*/
954-
insertSnippet(snippet: SnippetString, selection?: Selection | Selection[], options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
954+
insertSnippet(snippet: SnippetString, location?: Position | Position[] | Range | Range[], options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
955955

956956
/**
957957
* Adds a set of decorations to the text editor. If a set of decorations already exists with

src/vs/workbench/api/node/extHost.protocol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export abstract class MainThreadEditorsShape {
138138
$tryRevealRange(id: string, range: editorCommon.IRange, revealType: TextEditorRevealType): TPromise<any> { throw ni(); }
139139
$trySetSelections(id: string, selections: editorCommon.ISelection[]): TPromise<any> { throw ni(); }
140140
$tryApplyEdits(id: string, modelVersionId: number, edits: editorCommon.ISingleEditOperation[], opts: IApplyEditsOptions): TPromise<boolean> { throw ni(); }
141-
$tryInsertSnippet(id: string, template: string, selections: editorCommon.ISelection[], opts: IUndoStopOptions): TPromise<any> { throw ni(); }
141+
$tryInsertSnippet(id: string, template: string, selections: editorCommon.IRange[], opts: IUndoStopOptions): TPromise<any> { throw ni(); }
142142
}
143143

144144
export abstract class MainThreadTreeExplorersShape {

src/vs/workbench/api/node/extHostEditors.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
1313
import { IThreadService } from 'vs/workbench/services/thread/common/threadService';
1414
import { ExtHostDocuments, ExtHostDocumentData } from 'vs/workbench/api/node/extHostDocuments';
1515
import { Selection, Range, Position, EndOfLine, TextEditorRevealType, TextEditorSelectionChangeKind, TextEditorLineNumbersStyle, SnippetString } from './extHostTypes';
16-
import { ISingleEditOperation, TextEditorCursorStyle } from 'vs/editor/common/editorCommon';
16+
import { ISingleEditOperation, TextEditorCursorStyle, IRange } from 'vs/editor/common/editorCommon';
1717
import { IResolvedTextEditorConfiguration, ISelectionChangeEvent, ITextEditorConfigurationUpdate } from 'vs/workbench/api/node/mainThreadEditorsTracker';
1818
import * as TypeConverters from './extHostTypeConverters';
1919
import { MainContext, MainThreadEditorsShape, ExtHostEditorsShape, ITextEditorAddData, ITextEditorPositionData } from './extHost.protocol';
@@ -620,17 +620,32 @@ class ExtHostTextEditor implements vscode.TextEditor {
620620
});
621621
}
622622

623-
insertSnippet(snippet: SnippetString, where?: Selection | Selection[], options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> {
623+
insertSnippet(snippet: SnippetString, where?: Position | Position[] | Range | Range[], options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> {
624+
625+
let ranges: IRange[];
624626

625627
if (!where || (Array.isArray(where) && where.length === 0)) {
626-
where = this._selections;
627-
}
628+
ranges = this._selections.map(TypeConverters.fromRange);
629+
630+
} else if (where instanceof Position) {
631+
const {lineNumber, column} = TypeConverters.fromPosition(where);
632+
ranges = [{ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column }];
628633

629-
const selections = Array.isArray(where)
630-
? where.map(TypeConverters.fromSelection)
631-
: [TypeConverters.fromSelection(where)];
634+
} else if (where instanceof Range) {
635+
ranges = [TypeConverters.fromRange(where)];
636+
} else {
637+
ranges = [];
638+
for (const posOrRange of where) {
639+
if (posOrRange instanceof Range) {
640+
ranges.push(TypeConverters.fromRange(posOrRange));
641+
} else {
642+
const {lineNumber, column} = TypeConverters.fromPosition(posOrRange);
643+
ranges.push({ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column });
644+
}
645+
}
646+
}
632647

633-
return this._proxy.$tryInsertSnippet(this._id, snippet.value, selections, options);
648+
return this._proxy.$tryInsertSnippet(this._id, snippet.value, ranges, options);
634649
}
635650

636651
// ---- util

src/vs/workbench/api/node/mainThreadEditors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ export class MainThreadEditors extends MainThreadEditorsShape {
293293
return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits, opts));
294294
}
295295

296-
$tryInsertSnippet(id: string, template: string, selections: ISelection[], opts: IUndoStopOptions): TPromise<boolean> {
296+
$tryInsertSnippet(id: string, template: string, ranges: IRange[], opts: IUndoStopOptions): TPromise<boolean> {
297297
if (!this._textEditorsMap[id]) {
298298
return TPromise.wrapError('TextEditor disposed');
299299
}
300-
return TPromise.as(this._textEditorsMap[id].insertSnippet(template, selections, opts));
300+
return TPromise.as(this._textEditorsMap[id].insertSnippet(template, ranges, opts));
301301
}
302302

303303
$registerTextEditorDecorationType(key: string, options: IDecorationRenderOptions): void {

src/vs/workbench/api/node/mainThreadEditorsTracker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,14 +397,15 @@ export class MainThreadTextEditor {
397397
return false;
398398
}
399399

400-
insertSnippet(template: string, selections: EditorCommon.ISelection[], opts: IUndoStopOptions) {
400+
insertSnippet(template: string, ranges: EditorCommon.IRange[], opts: IUndoStopOptions) {
401401

402402
if (!this._codeEditor) {
403403
return false;
404404
}
405405

406-
this._codeEditor.focus();
406+
const selections = ranges.map(r => new Selection(r.startLineNumber, r.startColumn, r.endLineNumber, r.endColumn));
407407
this._codeEditor.setSelections(selections);
408+
this._codeEditor.focus();
408409

409410
if (opts.undoStopBefore) {
410411
this._codeEditor.pushUndoStop();

0 commit comments

Comments
 (0)