Skip to content

Commit 716ffd4

Browse files
committed
Adds helper Functions.is
1 parent 286da64 commit 716ffd4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/codelens/codeLensProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,5 +681,5 @@ function getRangeFromSymbol(symbol: DocumentSymbol | SymbolInformation) {
681681
}
682682

683683
function isDocumentSymbol(symbol: DocumentSymbol | SymbolInformation): symbol is DocumentSymbol {
684-
return (symbol as DocumentSymbol).children !== undefined;
684+
return Functions.is<DocumentSymbol>(symbol, 'children');
685685
}

src/system/function.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ export namespace Functions {
119119
: [];
120120
}
121121

122+
export function is<T extends object>(o: object, prop: keyof T, value?: any): o is T;
123+
export function is<T extends object>(o: object, matcher: (o: object) => boolean): o is T;
124+
export function is<T extends object>(
125+
o: object,
126+
propOrMatcher: keyof T | ((o: any) => boolean),
127+
value?: any
128+
): o is T {
129+
if (typeof propOrMatcher === 'function') {
130+
return propOrMatcher(o);
131+
}
132+
133+
return value === undefined ? (o as any)[propOrMatcher] !== undefined : (o as any)[propOrMatcher] === value;
134+
}
135+
122136
export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
123137
return obj && typeof (obj as Promise<T>).then === 'function';
124138
}

src/views/nodes/viewNode.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Command, Disposable, Event, TreeItem, TreeItemCollapsibleState, TreeViewVisibilityChangeEvent } from 'vscode';
33
import { GitUri } from '../../git/gitService';
44
import { Logger } from '../../logger';
5-
import { debug, gate, logName } from '../../system';
5+
import { debug, Functions, gate, logName } from '../../system';
66
import { TreeViewNodeStateChangeEvent, View } from '../viewBase';
77

88
export enum ResourceType {
@@ -118,16 +118,16 @@ export interface PageableViewNode {
118118
maxCount: number | undefined;
119119
}
120120

121-
export function isPageable(
122-
node: ViewNode
123-
): node is ViewNode & { supportsPaging: boolean; maxCount: number | undefined } {
124-
return Boolean((node as ViewNode & { supportsPaging: boolean }).supportsPaging);
121+
export function isPageable(node: ViewNode): node is ViewNode & PageableViewNode {
122+
return Functions.is<ViewNode & PageableViewNode>(node, 'supportsPaging', true);
125123
}
126124

127-
export function supportsAutoRefresh(
128-
view: View
129-
): view is View & { autoRefresh: boolean; onDidChangeAutoRefresh: Event<void> } {
130-
return (view as View & { onDidChangeAutoRefresh: Event<void> }).onDidChangeAutoRefresh !== undefined;
125+
interface AutoRefreshableView {
126+
autoRefresh: boolean;
127+
onDidChangeAutoRefresh: Event<void>;
128+
}
129+
export function supportsAutoRefresh(view: View): view is View & AutoRefreshableView {
130+
return Functions.is<View & AutoRefreshableView>(view, 'onDidChangeAutoRefresh');
131131
}
132132

133133
export abstract class SubscribeableViewNode<TView extends View = View> extends ViewNode<TView> {

0 commit comments

Comments
 (0)