From f381ce961d6e0225398019bf8a219ded65c3eb61 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 7 Jul 2017 12:20:34 +0200 Subject: [PATCH] Add API to open a file or diff editor on a specific selection range (fixes #30241) --- extensions/vscode-api-tests/src/commands.test.ts | 13 +++++++++---- extensions/vscode-api-tests/src/workspace.test.ts | 13 +++++++++++++ src/vs/vscode.d.ts | 5 +++++ .../api/electron-browser/mainThreadEditors.ts | 7 ++++--- src/vs/workbench/api/node/extHost.protocol.ts | 1 + src/vs/workbench/api/node/extHostApiCommands.ts | 7 ++++--- src/vs/workbench/api/node/extHostTextEditors.ts | 1 + 7 files changed, 37 insertions(+), 10 deletions(-) diff --git a/extensions/vscode-api-tests/src/commands.test.ts b/extensions/vscode-api-tests/src/commands.test.ts index 76544c649d1..47b4d85f347 100644 --- a/extensions/vscode-api-tests/src/commands.test.ts +++ b/extensions/vscode-api-tests/src/commands.test.ts @@ -7,7 +7,7 @@ import * as assert from 'assert'; import { join } from 'path'; -import { commands, workspace, window, Uri, ViewColumn } from 'vscode'; +import { commands, workspace, window, Uri, ViewColumn, Range, Position } from 'vscode'; suite('commands namespace tests', () => { @@ -114,10 +114,15 @@ suite('commands namespace tests', () => { registration.dispose(); }); - let c = commands.executeCommand('vscode.diff').then(() => assert.ok(false), () => assert.ok(true)); - let d = commands.executeCommand('vscode.diff', 1, 2, 3).then(() => assert.ok(false), () => assert.ok(true)); + let c = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b'), 'Title', { selection: new Range(new Position(1, 1), new Position(1, 2)) }).then(value => { + assert.ok(value === void 0); + registration.dispose(); + }); - return Promise.all([a, b, c, d]); + let d = commands.executeCommand('vscode.diff').then(() => assert.ok(false), () => assert.ok(true)); + let e = commands.executeCommand('vscode.diff', 1, 2, 3).then(() => assert.ok(false), () => assert.ok(true)); + + return Promise.all([a, b, c, d, e]); }); test('api-command: vscode.open', function () { diff --git a/extensions/vscode-api-tests/src/workspace.test.ts b/extensions/vscode-api-tests/src/workspace.test.ts index f5527062935..c0271c7a023 100644 --- a/extensions/vscode-api-tests/src/workspace.test.ts +++ b/extensions/vscode-api-tests/src/workspace.test.ts @@ -260,6 +260,19 @@ suite('workspace-namespace', () => { }); }); + test('openTextDocument, with selection', function () { + return createRandomFile('foo\nbar\nbar').then(file => { + return vscode.workspace.openTextDocument(file).then(doc => { + return vscode.window.showTextDocument(doc, { selection: new vscode.Range(new vscode.Position(1, 1), new vscode.Position(1, 2)) }).then(editor => { + assert.equal(editor.selection.start.line, 1); + assert.equal(editor.selection.start.character, 1); + assert.equal(editor.selection.end.line, 1); + assert.equal(editor.selection.end.character, 2); + }); + }); + }); + }); + test('registerTextDocumentContentProvider, simple', function () { let registration = vscode.workspace.registerTextDocumentContentProvider('foo', { diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 8d6a7554e5d..2f717e4ddbf 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -743,6 +743,11 @@ declare module 'vscode' { * with the next editor or if it will be kept. */ preview?: boolean; + + /** + * An optional selection to apply for the document in the [editor](#TextEditor). + */ + selection?: Range; } /** diff --git a/src/vs/workbench/api/electron-browser/mainThreadEditors.ts b/src/vs/workbench/api/electron-browser/mainThreadEditors.ts index b1d0589a319..24952bb7da7 100644 --- a/src/vs/workbench/api/electron-browser/mainThreadEditors.ts +++ b/src/vs/workbench/api/electron-browser/mainThreadEditors.ts @@ -12,7 +12,7 @@ import { ISingleEditOperation, IDecorationRenderOptions, IDecorationOptions, ILi import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService'; -import { IEditorOptions, Position as EditorPosition } from 'vs/platform/editor/common/editor'; +import { Position as EditorPosition, ITextEditorOptions } from 'vs/platform/editor/common/editor'; import { MainThreadTextEditor } from './mainThreadEditor'; import { ITextEditorConfigurationUpdate, TextEditorRevealType, IApplyEditsOptions, IUndoStopOptions } from 'vs/workbench/api/node/extHost.protocol'; @@ -107,9 +107,10 @@ export class MainThreadEditors extends MainThreadEditorsShape { // --- from extension host process $tryShowTextDocument(resource: URI, options: ITextDocumentShowOptions): TPromise { - const editorOptions: IEditorOptions = { + const editorOptions: ITextEditorOptions = { preserveFocus: options.preserveFocus, - pinned: options.pinned + pinned: options.pinned, + selection: options.selection }; const input = { diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index 31cf343bcde..4d0d6607be6 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -183,6 +183,7 @@ export interface ITextDocumentShowOptions { position?: EditorPosition; preserveFocus?: boolean; pinned?: boolean; + selection?: IRange; } export abstract class MainThreadEditorsShape { diff --git a/src/vs/workbench/api/node/extHostApiCommands.ts b/src/vs/workbench/api/node/extHostApiCommands.ts index 0390cec3705..9f3e5af1cbc 100644 --- a/src/vs/workbench/api/node/extHostApiCommands.ts +++ b/src/vs/workbench/api/node/extHostApiCommands.ts @@ -15,7 +15,7 @@ import * as modes from 'vs/editor/common/modes'; import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands'; import { ExtHostCommands } from 'vs/workbench/api/node/extHostCommands'; import { IWorkspaceSymbolProvider } from 'vs/workbench/parts/search/common/search'; -import { IEditorOptions } from 'vs/platform/editor/common/editor'; +import { ITextEditorOptions } from 'vs/platform/editor/common/editor'; export class ExtHostApiCommands { @@ -205,11 +205,12 @@ export class ExtHostApiCommands { }); this._register('vscode.diff', (left: URI, right: URI, label: string, options?: vscode.TextDocumentShowOptions) => { - let editorOptions: IEditorOptions; + let editorOptions: ITextEditorOptions; if (options) { editorOptions = { pinned: typeof options.preview === 'boolean' ? !options.preview : undefined, - preserveFocus: options.preserveFocus + preserveFocus: options.preserveFocus, + selection: typeof options.selection === 'object' ? typeConverters.fromRange(options.selection) : undefined }; } diff --git a/src/vs/workbench/api/node/extHostTextEditors.ts b/src/vs/workbench/api/node/extHostTextEditors.ts index c49aabf9382..493dd531412 100644 --- a/src/vs/workbench/api/node/extHostTextEditors.ts +++ b/src/vs/workbench/api/node/extHostTextEditors.ts @@ -69,6 +69,7 @@ export class ExtHostEditors extends ExtHostEditorsShape { options = { position: TypeConverters.fromViewColumn(columnOrOptions.viewColumn), preserveFocus: columnOrOptions.preserveFocus, + selection: typeof columnOrOptions.selection === 'object' ? TypeConverters.fromRange(columnOrOptions.selection) : undefined, pinned: typeof columnOrOptions.preview === 'boolean' ? !columnOrOptions.preview : undefined }; } else { -- GitLab