Skip to content

Commit

Permalink
Snippet insertion extension API changes
Browse files Browse the repository at this point in the history
- Basing snippet insertion failure on a new `_codeEditor` null-check.
- Now returns `Thenable<boolean>`.
- Removed vscode.proposed.d.ts copy of the `TextEditor` change.
- Removing empty options interface.
  • Loading branch information
icfjoeld committed Jan 18, 2017
1 parent 95fc032 commit c21734f
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 33 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Expand Up @@ -68,6 +68,7 @@
"name": "VS Code API Tests",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceRoot}",
"${workspaceRoot}/extensions/vscode-api-tests/testWorkspace",
"--extensionDevelopmentPath=${workspaceRoot}/extensions/vscode-api-tests",
"--extensionTestsPath=${workspaceRoot}/extensions/vscode-api-tests/out"
Expand Down
10 changes: 4 additions & 6 deletions extensions/vscode-api-tests/src/editor.test.ts
Expand Up @@ -41,9 +41,8 @@ suite('editor tests', () => {
.appendText(' snippet');

return withRandomFileEditor('', (editor, doc) => {
editor.edit(snippetString);

return editor.edit(() => {}).then(() => {
return editor.edit(snippetString).then(inserted => {
assert.ok(inserted);
assert.equal(doc.getText(), 'This is a placeholder snippet');
assert.ok(doc.isDirty);
});
Expand All @@ -60,9 +59,8 @@ suite('editor tests', () => {
new Position(0, 12)
);

editor.edit(snippetString);

return editor.edit(() => {}).then(() => {
return editor.edit(snippetString).then(inserted => {
assert.ok(inserted);
assert.equal(doc.getText(), 'This has been replaced');
assert.ok(doc.isDirty);
});
Expand Down
1 change: 0 additions & 1 deletion extensions/vscode-api-tests/src/typings/ref.d.ts
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

/// <reference path='../../../../src/vs/vscode.proposed.d.ts'/>
/// <reference path='../../../../src/typings/mocha.d.ts'/>
/// <reference path='../../../../extensions/declares.d.ts'/>
/// <reference path='../../../../extensions/node.d.ts'/>
Expand Down
3 changes: 2 additions & 1 deletion src/vs/vscode.d.ts
Expand Up @@ -941,8 +941,9 @@ declare module 'vscode' {
*
* @param snippet The snippet to insert in this edit.
* @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.
*/
edit(snippet: SnippetString, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): void;
edit(snippet: SnippetString, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;

/**
* Adds a set of decorations to the text editor. If a set of decorations already exists with
Expand Down
11 changes: 0 additions & 11 deletions src/vs/vscode.proposed.d.ts
Expand Up @@ -108,17 +108,6 @@ declare module 'vscode' {
getClickCommand?(node: T): string;
}

export interface TextEditor {
/**
* Enters snippet mode in the editor with the specified snippet.
*
* @param snippet The snippet to insert in this edit.
* @param options The undo/redo behaviour around this edit. By default, undo stops will be created before and after this edit.
*/
edit(snippet: SnippetString, options?: { undoStopBefore: boolean; undoStopAfter: boolean; }): void;

}

export interface SCMResourceThemableDecorations {
readonly iconPath?: string | Uri;
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/node/extHost.protocol.ts
Expand Up @@ -34,7 +34,7 @@ import { IWorkspaceConfigurationValues } from 'vs/workbench/services/configurati
import { IPickOpenEntry, IPickOptions } from 'vs/platform/quickOpen/common/quickOpen';
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
import { IWorkspaceSymbol } from 'vs/workbench/parts/search/common/search';
import { IApplyEditsOptions, IInsertSnippetOptions, TextEditorRevealType, ITextEditorConfigurationUpdate, IResolvedTextEditorConfiguration, ISelectionChangeEvent } from './mainThreadEditorsTracker';
import { IApplyEditsOptions, IUndoStopOptions, TextEditorRevealType, ITextEditorConfigurationUpdate, IResolvedTextEditorConfiguration, ISelectionChangeEvent } from './mainThreadEditorsTracker';

import { InternalTreeExplorerNodeContent } from 'vs/workbench/parts/explorers/common/treeExplorerViewModel';

Expand Down Expand Up @@ -137,7 +137,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, opts: IInsertSnippetOptions): TPromise<any> { throw ni(); }
$tryInsertSnippet(id: string, template: string, opts: IUndoStopOptions): TPromise<any> { throw ni(); }
}

export abstract class MainThreadTreeExplorersShape {
Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/api/node/extHostEditors.ts
Expand Up @@ -596,11 +596,11 @@ class ExtHostTextEditor implements vscode.TextEditor {
// ---- editing

edit(callback: (edit: TextEditorEdit) => void, options: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;
edit(snippet: SnippetString, options: { undoStopBefore: boolean; undoStopAfter: boolean; }): void;
edit(snippet: SnippetString, options: { undoStopBefore: boolean; undoStopAfter: boolean; }): Thenable<boolean>;

edit(callbackOrSnippet: ((edit: TextEditorEdit) => void) | SnippetString, options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> | void {
edit(callbackOrSnippet: ((edit: TextEditorEdit) => void) | SnippetString, options: { undoStopBefore: boolean; undoStopAfter: boolean; } = { undoStopBefore: true, undoStopAfter: true }): Thenable<boolean> {
if (SnippetString.isSnippetString(callbackOrSnippet)) {
this._proxy.$tryInsertSnippet(this._id, callbackOrSnippet.value, options);
return this._proxy.$tryInsertSnippet(this._id, callbackOrSnippet.value, options);
} else {
let edit = new TextEditorEdit(this._documentData.document, options);
callbackOrSnippet(edit);
Expand Down
7 changes: 3 additions & 4 deletions src/vs/workbench/api/node/mainThreadEditors.ts
Expand Up @@ -14,7 +14,7 @@ import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/edi
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
import { Position as EditorPosition } from 'vs/platform/editor/common/editor';
import { IModelService } from 'vs/editor/common/services/modelService';
import { MainThreadEditorsTracker, TextEditorRevealType, MainThreadTextEditor, IApplyEditsOptions, IInsertSnippetOptions, ITextEditorConfigurationUpdate } from 'vs/workbench/api/node/mainThreadEditorsTracker';
import { MainThreadEditorsTracker, TextEditorRevealType, MainThreadTextEditor, IApplyEditsOptions, IUndoStopOptions, ITextEditorConfigurationUpdate } from 'vs/workbench/api/node/mainThreadEditorsTracker';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { equals as arrayEquals } from 'vs/base/common/arrays';
import { equals as objectEquals } from 'vs/base/common/objects';
Expand Down Expand Up @@ -293,12 +293,11 @@ export class MainThreadEditors extends MainThreadEditorsShape {
return TPromise.as(this._textEditorsMap[id].applyEdits(modelVersionId, edits, opts));
}

$tryInsertSnippet(id: string, template: string, opts: IInsertSnippetOptions): TPromise<any> {
$tryInsertSnippet(id: string, template: string, opts: IUndoStopOptions): TPromise<boolean> {
if (!this._textEditorsMap[id]) {
return TPromise.wrapError('TextEditor disposed');
}
this._textEditorsMap[id].insertSnippet(template, opts);
return TPromise.as(null);
return TPromise.as(this._textEditorsMap[id].insertSnippet(template, opts));
}

$registerTextEditorDecorationType(key: string, options: IDecorationRenderOptions): void {
Expand Down
12 changes: 7 additions & 5 deletions src/vs/workbench/api/node/mainThreadEditorsTracker.ts
Expand Up @@ -68,10 +68,6 @@ export interface IApplyEditsOptions extends IUndoStopOptions {
setEndOfLine: EndOfLine;
}

export interface IInsertSnippetOptions extends IUndoStopOptions {

}

/**
* Text Editor that is permanently bound to the same model.
* It can be bound or not to a CodeEditor.
Expand Down Expand Up @@ -392,9 +388,13 @@ export class MainThreadTextEditor {
return false;
}

insertSnippet(template: string, opts: IInsertSnippetOptions) {
insertSnippet(template: string, opts: IUndoStopOptions) {
const snippetController = SnippetController.get(this._codeEditor);

if (!this._codeEditor) {
return false;
}

this._codeEditor.focus();

if (opts.undoStopBefore) {
Expand All @@ -406,6 +406,8 @@ export class MainThreadTextEditor {
if (opts.undoStopAfter) {
this._codeEditor.pushUndoStop();
}

return true;
}
}

Expand Down

0 comments on commit c21734f

Please sign in to comment.