diff --git a/src/codelens/codeLensProvider.ts b/src/codelens/codeLensProvider.ts index 02c22ac..bb258ea 100644 --- a/src/codelens/codeLensProvider.ts +++ b/src/codelens/codeLensProvider.ts @@ -681,5 +681,5 @@ function getRangeFromSymbol(symbol: DocumentSymbol | SymbolInformation) { } function isDocumentSymbol(symbol: DocumentSymbol | SymbolInformation): symbol is DocumentSymbol { - return (symbol as DocumentSymbol).children !== undefined; + return Functions.is(symbol, 'children'); } diff --git a/src/system/function.ts b/src/system/function.ts index 1dbabb4..a1d7147 100644 --- a/src/system/function.ts +++ b/src/system/function.ts @@ -119,6 +119,20 @@ export namespace Functions { : []; } + export function is(o: object, prop: keyof T, value?: any): o is T; + export function is(o: object, matcher: (o: object) => boolean): o is T; + export function is( + o: object, + propOrMatcher: keyof T | ((o: any) => boolean), + value?: any + ): o is T { + if (typeof propOrMatcher === 'function') { + return propOrMatcher(o); + } + + return value === undefined ? (o as any)[propOrMatcher] !== undefined : (o as any)[propOrMatcher] === value; + } + export function isPromise(obj: T | Promise): obj is Promise { return obj && typeof (obj as Promise).then === 'function'; } diff --git a/src/views/nodes/viewNode.ts b/src/views/nodes/viewNode.ts index 08a7b0b..4951f45 100644 --- a/src/views/nodes/viewNode.ts +++ b/src/views/nodes/viewNode.ts @@ -2,7 +2,7 @@ import { Command, Disposable, Event, TreeItem, TreeItemCollapsibleState, TreeViewVisibilityChangeEvent } from 'vscode'; import { GitUri } from '../../git/gitService'; import { Logger } from '../../logger'; -import { debug, gate, logName } from '../../system'; +import { debug, Functions, gate, logName } from '../../system'; import { TreeViewNodeStateChangeEvent, View } from '../viewBase'; export enum ResourceType { @@ -118,16 +118,16 @@ export interface PageableViewNode { maxCount: number | undefined; } -export function isPageable( - node: ViewNode -): node is ViewNode & { supportsPaging: boolean; maxCount: number | undefined } { - return Boolean((node as ViewNode & { supportsPaging: boolean }).supportsPaging); +export function isPageable(node: ViewNode): node is ViewNode & PageableViewNode { + return Functions.is(node, 'supportsPaging', true); } -export function supportsAutoRefresh( - view: View -): view is View & { autoRefresh: boolean; onDidChangeAutoRefresh: Event } { - return (view as View & { onDidChangeAutoRefresh: Event }).onDidChangeAutoRefresh !== undefined; +interface AutoRefreshableView { + autoRefresh: boolean; + onDidChangeAutoRefresh: Event; +} +export function supportsAutoRefresh(view: View): view is View & AutoRefreshableView { + return Functions.is(view, 'onDidChangeAutoRefresh'); } export abstract class SubscribeableViewNode extends ViewNode {