Skip to content

Commit

Permalink
add new api command to compare two resources, fixes #1865
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken authored and sandy081 committed May 18, 2016
1 parent 7469790 commit 08d41ce
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
55 changes: 40 additions & 15 deletions extensions/vscode-api-tests/src/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,28 @@ suite('commands namespace tests', () => {
}, done);
});

test('api-command: workbench.html.preview', function () {
test('editorCommand with extra args', function () {

let args: IArguments;
let registration = commands.registerTextEditorCommand('t1', function() {
args = arguments;
});

return workspace.openTextDocument(join(workspace.rootPath, './far.js')).then(doc => {
return window.showTextDocument(doc).then(editor => {
return commands.executeCommand('t1', 12345, commands);
}).then(() => {
assert.ok(args);
assert.equal(args.length, 4);
assert.ok(args[2] === 12345);
assert.ok(args[3] === commands);
registration.dispose();
});
});

});

test('api-command: vscode.previewHtm', function () {

let registration = workspace.registerTextDocumentContentProvider('speciale', {
provideTextDocumentContent(uri) {
Expand All @@ -57,24 +78,28 @@ suite('commands namespace tests', () => {

});

test('editorCommand with extra args', function () {
test('api-command: vscode.diff', function () {

let args: IArguments;
let registration = commands.registerTextEditorCommand('t1', function() {
args = arguments;
let registration = workspace.registerTextDocumentContentProvider('sc', {
provideTextDocumentContent(uri) {
return `content of URI <b>${uri.toString()}</b>#${Math.random()}`;
}
});

return workspace.openTextDocument(join(workspace.rootPath, './far.js')).then(doc => {
return window.showTextDocument(doc).then(editor => {
return commands.executeCommand('t1', 12345, commands);
}).then(() => {
assert.ok(args);
assert.equal(args.length, 4);
assert.ok(args[2] === 12345);
assert.ok(args[3] === commands);
registration.dispose();
});

let a = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b'), 'DIFF').then(value => {
assert.ok(value === void 0);
registration.dispose();
});

let b = commands.executeCommand('vscode.diff', Uri.parse('sc:a'), Uri.parse('sc:b')).then(value => {
assert.ok(value === void 0);
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));

return Promise.all([a, b, c]);
});
});
11 changes: 11 additions & 0 deletions src/vs/workbench/api/node/extHostApiCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ class ExtHostApiCommands {
{ name: 'configuration', description: '(optional) Name of the debug configuration from \'launch.json\' to use. Or a configuration json object to use.' }
]
});

this._register('vscode.diff', (left: URI, right: URI, label: string) => {
return this._commands.executeCommand('_workbench.diff', [left, right, label]);
}, {
description: 'Opens the provided resources in the diff editor to compare their contents.',
args: [
{ name: 'left', description: 'Left-hand side resource of the diff editor', constraint: URI },
{ name: 'right', description: 'Right-hand side resource of the diff editor', constraint: URI },
{ name: 'title', description: '(optional) Human readable title for the diff editor', constraint: v => v === void 0 || typeof v === 'string' }
]
});
}

// --- command impl
Expand Down
28 changes: 28 additions & 0 deletions src/vs/workbench/electron-browser/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@

'use strict';

import URI from 'vs/base/common/uri';
import {TPromise} from 'vs/base/common/winjs.base';
import timer = require('vs/base/common/timer');
import paths = require('vs/base/common/paths');
import {Action} from 'vs/base/common/actions';
import {IWindowService} from 'vs/workbench/services/window/electron-browser/windowService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {EditorInput} from 'vs/workbench/common/editor';
import {DiffEditorInput} from 'vs/workbench/common/editor/diffEditorInput';
import nls = require('vs/nls');
import {IMessageService, Severity} from 'vs/platform/message/common/message';
import {IWindowConfiguration} from 'vs/workbench/electron-browser/window';
Expand Down Expand Up @@ -459,4 +462,29 @@ KeybindingsRegistry.registerCommandDesc({
},
when: undefined,
primary: undefined
});

KeybindingsRegistry.registerCommandDesc({
id: '_workbench.diff',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
handler(accessor: ServicesAccessor, args: [URI, URI, string]) {

const editorService = accessor.get(IWorkbenchEditorService);
let [left, right, label] = args;

if (!label) {
label = nls.localize('diffLeftRightLabel', "{0} ⟷ {1}", left.toString(true), right.toString(true));
}

return TPromise.join([editorService.inputToType({ resource: left }), editorService.inputToType({ resource: right })]).then(inputs => {
const [left, right] = inputs;

const diff = new DiffEditorInput(label, undefined, <EditorInput>left, <EditorInput>right);
return editorService.openEditor(diff);
}).then(() => {
return void 0;
});
},
when: undefined,
primary: undefined
});

0 comments on commit 08d41ce

Please sign in to comment.