提交 263a9551 编写于 作者: J Johannes Rieken

debt - add toJSON methods to ext host types

上级 f8026782
......@@ -148,6 +148,10 @@ export class Position {
}
return new Position(line, character);
}
toJSON(): any {
return [this.line, this.character];
}
}
export class Range {
......@@ -248,6 +252,10 @@ export class Range {
}
return new Range(start, end);
}
toJSON(): any {
return [this.start, this.end];
}
}
export class Selection extends Range {
......@@ -291,6 +299,15 @@ export class Selection extends Range {
get isReversed(): boolean {
return this._anchor === this._end;
}
toJSON() {
return {
start: this.start,
end: this.end,
active: this.active,
anchor: this.anchor
}
}
}
export class TextEdit {
......@@ -334,6 +351,13 @@ export class TextEdit {
this.range = range;
this.newText = newText;
}
toJSON(): any {
return {
range: this.range,
newText: this.newText
};
}
}
export class Uri extends URI { };
......@@ -387,6 +411,10 @@ export class WorkspaceEdit {
get size(): number {
return this._values.length;
}
toJSON(): any {
return this._values;
}
}
export enum DiagnosticSeverity {
......@@ -412,6 +440,13 @@ export class Location {
throw new Error('Illegal argument');
}
}
toJSON(): any {
return {
uri: this.uri,
range: this.range
};
}
}
export class Diagnostic {
......@@ -427,6 +462,16 @@ export class Diagnostic {
this.message = message;
this.severity = severity;
}
toJSON(): any {
return {
severity: DiagnosticSeverity[this.severity],
message: this.message,
range: this.range,
source: this.source,
code: this.code,
}
}
}
export class Hover {
......@@ -463,6 +508,13 @@ export class DocumentHighlight {
this.range = range;
this.kind = kind;
}
toJSON(): any {
return {
range: this.range,
kind: DocumentHighlightKind[this.kind]
}
}
}
export enum SymbolKind {
......@@ -499,6 +551,15 @@ export class SymbolInformation {
this.location = new Location(uri, range);
this.containerName = containerName;
}
toJSON(): any {
return {
name: this.name,
kind: SymbolKind[this.kind],
location: this.location,
containerName: this.containerName
}
}
}
export class CodeLens {
......@@ -587,6 +648,19 @@ export class CompletionItem {
constructor(label: string) {
this.label = label;
}
toJSON(): any {
return {
label: this.label,
kind: CompletionItemKind[this.kind],
detail: this.detail,
documentation: this.documentation,
sortText: this.sortText,
filterText: this.filterText,
insertText: this.insertText,
textEdit: this.textEdit
}
}
}
export enum ViewColumn {
......
......@@ -9,7 +9,13 @@ import * as assert from 'assert';
import URI from 'vs/base/common/uri';
import * as types from 'vs/workbench/api/common/extHostTypes';
suite('PluginHostTypes', function() {
function assertToJSON(a: any, expected: any) {
const raw = JSON.stringify(a);
const actual = JSON.parse(raw);
assert.deepEqual(actual, expected);
}
suite('ExtHostTypes', function() {
test('Disposable', function() {
......@@ -47,6 +53,14 @@ suite('PluginHostTypes', function() {
assert.throws(() => pos.character = -1);
assert.throws(() => pos.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, [4, 2])
});
test('Position, isBefore(OrEqual)?', function() {
......@@ -133,6 +147,12 @@ suite('PluginHostTypes', function() {
assert.throws(() => range.start = new types.Position(0, 3));
});
test('Range, toJSON', function() {
let range = new types.Range(1, 2, 3, 4);
assertToJSON(range, [[1, 2], [3, 4]]);
});
test('Range, sorting', function() {
// sorts start/end
let range = new types.Range(1, 0, 0, 0);
......@@ -249,6 +269,7 @@ suite('PluginHostTypes', function() {
let range = new types.Range(1, 1, 2, 11);
let edit = new types.TextEdit(range, undefined);
assert.equal(edit.newText, '');
assertToJSON(edit, { range: [[1, 1], [2, 11]], newText: '' });
edit = new types.TextEdit(range, null);
assert.equal(edit.newText, '');
......@@ -268,11 +289,16 @@ suite('PluginHostTypes', function() {
edit.set(a, [types.TextEdit.insert(new types.Position(0, 0), 'fff')]);
assert.ok(edit.has(a));
assert.equal(edit.size, 1);
assertToJSON(edit, [['file://a.ts', [{ range: [[0, 0], [0, 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, [
['file://a.ts', [{ range: [[0, 0], [0, 0]], newText: 'fff' }]],
['file://b.ts', [{ range: [[1, 1], [1, 1]], newText: 'fff' }, { range: [[0, 0], [0, 0]], newText: '' }]]
]);
edit.set(b, undefined);
assert.ok(edit.has(b));
......@@ -280,5 +306,43 @@ suite('PluginHostTypes', function() {
edit.set(b, [types.TextEdit.insert(new types.Position(0, 0), 'ffff')]);
assert.equal(edit.get(b).length, 1);
});
test('toJSON & stringify', function() {
assertToJSON(new types.Selection(3, 4, 2, 1), { start: [2, 1], end: [3, 4], anchor: [3, 4], active: [2, 1] });
assertToJSON(new types.Location(types.Uri.file('u.ts'), new types.Range(1, 2, 3, 4)), { uri: 'file://u.ts', range: [[1, 2], [3, 4]] });
assertToJSON(new types.Location(types.Uri.file('u.ts'), new types.Position(3, 4)), { uri: 'file://u.ts', range: [[3, 4], [3, 4]] });
let diag = new types.Diagnostic(new types.Range(0, 1, 2, 3), 'hello');
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [[0, 1], [2, 3]] });
diag.source = 'me'
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [[0, 1], [2, 3]], source: 'me' });
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5)), { range: [[2, 3], [4, 5]], kind: 'Text' });
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5), types.DocumentHighlightKind.Read), { range: [[2, 3], [4, 5]], kind: 'Read' });
assertToJSON(new types.SymbolInformation('test', types.SymbolKind.Boolean, new types.Range(0, 1, 2, 3)), {
name: 'test',
kind: 'Boolean',
location: {
range: [[0, 1], [2, 3]]
}
});
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10)), { range: [[7, 8], [9, 10]] });
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10), { command: 'id', title: 'title' }), {
range: [[7, 8], [9, 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' });
});
});
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册