Skip to content

Commit 263a955

Browse files
committed
debt - add toJSON methods to ext host types
1 parent f802678 commit 263a955

2 files changed

Lines changed: 139 additions & 1 deletion

File tree

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ export class Position {
148148
}
149149
return new Position(line, character);
150150
}
151+
152+
toJSON(): any {
153+
return [this.line, this.character];
154+
}
151155
}
152156

153157
export class Range {
@@ -248,6 +252,10 @@ export class Range {
248252
}
249253
return new Range(start, end);
250254
}
255+
256+
toJSON(): any {
257+
return [this.start, this.end];
258+
}
251259
}
252260

253261
export class Selection extends Range {
@@ -291,6 +299,15 @@ export class Selection extends Range {
291299
get isReversed(): boolean {
292300
return this._anchor === this._end;
293301
}
302+
303+
toJSON() {
304+
return {
305+
start: this.start,
306+
end: this.end,
307+
active: this.active,
308+
anchor: this.anchor
309+
}
310+
}
294311
}
295312

296313
export class TextEdit {
@@ -334,6 +351,13 @@ export class TextEdit {
334351
this.range = range;
335352
this.newText = newText;
336353
}
354+
355+
toJSON(): any {
356+
return {
357+
range: this.range,
358+
newText: this.newText
359+
};
360+
}
337361
}
338362

339363
export class Uri extends URI { };
@@ -387,6 +411,10 @@ export class WorkspaceEdit {
387411
get size(): number {
388412
return this._values.length;
389413
}
414+
415+
toJSON(): any {
416+
return this._values;
417+
}
390418
}
391419

392420
export enum DiagnosticSeverity {
@@ -412,6 +440,13 @@ export class Location {
412440
throw new Error('Illegal argument');
413441
}
414442
}
443+
444+
toJSON(): any {
445+
return {
446+
uri: this.uri,
447+
range: this.range
448+
};
449+
}
415450
}
416451

417452
export class Diagnostic {
@@ -427,6 +462,16 @@ export class Diagnostic {
427462
this.message = message;
428463
this.severity = severity;
429464
}
465+
466+
toJSON(): any {
467+
return {
468+
severity: DiagnosticSeverity[this.severity],
469+
message: this.message,
470+
range: this.range,
471+
source: this.source,
472+
code: this.code,
473+
}
474+
}
430475
}
431476

432477
export class Hover {
@@ -463,6 +508,13 @@ export class DocumentHighlight {
463508
this.range = range;
464509
this.kind = kind;
465510
}
511+
512+
toJSON(): any {
513+
return {
514+
range: this.range,
515+
kind: DocumentHighlightKind[this.kind]
516+
}
517+
}
466518
}
467519

468520
export enum SymbolKind {
@@ -499,6 +551,15 @@ export class SymbolInformation {
499551
this.location = new Location(uri, range);
500552
this.containerName = containerName;
501553
}
554+
555+
toJSON(): any {
556+
return {
557+
name: this.name,
558+
kind: SymbolKind[this.kind],
559+
location: this.location,
560+
containerName: this.containerName
561+
}
562+
}
502563
}
503564

504565
export class CodeLens {
@@ -587,6 +648,19 @@ export class CompletionItem {
587648
constructor(label: string) {
588649
this.label = label;
589650
}
651+
652+
toJSON(): any {
653+
return {
654+
label: this.label,
655+
kind: CompletionItemKind[this.kind],
656+
detail: this.detail,
657+
documentation: this.documentation,
658+
sortText: this.sortText,
659+
filterText: this.filterText,
660+
insertText: this.insertText,
661+
textEdit: this.textEdit
662+
}
663+
}
590664
}
591665

592666
export enum ViewColumn {

src/vs/workbench/test/common/api/extHostTypes.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import * as assert from 'assert';
99
import URI from 'vs/base/common/uri';
1010
import * as types from 'vs/workbench/api/common/extHostTypes';
1111

12-
suite('PluginHostTypes', function() {
12+
function assertToJSON(a: any, expected: any) {
13+
const raw = JSON.stringify(a);
14+
const actual = JSON.parse(raw);
15+
assert.deepEqual(actual, expected);
16+
}
17+
18+
suite('ExtHostTypes', function() {
1319

1420
test('Disposable', function() {
1521

@@ -47,6 +53,14 @@ suite('PluginHostTypes', function() {
4753
assert.throws(() => pos.character = -1);
4854
assert.throws(() => pos.line = 12);
4955

56+
let [line, character] = pos.toJSON();
57+
assert.equal(line, 0);
58+
assert.equal(character, 0);
59+
});
60+
61+
test('Position, toJSON', function() {
62+
let pos = new types.Position(4, 2);
63+
assertToJSON(pos, [4, 2])
5064
});
5165

5266
test('Position, isBefore(OrEqual)?', function() {
@@ -133,6 +147,12 @@ suite('PluginHostTypes', function() {
133147
assert.throws(() => range.start = new types.Position(0, 3));
134148
});
135149

150+
test('Range, toJSON', function() {
151+
152+
let range = new types.Range(1, 2, 3, 4);
153+
assertToJSON(range, [[1, 2], [3, 4]]);
154+
});
155+
136156
test('Range, sorting', function() {
137157
// sorts start/end
138158
let range = new types.Range(1, 0, 0, 0);
@@ -249,6 +269,7 @@ suite('PluginHostTypes', function() {
249269
let range = new types.Range(1, 1, 2, 11);
250270
let edit = new types.TextEdit(range, undefined);
251271
assert.equal(edit.newText, '');
272+
assertToJSON(edit, { range: [[1, 1], [2, 11]], newText: '' });
252273

253274
edit = new types.TextEdit(range, null);
254275
assert.equal(edit.newText, '');
@@ -268,17 +289,60 @@ suite('PluginHostTypes', function() {
268289
edit.set(a, [types.TextEdit.insert(new types.Position(0, 0), 'fff')]);
269290
assert.ok(edit.has(a));
270291
assert.equal(edit.size, 1);
292+
assertToJSON(edit, [['file://a.ts', [{ range: [[0, 0], [0, 0]], newText: 'fff' }]]]);
271293

272294
edit.insert(b, new types.Position(1, 1), 'fff');
273295
edit.delete(b, new types.Range(0, 0, 0, 0));
274296
assert.ok(edit.has(b));
275297
assert.equal(edit.size, 2);
298+
assertToJSON(edit, [
299+
['file://a.ts', [{ range: [[0, 0], [0, 0]], newText: 'fff' }]],
300+
['file://b.ts', [{ range: [[1, 1], [1, 1]], newText: 'fff' }, { range: [[0, 0], [0, 0]], newText: '' }]]
301+
]);
276302

277303
edit.set(b, undefined);
278304
assert.ok(edit.has(b));
279305
assert.equal(edit.size, 2);
280306

281307
edit.set(b, [types.TextEdit.insert(new types.Position(0, 0), 'ffff')]);
282308
assert.equal(edit.get(b).length, 1);
309+
310+
});
311+
312+
test('toJSON & stringify', function() {
313+
314+
assertToJSON(new types.Selection(3, 4, 2, 1), { start: [2, 1], end: [3, 4], anchor: [3, 4], active: [2, 1] });
315+
316+
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]] });
317+
assertToJSON(new types.Location(types.Uri.file('u.ts'), new types.Position(3, 4)), { uri: 'file://u.ts', range: [[3, 4], [3, 4]] });
318+
319+
let diag = new types.Diagnostic(new types.Range(0, 1, 2, 3), 'hello');
320+
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [[0, 1], [2, 3]] });
321+
diag.source = 'me'
322+
assertToJSON(diag, { severity: 'Error', message: 'hello', range: [[0, 1], [2, 3]], source: 'me' });
323+
324+
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5)), { range: [[2, 3], [4, 5]], kind: 'Text' });
325+
assertToJSON(new types.DocumentHighlight(new types.Range(2, 3, 4, 5), types.DocumentHighlightKind.Read), { range: [[2, 3], [4, 5]], kind: 'Read' });
326+
327+
assertToJSON(new types.SymbolInformation('test', types.SymbolKind.Boolean, new types.Range(0, 1, 2, 3)), {
328+
name: 'test',
329+
kind: 'Boolean',
330+
location: {
331+
range: [[0, 1], [2, 3]]
332+
}
333+
});
334+
335+
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10)), { range: [[7, 8], [9, 10]] });
336+
assertToJSON(new types.CodeLens(new types.Range(7, 8, 9, 10), { command: 'id', title: 'title' }), {
337+
range: [[7, 8], [9, 10]],
338+
command: { command: 'id', title: 'title' }
339+
});
340+
341+
assertToJSON(new types.CompletionItem('complete'), { label: 'complete' });
342+
343+
let item = new types.CompletionItem('complete');
344+
item.kind = types.CompletionItemKind.Interface
345+
assertToJSON(item, { label: 'complete', kind: 'Interface' });
346+
283347
});
284348
});

0 commit comments

Comments
 (0)