Browse Source

Adds helper Functions.is

main
Eric Amodio 5 years ago
parent
commit
716ffd495e
3 changed files with 24 additions and 10 deletions
  1. +1
    -1
      src/codelens/codeLensProvider.ts
  2. +14
    -0
      src/system/function.ts
  3. +9
    -9
      src/views/nodes/viewNode.ts

+ 1
- 1
src/codelens/codeLensProvider.ts View File

@ -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<DocumentSymbol>(symbol, 'children');
}

+ 14
- 0
src/system/function.ts View File

@ -119,6 +119,20 @@ export namespace Functions {
: [];
}
export function is<T extends object>(o: object, prop: keyof T, value?: any): o is T;
export function is<T extends object>(o: object, matcher: (o: object) => boolean): o is T;
export function is<T extends object>(
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<T>(obj: T | Promise<T>): obj is Promise<T> {
return obj && typeof (obj as Promise<T>).then === 'function';
}

+ 9
- 9
src/views/nodes/viewNode.ts View File

@ -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<ViewNode & PageableViewNode>(node, 'supportsPaging', true);
}
export function supportsAutoRefresh(
view: View
): view is View & { autoRefresh: boolean; onDidChangeAutoRefresh: Event<void> } {
return (view as View & { onDidChangeAutoRefresh: Event<void> }).onDidChangeAutoRefresh !== undefined;
interface AutoRefreshableView {
autoRefresh: boolean;
onDidChangeAutoRefresh: Event<void>;
}
export function supportsAutoRefresh(view: View): view is View & AutoRefreshableView {
return Functions.is<View & AutoRefreshableView>(view, 'onDidChangeAutoRefresh');
}
export abstract class SubscribeableViewNode<TView extends View = View> extends ViewNode<TView> {

Loading…
Cancel
Save