From fbbb9061bc9c86bb4f21f08d01d99d89bb25bd7a Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 3 Aug 2016 10:28:35 +0200 Subject: [PATCH] debt - add some XYZ.isXYZ methods to the ext host types. --- src/vs/base/common/uri.ts | 14 +++++++ src/vs/monaco.d.ts | 1 + src/vs/workbench/api/node/extHostTypes.ts | 47 ++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/vs/base/common/uri.ts b/src/vs/base/common/uri.ts index 9ed5aa14e82..2ea9f70cefb 100644 --- a/src/vs/base/common/uri.ts +++ b/src/vs/base/common/uri.ts @@ -39,6 +39,20 @@ function encodeNoop(str: string): string { */ export default class URI { + static isUri(thing: any): thing is URI { + if (thing instanceof URI) { + return true; + } + if (!thing) { + return false; + } + return typeof (thing).authority === 'string' + && typeof (thing).fragment === 'string' + && typeof (thing).path === 'string' + && typeof (thing).query === 'string' + && typeof (thing).scheme === 'string'; + } + private static _empty = ''; private static _slash = '/'; private static _regexp = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/; diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 97952473502..725ca4b5e7f 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -111,6 +111,7 @@ declare module monaco { * */ export class Uri { + static isUri(thing: any): thing is Uri; constructor(); /** * scheme is the 'http' part of 'http://www.msft.com/some/path?query#fragment'. diff --git a/src/vs/workbench/api/node/extHostTypes.ts b/src/vs/workbench/api/node/extHostTypes.ts index c0058a88efd..b10990dec48 100644 --- a/src/vs/workbench/api/node/extHostTypes.ts +++ b/src/vs/workbench/api/node/extHostTypes.ts @@ -63,7 +63,7 @@ export class Position { return result; } - static is(other: any): other is Position { + static isPosition(other: any): other is Position { if (!other) { return false; } @@ -206,15 +206,15 @@ export class Position { export class Range { - static is(thing: any): thing is Range { + static isRange(thing: any): thing is Range { if (thing instanceof Range) { return true; } if (!thing) { return false; } - return Position.is((thing).start) - && Position.is((thing.end)); + return Position.isPosition((thing).start) + && Position.isPosition((thing.end)); } protected _start: Position; @@ -319,7 +319,7 @@ export class Range { if (!startOrChange) { start = this.start; - } else if (Position.is(startOrChange)) { + } else if (Position.isPosition(startOrChange)) { start = startOrChange; } else { @@ -340,6 +340,19 @@ export class Range { export class Selection extends Range { + static isSelection(thing: any): thing is Selection { + if (thing instanceof Selection) { + return true; + } + if (!thing) { + return false; + } + return Range.isRange(thing) + && Position.isPosition((thing).anchor) + && Position.isPosition((thing).active) + && typeof (thing).isReversed === 'boolean'; + } + private _anchor: Position; public get anchor(): Position { @@ -392,6 +405,17 @@ export class Selection extends Range { export class TextEdit { + static isTextEdit(thing: any): thing is TextEdit { + if (thing instanceof TextEdit) { + return true; + } + if (!thing) { + return false; + } + return Range.isRange((thing)) + && typeof (thing).newText === 'string'; + } + static replace(range: Range, newText: string): TextEdit { return new TextEdit(range, newText); } @@ -506,6 +530,17 @@ export enum DiagnosticSeverity { export class Location { + static isLocation(thing: any): thing is Location { + if (thing instanceof Location) { + return true; + } + if (!thing) { + return false; + } + return Range.isRange((thing).range) + && URI.isUri((thing).uri); + } + uri: URI; range: Range; @@ -791,7 +826,7 @@ export class DocumentLink { if (!(target instanceof URI)) { throw illegalArgument('target'); } - if (!Range.is(range) || range.isEmpty) { + if (!Range.isRange(range) || range.isEmpty) { throw illegalArgument('range'); } this.range = range;