Merge from vscode 718331d6f3ebd1b571530ab499edb266ddd493d5

This commit is contained in:
ADS Merger
2020-02-08 04:50:58 +00:00
parent 8c61538a27
commit 2af13c18d2
752 changed files with 16458 additions and 10063 deletions

View File

@@ -1,21 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { originalFSPath } from 'vs/base/common/resources';
import { isWindows } from 'vs/base/common/platform';
suite('ExtHost API', function () {
test('issue #51387: originalFSPath', function () {
if (isWindows) {
assert.equal(originalFSPath(URI.file('C:\\test')).charAt(0), 'C');
assert.equal(originalFSPath(URI.file('c:\\test')).charAt(0), 'c');
assert.equal(originalFSPath(URI.revive(JSON.parse(JSON.stringify(URI.file('C:\\test'))))).charAt(0), 'C');
assert.equal(originalFSPath(URI.revive(JSON.parse(JSON.stringify(URI.file('c:\\test'))))).charAt(0), 'c');
}
});
});

View File

@@ -1,925 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { setUnexpectedErrorHandler, errorHandler } from 'vs/base/common/errors';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { URI } from 'vs/base/common/uri';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { TextModel as EditorModel } from 'vs/editor/common/model/textModel';
import { TestRPCProtocol } from './testRPCProtocol';
import { MarkerService } from 'vs/platform/markers/common/markerService';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IModelService } from 'vs/editor/common/services/modelService';
import { ExtHostLanguageFeatures } from 'vs/workbench/api/common/extHostLanguageFeatures';
import { MainThreadLanguageFeatures } from 'vs/workbench/api/browser/mainThreadLanguageFeatures';
import { ExtHostApiCommands } from 'vs/workbench/api/common/extHostApiCommands';
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { MainThreadCommands } from 'vs/workbench/api/browser/mainThreadCommands';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { MainContext, ExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
import { ExtHostDiagnostics } from 'vs/workbench/api/common/extHostDiagnostics';
import type * as vscode from 'vscode';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import 'vs/workbench/contrib/search/browser/search.contribution';
import { NullLogService } from 'vs/platform/log/common/log';
import { ITextModel } from 'vs/editor/common/model';
import { nullExtensionDescription } from 'vs/workbench/services/extensions/common/extensions';
import { dispose } from 'vs/base/common/lifecycle';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { NullApiDeprecationService } from 'vs/workbench/api/common/extHostApiDeprecationService';
const defaultSelector = { scheme: 'far' };
const model: ITextModel = EditorModel.createFromString(
[
'This is the first line',
'This is the second line',
'This is the third line',
].join('\n'),
undefined,
undefined,
URI.parse('far://testing/file.b'));
let rpcProtocol: TestRPCProtocol;
let extHost: ExtHostLanguageFeatures;
let mainThread: MainThreadLanguageFeatures;
let commands: ExtHostCommands;
let disposables: vscode.Disposable[] = [];
let originalErrorHandler: (e: any) => any;
function assertRejects(fn: () => Promise<any>, message: string = 'Expected rejection') {
return fn().then(() => assert.ok(false, message), _err => assert.ok(true));
}
suite('ExtHostLanguageFeatureCommands', function () {
suiteSetup(() => {
originalErrorHandler = errorHandler.getUnexpectedErrorHandler();
setUnexpectedErrorHandler(() => { });
// Use IInstantiationService to get typechecking when instantiating
let inst: IInstantiationService;
{
let instantiationService = new TestInstantiationService();
rpcProtocol = new TestRPCProtocol();
instantiationService.stub(ICommandService, {
_serviceBrand: undefined,
executeCommand(id: string, ...args: any): any {
const command = CommandsRegistry.getCommands().get(id);
if (!command) {
return Promise.reject(new Error(id + ' NOT known'));
}
const { handler } = command;
return Promise.resolve(instantiationService.invokeFunction(handler, ...args));
}
});
instantiationService.stub(IMarkerService, new MarkerService());
instantiationService.stub(IModelService, <IModelService>{
_serviceBrand: undefined,
getModel(): any { return model; },
createModel() { throw new Error(); },
updateModel() { throw new Error(); },
setMode() { throw new Error(); },
destroyModel() { throw new Error(); },
getModels() { throw new Error(); },
onModelAdded: undefined!,
onModelModeChanged: undefined!,
onModelRemoved: undefined!,
getCreationOptions() { throw new Error(); }
});
instantiationService.stub(IEditorWorkerService, new class extends mock<IEditorWorkerService>() {
async computeMoreMinimalEdits(_uri: any, edits: any) {
return edits || undefined;
}
});
inst = instantiationService;
}
const extHostDocumentsAndEditors = new ExtHostDocumentsAndEditors(rpcProtocol, new NullLogService());
extHostDocumentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
versionId: model.getVersionId(),
modeId: model.getLanguageIdentifier().language,
uri: model.uri,
lines: model.getValue().split(model.getEOL()),
EOL: model.getEOL(),
}]
});
const extHostDocuments = new ExtHostDocuments(rpcProtocol, extHostDocumentsAndEditors);
rpcProtocol.set(ExtHostContext.ExtHostDocuments, extHostDocuments);
commands = new ExtHostCommands(rpcProtocol, new NullLogService());
rpcProtocol.set(ExtHostContext.ExtHostCommands, commands);
rpcProtocol.set(MainContext.MainThreadCommands, inst.createInstance(MainThreadCommands, rpcProtocol));
ExtHostApiCommands.register(commands);
const diagnostics = new ExtHostDiagnostics(rpcProtocol, new NullLogService());
rpcProtocol.set(ExtHostContext.ExtHostDiagnostics, diagnostics);
extHost = new ExtHostLanguageFeatures(rpcProtocol, null, extHostDocuments, commands, diagnostics, new NullLogService(), NullApiDeprecationService);
rpcProtocol.set(ExtHostContext.ExtHostLanguageFeatures, extHost);
mainThread = rpcProtocol.set(MainContext.MainThreadLanguageFeatures, inst.createInstance(MainThreadLanguageFeatures, rpcProtocol));
return rpcProtocol.sync();
});
suiteTeardown(() => {
setUnexpectedErrorHandler(originalErrorHandler);
model.dispose();
mainThread.dispose();
});
teardown(() => {
disposables = dispose(disposables);
return rpcProtocol.sync();
});
// --- workspace symbols
test('WorkspaceSymbols, invalid arguments', function () {
let promises = [
assertRejects(() => commands.executeCommand('vscode.executeWorkspaceSymbolProvider')),
assertRejects(() => commands.executeCommand('vscode.executeWorkspaceSymbolProvider', null)),
assertRejects(() => commands.executeCommand('vscode.executeWorkspaceSymbolProvider', undefined)),
assertRejects(() => commands.executeCommand('vscode.executeWorkspaceSymbolProvider', true))
];
return Promise.all(promises);
});
test('WorkspaceSymbols, back and forth', function () {
disposables.push(extHost.registerWorkspaceSymbolProvider(nullExtensionDescription, <vscode.WorkspaceSymbolProvider>{
provideWorkspaceSymbols(query): any {
return [
new types.SymbolInformation(query, types.SymbolKind.Array, new types.Range(0, 0, 1, 1), URI.parse('far://testing/first')),
new types.SymbolInformation(query, types.SymbolKind.Array, new types.Range(0, 0, 1, 1), URI.parse('far://testing/second'))
];
}
}));
disposables.push(extHost.registerWorkspaceSymbolProvider(nullExtensionDescription, <vscode.WorkspaceSymbolProvider>{
provideWorkspaceSymbols(query): any {
return [
new types.SymbolInformation(query, types.SymbolKind.Array, new types.Range(0, 0, 1, 1), URI.parse('far://testing/first'))
];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeWorkspaceSymbolProvider', 'testing').then(value => {
for (let info of value) {
assert.ok(info instanceof types.SymbolInformation);
assert.equal(info.name, 'testing');
assert.equal(info.kind, types.SymbolKind.Array);
}
assert.equal(value.length, 3);
});
});
});
test('executeWorkspaceSymbolProvider should accept empty string, #39522', async function () {
disposables.push(extHost.registerWorkspaceSymbolProvider(nullExtensionDescription, {
provideWorkspaceSymbols(): vscode.SymbolInformation[] {
return [new types.SymbolInformation('hello', types.SymbolKind.Array, new types.Range(0, 0, 0, 0), URI.parse('foo:bar')) as vscode.SymbolInformation];
}
}));
await rpcProtocol.sync();
let symbols = await commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeWorkspaceSymbolProvider', '');
assert.equal(symbols.length, 1);
await rpcProtocol.sync();
symbols = await commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeWorkspaceSymbolProvider', '*');
assert.equal(symbols.length, 1);
});
// --- formatting
test('executeFormatDocumentProvider, back and forth', async function () {
disposables.push(extHost.registerDocumentFormattingEditProvider(nullExtensionDescription, defaultSelector, new class implements vscode.DocumentFormattingEditProvider {
provideDocumentFormattingEdits() {
return [types.TextEdit.insert(new types.Position(0, 0), '42')];
}
}));
await rpcProtocol.sync();
let edits = await commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeFormatDocumentProvider', model.uri);
assert.equal(edits.length, 1);
});
// --- definition
test('Definition, invalid arguments', function () {
let promises = [
assertRejects(() => commands.executeCommand('vscode.executeDefinitionProvider')),
assertRejects(() => commands.executeCommand('vscode.executeDefinitionProvider', null)),
assertRejects(() => commands.executeCommand('vscode.executeDefinitionProvider', undefined)),
assertRejects(() => commands.executeCommand('vscode.executeDefinitionProvider', true, false))
];
return Promise.all(promises);
});
test('Definition, back and forth', function () {
disposables.push(extHost.registerDefinitionProvider(nullExtensionDescription, defaultSelector, <vscode.DefinitionProvider>{
provideDefinition(doc: any): any {
return new types.Location(doc.uri, new types.Range(0, 0, 0, 0));
}
}));
disposables.push(extHost.registerDefinitionProvider(nullExtensionDescription, defaultSelector, <vscode.DefinitionProvider>{
provideDefinition(doc: any): any {
return [
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', model.uri, new types.Position(0, 0)).then(values => {
assert.equal(values.length, 4);
for (let v of values) {
assert.ok(v.uri instanceof URI);
assert.ok(v.range instanceof types.Range);
}
});
});
});
// --- declaration
test('Declaration, back and forth', function () {
disposables.push(extHost.registerDeclarationProvider(nullExtensionDescription, defaultSelector, <vscode.DeclarationProvider>{
provideDeclaration(doc: any): any {
return new types.Location(doc.uri, new types.Range(0, 0, 0, 0));
}
}));
disposables.push(extHost.registerDeclarationProvider(nullExtensionDescription, defaultSelector, <vscode.DeclarationProvider>{
provideDeclaration(doc: any): any {
return [
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.Location[]>('vscode.executeDeclarationProvider', model.uri, new types.Position(0, 0)).then(values => {
assert.equal(values.length, 4);
for (let v of values) {
assert.ok(v.uri instanceof URI);
assert.ok(v.range instanceof types.Range);
}
});
});
});
// --- type definition
test('Type Definition, invalid arguments', function () {
const promises = [
assertRejects(() => commands.executeCommand('vscode.executeTypeDefinitionProvider')),
assertRejects(() => commands.executeCommand('vscode.executeTypeDefinitionProvider', null)),
assertRejects(() => commands.executeCommand('vscode.executeTypeDefinitionProvider', undefined)),
assertRejects(() => commands.executeCommand('vscode.executeTypeDefinitionProvider', true, false))
];
return Promise.all(promises);
});
test('Type Definition, back and forth', function () {
disposables.push(extHost.registerTypeDefinitionProvider(nullExtensionDescription, defaultSelector, <vscode.TypeDefinitionProvider>{
provideTypeDefinition(doc: any): any {
return new types.Location(doc.uri, new types.Range(0, 0, 0, 0));
}
}));
disposables.push(extHost.registerTypeDefinitionProvider(nullExtensionDescription, defaultSelector, <vscode.TypeDefinitionProvider>{
provideTypeDefinition(doc: any): any {
return [
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
new types.Location(doc.uri, new types.Range(0, 0, 0, 0)),
];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.Location[]>('vscode.executeTypeDefinitionProvider', model.uri, new types.Position(0, 0)).then(values => {
assert.equal(values.length, 4);
for (const v of values) {
assert.ok(v.uri instanceof URI);
assert.ok(v.range instanceof types.Range);
}
});
});
});
// --- references
test('reference search, back and forth', function () {
disposables.push(extHost.registerReferenceProvider(nullExtensionDescription, defaultSelector, <vscode.ReferenceProvider>{
provideReferences() {
return [
new types.Location(URI.parse('some:uri/path'), new types.Range(0, 1, 0, 5))
];
}
}));
return commands.executeCommand<vscode.Location[]>('vscode.executeReferenceProvider', model.uri, new types.Position(0, 0)).then(values => {
assert.equal(values.length, 1);
let [first] = values;
assert.equal(first.uri.toString(), 'some:uri/path');
assert.equal(first.range.start.line, 0);
assert.equal(first.range.start.character, 1);
assert.equal(first.range.end.line, 0);
assert.equal(first.range.end.character, 5);
});
});
// --- outline
test('Outline, back and forth', function () {
disposables.push(extHost.registerDocumentSymbolProvider(nullExtensionDescription, defaultSelector, <vscode.DocumentSymbolProvider>{
provideDocumentSymbols(): any {
return [
new types.SymbolInformation('testing1', types.SymbolKind.Enum, new types.Range(1, 0, 1, 0)),
new types.SymbolInformation('testing2', types.SymbolKind.Enum, new types.Range(0, 1, 0, 3)),
];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeDocumentSymbolProvider', model.uri).then(values => {
assert.equal(values.length, 2);
let [first, second] = values;
assert.ok(first instanceof types.SymbolInformation);
assert.ok(second instanceof types.SymbolInformation);
assert.equal(first.name, 'testing2');
assert.equal(second.name, 'testing1');
});
});
});
test('vscode.executeDocumentSymbolProvider command only returns SymbolInformation[] rather than DocumentSymbol[] #57984', function () {
disposables.push(extHost.registerDocumentSymbolProvider(nullExtensionDescription, defaultSelector, <vscode.DocumentSymbolProvider>{
provideDocumentSymbols(): any {
return [
new types.SymbolInformation('SymbolInformation', types.SymbolKind.Enum, new types.Range(1, 0, 1, 0))
];
}
}));
disposables.push(extHost.registerDocumentSymbolProvider(nullExtensionDescription, defaultSelector, <vscode.DocumentSymbolProvider>{
provideDocumentSymbols(): any {
let root = new types.DocumentSymbol('DocumentSymbol', 'DocumentSymbol#detail', types.SymbolKind.Enum, new types.Range(1, 0, 1, 0), new types.Range(1, 0, 1, 0));
root.children = [new types.DocumentSymbol('DocumentSymbol#child', 'DocumentSymbol#detail#child', types.SymbolKind.Enum, new types.Range(1, 0, 1, 0), new types.Range(1, 0, 1, 0))];
return [root];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<(vscode.SymbolInformation & vscode.DocumentSymbol)[]>('vscode.executeDocumentSymbolProvider', model.uri).then(values => {
assert.equal(values.length, 2);
let [first, second] = values;
assert.ok(first instanceof types.SymbolInformation);
assert.ok(!(first instanceof types.DocumentSymbol));
assert.ok(second instanceof types.SymbolInformation);
assert.equal(first.name, 'DocumentSymbol');
assert.equal(first.children.length, 1);
assert.equal(second.name, 'SymbolInformation');
});
});
});
// --- suggest
test('Suggest, back and forth', function () {
disposables.push(extHost.registerCompletionItemProvider(nullExtensionDescription, defaultSelector, <vscode.CompletionItemProvider>{
provideCompletionItems(): any {
let a = new types.CompletionItem('item1');
let b = new types.CompletionItem('item2');
b.textEdit = types.TextEdit.replace(new types.Range(0, 4, 0, 8), 'foo'); // overwite after
let c = new types.CompletionItem('item3');
c.textEdit = types.TextEdit.replace(new types.Range(0, 1, 0, 6), 'foobar'); // overwite before & after
// snippet string!
let d = new types.CompletionItem('item4');
d.range = new types.Range(0, 1, 0, 4);// overwite before
d.insertText = new types.SnippetString('foo$0bar');
return [a, b, c, d];
}
}, []));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', model.uri, new types.Position(0, 4)).then(list => {
assert.ok(list instanceof types.CompletionList);
let values = list.items;
assert.ok(Array.isArray(values));
assert.equal(values.length, 4);
let [first, second, third, fourth] = values;
assert.equal(first.label, 'item1');
assert.equal(first.textEdit, undefined);// no text edit, default ranges
assert.ok(!types.Range.isRange(first.range));
assert.equal(second.label, 'item2');
assert.equal(second.textEdit!.newText, 'foo');
assert.equal(second.textEdit!.range.start.line, 0);
assert.equal(second.textEdit!.range.start.character, 4);
assert.equal(second.textEdit!.range.end.line, 0);
assert.equal(second.textEdit!.range.end.character, 8);
assert.equal(third.label, 'item3');
assert.equal(third.textEdit!.newText, 'foobar');
assert.equal(third.textEdit!.range.start.line, 0);
assert.equal(third.textEdit!.range.start.character, 1);
assert.equal(third.textEdit!.range.end.line, 0);
assert.equal(third.textEdit!.range.end.character, 6);
assert.equal(fourth.label, 'item4');
assert.equal(fourth.textEdit, undefined);
const range: any = fourth.range!;
assert.ok(types.Range.isRange(range));
assert.equal(range.start.line, 0);
assert.equal(range.start.character, 1);
assert.equal(range.end.line, 0);
assert.equal(range.end.character, 4);
assert.ok(fourth.insertText instanceof types.SnippetString);
assert.equal((<types.SnippetString>fourth.insertText).value, 'foo$0bar');
});
});
});
test('Suggest, return CompletionList !array', function () {
disposables.push(extHost.registerCompletionItemProvider(nullExtensionDescription, defaultSelector, <vscode.CompletionItemProvider>{
provideCompletionItems(): any {
let a = new types.CompletionItem('item1');
let b = new types.CompletionItem('item2');
return new types.CompletionList(<any>[a, b], true);
}
}, []));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CompletionList>('vscode.executeCompletionItemProvider', model.uri, new types.Position(0, 4)).then(list => {
assert.ok(list instanceof types.CompletionList);
assert.equal(list.isIncomplete, true);
});
});
});
test('Suggest, resolve completion items', async function () {
let resolveCount = 0;
disposables.push(extHost.registerCompletionItemProvider(nullExtensionDescription, defaultSelector, <vscode.CompletionItemProvider>{
provideCompletionItems(): any {
let a = new types.CompletionItem('item1');
let b = new types.CompletionItem('item2');
let c = new types.CompletionItem('item3');
let d = new types.CompletionItem('item4');
return new types.CompletionList([a, b, c, d], false);
},
resolveCompletionItem(item) {
resolveCount += 1;
return item;
}
}, []));
await rpcProtocol.sync();
let list = await commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
model.uri,
new types.Position(0, 4),
undefined,
2 // maxItemsToResolve
);
assert.ok(list instanceof types.CompletionList);
assert.equal(resolveCount, 2);
});
test('"vscode.executeCompletionItemProvider" doesnot return a preselect field #53749', async function () {
disposables.push(extHost.registerCompletionItemProvider(nullExtensionDescription, defaultSelector, <vscode.CompletionItemProvider>{
provideCompletionItems(): any {
let a = new types.CompletionItem('item1');
a.preselect = true;
let b = new types.CompletionItem('item2');
let c = new types.CompletionItem('item3');
c.preselect = true;
let d = new types.CompletionItem('item4');
return new types.CompletionList([a, b, c, d], false);
}
}, []));
await rpcProtocol.sync();
let list = await commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
model.uri,
new types.Position(0, 4),
undefined
);
assert.ok(list instanceof types.CompletionList);
assert.equal(list.items.length, 4);
let [a, b, c, d] = list.items;
assert.equal(a.preselect, true);
assert.equal(b.preselect, undefined);
assert.equal(c.preselect, true);
assert.equal(d.preselect, undefined);
});
test('executeCompletionItemProvider doesn\'t capture commitCharacters #58228', async function () {
disposables.push(extHost.registerCompletionItemProvider(nullExtensionDescription, defaultSelector, <vscode.CompletionItemProvider>{
provideCompletionItems(): any {
let a = new types.CompletionItem('item1');
a.commitCharacters = ['a', 'b'];
let b = new types.CompletionItem('item2');
return new types.CompletionList([a, b], false);
}
}, []));
await rpcProtocol.sync();
let list = await commands.executeCommand<vscode.CompletionList>(
'vscode.executeCompletionItemProvider',
model.uri,
new types.Position(0, 4),
undefined
);
assert.ok(list instanceof types.CompletionList);
assert.equal(list.items.length, 2);
let [a, b] = list.items;
assert.deepEqual(a.commitCharacters, ['a', 'b']);
assert.equal(b.commitCharacters, undefined);
});
// --- signatureHelp
test('Parameter Hints, back and forth', async () => {
disposables.push(extHost.registerSignatureHelpProvider(nullExtensionDescription, defaultSelector, new class implements vscode.SignatureHelpProvider {
provideSignatureHelp(_document: vscode.TextDocument, _position: vscode.Position, _token: vscode.CancellationToken, context: vscode.SignatureHelpContext): vscode.SignatureHelp {
return {
activeSignature: 0,
activeParameter: 1,
signatures: [
{
label: 'abc',
documentation: `${context.triggerKind === 1 /* vscode.SignatureHelpTriggerKind.Invoke */ ? 'invoked' : 'unknown'} ${context.triggerCharacter}`,
parameters: []
}
]
};
}
}, []));
await rpcProtocol.sync();
const firstValue = await commands.executeCommand<vscode.SignatureHelp>('vscode.executeSignatureHelpProvider', model.uri, new types.Position(0, 1), ',');
assert.strictEqual(firstValue.activeSignature, 0);
assert.strictEqual(firstValue.activeParameter, 1);
assert.strictEqual(firstValue.signatures.length, 1);
assert.strictEqual(firstValue.signatures[0].label, 'abc');
assert.strictEqual(firstValue.signatures[0].documentation, 'invoked ,');
});
// --- quickfix
test('QuickFix, back and forth', function () {
disposables.push(extHost.registerCodeActionProvider(nullExtensionDescription, defaultSelector, {
provideCodeActions(): vscode.Command[] {
return [{ command: 'testing', title: 'Title', arguments: [1, 2, true] }];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.Command[]>('vscode.executeCodeActionProvider', model.uri, new types.Range(0, 0, 1, 1)).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.title, 'Title');
assert.equal(first.command, 'testing');
assert.deepEqual(first.arguments, [1, 2, true]);
});
});
});
test('vscode.executeCodeActionProvider results seem to be missing their `command` property #45124', function () {
disposables.push(extHost.registerCodeActionProvider(nullExtensionDescription, defaultSelector, {
provideCodeActions(document, range): vscode.CodeAction[] {
return [{
command: {
arguments: [document, range],
command: 'command',
title: 'command_title',
},
kind: types.CodeActionKind.Empty.append('foo'),
title: 'title',
}];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CodeAction[]>('vscode.executeCodeActionProvider', model.uri, new types.Range(0, 0, 1, 1)).then(value => {
assert.equal(value.length, 1);
const [first] = value;
assert.ok(first.command);
assert.equal(first.command!.command, 'command');
assert.equal(first.command!.title, 'command_title');
assert.equal(first.kind!.value, 'foo');
assert.equal(first.title, 'title');
});
});
});
test('vscode.executeCodeActionProvider passes Range to provider although Selection is passed in #77997', function () {
disposables.push(extHost.registerCodeActionProvider(nullExtensionDescription, defaultSelector, {
provideCodeActions(document, rangeOrSelection): vscode.CodeAction[] {
return [{
command: {
arguments: [document, rangeOrSelection],
command: 'command',
title: 'command_title',
},
kind: types.CodeActionKind.Empty.append('foo'),
title: 'title',
}];
}
}));
const selection = new types.Selection(0, 0, 1, 1);
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CodeAction[]>('vscode.executeCodeActionProvider', model.uri, selection).then(value => {
assert.equal(value.length, 1);
const [first] = value;
assert.ok(first.command);
assert.ok(first.command!.arguments![1] instanceof types.Selection);
assert.ok(first.command!.arguments![1].isEqual(selection));
});
});
});
test('vscode.executeCodeActionProvider results seem to be missing their `isPreferred` property #78098', function () {
disposables.push(extHost.registerCodeActionProvider(nullExtensionDescription, defaultSelector, {
provideCodeActions(document, rangeOrSelection): vscode.CodeAction[] {
return [{
command: {
arguments: [document, rangeOrSelection],
command: 'command',
title: 'command_title',
},
kind: types.CodeActionKind.Empty.append('foo'),
title: 'title',
isPreferred: true
}];
}
}));
const selection = new types.Selection(0, 0, 1, 1);
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CodeAction[]>('vscode.executeCodeActionProvider', model.uri, selection).then(value => {
assert.equal(value.length, 1);
const [first] = value;
assert.equal(first.isPreferred, true);
});
});
});
// --- code lens
test('CodeLens, back and forth', function () {
const complexArg = {
foo() { },
bar() { },
big: extHost
};
disposables.push(extHost.registerCodeLensProvider(nullExtensionDescription, defaultSelector, <vscode.CodeLensProvider>{
provideCodeLenses(): any {
return [new types.CodeLens(new types.Range(0, 0, 1, 1), { title: 'Title', command: 'cmd', arguments: [1, true, complexArg] })];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.CodeLens[]>('vscode.executeCodeLensProvider', model.uri).then(value => {
assert.equal(value.length, 1);
const [first] = value;
assert.equal(first.command!.title, 'Title');
assert.equal(first.command!.command, 'cmd');
assert.equal(first.command!.arguments![0], 1);
assert.equal(first.command!.arguments![1], true);
assert.equal(first.command!.arguments![2], complexArg);
});
});
});
test('CodeLens, resolve', async function () {
let resolveCount = 0;
disposables.push(extHost.registerCodeLensProvider(nullExtensionDescription, defaultSelector, <vscode.CodeLensProvider>{
provideCodeLenses(): any {
return [
new types.CodeLens(new types.Range(0, 0, 1, 1)),
new types.CodeLens(new types.Range(0, 0, 1, 1)),
new types.CodeLens(new types.Range(0, 0, 1, 1)),
new types.CodeLens(new types.Range(0, 0, 1, 1), { title: 'Already resolved', command: 'fff' })
];
},
resolveCodeLens(codeLens: types.CodeLens) {
codeLens.command = { title: resolveCount.toString(), command: 'resolved' };
resolveCount += 1;
return codeLens;
}
}));
await rpcProtocol.sync();
let value = await commands.executeCommand<vscode.CodeLens[]>('vscode.executeCodeLensProvider', model.uri, 2);
assert.equal(value.length, 3); // the resolve argument defines the number of results being returned
assert.equal(resolveCount, 2);
resolveCount = 0;
value = await commands.executeCommand<vscode.CodeLens[]>('vscode.executeCodeLensProvider', model.uri);
assert.equal(value.length, 4);
assert.equal(resolveCount, 0);
});
test('Links, back and forth', function () {
disposables.push(extHost.registerDocumentLinkProvider(nullExtensionDescription, defaultSelector, <vscode.DocumentLinkProvider>{
provideDocumentLinks(): any {
return [new types.DocumentLink(new types.Range(0, 0, 0, 20), URI.parse('foo:bar'))];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.DocumentLink[]>('vscode.executeLinkProvider', model.uri).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.target + '', 'foo:bar');
assert.equal(first.range.start.line, 0);
assert.equal(first.range.start.character, 0);
assert.equal(first.range.end.line, 0);
assert.equal(first.range.end.character, 20);
});
});
});
test('Color provider', function () {
disposables.push(extHost.registerColorProvider(nullExtensionDescription, defaultSelector, <vscode.DocumentColorProvider>{
provideDocumentColors(): vscode.ColorInformation[] {
return [new types.ColorInformation(new types.Range(0, 0, 0, 20), new types.Color(0.1, 0.2, 0.3, 0.4))];
},
provideColorPresentations(): vscode.ColorPresentation[] {
const cp = new types.ColorPresentation('#ABC');
cp.textEdit = types.TextEdit.replace(new types.Range(1, 0, 1, 20), '#ABC');
cp.additionalTextEdits = [types.TextEdit.insert(new types.Position(2, 20), '*')];
return [cp];
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.ColorInformation[]>('vscode.executeDocumentColorProvider', model.uri).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.color.red, 0.1);
assert.equal(first.color.green, 0.2);
assert.equal(first.color.blue, 0.3);
assert.equal(first.color.alpha, 0.4);
assert.equal(first.range.start.line, 0);
assert.equal(first.range.start.character, 0);
assert.equal(first.range.end.line, 0);
assert.equal(first.range.end.character, 20);
});
}).then(() => {
const color = new types.Color(0.5, 0.6, 0.7, 0.8);
const range = new types.Range(0, 0, 0, 20);
return commands.executeCommand<vscode.ColorPresentation[]>('vscode.executeColorPresentationProvider', color, { uri: model.uri, range }).then(value => {
assert.equal(value.length, 1);
let [first] = value;
assert.equal(first.label, '#ABC');
assert.equal(first.textEdit!.newText, '#ABC');
assert.equal(first.textEdit!.range.start.line, 1);
assert.equal(first.textEdit!.range.start.character, 0);
assert.equal(first.textEdit!.range.end.line, 1);
assert.equal(first.textEdit!.range.end.character, 20);
assert.equal(first.additionalTextEdits!.length, 1);
assert.equal(first.additionalTextEdits![0].range.start.line, 2);
assert.equal(first.additionalTextEdits![0].range.start.character, 20);
assert.equal(first.additionalTextEdits![0].range.end.line, 2);
assert.equal(first.additionalTextEdits![0].range.end.character, 20);
});
});
});
test('"TypeError: e.onCancellationRequested is not a function" calling hover provider in Insiders #54174', function () {
disposables.push(extHost.registerHoverProvider(nullExtensionDescription, defaultSelector, <vscode.HoverProvider>{
provideHover(): any {
return new types.Hover('fofofofo');
}
}));
return rpcProtocol.sync().then(() => {
return commands.executeCommand<vscode.Hover[]>('vscode.executeHoverProvider', model.uri, new types.Position(1, 1)).then(value => {
assert.equal(value.length, 1);
assert.equal(value[0].contents.length, 1);
});
});
});
// --- selection ranges
test('Selection Range, back and forth', async function () {
disposables.push(extHost.registerSelectionRangeProvider(nullExtensionDescription, defaultSelector, <vscode.SelectionRangeProvider>{
provideSelectionRanges() {
return [
new types.SelectionRange(new types.Range(0, 10, 0, 18), new types.SelectionRange(new types.Range(0, 2, 0, 20))),
];
}
}));
await rpcProtocol.sync();
let value = await commands.executeCommand<vscode.SelectionRange[]>('vscode.executeSelectionRangeProvider', model.uri, [new types.Position(0, 10)]);
assert.equal(value.length, 1);
assert.ok(value[0].parent);
});
// --- call hierarcht
test('CallHierarchy, back and forth', async function () {
disposables.push(extHost.registerCallHierarchyProvider(nullExtensionDescription, defaultSelector, new class implements vscode.CallHierarchyProvider {
prepareCallHierarchy(document: vscode.TextDocument, position: vscode.Position, ): vscode.ProviderResult<vscode.CallHierarchyItem> {
return new types.CallHierarchyItem(types.SymbolKind.Constant, 'ROOT', 'ROOT', document.uri, new types.Range(0, 0, 0, 0), new types.Range(0, 0, 0, 0));
}
provideCallHierarchyIncomingCalls(item: vscode.CallHierarchyItem, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CallHierarchyIncomingCall[]> {
return [new types.CallHierarchyIncomingCall(
new types.CallHierarchyItem(types.SymbolKind.Constant, 'INCOMING', 'INCOMING', item.uri, new types.Range(0, 0, 0, 0), new types.Range(0, 0, 0, 0)),
[new types.Range(0, 0, 0, 0)]
)];
}
provideCallHierarchyOutgoingCalls(item: vscode.CallHierarchyItem, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CallHierarchyOutgoingCall[]> {
return [new types.CallHierarchyOutgoingCall(
new types.CallHierarchyItem(types.SymbolKind.Constant, 'OUTGOING', 'OUTGOING', item.uri, new types.Range(0, 0, 0, 0), new types.Range(0, 0, 0, 0)),
[new types.Range(0, 0, 0, 0)]
)];
}
}));
await rpcProtocol.sync();
const root = await commands.executeCommand<vscode.CallHierarchyItem[]>('vscode.prepareCallHierarchy', model.uri, new types.Position(0, 0));
assert.ok(Array.isArray(root));
assert.equal(root.length, 1);
assert.equal(root[0].name, 'ROOT');
const incoming = await commands.executeCommand<vscode.CallHierarchyIncomingCall[]>('vscode.provideIncomingCalls', root[0]);
assert.equal(incoming.length, 1);
assert.equal(incoming[0].from.name, 'INCOMING');
const outgoing = await commands.executeCommand<vscode.CallHierarchyOutgoingCall[]>('vscode.provideOutgoingCalls', root[0]);
assert.equal(outgoing.length, 1);
assert.equal(outgoing[0].to.name, 'OUTGOING');
});
});

View File

@@ -1,93 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { MainThreadCommandsShape } from 'vs/workbench/api/common/extHost.protocol';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostCommands', function () {
test('dispose calls unregister', function () {
let lastUnregister: string;
const shape = new class extends mock<MainThreadCommandsShape>() {
$registerCommand(id: string): void {
//
}
$unregisterCommand(id: string): void {
lastUnregister = id;
}
};
const commands = new ExtHostCommands(
SingleProxyRPCProtocol(shape),
new NullLogService()
);
commands.registerCommand(true, 'foo', (): any => { }).dispose();
assert.equal(lastUnregister!, 'foo');
assert.equal(CommandsRegistry.getCommand('foo'), undefined);
});
test('dispose bubbles only once', function () {
let unregisterCounter = 0;
const shape = new class extends mock<MainThreadCommandsShape>() {
$registerCommand(id: string): void {
//
}
$unregisterCommand(id: string): void {
unregisterCounter += 1;
}
};
const commands = new ExtHostCommands(
SingleProxyRPCProtocol(shape),
new NullLogService()
);
const reg = commands.registerCommand(true, 'foo', (): any => { });
reg.dispose();
reg.dispose();
reg.dispose();
assert.equal(unregisterCounter, 1);
});
test('execute with retry', async function () {
let count = 0;
const shape = new class extends mock<MainThreadCommandsShape>() {
$registerCommand(id: string): void {
//
}
async $executeCommand<T>(id: string, args: any[], retry: boolean): Promise<T | undefined> {
count++;
assert.equal(retry, count === 1);
if (count === 1) {
assert.equal(retry, true);
throw new Error('$executeCommand:retry');
} else {
assert.equal(retry, false);
return <any>17;
}
}
};
const commands = new ExtHostCommands(
SingleProxyRPCProtocol(shape),
new NullLogService()
);
const result = await commands.executeCommand('fooo', [this, true]);
assert.equal(result, 17);
assert.equal(count, 2);
});
});

View File

@@ -1,657 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI, UriComponents } from 'vs/base/common/uri';
import { ExtHostWorkspace } from 'vs/workbench/api/common/extHostWorkspace';
import { ExtHostConfigProvider } from 'vs/workbench/api/common/extHostConfiguration';
import { MainThreadConfigurationShape, IConfigurationInitData } from 'vs/workbench/api/common/extHost.protocol';
import { ConfigurationModel } from 'vs/platform/configuration/common/configurationModels';
import { TestRPCProtocol } from './testRPCProtocol';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { IWorkspaceFolder, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { ConfigurationTarget, IConfigurationModel, IConfigurationChange } from 'vs/platform/configuration/common/configuration';
import { NullLogService } from 'vs/platform/log/common/log';
import { assign } from 'vs/base/common/objects';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
suite('ExtHostConfiguration', function () {
class RecordingShape extends mock<MainThreadConfigurationShape>() {
lastArgs!: [ConfigurationTarget, string, any];
$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): Promise<void> {
this.lastArgs = [target, key, value];
return Promise.resolve(undefined);
}
}
function createExtHostWorkspace(): ExtHostWorkspace {
return new ExtHostWorkspace(new TestRPCProtocol(), new class extends mock<IExtHostInitDataService>() { }, new NullLogService());
}
function createExtHostConfiguration(contents: any = Object.create(null), shape?: MainThreadConfigurationShape) {
if (!shape) {
shape = new class extends mock<MainThreadConfigurationShape>() { };
}
return new ExtHostConfigProvider(shape, createExtHostWorkspace(), createConfigurationData(contents), new NullLogService());
}
function createConfigurationData(contents: any): IConfigurationInitData {
return {
defaults: new ConfigurationModel(contents),
user: new ConfigurationModel(contents),
workspace: new ConfigurationModel(),
folders: [],
configurationScopes: []
};
}
test('getConfiguration fails regression test 1.7.1 -> 1.8 #15552', function () {
const extHostConfig = createExtHostConfiguration({
'search': {
'exclude': {
'**/node_modules': true
}
}
});
assert.equal(extHostConfig.getConfiguration('search.exclude')['**/node_modules'], true);
assert.equal(extHostConfig.getConfiguration('search.exclude').get('**/node_modules'), true);
assert.equal(extHostConfig.getConfiguration('search').get<any>('exclude')['**/node_modules'], true);
assert.equal(extHostConfig.getConfiguration('search.exclude').has('**/node_modules'), true);
assert.equal(extHostConfig.getConfiguration('search').has('exclude.**/node_modules'), true);
});
test('has/get', () => {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
}
});
const config = all.getConfiguration('farboo');
assert.ok(config.has('config0'));
assert.equal(config.get('config0'), true);
assert.equal(config.get('config4'), '');
assert.equal(config['config0'], true);
assert.equal(config['config4'], '');
assert.ok(config.has('nested.config1'));
assert.equal(config.get('nested.config1'), 42);
assert.ok(config.has('nested.config2'));
assert.equal(config.get('nested.config2'), 'Das Pferd frisst kein Reis.');
assert.ok(config.has('nested'));
assert.deepEqual(config.get('nested'), { config1: 42, config2: 'Das Pferd frisst kein Reis.' });
});
test('can modify the returned configuration', function () {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
},
'workbench': {
'colorCustomizations': {
'statusBar.foreground': 'somevalue'
}
}
});
let testObject = all.getConfiguration();
let actual = testObject.get<any>('farboo')!;
actual['nested']['config1'] = 41;
assert.equal(41, actual['nested']['config1']);
actual['farboo1'] = 'newValue';
assert.equal('newValue', actual['farboo1']);
testObject = all.getConfiguration();
actual = testObject.get('farboo')!;
assert.equal(actual['nested']['config1'], 42);
assert.equal(actual['farboo1'], undefined);
testObject = all.getConfiguration();
actual = testObject.get('farboo')!;
assert.equal(actual['config0'], true);
actual['config0'] = false;
assert.equal(actual['config0'], false);
testObject = all.getConfiguration();
actual = testObject.get('farboo')!;
assert.equal(actual['config0'], true);
testObject = all.getConfiguration();
actual = testObject.inspect('farboo')!;
actual['value'] = 'effectiveValue';
assert.equal('effectiveValue', actual['value']);
testObject = all.getConfiguration('workbench');
actual = testObject.get('colorCustomizations')!;
actual['statusBar.foreground'] = undefined;
assert.equal(actual['statusBar.foreground'], undefined);
testObject = all.getConfiguration('workbench');
actual = testObject.get('colorCustomizations')!;
assert.equal(actual['statusBar.foreground'], 'somevalue');
});
test('Stringify returned configuration', function () {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
},
'workbench': {
'colorCustomizations': {
'statusBar.foreground': 'somevalue'
},
'emptyobjectkey': {
}
}
});
const testObject = all.getConfiguration();
let actual: any = testObject.get('farboo');
assert.deepEqual(JSON.stringify({
'config0': true,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
}), JSON.stringify(actual));
assert.deepEqual(undefined, JSON.stringify(testObject.get('unknownkey')));
actual = testObject.get('farboo')!;
actual['config0'] = false;
assert.deepEqual(JSON.stringify({
'config0': false,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
}), JSON.stringify(actual));
actual = testObject.get<any>('workbench')!['colorCustomizations']!;
actual['statusBar.background'] = 'anothervalue';
assert.deepEqual(JSON.stringify({
'statusBar.foreground': 'somevalue',
'statusBar.background': 'anothervalue'
}), JSON.stringify(actual));
actual = testObject.get('workbench');
actual['unknownkey'] = 'somevalue';
assert.deepEqual(JSON.stringify({
'colorCustomizations': {
'statusBar.foreground': 'somevalue'
},
'emptyobjectkey': {},
'unknownkey': 'somevalue'
}), JSON.stringify(actual));
actual = all.getConfiguration('workbench').get('emptyobjectkey');
actual = assign(actual || {}, {
'statusBar.background': `#0ff`,
'statusBar.foreground': `#ff0`,
});
assert.deepEqual(JSON.stringify({
'statusBar.background': `#0ff`,
'statusBar.foreground': `#ff0`,
}), JSON.stringify(actual));
actual = all.getConfiguration('workbench').get('unknownkey');
actual = assign(actual || {}, {
'statusBar.background': `#0ff`,
'statusBar.foreground': `#ff0`,
});
assert.deepEqual(JSON.stringify({
'statusBar.background': `#0ff`,
'statusBar.foreground': `#ff0`,
}), JSON.stringify(actual));
});
test('cannot modify returned configuration', function () {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'nested': {
'config1': 42,
'config2': 'Das Pferd frisst kein Reis.'
},
'config4': ''
}
});
let testObject: any = all.getConfiguration();
try {
testObject['get'] = null;
assert.fail('This should be readonly');
} catch (e) {
}
try {
testObject['farboo']['config0'] = false;
assert.fail('This should be readonly');
} catch (e) {
}
try {
testObject['farboo']['farboo1'] = 'hello';
assert.fail('This should be readonly');
} catch (e) {
}
});
test('inspect in no workspace context', function () {
const testObject = new ExtHostConfigProvider(
new class extends mock<MainThreadConfigurationShape>() { },
createExtHostWorkspace(),
{
defaults: new ConfigurationModel({
'editor': {
'wordWrap': 'off'
}
}, ['editor.wordWrap']),
user: new ConfigurationModel({
'editor': {
'wordWrap': 'on'
}
}, ['editor.wordWrap']),
workspace: new ConfigurationModel({}, []),
folders: [],
configurationScopes: []
},
new NullLogService()
);
let actual = testObject.getConfiguration().inspect('editor.wordWrap')!;
assert.equal(actual.defaultValue, 'off');
assert.equal(actual.globalValue, 'on');
assert.equal(actual.workspaceValue, undefined);
assert.equal(actual.workspaceFolderValue, undefined);
actual = testObject.getConfiguration('editor').inspect('wordWrap')!;
assert.equal(actual.defaultValue, 'off');
assert.equal(actual.globalValue, 'on');
assert.equal(actual.workspaceValue, undefined);
assert.equal(actual.workspaceFolderValue, undefined);
});
test('inspect in single root context', function () {
const workspaceUri = URI.file('foo');
const folders: [UriComponents, IConfigurationModel][] = [];
const workspace = new ConfigurationModel({
'editor': {
'wordWrap': 'bounded'
}
}, ['editor.wordWrap']);
folders.push([workspaceUri, workspace]);
const extHostWorkspace = createExtHostWorkspace();
extHostWorkspace.$initializeWorkspace({
'id': 'foo',
'folders': [aWorkspaceFolder(URI.file('foo'), 0)],
'name': 'foo'
});
const testObject = new ExtHostConfigProvider(
new class extends mock<MainThreadConfigurationShape>() { },
extHostWorkspace,
{
defaults: new ConfigurationModel({
'editor': {
'wordWrap': 'off'
}
}, ['editor.wordWrap']),
user: new ConfigurationModel({
'editor': {
'wordWrap': 'on'
}
}, ['editor.wordWrap']),
workspace,
folders,
configurationScopes: []
},
new NullLogService()
);
let actual1 = testObject.getConfiguration().inspect('editor.wordWrap')!;
assert.equal(actual1.defaultValue, 'off');
assert.equal(actual1.globalValue, 'on');
assert.equal(actual1.workspaceValue, 'bounded');
assert.equal(actual1.workspaceFolderValue, undefined);
actual1 = testObject.getConfiguration('editor').inspect('wordWrap')!;
assert.equal(actual1.defaultValue, 'off');
assert.equal(actual1.globalValue, 'on');
assert.equal(actual1.workspaceValue, 'bounded');
assert.equal(actual1.workspaceFolderValue, undefined);
let actual2 = testObject.getConfiguration(undefined, workspaceUri).inspect('editor.wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'bounded');
actual2 = testObject.getConfiguration('editor', workspaceUri).inspect('wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'bounded');
});
test('inspect in multi root context', function () {
const workspace = new ConfigurationModel({
'editor': {
'wordWrap': 'bounded'
}
}, ['editor.wordWrap']);
const firstRoot = URI.file('foo1');
const secondRoot = URI.file('foo2');
const thirdRoot = URI.file('foo3');
const folders: [UriComponents, IConfigurationModel][] = [];
folders.push([firstRoot, new ConfigurationModel({
'editor': {
'wordWrap': 'off',
'lineNumbers': 'relative'
}
}, ['editor.wordWrap'])]);
folders.push([secondRoot, new ConfigurationModel({
'editor': {
'wordWrap': 'on'
}
}, ['editor.wordWrap'])]);
folders.push([thirdRoot, new ConfigurationModel({}, [])]);
const extHostWorkspace = createExtHostWorkspace();
extHostWorkspace.$initializeWorkspace({
'id': 'foo',
'folders': [aWorkspaceFolder(firstRoot, 0), aWorkspaceFolder(secondRoot, 1)],
'name': 'foo'
});
const testObject = new ExtHostConfigProvider(
new class extends mock<MainThreadConfigurationShape>() { },
extHostWorkspace,
{
defaults: new ConfigurationModel({
'editor': {
'wordWrap': 'off',
'lineNumbers': 'on'
}
}, ['editor.wordWrap']),
user: new ConfigurationModel({
'editor': {
'wordWrap': 'on'
}
}, ['editor.wordWrap']),
workspace,
folders,
configurationScopes: []
},
new NullLogService()
);
let actual1 = testObject.getConfiguration().inspect('editor.wordWrap')!;
assert.equal(actual1.defaultValue, 'off');
assert.equal(actual1.globalValue, 'on');
assert.equal(actual1.workspaceValue, 'bounded');
assert.equal(actual1.workspaceFolderValue, undefined);
actual1 = testObject.getConfiguration('editor').inspect('wordWrap')!;
assert.equal(actual1.defaultValue, 'off');
assert.equal(actual1.globalValue, 'on');
assert.equal(actual1.workspaceValue, 'bounded');
assert.equal(actual1.workspaceFolderValue, undefined);
actual1 = testObject.getConfiguration('editor').inspect('lineNumbers')!;
assert.equal(actual1.defaultValue, 'on');
assert.equal(actual1.globalValue, undefined);
assert.equal(actual1.workspaceValue, undefined);
assert.equal(actual1.workspaceFolderValue, undefined);
let actual2 = testObject.getConfiguration(undefined, firstRoot).inspect('editor.wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'off');
actual2 = testObject.getConfiguration('editor', firstRoot).inspect('wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'off');
actual2 = testObject.getConfiguration('editor', firstRoot).inspect('lineNumbers')!;
assert.equal(actual2.defaultValue, 'on');
assert.equal(actual2.globalValue, undefined);
assert.equal(actual2.workspaceValue, undefined);
assert.equal(actual2.workspaceFolderValue, 'relative');
actual2 = testObject.getConfiguration(undefined, secondRoot).inspect('editor.wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'on');
actual2 = testObject.getConfiguration('editor', secondRoot).inspect('wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.equal(actual2.workspaceFolderValue, 'on');
actual2 = testObject.getConfiguration(undefined, thirdRoot).inspect('editor.wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.ok(Object.keys(actual2).indexOf('workspaceFolderValue') !== -1);
assert.equal(actual2.workspaceFolderValue, undefined);
actual2 = testObject.getConfiguration('editor', thirdRoot).inspect('wordWrap')!;
assert.equal(actual2.defaultValue, 'off');
assert.equal(actual2.globalValue, 'on');
assert.equal(actual2.workspaceValue, 'bounded');
assert.ok(Object.keys(actual2).indexOf('workspaceFolderValue') !== -1);
assert.equal(actual2.workspaceFolderValue, undefined);
});
test('getConfiguration vs get', function () {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'config4': 38
}
});
let config = all.getConfiguration('farboo.config0');
assert.equal(config.get(''), undefined);
assert.equal(config.has(''), false);
config = all.getConfiguration('farboo');
assert.equal(config.get('config0'), true);
assert.equal(config.has('config0'), true);
});
test('getConfiguration vs get', function () {
const all = createExtHostConfiguration({
'farboo': {
'config0': true,
'config4': 38
}
});
let config = all.getConfiguration('farboo.config0');
assert.equal(config.get(''), undefined);
assert.equal(config.has(''), false);
config = all.getConfiguration('farboo');
assert.equal(config.get('config0'), true);
assert.equal(config.has('config0'), true);
});
test('name vs property', function () {
const all = createExtHostConfiguration({
'farboo': {
'get': 'get-prop'
}
});
const config = all.getConfiguration('farboo');
assert.ok(config.has('get'));
assert.equal(config.get('get'), 'get-prop');
assert.deepEqual(config['get'], config.get);
assert.throws(() => config['get'] = <any>'get-prop');
});
test('update: no target passes null', function () {
const shape = new RecordingShape();
const allConfig = createExtHostConfiguration({
'foo': {
'bar': 1,
'far': 1
}
}, shape);
let config = allConfig.getConfiguration('foo');
config.update('bar', 42);
assert.equal(shape.lastArgs[0], null);
});
test('update/section to key', function () {
const shape = new RecordingShape();
const allConfig = createExtHostConfiguration({
'foo': {
'bar': 1,
'far': 1
}
}, shape);
let config = allConfig.getConfiguration('foo');
config.update('bar', 42, true);
assert.equal(shape.lastArgs[0], ConfigurationTarget.USER);
assert.equal(shape.lastArgs[1], 'foo.bar');
assert.equal(shape.lastArgs[2], 42);
config = allConfig.getConfiguration('');
config.update('bar', 42, true);
assert.equal(shape.lastArgs[1], 'bar');
config.update('foo.bar', 42, true);
assert.equal(shape.lastArgs[1], 'foo.bar');
});
test('update, what is #15834', function () {
const shape = new RecordingShape();
const allConfig = createExtHostConfiguration({
'editor': {
'formatOnSave': true
}
}, shape);
allConfig.getConfiguration('editor').update('formatOnSave', { extensions: ['ts'] });
assert.equal(shape.lastArgs[1], 'editor.formatOnSave');
assert.deepEqual(shape.lastArgs[2], { extensions: ['ts'] });
});
test('update/error-state not OK', function () {
const shape = new class extends mock<MainThreadConfigurationShape>() {
$updateConfigurationOption(target: ConfigurationTarget, key: string, value: any): Promise<any> {
return Promise.reject(new Error('Unknown Key')); // something !== OK
}
};
return createExtHostConfiguration({}, shape)
.getConfiguration('')
.update('', true, false)
.then(() => assert.ok(false), err => { /* expecting rejection */ });
});
test('configuration change event', (done) => {
const workspaceFolder = aWorkspaceFolder(URI.file('folder1'), 0);
const extHostWorkspace = createExtHostWorkspace();
extHostWorkspace.$initializeWorkspace({
'id': 'foo',
'folders': [workspaceFolder],
'name': 'foo'
});
const testObject = new ExtHostConfigProvider(
new class extends mock<MainThreadConfigurationShape>() { },
extHostWorkspace,
createConfigurationData({
'farboo': {
'config': false,
'updatedConfig': false
}
}),
new NullLogService()
);
const newConfigData = createConfigurationData({
'farboo': {
'config': false,
'updatedConfig': true,
'newConfig': true,
}
});
const configEventData: IConfigurationChange = { keys: ['farboo.updatedConfig', 'farboo.newConfig'], overrides: [] };
testObject.onDidChangeConfiguration(e => {
assert.deepEqual(testObject.getConfiguration().get('farboo'), {
'config': false,
'updatedConfig': true,
'newConfig': true,
});
assert.ok(e.affectsConfiguration('farboo'));
assert.ok(e.affectsConfiguration('farboo', workspaceFolder.uri));
assert.ok(e.affectsConfiguration('farboo', URI.file('any')));
assert.ok(e.affectsConfiguration('farboo.updatedConfig'));
assert.ok(e.affectsConfiguration('farboo.updatedConfig', workspaceFolder.uri));
assert.ok(e.affectsConfiguration('farboo.updatedConfig', URI.file('any')));
assert.ok(e.affectsConfiguration('farboo.newConfig'));
assert.ok(e.affectsConfiguration('farboo.newConfig', workspaceFolder.uri));
assert.ok(e.affectsConfiguration('farboo.newConfig', URI.file('any')));
assert.ok(!e.affectsConfiguration('farboo.config'));
assert.ok(!e.affectsConfiguration('farboo.config', workspaceFolder.uri));
assert.ok(!e.affectsConfiguration('farboo.config', URI.file('any')));
done();
});
testObject.$acceptConfigurationChanged(newConfigData, configEventData);
});
function aWorkspaceFolder(uri: URI, index: number, name: string = ''): IWorkspaceFolder {
return new WorkspaceFolder({ uri, name, index });
}
});

View File

@@ -1,463 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI, UriComponents } from 'vs/base/common/uri';
import { DiagnosticCollection, ExtHostDiagnostics } from 'vs/workbench/api/common/extHostDiagnostics';
import { Diagnostic, DiagnosticSeverity, Range, DiagnosticRelatedInformation, Location } from 'vs/workbench/api/common/extHostTypes';
import { MainThreadDiagnosticsShape, IMainContext } from 'vs/workbench/api/common/extHost.protocol';
import { IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { Emitter, Event } from 'vs/base/common/event';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostDiagnostics', () => {
class DiagnosticsShape extends mock<MainThreadDiagnosticsShape>() {
$changeMany(owner: string, entries: [UriComponents, IMarkerData[]][]): void {
//
}
$clear(owner: string): void {
//
}
}
test('disposeCheck', () => {
const collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
collection.dispose();
collection.dispose(); // that's OK
assert.throws(() => collection.name);
assert.throws(() => collection.clear());
assert.throws(() => collection.delete(URI.parse('aa:bb')));
assert.throws(() => collection.forEach(() => { }));
assert.throws(() => collection.get(URI.parse('aa:bb')));
assert.throws(() => collection.has(URI.parse('aa:bb')));
assert.throws(() => collection.set(URI.parse('aa:bb'), []));
assert.throws(() => collection.set(URI.parse('aa:bb'), undefined!));
});
test('diagnostic collection, forEach, clear, has', function () {
let collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
assert.equal(collection.name, 'test');
collection.dispose();
assert.throws(() => collection.name);
let c = 0;
collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
collection.forEach(() => c++);
assert.equal(c, 0);
collection.set(URI.parse('foo:bar'), [
new Diagnostic(new Range(0, 0, 1, 1), 'message-1'),
new Diagnostic(new Range(0, 0, 1, 1), 'message-2')
]);
collection.forEach(() => c++);
assert.equal(c, 1);
c = 0;
collection.clear();
collection.forEach(() => c++);
assert.equal(c, 0);
collection.set(URI.parse('foo:bar1'), [
new Diagnostic(new Range(0, 0, 1, 1), 'message-1'),
new Diagnostic(new Range(0, 0, 1, 1), 'message-2')
]);
collection.set(URI.parse('foo:bar2'), [
new Diagnostic(new Range(0, 0, 1, 1), 'message-1'),
new Diagnostic(new Range(0, 0, 1, 1), 'message-2')
]);
collection.forEach(() => c++);
assert.equal(c, 2);
assert.ok(collection.has(URI.parse('foo:bar1')));
assert.ok(collection.has(URI.parse('foo:bar2')));
assert.ok(!collection.has(URI.parse('foo:bar3')));
collection.delete(URI.parse('foo:bar1'));
assert.ok(!collection.has(URI.parse('foo:bar1')));
collection.dispose();
});
test('diagnostic collection, immutable read', function () {
let collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
collection.set(URI.parse('foo:bar'), [
new Diagnostic(new Range(0, 0, 1, 1), 'message-1'),
new Diagnostic(new Range(0, 0, 1, 1), 'message-2')
]);
let array = collection.get(URI.parse('foo:bar')) as Diagnostic[];
assert.throws(() => array.length = 0);
assert.throws(() => array.pop());
assert.throws(() => array[0] = new Diagnostic(new Range(0, 0, 0, 0), 'evil'));
collection.forEach((uri, array: readonly Diagnostic[]) => {
assert.throws(() => (array as Diagnostic[]).length = 0);
assert.throws(() => (array as Diagnostic[]).pop());
assert.throws(() => (array as Diagnostic[])[0] = new Diagnostic(new Range(0, 0, 0, 0), 'evil'));
});
array = collection.get(URI.parse('foo:bar')) as Diagnostic[];
assert.equal(array.length, 2);
collection.dispose();
});
test('diagnostics collection, set with dupliclated tuples', function () {
let collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
let uri = URI.parse('sc:hightower');
collection.set([
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-1')]],
[URI.parse('some:thing'), [new Diagnostic(new Range(0, 0, 1, 1), 'something')]],
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-2')]],
]);
let array = collection.get(uri);
assert.equal(array.length, 2);
let [first, second] = array;
assert.equal(first.message, 'message-1');
assert.equal(second.message, 'message-2');
// clear
collection.delete(uri);
assert.ok(!collection.has(uri));
// bad tuple clears 1/2
collection.set([
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-1')]],
[URI.parse('some:thing'), [new Diagnostic(new Range(0, 0, 1, 1), 'something')]],
[uri, undefined!]
]);
assert.ok(!collection.has(uri));
// clear
collection.delete(uri);
assert.ok(!collection.has(uri));
// bad tuple clears 2/2
collection.set([
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-1')]],
[URI.parse('some:thing'), [new Diagnostic(new Range(0, 0, 1, 1), 'something')]],
[uri, undefined!],
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-2')]],
[uri, [new Diagnostic(new Range(0, 0, 0, 1), 'message-3')]],
]);
array = collection.get(uri);
assert.equal(array.length, 2);
[first, second] = array;
assert.equal(first.message, 'message-2');
assert.equal(second.message, 'message-3');
collection.dispose();
});
test('diagnostics collection, set tuple overrides, #11547', function () {
let lastEntries!: [UriComponents, IMarkerData[]][];
let collection = new DiagnosticCollection('test', 'test', 100, new class extends DiagnosticsShape {
$changeMany(owner: string, entries: [UriComponents, IMarkerData[]][]): void {
lastEntries = entries;
return super.$changeMany(owner, entries);
}
}, new Emitter());
let uri = URI.parse('sc:hightower');
collection.set([[uri, [new Diagnostic(new Range(0, 0, 1, 1), 'error')]]]);
assert.equal(collection.get(uri).length, 1);
assert.equal(collection.get(uri)[0].message, 'error');
assert.equal(lastEntries.length, 1);
let [[, data1]] = lastEntries;
assert.equal(data1.length, 1);
assert.equal(data1[0].message, 'error');
lastEntries = undefined!;
collection.set([[uri, [new Diagnostic(new Range(0, 0, 1, 1), 'warning')]]]);
assert.equal(collection.get(uri).length, 1);
assert.equal(collection.get(uri)[0].message, 'warning');
assert.equal(lastEntries.length, 1);
let [[, data2]] = lastEntries;
assert.equal(data2.length, 1);
assert.equal(data2[0].message, 'warning');
lastEntries = undefined!;
});
test('do send message when not making a change', function () {
let changeCount = 0;
let eventCount = 0;
const emitter = new Emitter<any>();
emitter.event(_ => eventCount += 1);
const collection = new DiagnosticCollection('test', 'test', 100, new class extends DiagnosticsShape {
$changeMany() {
changeCount += 1;
}
}, emitter);
let uri = URI.parse('sc:hightower');
let diag = new Diagnostic(new Range(0, 0, 0, 1), 'ffff');
collection.set(uri, [diag]);
assert.equal(changeCount, 1);
assert.equal(eventCount, 1);
collection.set(uri, [diag]);
assert.equal(changeCount, 2);
assert.equal(eventCount, 2);
});
test('diagnostics collection, tuples and undefined (small array), #15585', function () {
const collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
let uri = URI.parse('sc:hightower');
let uri2 = URI.parse('sc:nomad');
let diag = new Diagnostic(new Range(0, 0, 0, 1), 'ffff');
collection.set([
[uri, [diag, diag, diag]],
[uri, undefined!],
[uri, [diag]],
[uri2, [diag, diag]],
[uri2, undefined!],
[uri2, [diag]],
]);
assert.equal(collection.get(uri).length, 1);
assert.equal(collection.get(uri2).length, 1);
});
test('diagnostics collection, tuples and undefined (large array), #15585', function () {
const collection = new DiagnosticCollection('test', 'test', 100, new DiagnosticsShape(), new Emitter());
const tuples: [URI, Diagnostic[]][] = [];
for (let i = 0; i < 500; i++) {
let uri = URI.parse('sc:hightower#' + i);
let diag = new Diagnostic(new Range(0, 0, 0, 1), i.toString());
tuples.push([uri, [diag, diag, diag]]);
tuples.push([uri, undefined!]);
tuples.push([uri, [diag]]);
}
collection.set(tuples);
for (let i = 0; i < 500; i++) {
let uri = URI.parse('sc:hightower#' + i);
assert.equal(collection.has(uri), true);
assert.equal(collection.get(uri).length, 1);
}
});
test('diagnostic capping', function () {
let lastEntries!: [UriComponents, IMarkerData[]][];
let collection = new DiagnosticCollection('test', 'test', 250, new class extends DiagnosticsShape {
$changeMany(owner: string, entries: [UriComponents, IMarkerData[]][]): void {
lastEntries = entries;
return super.$changeMany(owner, entries);
}
}, new Emitter());
let uri = URI.parse('aa:bb');
let diagnostics: Diagnostic[] = [];
for (let i = 0; i < 500; i++) {
diagnostics.push(new Diagnostic(new Range(i, 0, i + 1, 0), `error#${i}`, i < 300
? DiagnosticSeverity.Warning
: DiagnosticSeverity.Error));
}
collection.set(uri, diagnostics);
assert.equal(collection.get(uri).length, 500);
assert.equal(lastEntries.length, 1);
assert.equal(lastEntries[0][1].length, 251);
assert.equal(lastEntries[0][1][0].severity, MarkerSeverity.Error);
assert.equal(lastEntries[0][1][200].severity, MarkerSeverity.Warning);
assert.equal(lastEntries[0][1][250].severity, MarkerSeverity.Info);
});
test('diagnostic eventing', async function () {
let emitter = new Emitter<Array<string | URI>>();
let collection = new DiagnosticCollection('ddd', 'test', 100, new DiagnosticsShape(), emitter);
let diag1 = new Diagnostic(new Range(1, 1, 2, 3), 'diag1');
let diag2 = new Diagnostic(new Range(1, 1, 2, 3), 'diag2');
let diag3 = new Diagnostic(new Range(1, 1, 2, 3), 'diag3');
let p = Event.toPromise(emitter.event).then(a => {
assert.equal(a.length, 1);
assert.equal(a[0].toString(), 'aa:bb');
assert.ok(URI.isUri(a[0]));
});
collection.set(URI.parse('aa:bb'), []);
await p;
p = Event.toPromise(emitter.event).then(e => {
assert.equal(e.length, 2);
assert.ok(URI.isUri(e[0]));
assert.ok(URI.isUri(e[1]));
assert.equal(e[0].toString(), 'aa:bb');
assert.equal(e[1].toString(), 'aa:cc');
});
collection.set([
[URI.parse('aa:bb'), [diag1]],
[URI.parse('aa:cc'), [diag2, diag3]],
]);
await p;
p = Event.toPromise(emitter.event).then(e => {
assert.equal(e.length, 2);
assert.ok(typeof e[0] === 'string');
assert.ok(typeof e[1] === 'string');
});
collection.clear();
await p;
});
test('vscode.languages.onDidChangeDiagnostics Does Not Provide Document URI #49582', async function () {
let emitter = new Emitter<Array<string | URI>>();
let collection = new DiagnosticCollection('ddd', 'test', 100, new DiagnosticsShape(), emitter);
let diag1 = new Diagnostic(new Range(1, 1, 2, 3), 'diag1');
// delete
collection.set(URI.parse('aa:bb'), [diag1]);
let p = Event.toPromise(emitter.event).then(e => {
assert.equal(e[0].toString(), 'aa:bb');
});
collection.delete(URI.parse('aa:bb'));
await p;
// set->undefined (as delete)
collection.set(URI.parse('aa:bb'), [diag1]);
p = Event.toPromise(emitter.event).then(e => {
assert.equal(e[0].toString(), 'aa:bb');
});
collection.set(URI.parse('aa:bb'), undefined!);
await p;
});
test('diagnostics with related information', function (done) {
let collection = new DiagnosticCollection('ddd', 'test', 100, new class extends DiagnosticsShape {
$changeMany(owner: string, entries: [UriComponents, IMarkerData[]][]) {
let [[, data]] = entries;
assert.equal(entries.length, 1);
assert.equal(data.length, 1);
let [diag] = data;
assert.equal(diag.relatedInformation!.length, 2);
assert.equal(diag.relatedInformation![0].message, 'more1');
assert.equal(diag.relatedInformation![1].message, 'more2');
done();
}
}, new Emitter<any>());
let diag = new Diagnostic(new Range(0, 0, 1, 1), 'Foo');
diag.relatedInformation = [
new DiagnosticRelatedInformation(new Location(URI.parse('cc:dd'), new Range(0, 0, 0, 0)), 'more1'),
new DiagnosticRelatedInformation(new Location(URI.parse('cc:ee'), new Range(0, 0, 0, 0)), 'more2')
];
collection.set(URI.parse('aa:bb'), [diag]);
});
test('vscode.languages.getDiagnostics appears to return old diagnostics in some circumstances #54359', function () {
const ownerHistory: string[] = [];
const diags = new ExtHostDiagnostics(new class implements IMainContext {
getProxy(id: any): any {
return new class DiagnosticsShape {
$clear(owner: string): void {
ownerHistory.push(owner);
}
};
}
set(): any {
return null;
}
assertRegistered(): void {
}
}, new NullLogService());
let collection1 = diags.createDiagnosticCollection('foo');
let collection2 = diags.createDiagnosticCollection('foo'); // warns, uses a different owner
collection1.clear();
collection2.clear();
assert.equal(ownerHistory.length, 2);
assert.equal(ownerHistory[0], 'foo');
assert.equal(ownerHistory[1], 'foo0');
});
test('Error updating diagnostics from extension #60394', function () {
let callCount = 0;
let collection = new DiagnosticCollection('ddd', 'test', 100, new class extends DiagnosticsShape {
$changeMany(owner: string, entries: [UriComponents, IMarkerData[]][]) {
callCount += 1;
}
}, new Emitter<any>());
let array: Diagnostic[] = [];
let diag1 = new Diagnostic(new Range(0, 0, 1, 1), 'Foo');
let diag2 = new Diagnostic(new Range(0, 0, 1, 1), 'Bar');
array.push(diag1, diag2);
collection.set(URI.parse('test:me'), array);
assert.equal(callCount, 1);
collection.set(URI.parse('test:me'), array);
assert.equal(callCount, 2); // equal array
array.push(diag2);
collection.set(URI.parse('test:me'), array);
assert.equal(callCount, 3); // same but un-equal array
});
test('Diagnostics created by tasks aren\'t accessible to extensions #47292', async function () {
const diags = new ExtHostDiagnostics(new class implements IMainContext {
getProxy(id: any): any {
return {};
}
set(): any {
return null;
}
assertRegistered(): void {
}
}, new NullLogService());
//
const uri = URI.parse('foo:bar');
const data: IMarkerData[] = [{
message: 'message',
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 1,
severity: 3
}];
const p1 = Event.toPromise(diags.onDidChangeDiagnostics);
diags.$acceptMarkersChange([[uri, data]]);
await p1;
assert.equal(diags.getDiagnostics(uri).length, 1);
const p2 = Event.toPromise(diags.onDidChangeDiagnostics);
diags.$acceptMarkersChange([[uri, []]]);
await p2;
assert.equal(diags.getDiagnostics(uri).length, 0);
});
});

View File

@@ -1,468 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ExtHostDocumentData } from 'vs/workbench/api/common/extHostDocumentData';
import { Position } from 'vs/workbench/api/common/extHostTypes';
import { Range } from 'vs/editor/common/core/range';
import { MainThreadDocumentsShape } from 'vs/workbench/api/common/extHost.protocol';
import { IModelChangedEvent } from 'vs/editor/common/model/mirrorTextModel';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
suite('ExtHostDocumentData', () => {
let data: ExtHostDocumentData;
function assertPositionAt(offset: number, line: number, character: number) {
let position = data.document.positionAt(offset);
assert.equal(position.line, line);
assert.equal(position.character, character);
}
function assertOffsetAt(line: number, character: number, offset: number) {
let pos = new Position(line, character);
let actual = data.document.offsetAt(pos);
assert.equal(actual, offset);
}
setup(function () {
data = new ExtHostDocumentData(undefined!, URI.file(''), [
'This is line one', //16
'and this is line number two', //27
'it is followed by #3', //20
'and finished with the fourth.', //29
], '\n', 'text', 1, false);
});
test('readonly-ness', () => {
assert.throws(() => (data as any).document.uri = null);
assert.throws(() => (data as any).document.fileName = 'foofile');
assert.throws(() => (data as any).document.isDirty = false);
assert.throws(() => (data as any).document.isUntitled = false);
assert.throws(() => (data as any).document.languageId = 'dddd');
assert.throws(() => (data as any).document.lineCount = 9);
});
test('save, when disposed', function () {
let saved: URI;
let data = new ExtHostDocumentData(new class extends mock<MainThreadDocumentsShape>() {
$trySaveDocument(uri: URI) {
assert.ok(!saved);
saved = uri;
return Promise.resolve(true);
}
}, URI.parse('foo:bar'), [], '\n', 'text', 1, true);
return data.document.save().then(() => {
assert.equal(saved.toString(), 'foo:bar');
data.dispose();
return data.document.save().then(() => {
assert.ok(false, 'expected failure');
}, err => {
assert.ok(err);
});
});
});
test('read, when disposed', function () {
data.dispose();
const { document } = data;
assert.equal(document.lineCount, 4);
assert.equal(document.lineAt(0).text, 'This is line one');
});
test('lines', () => {
assert.equal(data.document.lineCount, 4);
assert.throws(() => data.document.lineAt(-1));
assert.throws(() => data.document.lineAt(data.document.lineCount));
assert.throws(() => data.document.lineAt(Number.MAX_VALUE));
assert.throws(() => data.document.lineAt(Number.MIN_VALUE));
assert.throws(() => data.document.lineAt(0.8));
let line = data.document.lineAt(0);
assert.equal(line.lineNumber, 0);
assert.equal(line.text.length, 16);
assert.equal(line.text, 'This is line one');
assert.equal(line.isEmptyOrWhitespace, false);
assert.equal(line.firstNonWhitespaceCharacterIndex, 0);
data.onEvents({
changes: [{
range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: '\t '
}],
eol: undefined!,
versionId: undefined!,
});
// line didn't change
assert.equal(line.text, 'This is line one');
assert.equal(line.firstNonWhitespaceCharacterIndex, 0);
// fetch line again
line = data.document.lineAt(0);
assert.equal(line.text, '\t This is line one');
assert.equal(line.firstNonWhitespaceCharacterIndex, 2);
});
test('line, issue #5704', function () {
let line = data.document.lineAt(0);
let { range, rangeIncludingLineBreak } = line;
assert.equal(range.end.line, 0);
assert.equal(range.end.character, 16);
assert.equal(rangeIncludingLineBreak.end.line, 1);
assert.equal(rangeIncludingLineBreak.end.character, 0);
line = data.document.lineAt(data.document.lineCount - 1);
range = line.range;
rangeIncludingLineBreak = line.rangeIncludingLineBreak;
assert.equal(range.end.line, 3);
assert.equal(range.end.character, 29);
assert.equal(rangeIncludingLineBreak.end.line, 3);
assert.equal(rangeIncludingLineBreak.end.character, 29);
});
test('offsetAt', () => {
assertOffsetAt(0, 0, 0);
assertOffsetAt(0, 1, 1);
assertOffsetAt(0, 16, 16);
assertOffsetAt(1, 0, 17);
assertOffsetAt(1, 3, 20);
assertOffsetAt(2, 0, 45);
assertOffsetAt(4, 29, 95);
assertOffsetAt(4, 30, 95);
assertOffsetAt(4, Number.MAX_VALUE, 95);
assertOffsetAt(5, 29, 95);
assertOffsetAt(Number.MAX_VALUE, 29, 95);
assertOffsetAt(Number.MAX_VALUE, Number.MAX_VALUE, 95);
});
test('offsetAt, after remove', function () {
data.onEvents({
changes: [{
range: { startLineNumber: 1, startColumn: 3, endLineNumber: 1, endColumn: 6 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: ''
}],
eol: undefined!,
versionId: undefined!,
});
assertOffsetAt(0, 1, 1);
assertOffsetAt(0, 13, 13);
assertOffsetAt(1, 0, 14);
});
test('offsetAt, after replace', function () {
data.onEvents({
changes: [{
range: { startLineNumber: 1, startColumn: 3, endLineNumber: 1, endColumn: 6 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: 'is could be'
}],
eol: undefined!,
versionId: undefined!,
});
assertOffsetAt(0, 1, 1);
assertOffsetAt(0, 24, 24);
assertOffsetAt(1, 0, 25);
});
test('offsetAt, after insert line', function () {
data.onEvents({
changes: [{
range: { startLineNumber: 1, startColumn: 3, endLineNumber: 1, endColumn: 6 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: 'is could be\na line with number'
}],
eol: undefined!,
versionId: undefined!,
});
assertOffsetAt(0, 1, 1);
assertOffsetAt(0, 13, 13);
assertOffsetAt(1, 0, 14);
assertOffsetAt(1, 18, 13 + 1 + 18);
assertOffsetAt(1, 29, 13 + 1 + 29);
assertOffsetAt(2, 0, 13 + 1 + 29 + 1);
});
test('offsetAt, after remove line', function () {
data.onEvents({
changes: [{
range: { startLineNumber: 1, startColumn: 3, endLineNumber: 2, endColumn: 6 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: ''
}],
eol: undefined!,
versionId: undefined!,
});
assertOffsetAt(0, 1, 1);
assertOffsetAt(0, 2, 2);
assertOffsetAt(1, 0, 25);
});
test('positionAt', () => {
assertPositionAt(0, 0, 0);
assertPositionAt(Number.MIN_VALUE, 0, 0);
assertPositionAt(1, 0, 1);
assertPositionAt(16, 0, 16);
assertPositionAt(17, 1, 0);
assertPositionAt(20, 1, 3);
assertPositionAt(45, 2, 0);
assertPositionAt(95, 3, 29);
assertPositionAt(96, 3, 29);
assertPositionAt(99, 3, 29);
assertPositionAt(Number.MAX_VALUE, 3, 29);
});
test('getWordRangeAtPosition', () => {
data = new ExtHostDocumentData(undefined!, URI.file(''), [
'aaaa bbbb+cccc abc'
], '\n', 'text', 1, false);
let range = data.document.getWordRangeAtPosition(new Position(0, 2))!;
assert.equal(range.start.line, 0);
assert.equal(range.start.character, 0);
assert.equal(range.end.line, 0);
assert.equal(range.end.character, 4);
// ignore bad regular expresson /.*/
assert.throws(() => data.document.getWordRangeAtPosition(new Position(0, 2), /.*/)!);
range = data.document.getWordRangeAtPosition(new Position(0, 5), /[a-z+]+/)!;
assert.equal(range.start.line, 0);
assert.equal(range.start.character, 5);
assert.equal(range.end.line, 0);
assert.equal(range.end.character, 14);
range = data.document.getWordRangeAtPosition(new Position(0, 17), /[a-z+]+/)!;
assert.equal(range.start.line, 0);
assert.equal(range.start.character, 15);
assert.equal(range.end.line, 0);
assert.equal(range.end.character, 18);
range = data.document.getWordRangeAtPosition(new Position(0, 11), /yy/)!;
assert.equal(range, undefined);
});
test('getWordRangeAtPosition doesn\'t quite use the regex as expected, #29102', function () {
data = new ExtHostDocumentData(undefined!, URI.file(''), [
'some text here',
'/** foo bar */',
'function() {',
' "far boo"',
'}'
], '\n', 'text', 1, false);
let range = data.document.getWordRangeAtPosition(new Position(0, 0), /\/\*.+\*\//);
assert.equal(range, undefined);
range = data.document.getWordRangeAtPosition(new Position(1, 0), /\/\*.+\*\//)!;
assert.equal(range.start.line, 1);
assert.equal(range.start.character, 0);
assert.equal(range.end.line, 1);
assert.equal(range.end.character, 14);
range = data.document.getWordRangeAtPosition(new Position(3, 0), /("|').*\1/);
assert.equal(range, undefined);
range = data.document.getWordRangeAtPosition(new Position(3, 1), /("|').*\1/)!;
assert.equal(range.start.line, 3);
assert.equal(range.start.character, 1);
assert.equal(range.end.line, 3);
assert.equal(range.end.character, 10);
});
});
enum AssertDocumentLineMappingDirection {
OffsetToPosition,
PositionToOffset
}
suite('ExtHostDocumentData updates line mapping', () => {
function positionToStr(position: { line: number; character: number; }): string {
return '(' + position.line + ',' + position.character + ')';
}
function assertDocumentLineMapping(doc: ExtHostDocumentData, direction: AssertDocumentLineMappingDirection): void {
let allText = doc.getText();
let line = 0, character = 0, previousIsCarriageReturn = false;
for (let offset = 0; offset <= allText.length; offset++) {
// The position coordinate system cannot express the position between \r and \n
let position = new Position(line, character + (previousIsCarriageReturn ? -1 : 0));
if (direction === AssertDocumentLineMappingDirection.OffsetToPosition) {
let actualPosition = doc.document.positionAt(offset);
assert.equal(positionToStr(actualPosition), positionToStr(position), 'positionAt mismatch for offset ' + offset);
} else {
// The position coordinate system cannot express the position between \r and \n
let expectedOffset = offset + (previousIsCarriageReturn ? -1 : 0);
let actualOffset = doc.document.offsetAt(position);
assert.equal(actualOffset, expectedOffset, 'offsetAt mismatch for position ' + positionToStr(position));
}
if (allText.charAt(offset) === '\n') {
line++;
character = 0;
} else {
character++;
}
previousIsCarriageReturn = (allText.charAt(offset) === '\r');
}
}
function createChangeEvent(range: Range, text: string, eol?: string): IModelChangedEvent {
return {
changes: [{
range: range,
rangeOffset: undefined!,
rangeLength: undefined!,
text: text
}],
eol: eol!,
versionId: undefined!,
};
}
function testLineMappingDirectionAfterEvents(lines: string[], eol: string, direction: AssertDocumentLineMappingDirection, e: IModelChangedEvent): void {
let myDocument = new ExtHostDocumentData(undefined!, URI.file(''), lines.slice(0), eol, 'text', 1, false);
assertDocumentLineMapping(myDocument, direction);
myDocument.onEvents(e);
assertDocumentLineMapping(myDocument, direction);
}
function testLineMappingAfterEvents(lines: string[], e: IModelChangedEvent): void {
testLineMappingDirectionAfterEvents(lines, '\n', AssertDocumentLineMappingDirection.PositionToOffset, e);
testLineMappingDirectionAfterEvents(lines, '\n', AssertDocumentLineMappingDirection.OffsetToPosition, e);
testLineMappingDirectionAfterEvents(lines, '\r\n', AssertDocumentLineMappingDirection.PositionToOffset, e);
testLineMappingDirectionAfterEvents(lines, '\r\n', AssertDocumentLineMappingDirection.OffsetToPosition, e);
}
test('line mapping', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], { changes: [], eol: undefined!, versionId: 7 });
});
test('after remove', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 1, 6), ''));
});
test('after replace', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 1, 6), 'is could be'));
});
test('after insert line', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 1, 6), 'is could be\na line with number'));
});
test('after insert two lines', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 1, 6), 'is could be\na line with number\nyet another line'));
});
test('after remove line', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 2, 6), ''));
});
test('after remove two lines', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 3, 6), ''));
});
test('after deleting entire content', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 4, 30), ''));
});
test('after replacing entire content', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 3, 4, 30), 'some new text\nthat\nspans multiple lines'));
});
test('after changing EOL to CRLF', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 1, 1, 1), '', '\r\n'));
});
test('after changing EOL to LF', () => {
testLineMappingAfterEvents([
'This is line one',
'and this is line number two',
'it is followed by #3',
'and finished with the fourth.',
], createChangeEvent(new Range(1, 1, 1, 1), '', '\n'));
});
});

View File

@@ -1,395 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { TextDocumentSaveReason, TextEdit, Position, EndOfLine } from 'vs/workbench/api/common/extHostTypes';
import { MainThreadTextEditorsShape, IWorkspaceEditDto, IWorkspaceTextEditDto } from 'vs/workbench/api/common/extHost.protocol';
import { ExtHostDocumentSaveParticipant } from 'vs/workbench/api/common/extHostDocumentSaveParticipant';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
import { SaveReason } from 'vs/workbench/common/editor';
import type * as vscode from 'vscode';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { NullLogService } from 'vs/platform/log/common/log';
import { timeout } from 'vs/base/common/async';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
suite('ExtHostDocumentSaveParticipant', () => {
let resource = URI.parse('foo:bar');
let mainThreadEditors = new class extends mock<MainThreadTextEditorsShape>() { };
let documents: ExtHostDocuments;
let nullLogService = new NullLogService();
let nullExtensionDescription: IExtensionDescription = {
identifier: new ExtensionIdentifier('nullExtensionDescription'),
name: 'Null Extension Description',
publisher: 'vscode',
enableProposedApi: false,
engines: undefined!,
extensionLocation: undefined!,
isBuiltin: false,
isUnderDevelopment: false,
version: undefined!
};
setup(() => {
const documentsAndEditors = new ExtHostDocumentsAndEditors(SingleProxyRPCProtocol(null), new NullLogService());
documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
modeId: 'foo',
uri: resource,
versionId: 1,
lines: ['foo'],
EOL: '\n',
}]
});
documents = new ExtHostDocuments(SingleProxyRPCProtocol(null), documentsAndEditors);
});
test('no listeners, no problem', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => assert.ok(true));
});
test('event delivery', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let event: vscode.TextDocumentWillSaveEvent;
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
event = e;
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
assert.ok(event);
assert.equal(event.reason, TextDocumentSaveReason.Manual);
assert.equal(typeof event.waitUntil, 'function');
});
});
test('event delivery, immutable', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let event: vscode.TextDocumentWillSaveEvent;
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
event = e;
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
assert.ok(event);
assert.throws(() => { (event.document as any) = null!; });
});
});
test('event delivery, bad listener', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
throw new Error('💀');
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(values => {
sub.dispose();
const [first] = values;
assert.equal(first, false);
});
});
test('event delivery, bad listener doesn\'t prevent more events', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let sub1 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
throw new Error('💀');
});
let event: vscode.TextDocumentWillSaveEvent;
let sub2 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
event = e;
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub1.dispose();
sub2.dispose();
assert.ok(event);
});
});
test('event delivery, in subscriber order', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let counter = 0;
let sub1 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
assert.equal(counter++, 0);
});
let sub2 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
assert.equal(counter++, 1);
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub1.dispose();
sub2.dispose();
});
});
test('event delivery, ignore bad listeners', async () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors, { timeout: 5, errors: 1 });
let callCount = 0;
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
callCount += 1;
throw new Error('boom');
});
await participant.$participateInSave(resource, SaveReason.EXPLICIT);
await participant.$participateInSave(resource, SaveReason.EXPLICIT);
await participant.$participateInSave(resource, SaveReason.EXPLICIT);
await participant.$participateInSave(resource, SaveReason.EXPLICIT);
sub.dispose();
assert.equal(callCount, 2);
});
test('event delivery, overall timeout', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors, { timeout: 20, errors: 5 });
let callCount = 0;
let sub1 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
callCount += 1;
event.waitUntil(timeout(1));
});
let sub2 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
callCount += 1;
event.waitUntil(timeout(170));
});
let sub3 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
callCount += 1;
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(values => {
sub1.dispose();
sub2.dispose();
sub3.dispose();
assert.equal(callCount, 2);
assert.equal(values.length, 2);
});
});
test('event delivery, waitUntil', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
event.waitUntil(timeout(10));
event.waitUntil(timeout(10));
event.waitUntil(timeout(10));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
});
});
test('event delivery, waitUntil must be called sync', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
event.waitUntil(new Promise((resolve, reject) => {
setTimeout(() => {
try {
assert.throws(() => event.waitUntil(timeout(10)));
resolve(undefined);
} catch (e) {
reject(e);
}
}, 10);
}));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
});
});
test('event delivery, waitUntil will timeout', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors, { timeout: 5, errors: 3 });
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (event) {
event.waitUntil(timeout(15));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(values => {
sub.dispose();
const [first] = values;
assert.equal(first, false);
});
});
test('event delivery, waitUntil failure handling', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, mainThreadEditors);
let sub1 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
e.waitUntil(Promise.reject(new Error('dddd')));
});
let event: vscode.TextDocumentWillSaveEvent;
let sub2 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
event = e;
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
assert.ok(event);
sub1.dispose();
sub2.dispose();
});
});
test('event delivery, pushEdits sync', () => {
let dto: IWorkspaceEditDto;
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, new class extends mock<MainThreadTextEditorsShape>() {
$tryApplyWorkspaceEdit(_edits: IWorkspaceEditDto) {
dto = _edits;
return Promise.resolve(true);
}
});
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
e.waitUntil(Promise.resolve([TextEdit.insert(new Position(0, 0), 'bar')]));
e.waitUntil(Promise.resolve([TextEdit.setEndOfLine(EndOfLine.CRLF)]));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
assert.equal(dto.edits.length, 2);
assert.ok((<IWorkspaceTextEditDto>dto.edits[0]).edit);
assert.ok((<IWorkspaceTextEditDto>dto.edits[1]).edit);
});
});
test('event delivery, concurrent change', () => {
let edits: IWorkspaceEditDto;
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, new class extends mock<MainThreadTextEditorsShape>() {
$tryApplyWorkspaceEdit(_edits: IWorkspaceEditDto) {
edits = _edits;
return Promise.resolve(true);
}
});
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
// concurrent change from somewhere
documents.$acceptModelChanged(resource, {
changes: [{
range: { startLineNumber: 1, startColumn: 1, endLineNumber: 1, endColumn: 1 },
rangeOffset: undefined!,
rangeLength: undefined!,
text: 'bar'
}],
eol: undefined!,
versionId: 2
}, true);
e.waitUntil(Promise.resolve([TextEdit.insert(new Position(0, 0), 'bar')]));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(values => {
sub.dispose();
assert.equal(edits, undefined);
assert.equal(values[0], false);
});
});
test('event delivery, two listeners -> two document states', () => {
const participant = new ExtHostDocumentSaveParticipant(nullLogService, documents, new class extends mock<MainThreadTextEditorsShape>() {
$tryApplyWorkspaceEdit(dto: IWorkspaceEditDto) {
for (const edit of dto.edits) {
const uri = URI.revive((<IWorkspaceTextEditDto>edit).resource);
const { text, range } = (<IWorkspaceTextEditDto>edit).edit;
documents.$acceptModelChanged(uri, {
changes: [{
range,
text,
rangeOffset: undefined!,
rangeLength: undefined!,
}],
eol: undefined!,
versionId: documents.getDocumentData(uri)!.version + 1
}, true);
// }
}
return Promise.resolve(true);
}
});
const document = documents.getDocument(resource);
let sub1 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
// the document state we started with
assert.equal(document.version, 1);
assert.equal(document.getText(), 'foo');
e.waitUntil(Promise.resolve([TextEdit.insert(new Position(0, 0), 'bar')]));
});
let sub2 = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
// the document state AFTER the first listener kicked in
assert.equal(document.version, 2);
assert.equal(document.getText(), 'barfoo');
e.waitUntil(Promise.resolve([TextEdit.insert(new Position(0, 0), 'bar')]));
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(values => {
sub1.dispose();
sub2.dispose();
// the document state AFTER eventing is done
assert.equal(document.version, 3);
assert.equal(document.getText(), 'barbarfoo');
});
});
test('Log failing listener', function () {
let didLogSomething = false;
let participant = new ExtHostDocumentSaveParticipant(new class extends NullLogService {
error(message: string | Error, ...args: any[]): void {
didLogSomething = true;
}
}, documents, mainThreadEditors);
let sub = participant.getOnWillSaveTextDocumentEvent(nullExtensionDescription)(function (e) {
throw new Error('boom');
});
return participant.$participateInSave(resource, SaveReason.EXPLICIT).then(() => {
sub.dispose();
assert.equal(didLogSomething, true);
});
});
});

View File

@@ -1,57 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { TestRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostDocumentsAndEditors', () => {
let editors: ExtHostDocumentsAndEditors;
setup(function () {
editors = new ExtHostDocumentsAndEditors(new TestRPCProtocol(), new NullLogService());
});
test('The value of TextDocument.isClosed is incorrect when a text document is closed, #27949', () => {
editors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
EOL: '\n',
isDirty: true,
modeId: 'fooLang',
uri: URI.parse('foo:bar'),
versionId: 1,
lines: [
'first',
'second'
]
}]
});
return new Promise((resolve, reject) => {
editors.onDidRemoveDocuments(e => {
try {
for (const data of e) {
assert.equal(data.document.isClosed, true);
}
resolve(undefined);
} catch (e) {
reject(e);
}
});
editors.$acceptDocumentsAndEditorsDelta({
removedDocuments: [URI.parse('foo:bar')]
});
});
});
});

View File

@@ -1,32 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { ExtHostFileSystemEventService } from 'vs/workbench/api/common/extHostFileSystemEventService';
import { IMainContext } from 'vs/workbench/api/common/extHost.protocol';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostFileSystemEventService', () => {
test('FileSystemWatcher ignore events properties are reversed #26851', function () {
const protocol: IMainContext = {
getProxy: () => { return undefined!; },
set: undefined!,
assertRegistered: undefined!
};
const watcher1 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher('**/somethingInteresting', false, false, false);
assert.equal(watcher1.ignoreChangeEvents, false);
assert.equal(watcher1.ignoreCreateEvents, false);
assert.equal(watcher1.ignoreDeleteEvents, false);
const watcher2 = new ExtHostFileSystemEventService(protocol, new NullLogService(), undefined!).createFileSystemWatcher('**/somethingBoring', true, true, true);
assert.equal(watcher2.ignoreChangeEvents, true);
assert.equal(watcher2.ignoreCreateEvents, true);
assert.equal(watcher2.ignoreDeleteEvents, true);
});
});

View File

@@ -1,146 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MainThreadMessageService } from 'vs/workbench/api/browser/mainThreadMessageService';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { INotificationService, INotification, NoOpNotification, INotificationHandle, Severity, IPromptChoice, IPromptOptions, IStatusMessageOptions, NotificationsFilter } from 'vs/platform/notification/common/notification';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { IDisposable, Disposable } from 'vs/base/common/lifecycle';
const emptyDialogService = new class implements IDialogService {
_serviceBrand: undefined;
show(): never {
throw new Error('not implemented');
}
confirm(): never {
throw new Error('not implemented');
}
about(): never {
throw new Error('not implemented');
}
};
const emptyCommandService: ICommandService = {
_serviceBrand: undefined,
onWillExecuteCommand: () => Disposable.None,
onDidExecuteCommand: () => Disposable.None,
executeCommand: (commandId: string, ...args: any[]): Promise<any> => {
return Promise.resolve(undefined);
}
};
const emptyNotificationService = new class implements INotificationService {
_serviceBrand: undefined;
notify(...args: any[]): never {
throw new Error('not implemented');
}
info(...args: any[]): never {
throw new Error('not implemented');
}
warn(...args: any[]): never {
throw new Error('not implemented');
}
error(...args: any[]): never {
throw new Error('not implemented');
}
prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
throw new Error('not implemented');
}
status(message: string | Error, options?: IStatusMessageOptions): IDisposable {
return Disposable.None;
}
setFilter(filter: NotificationsFilter): void {
throw new Error('not implemented.');
}
};
class EmptyNotificationService implements INotificationService {
_serviceBrand: undefined;
constructor(private withNotify: (notification: INotification) => void) {
}
notify(notification: INotification): INotificationHandle {
this.withNotify(notification);
return new NoOpNotification();
}
info(message: any): void {
throw new Error('Method not implemented.');
}
warn(message: any): void {
throw new Error('Method not implemented.');
}
error(message: any): void {
throw new Error('Method not implemented.');
}
prompt(severity: Severity, message: string, choices: IPromptChoice[], options?: IPromptOptions): INotificationHandle {
throw new Error('Method not implemented');
}
status(message: string, options?: IStatusMessageOptions): IDisposable {
return Disposable.None;
}
setFilter(filter: NotificationsFilter): void {
throw new Error('Method not implemented.');
}
}
suite('ExtHostMessageService', function () {
test('propagte handle on select', async function () {
let service = new MainThreadMessageService(null!, new EmptyNotificationService(notification => {
assert.equal(notification.actions!.primary!.length, 1);
setImmediate(() => notification.actions!.primary![0].run());
}), emptyCommandService, emptyDialogService);
const handle = await service.$showMessage(1, 'h', {}, [{ handle: 42, title: 'a thing', isCloseAffordance: true }]);
assert.equal(handle, 42);
});
suite('modal', () => {
test('calls dialog service', async () => {
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
show(severity: Severity, message: string, buttons: string[]) {
assert.equal(severity, 1);
assert.equal(message, 'h');
assert.equal(buttons.length, 2);
assert.equal(buttons[1], 'Cancel');
return Promise.resolve({ choice: 0 });
}
} as IDialogService);
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: false }]);
assert.equal(handle, 42);
});
test('returns undefined when cancelled', async () => {
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
show() {
return Promise.resolve({ choice: 1 });
}
} as IDialogService);
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: false }]);
assert.equal(handle, undefined);
});
test('hides Cancel button when not needed', async () => {
const service = new MainThreadMessageService(null!, emptyNotificationService, emptyCommandService, new class extends mock<IDialogService>() {
show(severity: Severity, message: string, buttons: string[]) {
assert.equal(buttons.length, 1);
return Promise.resolve({ choice: 0 });
}
} as IDialogService);
const handle = await service.$showMessage(1, 'h', { modal: true }, [{ handle: 42, title: 'a thing', isCloseAffordance: true }]);
assert.equal(handle, 42);
});
});
});

View File

@@ -15,11 +15,11 @@ import { MainContext, MainThreadSearchShape } from 'vs/workbench/api/common/extH
import { NativeExtHostSearch } from 'vs/workbench/api/node/extHostSearch';
import { Range } from 'vs/workbench/api/common/extHostTypes';
import { IFileMatch, IFileQuery, IPatternInfo, IRawFileMatch2, ISearchCompleteStats, ISearchQuery, ITextQuery, QueryType, resultIsMatch } from 'vs/workbench/services/search/common/search';
import { TestRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol';
import { TestRPCProtocol } from 'vs/workbench/test/browser/api/testRPCProtocol';
import type * as vscode from 'vscode';
import { NullLogService } from 'vs/platform/log/common/log';
import { URITransformerService } from 'vs/workbench/api/common/extHostUriTransformerService';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { mock } from 'vs/workbench/test/browser/api/mock';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { TextSearchManager } from 'vs/workbench/services/search/common/textSearchManager';
import { NativeTextSearchManager } from 'vs/workbench/services/search/node/textSearchManager';

View File

@@ -1,514 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { TextEditorLineNumbersStyle, Range } from 'vs/workbench/api/common/extHostTypes';
import { TextEditorCursorStyle, RenderLineNumbersType } from 'vs/editor/common/config/editorOptions';
import { MainThreadTextEditorsShape, IResolvedTextEditorConfiguration, ITextEditorConfigurationUpdate } from 'vs/workbench/api/common/extHost.protocol';
import { ExtHostTextEditorOptions, ExtHostTextEditor } from 'vs/workbench/api/common/extHostTextEditor';
import { ExtHostDocumentData } from 'vs/workbench/api/common/extHostDocumentData';
import { URI } from 'vs/base/common/uri';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostTextEditor', () => {
let editor: ExtHostTextEditor;
let doc = new ExtHostDocumentData(undefined!, URI.file(''), [
'aaaa bbbb+cccc abc'
], '\n', 'text', 1, false);
setup(() => {
editor = new ExtHostTextEditor('fake', null!, new NullLogService(), doc, [], { cursorStyle: 0, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4 }, [], 1);
});
test('disposed editor', () => {
assert.ok(editor.document);
editor._acceptViewColumn(3);
assert.equal(3, editor.viewColumn);
editor.dispose();
assert.throws(() => editor._acceptViewColumn(2));
assert.equal(3, editor.viewColumn);
assert.ok(editor.document);
assert.throws(() => editor._acceptOptions(null!));
assert.throws(() => editor._acceptSelections([]));
});
test('API [bug]: registerTextEditorCommand clears redo stack even if no edits are made #55163', async function () {
let applyCount = 0;
let editor = new ExtHostTextEditor('edt1',
new class extends mock<MainThreadTextEditorsShape>() {
$tryApplyEdits(): Promise<boolean> {
applyCount += 1;
return Promise.resolve(true);
}
}, new NullLogService(), doc, [], { cursorStyle: 0, insertSpaces: true, lineNumbers: 1, tabSize: 4, indentSize: 4 }, [], 1);
await editor.edit(edit => { });
assert.equal(applyCount, 0);
await editor.edit(edit => { edit.setEndOfLine(1); });
assert.equal(applyCount, 1);
await editor.edit(edit => { edit.delete(new Range(0, 0, 1, 1)); });
assert.equal(applyCount, 2);
});
});
suite('ExtHostTextEditorOptions', () => {
let opts: ExtHostTextEditorOptions;
let calls: ITextEditorConfigurationUpdate[] = [];
setup(() => {
calls = [];
let mockProxy: MainThreadTextEditorsShape = {
dispose: undefined!,
$trySetOptions: (id: string, options: ITextEditorConfigurationUpdate) => {
assert.equal(id, '1');
calls.push(options);
return Promise.resolve(undefined);
},
$tryShowTextDocument: undefined!,
$registerTextEditorDecorationType: undefined!,
$removeTextEditorDecorationType: undefined!,
$tryShowEditor: undefined!,
$tryHideEditor: undefined!,
$trySetDecorations: undefined!,
$trySetDecorationsFast: undefined!,
$tryRevealRange: undefined!,
$trySetSelections: undefined!,
$tryApplyEdits: undefined!,
$tryApplyWorkspaceEdit: undefined!,
$tryInsertSnippet: undefined!,
$getDiffInformation: undefined!
};
opts = new ExtHostTextEditorOptions(mockProxy, '1', {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
}, new NullLogService());
});
teardown(() => {
opts = null!;
calls = null!;
});
function assertState(opts: ExtHostTextEditorOptions, expected: IResolvedTextEditorConfiguration): void {
let actual = {
tabSize: opts.tabSize,
indentSize: opts.indentSize,
insertSpaces: opts.insertSpaces,
cursorStyle: opts.cursorStyle,
lineNumbers: opts.lineNumbers
};
assert.deepEqual(actual, expected);
}
test('can set tabSize to the same value', () => {
opts.tabSize = 4;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can change tabSize to positive integer', () => {
opts.tabSize = 1;
assertState(opts, {
tabSize: 1,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 1 }]);
});
test('can change tabSize to positive float', () => {
opts.tabSize = 2.3;
assertState(opts, {
tabSize: 2,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 2 }]);
});
test('can change tabSize to a string number', () => {
opts.tabSize = '2';
assertState(opts, {
tabSize: 2,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 2 }]);
});
test('tabSize can request indentation detection', () => {
opts.tabSize = 'auto';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 'auto' }]);
});
test('ignores invalid tabSize 1', () => {
opts.tabSize = null!;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid tabSize 2', () => {
opts.tabSize = -5;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid tabSize 3', () => {
opts.tabSize = 'hello';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid tabSize 4', () => {
opts.tabSize = '-17';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can set indentSize to the same value', () => {
opts.indentSize = 4;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can change indentSize to positive integer', () => {
opts.indentSize = 1;
assertState(opts, {
tabSize: 4,
indentSize: 1,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ indentSize: 1 }]);
});
test('can change indentSize to positive float', () => {
opts.indentSize = 2.3;
assertState(opts, {
tabSize: 4,
indentSize: 2,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ indentSize: 2 }]);
});
test('can change indentSize to a string number', () => {
opts.indentSize = '2';
assertState(opts, {
tabSize: 4,
indentSize: 2,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ indentSize: 2 }]);
});
test('indentSize can request to use tabSize', () => {
opts.indentSize = 'tabSize';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ indentSize: 'tabSize' }]);
});
test('indentSize cannot request indentation detection', () => {
opts.indentSize = 'auto';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid indentSize 1', () => {
opts.indentSize = null!;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid indentSize 2', () => {
opts.indentSize = -5;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid indentSize 3', () => {
opts.indentSize = 'hello';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('ignores invalid indentSize 4', () => {
opts.indentSize = '-17';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can set insertSpaces to the same value', () => {
opts.insertSpaces = false;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can set insertSpaces to boolean', () => {
opts.insertSpaces = true;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: true,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ insertSpaces: true }]);
});
test('can set insertSpaces to false string', () => {
opts.insertSpaces = 'false';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can set insertSpaces to truey', () => {
opts.insertSpaces = 'hello';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: true,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ insertSpaces: true }]);
});
test('insertSpaces can request indentation detection', () => {
opts.insertSpaces = 'auto';
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ insertSpaces: 'auto' }]);
});
test('can set cursorStyle to same value', () => {
opts.cursorStyle = TextEditorCursorStyle.Line;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can change cursorStyle', () => {
opts.cursorStyle = TextEditorCursorStyle.Block;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Block,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ cursorStyle: TextEditorCursorStyle.Block }]);
});
test('can set lineNumbers to same value', () => {
opts.lineNumbers = TextEditorLineNumbersStyle.On;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can change lineNumbers', () => {
opts.lineNumbers = TextEditorLineNumbersStyle.Off;
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.Off
});
assert.deepEqual(calls, [{ lineNumbers: RenderLineNumbersType.Off }]);
});
test('can do bulk updates 0', () => {
opts.assign({
tabSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: TextEditorLineNumbersStyle.On
});
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, []);
});
test('can do bulk updates 1', () => {
opts.assign({
tabSize: 'auto',
insertSpaces: true
});
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: true,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 'auto', insertSpaces: true }]);
});
test('can do bulk updates 2', () => {
opts.assign({
tabSize: 3,
insertSpaces: 'auto'
});
assertState(opts, {
tabSize: 3,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Line,
lineNumbers: RenderLineNumbersType.On
});
assert.deepEqual(calls, [{ tabSize: 3, insertSpaces: 'auto' }]);
});
test('can do bulk updates 3', () => {
opts.assign({
cursorStyle: TextEditorCursorStyle.Block,
lineNumbers: TextEditorLineNumbersStyle.Relative
});
assertState(opts, {
tabSize: 4,
indentSize: 4,
insertSpaces: false,
cursorStyle: TextEditorCursorStyle.Block,
lineNumbers: RenderLineNumbersType.Relative
});
assert.deepEqual(calls, [{ cursorStyle: TextEditorCursorStyle.Block, lineNumbers: RenderLineNumbersType.Relative }]);
});
});

View File

@@ -1,62 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as extHostTypes from 'vs/workbench/api/common/extHostTypes';
import { MainContext, MainThreadTextEditorsShape, IWorkspaceEditDto } from 'vs/workbench/api/common/extHost.protocol';
import { URI } from 'vs/base/common/uri';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
import { SingleProxyRPCProtocol, TestRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol';
import { ExtHostEditors } from 'vs/workbench/api/common/extHostTextEditors';
import { WorkspaceTextEdit } from 'vs/editor/common/modes';
import { NullLogService } from 'vs/platform/log/common/log';
suite('ExtHostTextEditors.applyWorkspaceEdit', () => {
const resource = URI.parse('foo:bar');
let editors: ExtHostEditors;
let workspaceResourceEdits: IWorkspaceEditDto;
setup(() => {
workspaceResourceEdits = null!;
let rpcProtocol = new TestRPCProtocol();
rpcProtocol.set(MainContext.MainThreadTextEditors, new class extends mock<MainThreadTextEditorsShape>() {
$tryApplyWorkspaceEdit(_workspaceResourceEdits: IWorkspaceEditDto): Promise<boolean> {
workspaceResourceEdits = _workspaceResourceEdits;
return Promise.resolve(true);
}
});
const documentsAndEditors = new ExtHostDocumentsAndEditors(SingleProxyRPCProtocol(null), new NullLogService());
documentsAndEditors.$acceptDocumentsAndEditorsDelta({
addedDocuments: [{
isDirty: false,
modeId: 'foo',
uri: resource,
versionId: 1337,
lines: ['foo'],
EOL: '\n',
}]
});
editors = new ExtHostEditors(rpcProtocol, documentsAndEditors);
});
test('uses version id if document available', async () => {
let edit = new extHostTypes.WorkspaceEdit();
edit.replace(resource, new extHostTypes.Range(0, 0, 0, 0), 'hello');
await editors.applyWorkspaceEdit(edit);
assert.equal(workspaceResourceEdits.edits.length, 1);
assert.equal((<WorkspaceTextEdit>workspaceResourceEdits.edits[0]).modelVersionId, 1337);
});
test('does not use version id if document is not available', async () => {
let edit = new extHostTypes.WorkspaceEdit();
edit.replace(URI.parse('foo:bar2'), new extHostTypes.Range(0, 0, 0, 0), 'hello');
await editors.applyWorkspaceEdit(edit);
assert.equal(workspaceResourceEdits.edits.length, 1);
assert.ok(typeof (<WorkspaceTextEdit>workspaceResourceEdits.edits[0]).modelVersionId === 'undefined');
});
});

View File

@@ -1,741 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import { Emitter } from 'vs/base/common/event';
import { ExtHostTreeViews } from 'vs/workbench/api/common/extHostTreeViews';
import { ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
import { MainThreadTreeViewsShape, MainContext } from 'vs/workbench/api/common/extHost.protocol';
import { TreeDataProvider, TreeItem } from 'vscode';
import { TestRPCProtocol } from './testRPCProtocol';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { MainThreadCommands } from 'vs/workbench/api/browser/mainThreadCommands';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { TreeItemCollapsibleState, ITreeItem } from 'vs/workbench/common/views';
import { NullLogService } from 'vs/platform/log/common/log';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import type { IDisposable } from 'vs/base/common/lifecycle';
suite('ExtHostTreeView', function () {
class RecordingShape extends mock<MainThreadTreeViewsShape>() {
onRefresh = new Emitter<{ [treeItemHandle: string]: ITreeItem }>();
$registerTreeViewDataProvider(treeViewId: string): void {
}
$refresh(viewId: string, itemsToRefresh: { [treeItemHandle: string]: ITreeItem }): Promise<void> {
return Promise.resolve(null).then(() => {
this.onRefresh.fire(itemsToRefresh);
});
}
$reveal(): Promise<void> {
return Promise.resolve();
}
}
let testObject: ExtHostTreeViews;
let target: RecordingShape;
let onDidChangeTreeNode: Emitter<{ key: string } | undefined>;
let onDidChangeTreeNodeWithId: Emitter<{ key: string }>;
let tree: { [key: string]: any };
let labels: { [key: string]: string };
let nodes: { [key: string]: { key: string } };
setup(() => {
tree = {
'a': {
'aa': {},
'ab': {}
},
'b': {
'ba': {},
'bb': {}
}
};
labels = {};
nodes = {};
let rpcProtocol = new TestRPCProtocol();
// Use IInstantiationService to get typechecking when instantiating
let inst: IInstantiationService;
{
let instantiationService = new TestInstantiationService();
inst = instantiationService;
}
rpcProtocol.set(MainContext.MainThreadCommands, inst.createInstance(MainThreadCommands, rpcProtocol));
target = new RecordingShape();
testObject = new ExtHostTreeViews(target, new ExtHostCommands(
rpcProtocol,
new NullLogService()
), new NullLogService());
onDidChangeTreeNode = new Emitter<{ key: string } | undefined>();
onDidChangeTreeNodeWithId = new Emitter<{ key: string }>();
testObject.createTreeView('testNodeTreeProvider', { treeDataProvider: aNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
testObject.createTreeView('testNodeWithIdTreeProvider', { treeDataProvider: aNodeWithIdTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
testObject.createTreeView('testNodeWithHighlightsTreeProvider', { treeDataProvider: aNodeWithHighlightedLabelTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return loadCompleteTree('testNodeTreeProvider');
});
test('construct node tree', () => {
return testObject.$getChildren('testNodeTreeProvider')
.then(elements => {
const actuals = elements.map(e => e.handle);
assert.deepEqual(actuals, ['0/0:a', '0/0:b']);
return Promise.all([
testObject.$getChildren('testNodeTreeProvider', '0/0:a')
.then(children => {
const actuals = children.map(e => e.handle);
assert.deepEqual(actuals, ['0/0:a/0:aa', '0/0:a/0:ab']);
return Promise.all([
testObject.$getChildren('testNodeTreeProvider', '0/0:a/0:aa').then(children => assert.equal(children.length, 0)),
testObject.$getChildren('testNodeTreeProvider', '0/0:a/0:ab').then(children => assert.equal(children.length, 0))
]);
}),
testObject.$getChildren('testNodeTreeProvider', '0/0:b')
.then(children => {
const actuals = children.map(e => e.handle);
assert.deepEqual(actuals, ['0/0:b/0:ba', '0/0:b/0:bb']);
return Promise.all([
testObject.$getChildren('testNodeTreeProvider', '0/0:b/0:ba').then(children => assert.equal(children.length, 0)),
testObject.$getChildren('testNodeTreeProvider', '0/0:b/0:bb').then(children => assert.equal(children.length, 0))
]);
})
]);
});
});
test('construct id tree', () => {
return testObject.$getChildren('testNodeWithIdTreeProvider')
.then(elements => {
const actuals = elements.map(e => e.handle);
assert.deepEqual(actuals, ['1/a', '1/b']);
return Promise.all([
testObject.$getChildren('testNodeWithIdTreeProvider', '1/a')
.then(children => {
const actuals = children.map(e => e.handle);
assert.deepEqual(actuals, ['1/aa', '1/ab']);
return Promise.all([
testObject.$getChildren('testNodeWithIdTreeProvider', '1/aa').then(children => assert.equal(children.length, 0)),
testObject.$getChildren('testNodeWithIdTreeProvider', '1/ab').then(children => assert.equal(children.length, 0))
]);
}),
testObject.$getChildren('testNodeWithIdTreeProvider', '1/b')
.then(children => {
const actuals = children.map(e => e.handle);
assert.deepEqual(actuals, ['1/ba', '1/bb']);
return Promise.all([
testObject.$getChildren('testNodeWithIdTreeProvider', '1/ba').then(children => assert.equal(children.length, 0)),
testObject.$getChildren('testNodeWithIdTreeProvider', '1/bb').then(children => assert.equal(children.length, 0))
]);
})
]);
});
});
test('construct highlights tree', () => {
return testObject.$getChildren('testNodeWithHighlightsTreeProvider')
.then(elements => {
assert.deepEqual(removeUnsetKeys(elements), [{
handle: '1/a',
label: { label: 'a', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.Collapsed
}, {
handle: '1/b',
label: { label: 'b', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.Collapsed
}]);
return Promise.all([
testObject.$getChildren('testNodeWithHighlightsTreeProvider', '1/a')
.then(children => {
assert.deepEqual(removeUnsetKeys(children), [{
handle: '1/aa',
parentHandle: '1/a',
label: { label: 'aa', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.None
}, {
handle: '1/ab',
parentHandle: '1/a',
label: { label: 'ab', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.None
}]);
}),
testObject.$getChildren('testNodeWithHighlightsTreeProvider', '1/b')
.then(children => {
assert.deepEqual(removeUnsetKeys(children), [{
handle: '1/ba',
parentHandle: '1/b',
label: { label: 'ba', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.None
}, {
handle: '1/bb',
parentHandle: '1/b',
label: { label: 'bb', highlights: [[0, 2], [3, 5]] },
collapsibleState: TreeItemCollapsibleState.None
}]);
})
]);
});
});
test('error is thrown if id is not unique', (done) => {
tree['a'] = {
'aa': {},
};
tree['b'] = {
'aa': {},
'ba': {}
};
target.onRefresh.event(() => {
testObject.$getChildren('testNodeWithIdTreeProvider')
.then(elements => {
const actuals = elements.map(e => e.handle);
assert.deepEqual(actuals, ['1/a', '1/b']);
return testObject.$getChildren('testNodeWithIdTreeProvider', '1/a')
.then(() => testObject.$getChildren('testNodeWithIdTreeProvider', '1/b'))
.then(() => { assert.fail('Should fail with duplicate id'); done(); }, () => done());
});
});
onDidChangeTreeNode.fire(undefined);
});
test('refresh root', function (done) {
target.onRefresh.event(actuals => {
assert.equal(undefined, actuals);
done();
});
onDidChangeTreeNode.fire(undefined);
});
test('refresh a parent node', () => {
return new Promise((c, e) => {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:b'], Object.keys(actuals));
assert.deepEqual(removeUnsetKeys(actuals['0/0:b']), {
handle: '0/0:b',
label: { label: 'b' },
collapsibleState: TreeItemCollapsibleState.Collapsed
});
c(undefined);
});
onDidChangeTreeNode.fire(getNode('b'));
});
});
test('refresh a leaf node', function (done) {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:b/0:bb'], Object.keys(actuals));
assert.deepEqual(removeUnsetKeys(actuals['0/0:b/0:bb']), {
handle: '0/0:b/0:bb',
parentHandle: '0/0:b',
label: { label: 'bb' },
collapsibleState: TreeItemCollapsibleState.None
});
done();
});
onDidChangeTreeNode.fire(getNode('bb'));
});
async function runWithEventMerging(action: (resolve: () => void) => void) {
await new Promise((resolve) => {
let subscription: IDisposable | undefined = undefined;
subscription = target.onRefresh.event(() => {
subscription!.dispose();
resolve();
});
onDidChangeTreeNode.fire(getNode('b'));
});
await new Promise(action);
}
test('refresh parent and child node trigger refresh only on parent - scenario 1', async () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:b', '0/0:a/0:aa'], Object.keys(actuals));
assert.deepEqual(removeUnsetKeys(actuals['0/0:b']), {
handle: '0/0:b',
label: { label: 'b' },
collapsibleState: TreeItemCollapsibleState.Collapsed
});
assert.deepEqual(removeUnsetKeys(actuals['0/0:a/0:aa']), {
handle: '0/0:a/0:aa',
parentHandle: '0/0:a',
label: { label: 'aa' },
collapsibleState: TreeItemCollapsibleState.None
});
resolve();
});
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(getNode('aa'));
onDidChangeTreeNode.fire(getNode('bb'));
});
});
test('refresh parent and child node trigger refresh only on parent - scenario 2', async () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:a/0:aa', '0/0:b'], Object.keys(actuals));
assert.deepEqual(removeUnsetKeys(actuals['0/0:b']), {
handle: '0/0:b',
label: { label: 'b' },
collapsibleState: TreeItemCollapsibleState.Collapsed
});
assert.deepEqual(removeUnsetKeys(actuals['0/0:a/0:aa']), {
handle: '0/0:a/0:aa',
parentHandle: '0/0:a',
label: { label: 'aa' },
collapsibleState: TreeItemCollapsibleState.None
});
resolve();
});
onDidChangeTreeNode.fire(getNode('bb'));
onDidChangeTreeNode.fire(getNode('aa'));
onDidChangeTreeNode.fire(getNode('b'));
});
});
test('refresh an element for label change', function (done) {
labels['a'] = 'aa';
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:a'], Object.keys(actuals));
assert.deepEqual(removeUnsetKeys(actuals['0/0:a']), {
handle: '0/0:aa',
label: { label: 'aa' },
collapsibleState: TreeItemCollapsibleState.Collapsed
});
done();
});
onDidChangeTreeNode.fire(getNode('a'));
});
test('refresh calls are throttled on roots', () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.equal(undefined, actuals);
resolve();
});
onDidChangeTreeNode.fire(undefined);
onDidChangeTreeNode.fire(undefined);
onDidChangeTreeNode.fire(undefined);
onDidChangeTreeNode.fire(undefined);
});
});
test('refresh calls are throttled on elements', () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:a', '0/0:b'], Object.keys(actuals));
resolve();
});
onDidChangeTreeNode.fire(getNode('a'));
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(getNode('a'));
});
});
test('refresh calls are throttled on unknown elements', () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.deepEqual(['0/0:a', '0/0:b'], Object.keys(actuals));
resolve();
});
onDidChangeTreeNode.fire(getNode('a'));
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(getNode('g'));
onDidChangeTreeNode.fire(getNode('a'));
});
});
test('refresh calls are throttled on unknown elements and root', () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.equal(undefined, actuals);
resolve();
});
onDidChangeTreeNode.fire(getNode('a'));
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(getNode('g'));
onDidChangeTreeNode.fire(undefined);
});
});
test('refresh calls are throttled on elements and root', () => {
return runWithEventMerging((resolve) => {
target.onRefresh.event(actuals => {
assert.equal(undefined, actuals);
resolve();
});
onDidChangeTreeNode.fire(getNode('a'));
onDidChangeTreeNode.fire(getNode('b'));
onDidChangeTreeNode.fire(undefined);
onDidChangeTreeNode.fire(getNode('a'));
});
});
test('generate unique handles from labels by escaping them', (done) => {
tree = {
'a/0:b': {}
};
target.onRefresh.event(() => {
testObject.$getChildren('testNodeTreeProvider')
.then(elements => {
assert.deepEqual(elements.map(e => e.handle), ['0/0:a//0:b']);
done();
});
});
onDidChangeTreeNode.fire(undefined);
});
test('tree with duplicate labels', (done) => {
const dupItems = {
'adup1': 'c',
'adup2': 'g',
'bdup1': 'e',
'hdup1': 'i',
'hdup2': 'l',
'jdup1': 'k'
};
labels['c'] = 'a';
labels['e'] = 'b';
labels['g'] = 'a';
labels['i'] = 'h';
labels['l'] = 'h';
labels['k'] = 'j';
tree[dupItems['adup1']] = {};
tree['d'] = {};
const bdup1Tree: { [key: string]: any } = {};
bdup1Tree['h'] = {};
bdup1Tree[dupItems['hdup1']] = {};
bdup1Tree['j'] = {};
bdup1Tree[dupItems['jdup1']] = {};
bdup1Tree[dupItems['hdup2']] = {};
tree[dupItems['bdup1']] = bdup1Tree;
tree['f'] = {};
tree[dupItems['adup2']] = {};
target.onRefresh.event(() => {
testObject.$getChildren('testNodeTreeProvider')
.then(elements => {
const actuals = elements.map(e => e.handle);
assert.deepEqual(actuals, ['0/0:a', '0/0:b', '0/1:a', '0/0:d', '0/1:b', '0/0:f', '0/2:a']);
return testObject.$getChildren('testNodeTreeProvider', '0/1:b')
.then(elements => {
const actuals = elements.map(e => e.handle);
assert.deepEqual(actuals, ['0/1:b/0:h', '0/1:b/1:h', '0/1:b/0:j', '0/1:b/1:j', '0/1:b/2:h']);
done();
});
});
});
onDidChangeTreeNode.fire(undefined);
});
test('getChildren is not returned from cache if refreshed', (done) => {
tree = {
'c': {}
};
target.onRefresh.event(() => {
testObject.$getChildren('testNodeTreeProvider')
.then(elements => {
assert.deepEqual(elements.map(e => e.handle), ['0/0:c']);
done();
});
});
onDidChangeTreeNode.fire(undefined);
});
test('getChildren is returned from cache if not refreshed', () => {
tree = {
'c': {}
};
return testObject.$getChildren('testNodeTreeProvider')
.then(elements => {
assert.deepEqual(elements.map(e => e.handle), ['0/0:a', '0/0:b']);
});
});
test('reveal will throw an error if getParent is not implemented', () => {
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return treeView.reveal({ key: 'a' })
.then(() => assert.fail('Reveal should throw an error as getParent is not implemented'), () => null);
});
test('reveal will return empty array for root element', () => {
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return treeView.reveal({ key: 'a' })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:a', label: { label: 'a' }, collapsibleState: TreeItemCollapsibleState.Collapsed }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([], revealTarget.args[0][2]);
assert.deepEqual({ select: true, focus: false, expand: false }, revealTarget.args[0][3]);
});
});
test('reveal will return parents array for an element when hierarchy is not loaded', () => {
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return treeView.reveal({ key: 'aa' })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:a/0:aa', label: { label: 'aa' }, collapsibleState: TreeItemCollapsibleState.None, parentHandle: '0/0:a' }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([{ handle: '0/0:a', label: { label: 'a' }, collapsibleState: TreeItemCollapsibleState.Collapsed }], (<Array<any>>revealTarget.args[0][2]).map(arg => removeUnsetKeys(arg)));
assert.deepEqual({ select: true, focus: false, expand: false }, revealTarget.args[0][3]);
});
});
test('reveal will return parents array for an element when hierarchy is loaded', () => {
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return testObject.$getChildren('treeDataProvider')
.then(() => testObject.$getChildren('treeDataProvider', '0/0:a'))
.then(() => treeView.reveal({ key: 'aa' })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:a/0:aa', label: { label: 'aa' }, collapsibleState: TreeItemCollapsibleState.None, parentHandle: '0/0:a' }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([{ handle: '0/0:a', label: { label: 'a' }, collapsibleState: TreeItemCollapsibleState.Collapsed }], (<Array<any>>revealTarget.args[0][2]).map(arg => removeUnsetKeys(arg)));
assert.deepEqual({ select: true, focus: false, expand: false }, revealTarget.args[0][3]);
}));
});
test('reveal will return parents array for deeper element with no selection', () => {
tree = {
'b': {
'ba': {
'bac': {}
}
}
};
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return treeView.reveal({ key: 'bac' }, { select: false, focus: false, expand: false })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:b/0:ba/0:bac', label: { label: 'bac' }, collapsibleState: TreeItemCollapsibleState.None, parentHandle: '0/0:b/0:ba' }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([
{ handle: '0/0:b', label: { label: 'b' }, collapsibleState: TreeItemCollapsibleState.Collapsed },
{ handle: '0/0:b/0:ba', label: { label: 'ba' }, collapsibleState: TreeItemCollapsibleState.Collapsed, parentHandle: '0/0:b' }
], (<Array<any>>revealTarget.args[0][2]).map(arg => removeUnsetKeys(arg)));
assert.deepEqual({ select: false, focus: false, expand: false }, revealTarget.args[0][3]);
});
});
test('reveal after first udpate', () => {
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return loadCompleteTree('treeDataProvider')
.then(() => {
tree = {
'a': {
'aa': {},
'ac': {}
},
'b': {
'ba': {},
'bb': {}
}
};
onDidChangeTreeNode.fire(getNode('a'));
return treeView.reveal({ key: 'ac' })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:a/0:ac', label: { label: 'ac' }, collapsibleState: TreeItemCollapsibleState.None, parentHandle: '0/0:a' }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([{ handle: '0/0:a', label: { label: 'a' }, collapsibleState: TreeItemCollapsibleState.Collapsed }], (<Array<any>>revealTarget.args[0][2]).map(arg => removeUnsetKeys(arg)));
assert.deepEqual({ select: true, focus: false, expand: false }, revealTarget.args[0][3]);
});
});
});
test('reveal after second udpate', () => {
const revealTarget = sinon.spy(target, '$reveal');
const treeView = testObject.createTreeView('treeDataProvider', { treeDataProvider: aCompleteNodeTreeDataProvider() }, { enableProposedApi: true } as IExtensionDescription);
return loadCompleteTree('treeDataProvider')
.then(() => {
runWithEventMerging((resolve) => {
tree = {
'a': {
'aa': {},
'ac': {}
},
'b': {
'ba': {},
'bb': {}
}
};
onDidChangeTreeNode.fire(getNode('a'));
tree = {
'a': {
'aa': {},
'ac': {}
},
'b': {
'ba': {},
'bc': {}
}
};
onDidChangeTreeNode.fire(getNode('b'));
resolve();
}).then(() => {
return treeView.reveal({ key: 'bc' })
.then(() => {
assert.ok(revealTarget.calledOnce);
assert.deepEqual('treeDataProvider', revealTarget.args[0][0]);
assert.deepEqual({ handle: '0/0:b/0:bc', label: { label: 'bc' }, collapsibleState: TreeItemCollapsibleState.None, parentHandle: '0/0:b' }, removeUnsetKeys(revealTarget.args[0][1]));
assert.deepEqual([{ handle: '0/0:b', label: { label: 'b' }, collapsibleState: TreeItemCollapsibleState.Collapsed }], (<Array<any>>revealTarget.args[0][2]).map(arg => removeUnsetKeys(arg)));
assert.deepEqual({ select: true, focus: false, expand: false }, revealTarget.args[0][3]);
});
});
});
});
function loadCompleteTree(treeId: string, element?: string): Promise<null> {
return testObject.$getChildren(treeId, element)
.then(elements => elements.map(e => loadCompleteTree(treeId, e.handle)))
.then(() => null);
}
function removeUnsetKeys(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(o => removeUnsetKeys(o));
}
if (typeof obj === 'object') {
const result: { [key: string]: any } = {};
for (const key of Object.keys(obj)) {
if (obj[key] !== undefined) {
result[key] = removeUnsetKeys(obj[key]);
}
}
return result;
}
return obj;
}
function aNodeTreeDataProvider(): TreeDataProvider<{ key: string }> {
return {
getChildren: (element: { key: string }): { key: string }[] => {
return getChildren(element ? element.key : undefined).map(key => getNode(key));
},
getTreeItem: (element: { key: string }): TreeItem => {
return getTreeItem(element.key);
},
onDidChangeTreeData: onDidChangeTreeNode.event
};
}
function aCompleteNodeTreeDataProvider(): TreeDataProvider<{ key: string }> {
return {
getChildren: (element: { key: string }): { key: string }[] => {
return getChildren(element ? element.key : undefined).map(key => getNode(key));
},
getTreeItem: (element: { key: string }): TreeItem => {
return getTreeItem(element.key);
},
getParent: ({ key }: { key: string }): { key: string } | undefined => {
const parentKey = key.substring(0, key.length - 1);
return parentKey ? new Key(parentKey) : undefined;
},
onDidChangeTreeData: onDidChangeTreeNode.event
};
}
function aNodeWithIdTreeDataProvider(): TreeDataProvider<{ key: string }> {
return {
getChildren: (element: { key: string }): { key: string }[] => {
return getChildren(element ? element.key : undefined).map(key => getNode(key));
},
getTreeItem: (element: { key: string }): TreeItem => {
const treeItem = getTreeItem(element.key);
treeItem.id = element.key;
return treeItem;
},
onDidChangeTreeData: onDidChangeTreeNodeWithId.event
};
}
function aNodeWithHighlightedLabelTreeDataProvider(): TreeDataProvider<{ key: string }> {
return {
getChildren: (element: { key: string }): { key: string }[] => {
return getChildren(element ? element.key : undefined).map(key => getNode(key));
},
getTreeItem: (element: { key: string }): TreeItem => {
const treeItem = getTreeItem(element.key, [[0, 2], [3, 5]]);
treeItem.id = element.key;
return treeItem;
},
onDidChangeTreeData: onDidChangeTreeNodeWithId.event
};
}
function getTreeElement(element: string): any {
let parent = tree;
for (let i = 0; i < element.length; i++) {
parent = parent[element.substring(0, i + 1)];
if (!parent) {
return null;
}
}
return parent;
}
function getChildren(key: string | undefined): string[] {
if (!key) {
return Object.keys(tree);
}
let treeElement = getTreeElement(key);
if (treeElement) {
return Object.keys(treeElement);
}
return [];
}
function getTreeItem(key: string, highlights?: [number, number][]): TreeItem {
const treeElement = getTreeElement(key);
return {
label: <any>{ label: labels[key] || key, highlights },
collapsibleState: treeElement && Object.keys(treeElement).length ? TreeItemCollapsibleState.Collapsed : TreeItemCollapsibleState.None
};
}
function getNode(key: string): { key: string } {
if (!nodes[key]) {
nodes[key] = new Key(key);
}
return nodes[key];
}
class Key {
constructor(readonly key: string) { }
}
});

View File

@@ -1,86 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MarkdownString, LogLevel } from 'vs/workbench/api/common/extHostTypeConverters';
import { isEmptyObject } from 'vs/base/common/types';
import { size, forEach } from 'vs/base/common/collections';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { LogLevel as _MainLogLevel } from 'vs/platform/log/common/log';
import { URI } from 'vs/base/common/uri';
suite('ExtHostTypeConverter', function () {
test('MarkdownConvert - uris', function () {
let data = MarkdownString.from('Hello');
assert.equal(isEmptyObject(data.uris), true);
assert.equal(data.value, 'Hello');
data = MarkdownString.from('Hello [link](foo)');
assert.equal(data.value, 'Hello [link](foo)');
assert.equal(isEmptyObject(data.uris), true); // no scheme, no uri
data = MarkdownString.from('Hello [link](www.noscheme.bad)');
assert.equal(data.value, 'Hello [link](www.noscheme.bad)');
assert.equal(isEmptyObject(data.uris), true); // no scheme, no uri
data = MarkdownString.from('Hello [link](foo:path)');
assert.equal(data.value, 'Hello [link](foo:path)');
assert.equal(size(data.uris!), 1);
assert.ok(!!data.uris!['foo:path']);
data = MarkdownString.from('hello@foo.bar');
assert.equal(data.value, 'hello@foo.bar');
assert.equal(size(data.uris!), 1);
assert.ok(!!data.uris!['mailto:hello@foo.bar']);
data = MarkdownString.from('*hello* [click](command:me)');
assert.equal(data.value, '*hello* [click](command:me)');
assert.equal(size(data.uris!), 1);
assert.ok(!!data.uris!['command:me']);
data = MarkdownString.from('*hello* [click](file:///somepath/here). [click](file:///somepath/here)');
assert.equal(data.value, '*hello* [click](file:///somepath/here). [click](file:///somepath/here)');
assert.equal(size(data.uris!), 1);
assert.ok(!!data.uris!['file:///somepath/here']);
data = MarkdownString.from('*hello* [click](file:///somepath/here). [click](file:///somepath/here)');
assert.equal(data.value, '*hello* [click](file:///somepath/here). [click](file:///somepath/here)');
assert.equal(size(data.uris!), 1);
assert.ok(!!data.uris!['file:///somepath/here']);
data = MarkdownString.from('*hello* [click](file:///somepath/here). [click](file:///somepath/here2)');
assert.equal(data.value, '*hello* [click](file:///somepath/here). [click](file:///somepath/here2)');
assert.equal(size(data.uris!), 2);
assert.ok(!!data.uris!['file:///somepath/here']);
assert.ok(!!data.uris!['file:///somepath/here2']);
});
test('NPM script explorer running a script from the hover does not work #65561', function () {
let data = MarkdownString.from('*hello* [click](command:npm.runScriptFromHover?%7B%22documentUri%22%3A%7B%22%24mid%22%3A1%2C%22external%22%3A%22file%3A%2F%2F%2Fc%253A%2Ffoo%2Fbaz.ex%22%2C%22path%22%3A%22%2Fc%3A%2Ffoo%2Fbaz.ex%22%2C%22scheme%22%3A%22file%22%7D%2C%22script%22%3A%22dev%22%7D)');
// assert that both uri get extracted but that the latter is only decoded once...
assert.equal(size(data.uris!), 2);
forEach(data.uris!, entry => {
if (entry.value.scheme === 'file') {
assert.ok(URI.revive(entry.value).toString().indexOf('file:///c%3A') === 0);
} else {
assert.equal(entry.value.scheme, 'command');
}
});
});
test('LogLevel', () => {
assert.equal(LogLevel.from(types.LogLevel.Error), _MainLogLevel.Error);
assert.equal(LogLevel.from(types.LogLevel.Info), _MainLogLevel.Info);
assert.equal(LogLevel.from(types.LogLevel.Off), _MainLogLevel.Off);
assert.equal(LogLevel.to(_MainLogLevel.Error), types.LogLevel.Error);
assert.equal(LogLevel.to(_MainLogLevel.Info), types.LogLevel.Info);
assert.equal(LogLevel.to(_MainLogLevel.Off), types.LogLevel.Off);
});
});

View File

@@ -1,569 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import * as types from 'vs/workbench/api/common/extHostTypes';
import { isWindows } from 'vs/base/common/platform';
import { assertType } from 'vs/base/common/types';
function assertToJSON(a: any, expected: any) {
const raw = JSON.stringify(a);
const actual = JSON.parse(raw);
assert.deepEqual(actual, expected);
}
suite('ExtHostTypes', function () {
test('URI, toJSON', function () {
let uri = URI.parse('file:///path/test.file');
assert.deepEqual(uri.toJSON(), {
$mid: 1,
scheme: 'file',
path: '/path/test.file'
});
assert.ok(uri.fsPath);
assert.deepEqual(uri.toJSON(), {
$mid: 1,
scheme: 'file',
path: '/path/test.file',
fsPath: '/path/test.file'.replace(/\//g, isWindows ? '\\' : '/'),
_sep: isWindows ? 1 : undefined,
});
assert.ok(uri.toString());
assert.deepEqual(uri.toJSON(), {
$mid: 1,
scheme: 'file',
path: '/path/test.file',
fsPath: '/path/test.file'.replace(/\//g, isWindows ? '\\' : '/'),
_sep: isWindows ? 1 : undefined,
external: 'file:///path/test.file'
});
});
test('Disposable', () => {
let count = 0;
let d = new types.Disposable(() => {
count += 1;
return 12;
});
d.dispose();
assert.equal(count, 1);
d.dispose();
assert.equal(count, 1);
types.Disposable.from(undefined!, { dispose() { count += 1; } }).dispose();
assert.equal(count, 2);
assert.throws(() => {
new types.Disposable(() => {
throw new Error();
}).dispose();
});
new types.Disposable(undefined!).dispose();
});
test('Position', () => {
assert.throws(() => new types.Position(-1, 0));
assert.throws(() => new types.Position(0, -1));
let pos = new types.Position(0, 0);
assert.throws(() => (pos as any).line = -1);
assert.throws(() => (pos as any).character = -1);
assert.throws(() => (pos as any).line = 12);
let { line, character } = pos.toJSON();
assert.equal(line, 0);
assert.equal(character, 0);
});
test('Position, toJSON', function () {
let pos = new types.Position(4, 2);
assertToJSON(pos, { line: 4, character: 2 });
});
test('Position, isBefore(OrEqual)?', function () {
let p1 = new types.Position(1, 3);
let p2 = new types.Position(1, 2);
let p3 = new types.Position(0, 4);
assert.ok(p1.isBeforeOrEqual(p1));
assert.ok(!p1.isBefore(p1));
assert.ok(p2.isBefore(p1));
assert.ok(p3.isBefore(p2));
});
test('Position, isAfter(OrEqual)?', function () {
let p1 = new types.Position(1, 3);
let p2 = new types.Position(1, 2);
let p3 = new types.Position(0, 4);
assert.ok(p1.isAfterOrEqual(p1));
assert.ok(!p1.isAfter(p1));
assert.ok(p1.isAfter(p2));
assert.ok(p2.isAfter(p3));
assert.ok(p1.isAfter(p3));
});
test('Position, compareTo', function () {
let p1 = new types.Position(1, 3);
let p2 = new types.Position(1, 2);
let p3 = new types.Position(0, 4);
assert.equal(p1.compareTo(p1), 0);
assert.equal(p2.compareTo(p1), -1);
assert.equal(p1.compareTo(p2), 1);
assert.equal(p2.compareTo(p3), 1);
assert.equal(p1.compareTo(p3), 1);
});
test('Position, translate', function () {
let p1 = new types.Position(1, 3);
assert.ok(p1.translate() === p1);
assert.ok(p1.translate({}) === p1);
assert.ok(p1.translate(0, 0) === p1);
assert.ok(p1.translate(0) === p1);
assert.ok(p1.translate(undefined, 0) === p1);
assert.ok(p1.translate(undefined) === p1);
let res = p1.translate(-1);
assert.equal(res.line, 0);
assert.equal(res.character, 3);
res = p1.translate({ lineDelta: -1 });
assert.equal(res.line, 0);
assert.equal(res.character, 3);
res = p1.translate(undefined, -1);
assert.equal(res.line, 1);
assert.equal(res.character, 2);
res = p1.translate({ characterDelta: -1 });
assert.equal(res.line, 1);
assert.equal(res.character, 2);
res = p1.translate(11);
assert.equal(res.line, 12);
assert.equal(res.character, 3);
assert.throws(() => p1.translate(null!));
assert.throws(() => p1.translate(null!, null!));
assert.throws(() => p1.translate(-2));
assert.throws(() => p1.translate({ lineDelta: -2 }));
assert.throws(() => p1.translate(-2, null!));
assert.throws(() => p1.translate(0, -4));
});
test('Position, with', function () {
let p1 = new types.Position(1, 3);
assert.ok(p1.with() === p1);
assert.ok(p1.with(1) === p1);
assert.ok(p1.with(undefined, 3) === p1);
assert.ok(p1.with(1, 3) === p1);
assert.ok(p1.with(undefined) === p1);
assert.ok(p1.with({ line: 1 }) === p1);
assert.ok(p1.with({ character: 3 }) === p1);
assert.ok(p1.with({ line: 1, character: 3 }) === p1);
let p2 = p1.with({ line: 0, character: 11 });
assert.equal(p2.line, 0);
assert.equal(p2.character, 11);
assert.throws(() => p1.with(null!));
assert.throws(() => p1.with(-9));
assert.throws(() => p1.with(0, -9));
assert.throws(() => p1.with({ line: -1 }));
assert.throws(() => p1.with({ character: -1 }));
});
test('Range', () => {
assert.throws(() => new types.Range(-1, 0, 0, 0));
assert.throws(() => new types.Range(0, -1, 0, 0));
assert.throws(() => new types.Range(new types.Position(0, 0), undefined!));
assert.throws(() => new types.Range(new types.Position(0, 0), null!));
assert.throws(() => new types.Range(undefined!, new types.Position(0, 0)));
assert.throws(() => new types.Range(null!, new types.Position(0, 0)));
let range = new types.Range(1, 0, 0, 0);
assert.throws(() => { (range as any).start = null; });
assert.throws(() => { (range as any).start = new types.Position(0, 3); });
});
test('Range, toJSON', function () {
let range = new types.Range(1, 2, 3, 4);
assertToJSON(range, [{ line: 1, character: 2 }, { line: 3, character: 4 }]);
});
test('Range, sorting', function () {
// sorts start/end
let range = new types.Range(1, 0, 0, 0);
assert.equal(range.start.line, 0);
assert.equal(range.end.line, 1);
range = new types.Range(0, 0, 1, 0);
assert.equal(range.start.line, 0);
assert.equal(range.end.line, 1);
});
test('Range, isEmpty|isSingleLine', function () {
let range = new types.Range(1, 0, 0, 0);
assert.ok(!range.isEmpty);
assert.ok(!range.isSingleLine);
range = new types.Range(1, 1, 1, 1);
assert.ok(range.isEmpty);
assert.ok(range.isSingleLine);
range = new types.Range(0, 1, 0, 11);
assert.ok(!range.isEmpty);
assert.ok(range.isSingleLine);
range = new types.Range(0, 0, 1, 1);
assert.ok(!range.isEmpty);
assert.ok(!range.isSingleLine);
});
test('Range, contains', function () {
let range = new types.Range(1, 1, 2, 11);
assert.ok(range.contains(range.start));
assert.ok(range.contains(range.end));
assert.ok(range.contains(range));
assert.ok(!range.contains(new types.Range(1, 0, 2, 11)));
assert.ok(!range.contains(new types.Range(0, 1, 2, 11)));
assert.ok(!range.contains(new types.Range(1, 1, 2, 12)));
assert.ok(!range.contains(new types.Range(1, 1, 3, 11)));
});
test('Range, intersection', function () {
let range = new types.Range(1, 1, 2, 11);
let res: types.Range;
res = range.intersection(range)!;
assert.equal(res.start.line, 1);
assert.equal(res.start.character, 1);
assert.equal(res.end.line, 2);
assert.equal(res.end.character, 11);
res = range.intersection(new types.Range(2, 12, 4, 0))!;
assert.equal(res, undefined);
res = range.intersection(new types.Range(0, 0, 1, 0))!;
assert.equal(res, undefined);
res = range.intersection(new types.Range(0, 0, 1, 1))!;
assert.ok(res.isEmpty);
assert.equal(res.start.line, 1);
assert.equal(res.start.character, 1);
res = range.intersection(new types.Range(2, 11, 61, 1))!;
assert.ok(res.isEmpty);
assert.equal(res.start.line, 2);
assert.equal(res.start.character, 11);
assert.throws(() => range.intersection(null!));
assert.throws(() => range.intersection(undefined!));
});
test('Range, union', function () {
let ran1 = new types.Range(0, 0, 5, 5);
assert.ok(ran1.union(new types.Range(0, 0, 1, 1)) === ran1);
let res: types.Range;
res = ran1.union(new types.Range(2, 2, 9, 9));
assert.ok(res.start === ran1.start);
assert.equal(res.end.line, 9);
assert.equal(res.end.character, 9);
ran1 = new types.Range(2, 1, 5, 3);
res = ran1.union(new types.Range(1, 0, 4, 2));
assert.ok(res.end === ran1.end);
assert.equal(res.start.line, 1);
assert.equal(res.start.character, 0);
});
test('Range, with', function () {
let range = new types.Range(1, 1, 2, 11);
assert.ok(range.with(range.start) === range);
assert.ok(range.with(undefined, range.end) === range);
assert.ok(range.with(range.start, range.end) === range);
assert.ok(range.with(new types.Position(1, 1)) === range);
assert.ok(range.with(undefined, new types.Position(2, 11)) === range);
assert.ok(range.with() === range);
assert.ok(range.with({ start: range.start }) === range);
assert.ok(range.with({ start: new types.Position(1, 1) }) === range);
assert.ok(range.with({ end: range.end }) === range);
assert.ok(range.with({ end: new types.Position(2, 11) }) === range);
let res = range.with(undefined, new types.Position(9, 8));
assert.equal(res.end.line, 9);
assert.equal(res.end.character, 8);
assert.equal(res.start.line, 1);
assert.equal(res.start.character, 1);
res = range.with({ end: new types.Position(9, 8) });
assert.equal(res.end.line, 9);
assert.equal(res.end.character, 8);
assert.equal(res.start.line, 1);
assert.equal(res.start.character, 1);
res = range.with({ end: new types.Position(9, 8), start: new types.Position(2, 3) });
assert.equal(res.end.line, 9);
assert.equal(res.end.character, 8);
assert.equal(res.start.line, 2);
assert.equal(res.start.character, 3);
assert.throws(() => range.with(null!));
assert.throws(() => range.with(undefined, null!));
});
test('TextEdit', () => {
let range = new types.Range(1, 1, 2, 11);
let edit = new types.TextEdit(range, undefined!);
assert.equal(edit.newText, '');
assertToJSON(edit, { range: [{ line: 1, character: 1 }, { line: 2, character: 11 }], newText: '' });
edit = new types.TextEdit(range, null!);
assert.equal(edit.newText, '');
edit = new types.TextEdit(range, '');
assert.equal(edit.newText, '');
});
test('WorkspaceEdit', () => {
let a = URI.file('a.ts');
let b = URI.file('b.ts');
let edit = new types.WorkspaceEdit();
assert.ok(!edit.has(a));
edit.set(a, [types.TextEdit.insert(new types.Position(0, 0), 'fff')]);
assert.ok(edit.has(a));
assert.equal(edit.size, 1);
assertToJSON(edit, [[a.toJSON(), [{ range: [{ line: 0, character: 0 }, { line: 0, character: 0 }], newText: 'fff' }]]]);
edit.insert(b, new types.Position(1, 1), 'fff');
edit.delete(b, new types.Range(0, 0, 0, 0));
assert.ok(edit.has(b));
assert.equal(edit.size, 2);
assertToJSON(edit, [
[a.toJSON(), [{ range: [{ line: 0, character: 0 }, { line: 0, character: 0 }], newText: 'fff' }]],
[b.toJSON(), [{ range: [{ line: 1, character: 1 }, { line: 1, character: 1 }], newText: 'fff' }, { range: [{ line: 0, character: 0 }, { line: 0, character: 0 }], newText: '' }]]
]);
edit.set(b, undefined!);
assert.ok(!edit.has(b));
assert.equal(edit.size, 1);
edit.set(b, [types.TextEdit.insert(new types.Position(0, 0), 'ffff')]);
assert.equal(edit.get(b).length, 1);
});
test('WorkspaceEdit - keep order of text and file changes', function () {
const edit = new types.WorkspaceEdit();
edit.replace(URI.parse('foo:a'), new types.Range(1, 1, 1, 1), 'foo');
edit.renameFile(URI.parse('foo:a'), URI.parse('foo:b'));
edit.replace(URI.parse('foo:a'), new types.Range(2, 1, 2, 1), 'bar');
edit.replace(URI.parse('foo:b'), new types.Range(3, 1, 3, 1), 'bazz');
const all = edit.allEntries();
assert.equal(all.length, 4);
const [first, second, third, fourth] = all;
assertType(first._type === 2);
assert.equal(first.uri.toString(), 'foo:a');
assertType(second._type === 1);
assert.equal(second.from!.toString(), 'foo:a');
assert.equal(second.to!.toString(), 'foo:b');
assertType(third._type === 2);
assert.equal(third.uri.toString(), 'foo:a');
assertType(fourth._type === 2);
assert.equal(fourth.uri.toString(), 'foo:b');
});
test('WorkspaceEdit - two edits for one resource', function () {
let edit = new types.WorkspaceEdit();
let uri = URI.parse('foo:bar');
edit.insert(uri, new types.Position(0, 0), 'Hello');
edit.insert(uri, new types.Position(0, 0), 'Foo');
assert.equal(edit.allEntries().length, 2);
let [first, second] = edit.allEntries();
assertType(first._type === 2);
assertType(second._type === 2);
assert.equal(first.edit.newText, 'Hello');
assert.equal(second.edit.newText, 'Foo');
});
test('DocumentLink', () => {
assert.throws(() => new types.DocumentLink(null!, null!));
assert.throws(() => new types.DocumentLink(new types.Range(1, 1, 1, 1), null!));
});
test('toJSON & stringify', function () {
assertToJSON(new types.Selection(3, 4, 2, 1), { start: { line: 2, character: 1 }, end: { line: 3, character: 4 }, anchor: { line: 3, character: 4 }, active: { line: 2, character: 1 } });
assertToJSON(new types.Location(URI.file('u.ts'), new types.Position(3, 4)), { uri: URI.parse('file:///u.ts').toJSON(), range: [{ line: 3, character: 4 }, { line: 3, character: 4 }] });
assertToJSON(new types.Location(URI.file('u.ts'), new types.Range(1, 2, 3, 4)), { uri: URI.parse('file:///u.ts').toJSON(), range: [{ line: 1, character: 2 }, { line: 3, character: 4 }] });
let diag = new types.Diagnostic(new types.Range(0, 1, 2, 3), 'hello');
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [{ line: 0, character: 1 }, { line: 2, character: 3 }] });
diag.source = 'me';
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [{ line: 0, character: 1 }, { line: 2, character: 3 }], source: 'me' });
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5)), { range: [{ line: 2, character: 3 }, { line: 4, character: 5 }], kind: 'Text' });
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5), types.DocumentHighlightKind.Read), { range: [{ line: 2, character: 3 }, { line: 4, character: 5 }], kind: 'Read' });
assertToJSON(new types.SymbolInformation('test', types.SymbolKind.Boolean, new types.Range(0, 1, 2, 3)), {
name: 'test',
kind: 'Boolean',
location: {
range: [{ line: 0, character: 1 }, { line: 2, character: 3 }]
}
});
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10)), { range: [{ line: 7, character: 8 }, { line: 9, character: 10 }] });
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10), { command: 'id', title: 'title' }), {
range: [{ line: 7, character: 8 }, { line: 9, character: 10 }],
command: { command: 'id', title: 'title' }
});
assertToJSON(new types.CompletionItem('complete'), { label: 'complete' });
let item = new types.CompletionItem('complete');
item.kind = types.CompletionItemKind.Interface;
assertToJSON(item, { label: 'complete', kind: 'Interface' });
});
test('SymbolInformation, old ctor', function () {
let info = new types.SymbolInformation('foo', types.SymbolKind.Array, new types.Range(1, 1, 2, 3));
assert.ok(info.location instanceof types.Location);
assert.equal(info.location.uri, undefined);
});
test('SnippetString, builder-methods', function () {
let string: types.SnippetString;
string = new types.SnippetString();
assert.equal(string.appendText('I need $ and $').value, 'I need \\$ and \\$');
string = new types.SnippetString();
assert.equal(string.appendText('I need \\$').value, 'I need \\\\\\$');
string = new types.SnippetString();
string.appendPlaceholder('fo$o}');
assert.equal(string.value, '${1:fo\\$o\\}}');
string = new types.SnippetString();
string.appendText('foo').appendTabstop(0).appendText('bar');
assert.equal(string.value, 'foo$0bar');
string = new types.SnippetString();
string.appendText('foo').appendTabstop().appendText('bar');
assert.equal(string.value, 'foo$1bar');
string = new types.SnippetString();
string.appendText('foo').appendTabstop(42).appendText('bar');
assert.equal(string.value, 'foo$42bar');
string = new types.SnippetString();
string.appendText('foo').appendPlaceholder('farboo').appendText('bar');
assert.equal(string.value, 'foo${1:farboo}bar');
string = new types.SnippetString();
string.appendText('foo').appendPlaceholder('far$boo').appendText('bar');
assert.equal(string.value, 'foo${1:far\\$boo}bar');
string = new types.SnippetString();
string.appendText('foo').appendPlaceholder(b => b.appendText('abc').appendPlaceholder('nested')).appendText('bar');
assert.equal(string.value, 'foo${1:abc${2:nested}}bar');
string = new types.SnippetString();
string.appendVariable('foo');
assert.equal(string.value, '${foo}');
string = new types.SnippetString();
string.appendText('foo').appendVariable('TM_SELECTED_TEXT').appendText('bar');
assert.equal(string.value, 'foo${TM_SELECTED_TEXT}bar');
string = new types.SnippetString();
string.appendVariable('BAR', b => b.appendPlaceholder('ops'));
assert.equal(string.value, '${BAR:${1:ops}}');
string = new types.SnippetString();
string.appendVariable('BAR', b => { });
assert.equal(string.value, '${BAR}');
string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r']);
assert.equal(string.value, '${1|b,a,r|}');
string = new types.SnippetString();
string.appendChoice(['b', 'a', 'r'], 0);
assert.equal(string.value, '${0|b,a,r|}');
string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,boo|}bar');
string = new types.SnippetString();
string.appendText('foo').appendChoice(['far', '$boo']).appendText('bar');
assert.equal(string.value, 'foo${1|far,\\$boo|}bar');
string = new types.SnippetString();
string.appendText('foo').appendPlaceholder('farboo').appendChoice(['far', 'boo']).appendText('bar');
assert.equal(string.value, 'foo${1:farboo}${2|far,boo|}bar');
});
test('instanceof doesn\'t work for FileSystemError #49386', function () {
const error = types.FileSystemError.Unavailable('foo');
assert.ok(error instanceof Error);
assert.ok(error instanceof types.FileSystemError);
});
test('CodeActionKind contains', () => {
assert.ok(types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.RefactorExtract));
assert.ok(types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.RefactorExtract.append('other')));
assert.ok(!types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.Refactor));
assert.ok(!types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.Refactor.append('other')));
assert.ok(!types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.Empty.append('other').append('refactor')));
assert.ok(!types.CodeActionKind.RefactorExtract.contains(types.CodeActionKind.Empty.append('refactory')));
});
test('CodeActionKind intersects', () => {
assert.ok(types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.RefactorExtract));
assert.ok(types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.Refactor));
assert.ok(types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.RefactorExtract.append('other')));
assert.ok(!types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.Refactor.append('other')));
assert.ok(!types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.Empty.append('other').append('refactor')));
assert.ok(!types.CodeActionKind.RefactorExtract.intersects(types.CodeActionKind.Empty.append('refactory')));
});
});

View File

@@ -1,153 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { NullLogService } from 'vs/platform/log/common/log';
import { MainThreadWebviews } from 'vs/workbench/api/browser/mainThreadWebview';
import { ExtHostWebviews } from 'vs/workbench/api/common/extHostWebview';
import { EditorViewColumn } from 'vs/workbench/api/common/shared/editor';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import type * as vscode from 'vscode';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
suite('ExtHostWebview', () => {
test('Cannot register multiple serializers for the same view type', async () => {
const viewType = 'view.type';
const shape = createNoopMainThreadWebviews();
const extHostWebviews = new ExtHostWebviews(SingleProxyRPCProtocol(shape), {
webviewCspSource: '',
webviewResourceRoot: '',
isExtensionDevelopmentDebug: false,
}, undefined, new NullLogService());
let lastInvokedDeserializer: vscode.WebviewPanelSerializer | undefined = undefined;
class NoopSerializer implements vscode.WebviewPanelSerializer {
async deserializeWebviewPanel(_webview: vscode.WebviewPanel, _state: any): Promise<void> {
lastInvokedDeserializer = this;
}
}
const extension = {} as IExtensionDescription;
const serializerA = new NoopSerializer();
const serializerB = new NoopSerializer();
const serializerARegistration = extHostWebviews.registerWebviewPanelSerializer(extension, viewType, serializerA);
await extHostWebviews.$deserializeWebviewPanel('x', viewType, 'title', {}, 0 as EditorViewColumn, {});
assert.strictEqual(lastInvokedDeserializer, serializerA);
assert.throws(
() => extHostWebviews.registerWebviewPanelSerializer(extension, viewType, serializerB),
'Should throw when registering two serializers for the same view');
serializerARegistration.dispose();
extHostWebviews.registerWebviewPanelSerializer(extension, viewType, serializerB);
await extHostWebviews.$deserializeWebviewPanel('x', viewType, 'title', {}, 0 as EditorViewColumn, {});
assert.strictEqual(lastInvokedDeserializer, serializerB);
});
test('asWebviewUri for desktop vscode-resource scheme', () => {
const shape = createNoopMainThreadWebviews();
const extHostWebviews = new ExtHostWebviews(SingleProxyRPCProtocol(shape), {
webviewCspSource: '',
webviewResourceRoot: 'vscode-resource://{{resource}}',
isExtensionDevelopmentDebug: false,
}, undefined, new NullLogService());
const webview = extHostWebviews.createWebviewPanel({} as any, 'type', 'title', 1, {});
assert.strictEqual(
webview.webview.asWebviewUri(URI.parse('file:///Users/codey/file.html')).toString(),
'vscode-resource://file///Users/codey/file.html',
'Unix basic'
);
assert.strictEqual(
webview.webview.asWebviewUri(URI.parse('file:///Users/codey/file.html#frag')).toString(),
'vscode-resource://file///Users/codey/file.html#frag',
'Unix should preserve fragment'
);
assert.strictEqual(
webview.webview.asWebviewUri(URI.parse('file:///Users/codey/f%20ile.html')).toString(),
'vscode-resource://file///Users/codey/f%20ile.html',
'Unix with encoding'
);
assert.strictEqual(
webview.webview.asWebviewUri(URI.parse('file://localhost/Users/codey/file.html')).toString(),
'vscode-resource://file//localhost/Users/codey/file.html',
'Unix should preserve authority'
);
assert.strictEqual(
webview.webview.asWebviewUri(URI.parse('file:///c:/codey/file.txt')).toString(),
'vscode-resource://file///c%3A/codey/file.txt',
'Windows C drive'
);
});
test('asWebviewUri for web endpoint', () => {
const shape = createNoopMainThreadWebviews();
const extHostWebviews = new ExtHostWebviews(SingleProxyRPCProtocol(shape), {
webviewCspSource: '',
webviewResourceRoot: `https://{{uuid}}.webview.contoso.com/commit/{{resource}}`,
isExtensionDevelopmentDebug: false,
}, undefined, new NullLogService());
const webview = extHostWebviews.createWebviewPanel({} as any, 'type', 'title', 1, {});
function stripEndpointUuid(input: string) {
return input.replace(/^https:\/\/[^\.]+?\./, '');
}
assert.strictEqual(
stripEndpointUuid(webview.webview.asWebviewUri(URI.parse('file:///Users/codey/file.html')).toString()),
'webview.contoso.com/commit/file///Users/codey/file.html',
'Unix basic'
);
assert.strictEqual(
stripEndpointUuid(webview.webview.asWebviewUri(URI.parse('file:///Users/codey/file.html#frag')).toString()),
'webview.contoso.com/commit/file///Users/codey/file.html#frag',
'Unix should preserve fragment'
);
assert.strictEqual(
stripEndpointUuid(webview.webview.asWebviewUri(URI.parse('file:///Users/codey/f%20ile.html')).toString()),
'webview.contoso.com/commit/file///Users/codey/f%20ile.html',
'Unix with encoding'
);
assert.strictEqual(
stripEndpointUuid(webview.webview.asWebviewUri(URI.parse('file://localhost/Users/codey/file.html')).toString()),
'webview.contoso.com/commit/file//localhost/Users/codey/file.html',
'Unix should preserve authority'
);
assert.strictEqual(
stripEndpointUuid(webview.webview.asWebviewUri(URI.parse('file:///c:/codey/file.txt')).toString()),
'webview.contoso.com/commit/file///c%3A/codey/file.txt',
'Windows C drive'
);
});
});
function createNoopMainThreadWebviews() {
return new class extends mock<MainThreadWebviews>() {
$createWebviewPanel() { /* noop */ }
$registerSerializer() { /* noop */ }
$unregisterSerializer() { /* noop */ }
};
}

View File

@@ -1,781 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { CancellationToken } from 'vs/base/common/cancellation';
import { basename } from 'vs/base/common/path';
import { URI, UriComponents } from 'vs/base/common/uri';
import { ExtensionIdentifier, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { ILogService, NullLogService } from 'vs/platform/log/common/log';
import { IWorkspaceFolderData } from 'vs/platform/workspace/common/workspace';
import { MainThreadWorkspace } from 'vs/workbench/api/browser/mainThreadWorkspace';
import { IMainContext, IWorkspaceData, MainContext, ITextSearchComplete } from 'vs/workbench/api/common/extHost.protocol';
import { RelativePattern } from 'vs/workbench/api/common/extHostTypes';
import { ExtHostWorkspace } from 'vs/workbench/api/common/extHostWorkspace';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { TestRPCProtocol } from './testRPCProtocol';
import { ExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
import { ITextQueryBuilderOptions } from 'vs/workbench/contrib/search/common/queryBuilder';
import { IPatternInfo } from 'vs/workbench/services/search/common/search';
function createExtHostWorkspace(mainContext: IMainContext, data: IWorkspaceData, logService: ILogService): ExtHostWorkspace {
const result = new ExtHostWorkspace(
new ExtHostRpcService(mainContext),
new class extends mock<IExtHostInitDataService>() { workspace = data; },
logService
);
result.$initializeWorkspace(data);
return result;
}
suite('ExtHostWorkspace', function () {
const extensionDescriptor: IExtensionDescription = {
identifier: new ExtensionIdentifier('nullExtensionDescription'),
name: 'ext',
publisher: 'vscode',
enableProposedApi: false,
engines: undefined!,
extensionLocation: undefined!,
isBuiltin: false,
isUnderDevelopment: false,
version: undefined!
};
function assertAsRelativePath(workspace: ExtHostWorkspace, input: string, expected: string, includeWorkspace?: boolean) {
const actual = workspace.getRelativePath(input, includeWorkspace);
assert.equal(actual, expected);
}
test('asRelativePath', () => {
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/Applications/NewsWoWBot'), 0)], name: 'Test' }, new NullLogService());
assertAsRelativePath(ws, '/Coding/Applications/NewsWoWBot/bernd/das/brot', 'bernd/das/brot');
assertAsRelativePath(ws, '/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart',
'/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart');
assertAsRelativePath(ws, '', '');
assertAsRelativePath(ws, '/foo/bar', '/foo/bar');
assertAsRelativePath(ws, 'in/out', 'in/out');
});
test('asRelativePath, same paths, #11402', function () {
const root = '/home/aeschli/workspaces/samples/docker';
const input = '/home/aeschli/workspaces/samples/docker';
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
assertAsRelativePath(ws, input, input);
const input2 = '/home/aeschli/workspaces/samples/docker/a.file';
assertAsRelativePath(ws, input2, 'a.file');
});
test('asRelativePath, no workspace', function () {
const ws = createExtHostWorkspace(new TestRPCProtocol(), null!, new NullLogService());
assertAsRelativePath(ws, '', '');
assertAsRelativePath(ws, '/foo/bar', '/foo/bar');
});
test('asRelativePath, multiple folders', function () {
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
assertAsRelativePath(ws, '/Coding/One/file.txt', 'One/file.txt');
assertAsRelativePath(ws, '/Coding/Two/files/out.txt', 'Two/files/out.txt');
assertAsRelativePath(ws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
});
test('slightly inconsistent behaviour of asRelativePath and getWorkspaceFolder, #31553', function () {
const mrws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'One/file.txt');
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'One/file.txt', true);
assertAsRelativePath(mrws, '/Coding/One/file.txt', 'file.txt', false);
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'Two/files/out.txt');
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'Two/files/out.txt', true);
assertAsRelativePath(mrws, '/Coding/Two/files/out.txt', 'files/out.txt', false);
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', true);
assertAsRelativePath(mrws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', false);
const srws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0)], name: 'Test' }, new NullLogService());
assertAsRelativePath(srws, '/Coding/One/file.txt', 'file.txt');
assertAsRelativePath(srws, '/Coding/One/file.txt', 'file.txt', false);
assertAsRelativePath(srws, '/Coding/One/file.txt', 'One/file.txt', true);
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt');
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', true);
assertAsRelativePath(srws, '/Coding/Two2/files/out.txt', '/Coding/Two2/files/out.txt', false);
});
test('getPath, legacy', function () {
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
assert.equal(ws.getPath(), undefined);
ws = createExtHostWorkspace(new TestRPCProtocol(), null!, new NullLogService());
assert.equal(ws.getPath(), undefined);
ws = createExtHostWorkspace(new TestRPCProtocol(), undefined!, new NullLogService());
assert.equal(ws.getPath(), undefined);
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.file('Folder'), 0), aWorkspaceFolderData(URI.file('Another/Folder'), 1)] }, new NullLogService());
assert.equal(ws.getPath()!.replace(/\\/g, '/'), '/Folder');
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.file('/Folder'), 0)] }, new NullLogService());
assert.equal(ws.getPath()!.replace(/\\/g, '/'), '/Folder');
});
test('WorkspaceFolder has name and index', function () {
const ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', folders: [aWorkspaceFolderData(URI.file('/Coding/One'), 0), aWorkspaceFolderData(URI.file('/Coding/Two'), 1)], name: 'Test' }, new NullLogService());
const [one, two] = ws.getWorkspaceFolders()!;
assert.equal(one.name, 'One');
assert.equal(one.index, 0);
assert.equal(two.name, 'Two');
assert.equal(two.index, 1);
});
test('getContainingWorkspaceFolder', () => {
const ws = createExtHostWorkspace(new TestRPCProtocol(), {
id: 'foo',
name: 'Test',
folders: [
aWorkspaceFolderData(URI.file('/Coding/One'), 0),
aWorkspaceFolderData(URI.file('/Coding/Two'), 1),
aWorkspaceFolderData(URI.file('/Coding/Two/Nested'), 2)
]
}, new NullLogService());
let folder = ws.getWorkspaceFolder(URI.file('/foo/bar'));
assert.equal(folder, undefined);
folder = ws.getWorkspaceFolder(URI.file('/Coding/One/file/path.txt'))!;
assert.equal(folder.name, 'One');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/file/path.txt'))!;
assert.equal(folder.name, 'Two');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nest'))!;
assert.equal(folder.name, 'Two');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/file'))!;
assert.equal(folder.name, 'Nested');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/f'))!;
assert.equal(folder.name, 'Nested');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested'), true)!;
assert.equal(folder.name, 'Two');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/'), true)!;
assert.equal(folder.name, 'Two');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested'))!;
assert.equal(folder.name, 'Nested');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two/Nested/'))!;
assert.equal(folder.name, 'Nested');
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two'), true)!;
assert.equal(folder, undefined);
folder = ws.getWorkspaceFolder(URI.file('/Coding/Two'), false)!;
assert.equal(folder.name, 'Two');
});
test('Multiroot change event should have a delta, #29641', function (done) {
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
let finished = false;
const finish = (error?: any) => {
if (!finished) {
finished = true;
done(error);
}
};
let sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.added, []);
assert.deepEqual(e.removed, []);
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [] });
sub.dispose();
sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.removed, []);
assert.equal(e.added.length, 1);
assert.equal(e.added[0].uri.toString(), 'foo:bar');
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] });
sub.dispose();
sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.removed, []);
assert.equal(e.added.length, 1);
assert.equal(e.added[0].uri.toString(), 'foo:bar2');
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar2'), 1)] });
sub.dispose();
sub = ws.onDidChangeWorkspace(e => {
try {
assert.equal(e.removed.length, 2);
assert.equal(e.removed[0].uri.toString(), 'foo:bar');
assert.equal(e.removed[1].uri.toString(), 'foo:bar2');
assert.equal(e.added.length, 1);
assert.equal(e.added[0].uri.toString(), 'foo:bar3');
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0)] });
sub.dispose();
finish();
});
test('Multiroot change keeps existing workspaces live', function () {
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }, new NullLogService());
let firstFolder = ws.getWorkspaceFolders()![0];
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar2'), 0), aWorkspaceFolderData(URI.parse('foo:bar'), 1, 'renamed')] });
assert.equal(ws.getWorkspaceFolders()![1], firstFolder);
assert.equal(firstFolder.index, 1);
assert.equal(firstFolder.name, 'renamed');
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar2'), 1), aWorkspaceFolderData(URI.parse('foo:bar'), 2)] });
assert.equal(ws.getWorkspaceFolders()![2], firstFolder);
assert.equal(firstFolder.index, 2);
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0)] });
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar'), 1)] });
assert.notEqual(firstFolder, ws.workspace!.folders[0]);
});
test('updateWorkspaceFolders - invalid arguments', function () {
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, null!, null!));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 0));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 1));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 1, 0));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, -1, 0));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, -1, -1));
ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }, new NullLogService());
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 1, 1));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2));
assert.equal(false, ws.updateWorkspaceFolders(extensionDescriptor, 0, 1, asUpdateWorkspaceFolderData(URI.parse('foo:bar'))));
});
test('updateWorkspaceFolders - valid arguments', function (done) {
let finished = false;
const finish = (error?: any) => {
if (!finished) {
finished = true;
done(error);
}
};
const protocol: IMainContext = {
getProxy: () => { return undefined!; },
set: () => { return undefined!; },
assertRegistered: () => { }
};
const ws = createExtHostWorkspace(protocol, { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
//
// Add one folder
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar'))));
assert.equal(1, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
const firstAddedFolder = ws.getWorkspaceFolders()![0];
let gotEvent = false;
let sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.removed, []);
assert.equal(e.added.length, 1);
assert.equal(e.added[0].uri.toString(), 'foo:bar');
assert.equal(e.added[0], firstAddedFolder); // verify object is still live
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0)] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
//
// Add two more folders
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 1, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar1')), asUpdateWorkspaceFolderData(URI.parse('foo:bar2'))));
assert.equal(3, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
assert.equal(ws.workspace!.folders[2].uri.toString(), URI.parse('foo:bar2').toString());
const secondAddedFolder = ws.getWorkspaceFolders()![1];
const thirdAddedFolder = ws.getWorkspaceFolders()![2];
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.removed, []);
assert.equal(e.added.length, 2);
assert.equal(e.added[0].uri.toString(), 'foo:bar1');
assert.equal(e.added[1].uri.toString(), 'foo:bar2');
assert.equal(e.added[0], secondAddedFolder);
assert.equal(e.added[1], thirdAddedFolder);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar1'), 1), aWorkspaceFolderData(URI.parse('foo:bar2'), 2)] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![2], thirdAddedFolder); // verify object is still live
//
// Remove one folder
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 2, 1));
assert.equal(2, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.added, []);
assert.equal(e.removed.length, 1);
assert.equal(e.removed[0], thirdAddedFolder);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0), aWorkspaceFolderData(URI.parse('foo:bar1'), 1)] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
//
// Rename folder
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar'), 'renamed 1'), asUpdateWorkspaceFolderData(URI.parse('foo:bar1'), 'renamed 2')));
assert.equal(2, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar1').toString());
assert.equal(ws.workspace!.folders[0].name, 'renamed 1');
assert.equal(ws.workspace!.folders[1].name, 'renamed 2');
assert.equal(ws.getWorkspaceFolders()![0].name, 'renamed 1');
assert.equal(ws.getWorkspaceFolders()![1].name, 'renamed 2');
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.deepEqual(e.added, []);
assert.equal(e.removed.length, []);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar'), 0, 'renamed 1'), aWorkspaceFolderData(URI.parse('foo:bar1'), 1, 'renamed 2')] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], firstAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], secondAddedFolder); // verify object is still live
assert.equal(ws.workspace!.folders[0].name, 'renamed 1');
assert.equal(ws.workspace!.folders[1].name, 'renamed 2');
assert.equal(ws.getWorkspaceFolders()![0].name, 'renamed 1');
assert.equal(ws.getWorkspaceFolders()![1].name, 'renamed 2');
//
// Add and remove folders
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar3')), asUpdateWorkspaceFolderData(URI.parse('foo:bar4'))));
assert.equal(2, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar3').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar4').toString());
const fourthAddedFolder = ws.getWorkspaceFolders()![0];
const fifthAddedFolder = ws.getWorkspaceFolders()![1];
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.equal(e.added.length, 2);
assert.equal(e.added[0], fourthAddedFolder);
assert.equal(e.added[1], fifthAddedFolder);
assert.equal(e.removed.length, 2);
assert.equal(e.removed[0], firstAddedFolder);
assert.equal(e.removed[1], secondAddedFolder);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar3'), 0), aWorkspaceFolderData(URI.parse('foo:bar4'), 1)] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], fourthAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], fifthAddedFolder); // verify object is still live
//
// Swap folders
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 0, 2, asUpdateWorkspaceFolderData(URI.parse('foo:bar4')), asUpdateWorkspaceFolderData(URI.parse('foo:bar3'))));
assert.equal(2, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar4').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar3').toString());
assert.equal(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.equal(e.added.length, 0);
assert.equal(e.removed.length, 0);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [aWorkspaceFolderData(URI.parse('foo:bar4'), 0), aWorkspaceFolderData(URI.parse('foo:bar3'), 1)] }); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
assert.equal(fifthAddedFolder.index, 0);
assert.equal(fourthAddedFolder.index, 1);
//
// Add one folder after the other without waiting for confirmation (not supported currently)
//
assert.equal(true, ws.updateWorkspaceFolders(extensionDescriptor, 2, 0, asUpdateWorkspaceFolderData(URI.parse('foo:bar5'))));
assert.equal(3, ws.workspace!.folders.length);
assert.equal(ws.workspace!.folders[0].uri.toString(), URI.parse('foo:bar4').toString());
assert.equal(ws.workspace!.folders[1].uri.toString(), URI.parse('foo:bar3').toString());
assert.equal(ws.workspace!.folders[2].uri.toString(), URI.parse('foo:bar5').toString());
const sixthAddedFolder = ws.getWorkspaceFolders()![2];
gotEvent = false;
sub = ws.onDidChangeWorkspace(e => {
try {
assert.equal(e.added.length, 1);
assert.equal(e.added[0], sixthAddedFolder);
gotEvent = true;
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({
id: 'foo', name: 'Test', folders: [
aWorkspaceFolderData(URI.parse('foo:bar4'), 0),
aWorkspaceFolderData(URI.parse('foo:bar3'), 1),
aWorkspaceFolderData(URI.parse('foo:bar5'), 2)
]
}); // simulate acknowledgement from main side
assert.equal(gotEvent, true);
sub.dispose();
assert.equal(ws.getWorkspaceFolders()![0], fifthAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![1], fourthAddedFolder); // verify object is still live
assert.equal(ws.getWorkspaceFolders()![2], sixthAddedFolder); // verify object is still live
finish();
});
test('Multiroot change event is immutable', function (done) {
let finished = false;
const finish = (error?: any) => {
if (!finished) {
finished = true;
done(error);
}
};
let ws = createExtHostWorkspace(new TestRPCProtocol(), { id: 'foo', name: 'Test', folders: [] }, new NullLogService());
let sub = ws.onDidChangeWorkspace(e => {
try {
assert.throws(() => {
(<any>e).added = [];
});
// assert.throws(() => {
// (<any>e.added)[0] = null;
// });
} catch (error) {
finish(error);
}
});
ws.$acceptWorkspaceData({ id: 'foo', name: 'Test', folders: [] });
sub.dispose();
finish();
});
test('`vscode.workspace.getWorkspaceFolder(file)` don\'t return workspace folder when file open from command line. #36221', function () {
let ws = createExtHostWorkspace(new TestRPCProtocol(), {
id: 'foo', name: 'Test', folders: [
aWorkspaceFolderData(URI.file('c:/Users/marek/Desktop/vsc_test/'), 0)
]
}, new NullLogService());
assert.ok(ws.getWorkspaceFolder(URI.file('c:/Users/marek/Desktop/vsc_test/a.txt')));
assert.ok(ws.getWorkspaceFolder(URI.file('C:/Users/marek/Desktop/vsc_test/b.txt')));
});
function aWorkspaceFolderData(uri: URI, index: number, name: string = ''): IWorkspaceFolderData {
return {
uri,
index,
name: name || basename(uri.path)
};
}
function asUpdateWorkspaceFolderData(uri: URI, name?: string): { uri: URI, name?: string } {
return { uri, name };
}
test('findFiles - string include', () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
$startFileSearch(includePattern: string, _includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false, maxResults: number, token: CancellationToken): Promise<URI[] | null> {
mainThreadCalled = true;
assert.equal(includePattern, 'foo');
assert.equal(_includeFolder, null);
assert.equal(excludePatternOrDisregardExcludes, null);
assert.equal(maxResults, 10);
return Promise.resolve(null);
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
return ws.findFiles('foo', undefined, 10, new ExtensionIdentifier('test')).then(() => {
assert(mainThreadCalled, 'mainThreadCalled');
});
});
test('findFiles - RelativePattern include', () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
$startFileSearch(includePattern: string, _includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false, maxResults: number, token: CancellationToken): Promise<URI[] | null> {
mainThreadCalled = true;
assert.equal(includePattern, 'glob/**');
assert.deepEqual(_includeFolder, URI.file('/other/folder').toJSON());
assert.equal(excludePatternOrDisregardExcludes, null);
return Promise.resolve(null);
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
return ws.findFiles(new RelativePattern('/other/folder', 'glob/**'), undefined, 10, new ExtensionIdentifier('test')).then(() => {
assert(mainThreadCalled, 'mainThreadCalled');
});
});
test('findFiles - no excludes', () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
$startFileSearch(includePattern: string, _includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false, maxResults: number, token: CancellationToken): Promise<URI[] | null> {
mainThreadCalled = true;
assert.equal(includePattern, 'glob/**');
assert.deepEqual(_includeFolder, URI.file('/other/folder').toJSON());
assert.equal(excludePatternOrDisregardExcludes, false);
return Promise.resolve(null);
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
return ws.findFiles(new RelativePattern('/other/folder', 'glob/**'), null!, 10, new ExtensionIdentifier('test')).then(() => {
assert(mainThreadCalled, 'mainThreadCalled');
});
});
test('findFiles - with cancelled token', () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
$startFileSearch(includePattern: string, _includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false, maxResults: number, token: CancellationToken): Promise<URI[] | null> {
mainThreadCalled = true;
return Promise.resolve(null);
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
const token = CancellationToken.Cancelled;
return ws.findFiles(new RelativePattern('/other/folder', 'glob/**'), null!, 10, new ExtensionIdentifier('test'), token).then(() => {
assert(!mainThreadCalled, '!mainThreadCalled');
});
});
test('findFiles - RelativePattern exclude', () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
$startFileSearch(includePattern: string, _includeFolder: UriComponents | null, excludePatternOrDisregardExcludes: string | false, maxResults: number, token: CancellationToken): Promise<URI[] | null> {
mainThreadCalled = true;
assert(excludePatternOrDisregardExcludes, 'glob/**'); // Note that the base portion is ignored, see #52651
return Promise.resolve(null);
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
return ws.findFiles('', new RelativePattern(root, 'glob/**'), 10, new ExtensionIdentifier('test')).then(() => {
assert(mainThreadCalled, 'mainThreadCalled');
});
});
test('findTextInFiles - no include', async () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
mainThreadCalled = true;
assert.equal(query.pattern, 'foo');
assert.equal(folder, null);
assert.equal(options.includePattern, null);
assert.equal(options.excludePattern, null);
return null;
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
await ws.findTextInFiles({ pattern: 'foo' }, {}, () => { }, new ExtensionIdentifier('test'));
assert(mainThreadCalled, 'mainThreadCalled');
});
test('findTextInFiles - string include', async () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
mainThreadCalled = true;
assert.equal(query.pattern, 'foo');
assert.equal(folder, null);
assert.equal(options.includePattern, '**/files');
assert.equal(options.excludePattern, null);
return null;
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
await ws.findTextInFiles({ pattern: 'foo' }, { include: '**/files' }, () => { }, new ExtensionIdentifier('test'));
assert(mainThreadCalled, 'mainThreadCalled');
});
test('findTextInFiles - RelativePattern include', async () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
mainThreadCalled = true;
assert.equal(query.pattern, 'foo');
assert.deepEqual(folder, URI.file('/other/folder').toJSON());
assert.equal(options.includePattern, 'glob/**');
assert.equal(options.excludePattern, null);
return null;
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
await ws.findTextInFiles({ pattern: 'foo' }, { include: new RelativePattern('/other/folder', 'glob/**') }, () => { }, new ExtensionIdentifier('test'));
assert(mainThreadCalled, 'mainThreadCalled');
});
test('findTextInFiles - with cancelled token', async () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
mainThreadCalled = true;
return null;
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
const token = CancellationToken.Cancelled;
await ws.findTextInFiles({ pattern: 'foo' }, {}, () => { }, new ExtensionIdentifier('test'), token);
assert(!mainThreadCalled, '!mainThreadCalled');
});
test('findTextInFiles - RelativePattern exclude', async () => {
const root = '/project/foo';
const rpcProtocol = new TestRPCProtocol();
let mainThreadCalled = false;
rpcProtocol.set(MainContext.MainThreadWorkspace, new class extends mock<MainThreadWorkspace>() {
async $startTextSearch(query: IPatternInfo, folder: UriComponents | null, options: ITextQueryBuilderOptions, requestId: number, token: CancellationToken): Promise<ITextSearchComplete | null> {
mainThreadCalled = true;
assert.equal(query.pattern, 'foo');
assert.deepEqual(folder, null);
assert.equal(options.includePattern, null);
assert.equal(options.excludePattern, 'glob/**'); // exclude folder is ignored...
return null;
}
});
const ws = createExtHostWorkspace(rpcProtocol, { id: 'foo', folders: [aWorkspaceFolderData(URI.file(root), 0)], name: 'Test' }, new NullLogService());
await ws.findTextInFiles({ pattern: 'foo' }, { exclude: new RelativePattern('/other/folder', 'glob/**') }, () => { }, new ExtensionIdentifier('test'));
assert(mainThreadCalled, 'mainThreadCalled');
});
});

View File

@@ -1,87 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MainThreadCommands } from 'vs/workbench/api/browser/mainThreadCommands';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
suite('MainThreadCommands', function () {
test('dispose on unregister', function () {
const commands = new MainThreadCommands(SingleProxyRPCProtocol(null), undefined!, new class extends mock<IExtensionService>() { });
assert.equal(CommandsRegistry.getCommand('foo'), undefined);
// register
commands.$registerCommand('foo');
assert.ok(CommandsRegistry.getCommand('foo'));
// unregister
commands.$unregisterCommand('foo');
assert.equal(CommandsRegistry.getCommand('foo'), undefined);
});
test('unregister all on dispose', function () {
const commands = new MainThreadCommands(SingleProxyRPCProtocol(null), undefined!, new class extends mock<IExtensionService>() { });
assert.equal(CommandsRegistry.getCommand('foo'), undefined);
commands.$registerCommand('foo');
commands.$registerCommand('bar');
assert.ok(CommandsRegistry.getCommand('foo'));
assert.ok(CommandsRegistry.getCommand('bar'));
commands.dispose();
assert.equal(CommandsRegistry.getCommand('foo'), undefined);
assert.equal(CommandsRegistry.getCommand('bar'), undefined);
});
test('activate and throw when needed', async function () {
const activations: string[] = [];
const runs: string[] = [];
const commands = new MainThreadCommands(
SingleProxyRPCProtocol(null),
new class extends mock<ICommandService>() {
executeCommand<T>(id: string): Promise<T | undefined> {
runs.push(id);
return Promise.resolve(undefined);
}
},
new class extends mock<IExtensionService>() {
activateByEvent(id: string) {
activations.push(id);
return Promise.resolve();
}
}
);
// case 1: arguments and retry
try {
activations.length = 0;
await commands.$executeCommand('bazz', [1, 2, { n: 3 }], true);
assert.ok(false);
} catch (e) {
assert.deepEqual(activations, ['onCommand:bazz']);
assert.equal((<Error>e).message, '$executeCommand:retry');
}
// case 2: no arguments and retry
runs.length = 0;
await commands.$executeCommand('bazz', [], true);
assert.deepEqual(runs, ['bazz']);
// case 3: arguments and no retry
runs.length = 0;
await commands.$executeCommand('bazz', [1, 2, true], false);
assert.deepEqual(runs, ['bazz']);
});
});

View File

@@ -1,232 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import { URI } from 'vs/base/common/uri';
import { Registry } from 'vs/platform/registry/common/platform';
import { Extensions, IConfigurationRegistry, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
import { IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { MainThreadConfiguration } from 'vs/workbench/api/browser/mainThreadConfiguration';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configuration/common/configuration';
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
suite('MainThreadConfiguration', function () {
let proxy = {
$initializeConfiguration: () => { }
};
let instantiationService: TestInstantiationService;
let target: sinon.SinonSpy;
suiteSetup(() => {
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
'id': 'extHostConfiguration',
'title': 'a',
'type': 'object',
'properties': {
'extHostConfiguration.resource': {
'description': 'extHostConfiguration.resource',
'type': 'boolean',
'default': true,
'scope': ConfigurationScope.RESOURCE
},
'extHostConfiguration.window': {
'description': 'extHostConfiguration.resource',
'type': 'boolean',
'default': true,
'scope': ConfigurationScope.WINDOW
}
}
});
});
setup(() => {
target = sinon.spy();
instantiationService = new TestInstantiationService();
instantiationService.stub(IConfigurationService, WorkspaceService);
instantiationService.stub(IConfigurationService, 'onDidUpdateConfiguration', sinon.mock());
instantiationService.stub(IConfigurationService, 'onDidChangeConfiguration', sinon.mock());
instantiationService.stub(IConfigurationService, 'updateValue', target);
instantiationService.stub(IEnvironmentService, {
isBuilt: false
});
});
test('update resource configuration without configuration target defaults to workspace in multi root workspace when no resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.resource', 'value', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update resource configuration without configuration target defaults to workspace in folder workspace when resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.resource', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update resource configuration without configuration target defaults to workspace in folder workspace when no resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.resource', 'value', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update window configuration without configuration target defaults to workspace in multi root workspace when no resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.window', 'value', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update window configuration without configuration target defaults to workspace in multi root workspace when resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.window', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update window configuration without configuration target defaults to workspace in folder workspace when resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.window', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update window configuration without configuration target defaults to workspace in folder workspace when no resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.window', 'value', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update resource configuration without configuration target defaults to folder', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(null, 'extHostConfiguration.resource', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE_FOLDER, target.args[0][3]);
});
test('update configuration with user configuration target', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(ConfigurationTarget.USER, 'extHostConfiguration.window', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.USER, target.args[0][3]);
});
test('update configuration with workspace configuration target', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(ConfigurationTarget.WORKSPACE, 'extHostConfiguration.window', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('update configuration with folder configuration target', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$updateConfigurationOption(ConfigurationTarget.WORKSPACE_FOLDER, 'extHostConfiguration.window', 'value', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE_FOLDER, target.args[0][3]);
});
test('remove resource configuration without configuration target defaults to workspace in multi root workspace when no resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.resource', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove resource configuration without configuration target defaults to workspace in folder workspace when resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.resource', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove resource configuration without configuration target defaults to workspace in folder workspace when no resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.resource', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove window configuration without configuration target defaults to workspace in multi root workspace when no resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.window', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove window configuration without configuration target defaults to workspace in multi root workspace when resource is provided', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.window', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove window configuration without configuration target defaults to workspace in folder workspace when resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.window', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove window configuration without configuration target defaults to workspace in folder workspace when no resource is provider', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.FOLDER });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.window', undefined, undefined);
assert.equal(ConfigurationTarget.WORKSPACE, target.args[0][3]);
});
test('remove configuration without configuration target defaults to folder', function () {
instantiationService.stub(IWorkspaceContextService, <IWorkspaceContextService>{ getWorkbenchState: () => WorkbenchState.WORKSPACE });
const testObject: MainThreadConfiguration = instantiationService.createInstance(MainThreadConfiguration, SingleProxyRPCProtocol(proxy));
testObject.$removeConfigurationOption(null, 'extHostConfiguration.resource', { resource: URI.file('abc') }, undefined);
assert.equal(ConfigurationTarget.WORKSPACE_FOLDER, target.args[0][3]);
});
});

View File

@@ -1,49 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MarkerService } from 'vs/platform/markers/common/markerService';
import { MainThreadDiagnostics } from 'vs/workbench/api/browser/mainThreadDiagnostics';
import { URI } from 'vs/base/common/uri';
import { IExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
suite('MainThreadDiagnostics', function () {
let markerService: MarkerService;
setup(function () {
markerService = new MarkerService();
});
test('clear markers on dispose', function () {
let diag = new MainThreadDiagnostics(new class implements IExtHostContext {
remoteAuthority = '';
assertRegistered() { }
set(v: any): any { return null; }
getProxy(): any {
return {
$acceptMarkersChange() { }
};
}
}, markerService);
diag.$changeMany('foo', [[URI.file('a'), [{
code: '666',
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 1,
message: 'fffff',
severity: 1,
source: 'me'
}]]]);
assert.equal(markerService.read().length, 1);
diag.dispose();
assert.equal(markerService.read().length, 0);
});
});

View File

@@ -1,55 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { MainThreadDocumentContentProviders } from 'vs/workbench/api/browser/mainThreadDocumentContentProviders';
import { TextModel } from 'vs/editor/common/model/textModel';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
import { TestRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol';
import { TextEdit } from 'vs/editor/common/modes';
suite('MainThreadDocumentContentProviders', function () {
test('events are processed properly', function () {
let uri = URI.parse('test:uri');
let model = TextModel.createFromString('1', undefined, undefined, uri);
let providers = new MainThreadDocumentContentProviders(new TestRPCProtocol(), null!, null!,
new class extends mock<IModelService>() {
getModel(_uri: URI) {
assert.equal(uri.toString(), _uri.toString());
return model;
}
},
new class extends mock<IEditorWorkerService>() {
computeMoreMinimalEdits(_uri: URI, data: TextEdit[] | undefined) {
assert.equal(model.getValue(), '1');
return Promise.resolve(data);
}
},
);
return new Promise((resolve, reject) => {
let expectedEvents = 1;
model.onDidChangeContent(e => {
expectedEvents -= 1;
try {
assert.ok(expectedEvents >= 0);
} catch (err) {
reject(err);
}
if (model.getValue() === '1\n2\n3') {
resolve();
}
});
providers.$onVirtualDocumentChange(uri, '1\n2');
providers.$onVirtualDocumentChange(uri, '1\n2\n3');
});
});
});

View File

@@ -1,61 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { BoundModelReferenceCollection } from 'vs/workbench/api/browser/mainThreadDocuments';
import { TextModel } from 'vs/editor/common/model/textModel';
import { timeout } from 'vs/base/common/async';
suite('BoundModelReferenceCollection', () => {
let col = new BoundModelReferenceCollection(15, 75);
teardown(() => {
col.dispose();
});
test('max age', async () => {
let didDispose = false;
col.add({
object: <any>{ textEditorModel: TextModel.createFromString('farboo') },
dispose() {
didDispose = true;
}
});
await timeout(30);
assert.equal(didDispose, true);
});
test('max size', () => {
let disposed: number[] = [];
col.add({
object: <any>{ textEditorModel: TextModel.createFromString('farboo') },
dispose() {
disposed.push(0);
}
});
col.add({
object: <any>{ textEditorModel: TextModel.createFromString('boofar') },
dispose() {
disposed.push(1);
}
});
col.add({
object: <any>{ textEditorModel: TextModel.createFromString(new Array(71).join('x')) },
dispose() {
disposed.push(2);
}
});
assert.deepEqual(disposed, [0, 1]);
});
});

View File

@@ -1,203 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MainThreadDocumentsAndEditors } from 'vs/workbench/api/browser/mainThreadDocumentsAndEditors';
import { SingleProxyRPCProtocol } from './testRPCProtocol';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { TestCodeEditorService } from 'vs/editor/test/browser/editorTestServices';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ExtHostDocumentsAndEditorsShape, IDocumentsAndEditorsDelta } from 'vs/workbench/api/common/extHost.protocol';
import { createTestCodeEditor, TestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { TestEditorService, TestEditorGroupsService, TestTextResourcePropertiesService, TestEnvironmentService } from 'vs/workbench/test/workbenchTestServices';
import { Event } from 'vs/base/common/event';
import { ITextModel } from 'vs/editor/common/model';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IFileService } from 'vs/platform/files/common/files';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { NullLogService } from 'vs/platform/log/common/log';
suite('MainThreadDocumentsAndEditors', () => {
let modelService: ModelServiceImpl;
let codeEditorService: TestCodeEditorService;
let textFileService: ITextFileService;
let deltas: IDocumentsAndEditorsDelta[] = [];
const hugeModelString = new Array(2 + (50 * 1024 * 1024)).join('-');
function myCreateTestCodeEditor(model: ITextModel | undefined): TestCodeEditor {
return createTestCodeEditor({
model: model,
serviceCollection: new ServiceCollection(
[ICodeEditorService, codeEditorService]
)
});
}
setup(() => {
deltas.length = 0;
const configService = new TestConfigurationService();
configService.setUserConfiguration('editor', { 'detectIndentation': false });
modelService = new ModelServiceImpl(configService, new TestTextResourcePropertiesService(configService), new TestThemeService(), new NullLogService());
codeEditorService = new TestCodeEditorService();
textFileService = new class extends mock<ITextFileService>() {
isDirty() { return false; }
files = <any>{
onDidSave: Event.None,
onDidRevert: Event.None,
onDidChangeDirty: Event.None
};
};
const workbenchEditorService = new TestEditorService();
const editorGroupService = new TestEditorGroupsService();
const fileService = new class extends mock<IFileService>() {
onAfterOperation = Event.None;
};
new MainThreadDocumentsAndEditors(
SingleProxyRPCProtocol(new class extends mock<ExtHostDocumentsAndEditorsShape>() {
$acceptDocumentsAndEditorsDelta(delta: IDocumentsAndEditorsDelta) { deltas.push(delta); }
}),
modelService,
textFileService,
workbenchEditorService,
codeEditorService,
fileService,
null!,
editorGroupService,
null!,
new class extends mock<IPanelService>() implements IPanelService {
_serviceBrand: undefined;
onDidPanelOpen = Event.None;
onDidPanelClose = Event.None;
getActivePanel() {
return undefined;
}
},
TestEnvironmentService
);
});
test('Model#add', () => {
deltas.length = 0;
modelService.createModel('farboo', null);
assert.equal(deltas.length, 1);
const [delta] = deltas;
assert.equal(delta.addedDocuments!.length, 1);
assert.equal(delta.removedDocuments, undefined);
assert.equal(delta.addedEditors, undefined);
assert.equal(delta.removedEditors, undefined);
assert.equal(delta.newActiveEditor, null);
});
test('ignore huge model', function () {
this.timeout(1000 * 60); // increase timeout for this one test
const model = modelService.createModel(hugeModelString, null);
assert.ok(model.isTooLargeForSyncing());
assert.equal(deltas.length, 1);
const [delta] = deltas;
assert.equal(delta.newActiveEditor, null);
assert.equal(delta.addedDocuments, undefined);
assert.equal(delta.removedDocuments, undefined);
assert.equal(delta.addedEditors, undefined);
assert.equal(delta.removedEditors, undefined);
});
test('ignore simple widget model', function () {
this.timeout(1000 * 60); // increase timeout for this one test
const model = modelService.createModel('test', null, undefined, true);
assert.ok(model.isForSimpleWidget);
assert.equal(deltas.length, 1);
const [delta] = deltas;
assert.equal(delta.newActiveEditor, null);
assert.equal(delta.addedDocuments, undefined);
assert.equal(delta.removedDocuments, undefined);
assert.equal(delta.addedEditors, undefined);
assert.equal(delta.removedEditors, undefined);
});
test('ignore huge model from editor', function () {
this.timeout(1000 * 60); // increase timeout for this one test
const model = modelService.createModel(hugeModelString, null);
const editor = myCreateTestCodeEditor(model);
assert.equal(deltas.length, 1);
deltas.length = 0;
assert.equal(deltas.length, 0);
editor.dispose();
});
test('ignore editor w/o model', () => {
const editor = myCreateTestCodeEditor(undefined);
assert.equal(deltas.length, 1);
const [delta] = deltas;
assert.equal(delta.newActiveEditor, null);
assert.equal(delta.addedDocuments, undefined);
assert.equal(delta.removedDocuments, undefined);
assert.equal(delta.addedEditors, undefined);
assert.equal(delta.removedEditors, undefined);
editor.dispose();
});
test('editor with model', () => {
deltas.length = 0;
const model = modelService.createModel('farboo', null);
const editor = myCreateTestCodeEditor(model);
assert.equal(deltas.length, 2);
const [first, second] = deltas;
assert.equal(first.addedDocuments!.length, 1);
assert.equal(first.newActiveEditor, null);
assert.equal(first.removedDocuments, undefined);
assert.equal(first.addedEditors, undefined);
assert.equal(first.removedEditors, undefined);
assert.equal(second.addedEditors!.length, 1);
assert.equal(second.addedDocuments, undefined);
assert.equal(second.removedDocuments, undefined);
assert.equal(second.removedEditors, undefined);
assert.equal(second.newActiveEditor, undefined);
editor.dispose();
});
test('editor with dispos-ed/-ing model', () => {
modelService.createModel('foobar', null);
const model = modelService.createModel('farboo', null);
const editor = myCreateTestCodeEditor(model);
// ignore things until now
deltas.length = 0;
modelService.destroyModel(model.uri);
assert.equal(deltas.length, 1);
const [first] = deltas;
assert.equal(first.newActiveEditor, null);
assert.equal(first.removedEditors!.length, 1);
assert.equal(first.removedDocuments!.length, 1);
assert.equal(first.addedDocuments, undefined);
assert.equal(first.addedEditors, undefined);
editor.dispose();
});
});

View File

@@ -1,206 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { MainThreadDocumentsAndEditors } from 'vs/workbench/api/browser/mainThreadDocumentsAndEditors';
import { SingleProxyRPCProtocol, TestRPCProtocol } from './testRPCProtocol';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { TestCodeEditorService } from 'vs/editor/test/browser/editorTestServices';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ExtHostDocumentsAndEditorsShape, ExtHostContext, ExtHostDocumentsShape, IWorkspaceTextEditDto } from 'vs/workbench/api/common/extHost.protocol';
import { mock } from 'vs/workbench/test/electron-browser/api/mock';
import { Event } from 'vs/base/common/event';
import { MainThreadTextEditors } from 'vs/workbench/api/browser/mainThreadEditors';
import { URI } from 'vs/base/common/uri';
import { Range } from 'vs/editor/common/core/range';
import { Position } from 'vs/editor/common/core/position';
import { IModelService } from 'vs/editor/common/services/modelService';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { TestFileService, TestEditorService, TestEditorGroupsService, TestEnvironmentService, TestContextService, TestTextResourcePropertiesService } from 'vs/workbench/test/workbenchTestServices';
import { BulkEditService } from 'vs/workbench/services/bulkEdit/browser/bulkEditService';
import { NullLogService } from 'vs/platform/log/common/log';
import { ITextModelService, IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { IReference, ImmortalReference } from 'vs/base/common/lifecycle';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { LabelService } from 'vs/workbench/services/label/common/labelService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
suite('MainThreadEditors', () => {
const resource = URI.parse('foo:bar');
let modelService: IModelService;
let editors: MainThreadTextEditors;
const movedResources = new Map<URI, URI>();
const copiedResources = new Map<URI, URI>();
const createdResources = new Set<URI>();
const deletedResources = new Set<URI>();
setup(() => {
const configService = new TestConfigurationService();
modelService = new ModelServiceImpl(configService, new TestTextResourcePropertiesService(configService), new TestThemeService(), new NullLogService());
const codeEditorService = new TestCodeEditorService();
movedResources.clear();
copiedResources.clear();
createdResources.clear();
deletedResources.clear();
const fileService = new TestFileService();
const textFileService = new class extends mock<ITextFileService>() {
isDirty() { return false; }
create(uri: URI, contents?: string, options?: any) {
createdResources.add(uri);
return Promise.resolve(Object.create(null));
}
delete(resource: URI) {
deletedResources.add(resource);
return Promise.resolve(undefined);
}
move(source: URI, target: URI) {
movedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
copy(source: URI, target: URI) {
copiedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
files = <any>{
onDidSave: Event.None,
onDidRevert: Event.None,
onDidChangeDirty: Event.None
};
};
const workbenchEditorService = new TestEditorService();
const editorGroupService = new TestEditorGroupsService();
const textModelService = new class extends mock<ITextModelService>() {
createModelReference(resource: URI): Promise<IReference<IResolvedTextEditorModel>> {
const textEditorModel = new class extends mock<IResolvedTextEditorModel>() {
textEditorModel = modelService.getModel(resource)!;
};
textEditorModel.isReadonly = () => false;
return Promise.resolve(new ImmortalReference(textEditorModel));
}
};
const editorWorkerService = new class extends mock<IEditorWorkerService>() {
};
const bulkEditService = new BulkEditService(new NullLogService(), modelService, new TestEditorService(), editorWorkerService, textModelService, new TestFileService(), textFileService, new LabelService(TestEnvironmentService, new TestContextService()), configService);
const rpcProtocol = new TestRPCProtocol();
rpcProtocol.set(ExtHostContext.ExtHostDocuments, new class extends mock<ExtHostDocumentsShape>() {
$acceptModelChanged(): void {
}
});
rpcProtocol.set(ExtHostContext.ExtHostDocumentsAndEditors, new class extends mock<ExtHostDocumentsAndEditorsShape>() {
$acceptDocumentsAndEditorsDelta(): void {
}
});
const documentAndEditor = new MainThreadDocumentsAndEditors(
rpcProtocol,
modelService,
textFileService,
workbenchEditorService,
codeEditorService,
fileService,
null!,
editorGroupService,
bulkEditService,
new class extends mock<IPanelService>() implements IPanelService {
_serviceBrand: undefined;
onDidPanelOpen = Event.None;
onDidPanelClose = Event.None;
getActivePanel() {
return undefined;
}
},
TestEnvironmentService
);
editors = new MainThreadTextEditors(
documentAndEditor,
SingleProxyRPCProtocol(null),
codeEditorService,
bulkEditService,
workbenchEditorService,
editorGroupService,
);
});
test(`applyWorkspaceEdit returns false if model is changed by user`, () => {
let model = modelService.createModel('something', null, resource);
let workspaceResourceEdit: IWorkspaceTextEditDto = {
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
// Act as if the user edited the model
model.applyEdits([EditOperation.insert(new Position(0, 0), 'something')]);
return editors.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit] }).then((result) => {
assert.equal(result, false);
});
});
test(`issue #54773: applyWorkspaceEdit checks model version in race situation`, () => {
let model = modelService.createModel('something', null, resource);
let workspaceResourceEdit1: IWorkspaceTextEditDto = {
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
let workspaceResourceEdit2: IWorkspaceTextEditDto = {
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
let p1 = editors.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit1] }).then((result) => {
// first edit request succeeds
assert.equal(result, true);
});
let p2 = editors.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit2] }).then((result) => {
// second edit request fails
assert.equal(result, false);
});
return Promise.all([p1, p2]);
});
test(`applyWorkspaceEdit with only resource edit`, () => {
return editors.$tryApplyWorkspaceEdit({
edits: [
{ oldUri: resource, newUri: resource, options: undefined },
{ oldUri: undefined, newUri: resource, options: undefined },
{ oldUri: resource, newUri: undefined, options: undefined }
]
}).then((result) => {
assert.equal(result, true);
assert.equal(movedResources.get(resource), resource);
assert.equal(createdResources.has(resource), true);
assert.equal(deletedResources.has(resource), true);
});
});
});

View File

@@ -7,7 +7,7 @@ import * as assert from 'assert';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { FinalNewLineParticipant, TrimFinalNewLinesParticipant } from 'vs/workbench/api/browser/mainThreadSaveParticipant';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { workbenchInstantiationService, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
import { workbenchInstantiationService, TestTextFileService } from 'vs/workbench/test/electron-browser/workbenchTestServices';
import { toResource } from 'vs/base/test/common/utils';
import { IModelService } from 'vs/editor/common/services/modelService';
import { Range } from 'vs/editor/common/core/range';
@@ -34,7 +34,6 @@ suite('MainThreadSaveParticipant', function () {
teardown(() => {
(<TextFileEditorModelManager>accessor.textFileService.files).dispose();
TextFileEditorModel.setSaveParticipant(null); // reset any set participant
});
test('insert final new line', async function () {

View File

@@ -3,12 +3,12 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { workbenchInstantiationService } from 'vs/workbench/test/workbenchTestServices';
import { workbenchInstantiationService } from 'vs/workbench/test/electron-browser/workbenchTestServices';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { ISearchService, IFileQuery } from 'vs/workbench/services/search/common/search';
import { MainThreadWorkspace } from 'vs/workbench/api/browser/mainThreadWorkspace';
import * as assert from 'assert';
import { SingleProxyRPCProtocol } from 'vs/workbench/test/electron-browser/api/testRPCProtocol';
import { SingleProxyRPCProtocol } from 'vs/workbench/test/browser/api/testRPCProtocol';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';

View File

@@ -1,12 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export interface Ctor<T> {
new(): T;
}
export function mock<T>(): Ctor<T> {
return function () { } as any;
}

View File

@@ -1,138 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ProxyIdentifier } from 'vs/workbench/services/extensions/common/proxyIdentifier';
import { CharCode } from 'vs/base/common/charCode';
import { IExtHostContext } from 'vs/workbench/api/common/extHost.protocol';
import { isThenable } from 'vs/base/common/async';
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
export function SingleProxyRPCProtocol(thing: any): IExtHostContext & IExtHostRpcService {
return {
_serviceBrand: undefined,
remoteAuthority: null!,
getProxy<T>(): T {
return thing;
},
set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
return value;
},
assertRegistered: undefined!
};
}
export class TestRPCProtocol implements IExtHostContext, IExtHostRpcService {
public _serviceBrand: undefined;
public remoteAuthority = null!;
private _callCountValue: number = 0;
private _idle?: Promise<any>;
private _completeIdle?: Function;
private readonly _locals: { [id: string]: any; };
private readonly _proxies: { [id: string]: any; };
constructor() {
this._locals = Object.create(null);
this._proxies = Object.create(null);
}
private get _callCount(): number {
return this._callCountValue;
}
private set _callCount(value: number) {
this._callCountValue = value;
if (this._callCountValue === 0) {
if (this._completeIdle) {
this._completeIdle();
}
this._idle = undefined;
}
}
sync(): Promise<any> {
return new Promise<any>((c) => {
setTimeout(c, 0);
}).then(() => {
if (this._callCount === 0) {
return undefined;
}
if (!this._idle) {
this._idle = new Promise<any>((c, e) => {
this._completeIdle = c;
});
}
return this._idle;
});
}
public getProxy<T>(identifier: ProxyIdentifier<T>): T {
if (!this._proxies[identifier.sid]) {
this._proxies[identifier.sid] = this._createProxy(identifier.sid);
}
return this._proxies[identifier.sid];
}
private _createProxy<T>(proxyId: string): T {
let handler = {
get: (target: any, name: string) => {
if (!target[name] && name.charCodeAt(0) === CharCode.DollarSign) {
target[name] = (...myArgs: any[]) => {
return this._remoteCall(proxyId, name, myArgs);
};
}
return target[name];
}
};
return new Proxy(Object.create(null), handler);
}
public set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
this._locals[identifier.sid] = value;
return value;
}
protected _remoteCall(proxyId: string, path: string, args: any[]): Promise<any> {
this._callCount++;
return new Promise<any>((c) => {
setTimeout(c, 0);
}).then(() => {
const instance = this._locals[proxyId];
// pretend the args went over the wire... (invoke .toJSON on objects...)
const wireArgs = simulateWireTransfer(args);
let p: Promise<any>;
try {
let result = (<Function>instance[path]).apply(instance, wireArgs);
p = isThenable(result) ? result : Promise.resolve(result);
} catch (err) {
p = Promise.reject(err);
}
return p.then(result => {
this._callCount--;
// pretend the result went over the wire... (invoke .toJSON on objects...)
const wireResult = simulateWireTransfer(result);
return wireResult;
}, err => {
this._callCount--;
return Promise.reject(err);
});
});
}
public assertRegistered(identifiers: ProxyIdentifier<any>[]): void {
throw new Error('Not implemented!');
}
}
function simulateWireTransfer<T>(obj: T): T {
if (!obj) {
return obj;
}
return JSON.parse(JSON.stringify(obj));
}

View File

@@ -28,7 +28,8 @@ import { IEditorService } from 'vs/workbench/services/editor/common/editorServic
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { LocalSearchService } from 'vs/workbench/services/search/node/searchService';
import { IUntitledTextEditorService, UntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { TestContextService, TestEditorGroupsService, TestEditorService, TestEnvironmentService, TestTextResourcePropertiesService } from 'vs/workbench/test/workbenchTestServices';
import { TestContextService, TestEditorGroupsService, TestEditorService, TestTextResourcePropertiesService } from 'vs/workbench/test/browser/workbenchTestServices';
import { TestEnvironmentService } from 'vs/workbench/test/electron-browser/workbenchTestServices';
import { ClassifiedEvent, StrictPropertyCheck, GDPRClassification } from 'vs/platform/telemetry/common/gdprTypings';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { NullLogService } from 'vs/platform/log/common/log';

View File

@@ -17,7 +17,8 @@ import * as minimist from 'vscode-minimist';
import * as path from 'vs/base/common/path';
import { LocalSearchService } from 'vs/workbench/services/search/node/searchService';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { TestEnvironmentService, TestContextService, TestEditorService, TestEditorGroupsService, TestTextResourcePropertiesService } from 'vs/workbench/test/workbenchTestServices';
import { TestContextService, TestEditorService, TestEditorGroupsService, TestTextResourcePropertiesService } from 'vs/workbench/test/browser/workbenchTestServices';
import { TestEnvironmentService } from 'vs/workbench/test/electron-browser/workbenchTestServices';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { URI } from 'vs/base/common/uri';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';

View File

@@ -0,0 +1,189 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { workbenchInstantiationService as browserWorkbenchInstantiationService, ITestInstantiationService } from 'vs/workbench/test/browser/workbenchTestServices';
import { Event } from 'vs/base/common/event';
import { ISharedProcessService } from 'vs/platform/ipc/electron-browser/sharedProcessService';
import { NativeWorkbenchEnvironmentService } from 'vs/workbench/services/environment/electron-browser/environmentService';
import { NativeTextFileService } from 'vs/workbench/services/textfile/electron-browser/nativeTextFileService';
import { IElectronService } from 'vs/platform/electron/node/electron';
import { INativeOpenDialogOptions } from 'vs/platform/dialogs/node/dialogs';
import { FileOperationError, IFileService } from 'vs/platform/files/common/files';
import { IUntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IDialogService, IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfigurationService';
import { IProductService } from 'vs/platform/product/common/productService';
import { IFilesConfigurationService } from 'vs/workbench/services/filesConfiguration/common/filesConfigurationService';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { URI } from 'vs/base/common/uri';
import { IReadTextFileOptions, ITextFileStreamContent, ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { createTextBufferFactoryFromStream } from 'vs/editor/common/model/textModel';
import { IOpenedWindow, IOpenEmptyWindowOptions, IWindowOpenable, IOpenWindowOptions, IWindowConfiguration } from 'vs/platform/windows/common/windows';
import { parseArgs, OPTIONS } from 'vs/platform/environment/node/argv';
import { LogLevel } from 'vs/platform/log/common/log';
import { IRemotePathService } from 'vs/workbench/services/path/common/remotePathService';
export const TestWindowConfiguration: IWindowConfiguration = {
windowId: 0,
sessionId: 'testSessionId',
logLevel: LogLevel.Error,
mainPid: 0,
appRoot: '',
userEnv: {},
execPath: process.execPath,
perfEntries: [],
...parseArgs(process.argv, OPTIONS)
};
export const TestEnvironmentService = new NativeWorkbenchEnvironmentService(TestWindowConfiguration, process.execPath, 0);
export class TestTextFileService extends NativeTextFileService {
private resolveTextContentError!: FileOperationError | null;
constructor(
@IFileService protected fileService: IFileService,
@IUntitledTextEditorService untitledTextEditorService: IUntitledTextEditorService,
@ILifecycleService lifecycleService: ILifecycleService,
@IInstantiationService instantiationService: IInstantiationService,
@IModelService modelService: IModelService,
@IWorkbenchEnvironmentService environmentService: IWorkbenchEnvironmentService,
@IDialogService dialogService: IDialogService,
@IFileDialogService fileDialogService: IFileDialogService,
@ITextResourceConfigurationService textResourceConfigurationService: ITextResourceConfigurationService,
@IProductService productService: IProductService,
@IFilesConfigurationService filesConfigurationService: IFilesConfigurationService,
@ITextModelService textModelService: ITextModelService,
@ICodeEditorService codeEditorService: ICodeEditorService,
@INotificationService notificationService: INotificationService,
@IRemotePathService remotePathService: IRemotePathService
) {
super(
fileService,
untitledTextEditorService,
lifecycleService,
instantiationService,
modelService,
environmentService,
dialogService,
fileDialogService,
textResourceConfigurationService,
productService,
filesConfigurationService,
textModelService,
codeEditorService,
notificationService,
remotePathService
);
}
setResolveTextContentErrorOnce(error: FileOperationError): void {
this.resolveTextContentError = error;
}
async readStream(resource: URI, options?: IReadTextFileOptions): Promise<ITextFileStreamContent> {
if (this.resolveTextContentError) {
const error = this.resolveTextContentError;
this.resolveTextContentError = null;
throw error;
}
const content = await this.fileService.readFileStream(resource, options);
return {
resource: content.resource,
name: content.name,
mtime: content.mtime,
ctime: content.ctime,
etag: content.etag,
encoding: 'utf8',
value: await createTextBufferFactoryFromStream(content.value),
size: 10
};
}
}
export class TestSharedProcessService implements ISharedProcessService {
_serviceBrand: undefined;
getChannel(channelName: string): any { return undefined; }
registerChannel(channelName: string, channel: any): void { }
async toggleSharedProcessWindow(): Promise<void> { }
async whenSharedProcessReady(): Promise<void> { }
}
export class TestElectronService implements IElectronService {
_serviceBrand: undefined;
onWindowOpen: Event<number> = Event.None;
onWindowMaximize: Event<number> = Event.None;
onWindowUnmaximize: Event<number> = Event.None;
onWindowFocus: Event<number> = Event.None;
onWindowBlur: Event<number> = Event.None;
windowCount = Promise.resolve(1);
getWindowCount(): Promise<number> { return this.windowCount; }
async getWindows(): Promise<IOpenedWindow[]> { return []; }
async getActiveWindowId(): Promise<number | undefined> { return undefined; }
openWindow(options?: IOpenEmptyWindowOptions): Promise<void>;
openWindow(toOpen: IWindowOpenable[], options?: IOpenWindowOptions): Promise<void>;
openWindow(arg1?: IOpenEmptyWindowOptions | IWindowOpenable[], arg2?: IOpenWindowOptions): Promise<void> {
throw new Error('Method not implemented.');
}
async toggleFullScreen(): Promise<void> { }
async handleTitleDoubleClick(): Promise<void> { }
async isMaximized(): Promise<boolean> { return true; }
async maximizeWindow(): Promise<void> { }
async unmaximizeWindow(): Promise<void> { }
async minimizeWindow(): Promise<void> { }
async focusWindow(options?: { windowId?: number | undefined; } | undefined): Promise<void> { }
async showMessageBox(options: Electron.MessageBoxOptions): Promise<Electron.MessageBoxReturnValue> { throw new Error('Method not implemented.'); }
async showSaveDialog(options: Electron.SaveDialogOptions): Promise<Electron.SaveDialogReturnValue> { throw new Error('Method not implemented.'); }
async showOpenDialog(options: Electron.OpenDialogOptions): Promise<Electron.OpenDialogReturnValue> { throw new Error('Method not implemented.'); }
async pickFileFolderAndOpen(options: INativeOpenDialogOptions): Promise<void> { }
async pickFileAndOpen(options: INativeOpenDialogOptions): Promise<void> { }
async pickFolderAndOpen(options: INativeOpenDialogOptions): Promise<void> { }
async pickWorkspaceAndOpen(options: INativeOpenDialogOptions): Promise<void> { }
async showItemInFolder(path: string): Promise<void> { }
async setRepresentedFilename(path: string): Promise<void> { }
async setDocumentEdited(edited: boolean): Promise<void> { }
async openExternal(url: string): Promise<boolean> { return false; }
async updateTouchBar(): Promise<void> { }
async newWindowTab(): Promise<void> { }
async showPreviousWindowTab(): Promise<void> { }
async showNextWindowTab(): Promise<void> { }
async moveWindowTabToNewWindow(): Promise<void> { }
async mergeAllWindowTabs(): Promise<void> { }
async toggleWindowTabsBar(): Promise<void> { }
async relaunch(options?: { addArgs?: string[] | undefined; removeArgs?: string[] | undefined; } | undefined): Promise<void> { }
async reload(): Promise<void> { }
async closeWindow(): Promise<void> { }
async quit(): Promise<void> { }
async openDevTools(options?: Electron.OpenDevToolsOptions | undefined): Promise<void> { }
async toggleDevTools(): Promise<void> { }
async startCrashReporter(options: Electron.CrashReporterStartOptions): Promise<void> { }
async resolveProxy(url: string): Promise<string | undefined> { return undefined; }
}
export function workbenchInstantiationService(): ITestInstantiationService {
const instantiationService = browserWorkbenchInstantiationService({
textFileService: insta => <ITextFileService>insta.createInstance(TestTextFileService)
});
instantiationService.stub(IElectronService, new TestElectronService());
return instantiationService;
}