Skip to content

Commit eacbcb2

Browse files
committed
reuse common documentList functions
1 parent 9f6be03 commit eacbcb2

File tree

3 files changed

+64
-65
lines changed

3 files changed

+64
-65
lines changed

src/explorer/documentListPreviewItem.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
11
import * as vscode from 'vscode';
2-
import numeral from 'numeral';
3-
import path from 'path';
42

53
import type { DataService } from 'mongodb-data-service';
6-
import { CollectionType } from './documentListTreeItem';
7-
import { getImagesPath } from '../extensionConstants';
4+
import {
5+
CollectionType,
6+
formatDocCount,
7+
getDocumentsIconPath,
8+
getDocumentsTooltip,
9+
} from './documentListUtils';
810
import formatError from '../utils/formatError';
911

1012
export const PREVIEW_LIST_ITEM = 'documentListPreviewItem';
1113

12-
export const formatDocCount = (count: number): string => {
13-
// We format the count (30000 -> 30k) and then display it uppercase (30K).
14-
return `${numeral(count).format('0a') as string}`.toUpperCase();
15-
};
16-
17-
function getIconPath(): { light: vscode.Uri; dark: vscode.Uri } {
18-
const LIGHT = path.join(getImagesPath(), 'light');
19-
const DARK = path.join(getImagesPath(), 'dark');
20-
21-
return {
22-
light: vscode.Uri.file(path.join(LIGHT, 'documents.svg')),
23-
dark: vscode.Uri.file(path.join(DARK, 'documents.svg')),
24-
};
25-
}
26-
27-
function getTooltip(type: string, documentCount: number | null): string {
28-
const typeString = type === CollectionType.view ? 'View' : 'Collection';
29-
if (documentCount !== null) {
30-
return `${typeString} Documents - ${documentCount}`;
31-
}
32-
return `${typeString} Documents`;
33-
}
34-
3514
export default class ShowPreviewTreeItem extends vscode.TreeItem {
3615
cacheIsUpToDate = false;
3716
contextValue = PREVIEW_LIST_ITEM;
@@ -90,8 +69,8 @@ export default class ShowPreviewTreeItem extends vscode.TreeItem {
9069
this.description = formatDocCount(this._documentCount);
9170
}
9271

93-
this.iconPath = getIconPath();
94-
this.tooltip = getTooltip(type, cachedDocumentCount);
72+
this.iconPath = getDocumentsIconPath();
73+
this.tooltip = getDocumentsTooltip(type, cachedDocumentCount);
9574
}
9675

9776
async loadPreview(options?: {

src/explorer/documentListTreeItem.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import * as vscode from 'vscode';
2-
import numeral from 'numeral';
3-
import path from 'path';
42

53
import { createLogger } from '../logging';
64
import DocumentTreeItem from './documentTreeItem';
75
import formatError from '../utils/formatError';
8-
import { getImagesPath } from '../extensionConstants';
96
import type TreeItemParent from './treeItemParentInterface';
107
import type { DataService } from 'mongodb-data-service';
8+
import {
9+
CollectionType,
10+
formatDocCount,
11+
getDocumentsIconPath,
12+
getDocumentsTooltip,
13+
} from './documentListUtils';
14+
15+
export {
16+
CollectionType,
17+
formatDocCount,
18+
getDocumentsIconPath,
19+
getDocumentsTooltip,
20+
} from './documentListUtils';
1121

1222
const log = createLogger('documents tree item');
1323

@@ -17,14 +27,6 @@ const log = createLogger('documents tree item');
1727
export const MAX_DOCUMENTS_VISIBLE = 10;
1828

1929
export const DOCUMENT_LIST_ITEM = 'documentListTreeItem';
20-
export const CollectionType = {
21-
collection: 'collection',
22-
view: 'view',
23-
timeseries: 'timeseries',
24-
} as const;
25-
26-
export type CollectionType =
27-
(typeof CollectionType)[keyof typeof CollectionType];
2830

2931
const ITEM_LABEL = 'Documents';
3032

@@ -65,29 +67,6 @@ const getCollapsableStateForDocumentList = (
6567
: vscode.TreeItemCollapsibleState.Collapsed;
6668
};
6769

68-
export const formatDocCount = (count: number): string => {
69-
// We format the count (30000 -> 30k) and then display it uppercase (30K).
70-
return `${numeral(count).format('0a') as string}`.toUpperCase();
71-
};
72-
73-
function getIconPath(): { light: vscode.Uri; dark: vscode.Uri } {
74-
const LIGHT = path.join(getImagesPath(), 'light');
75-
const DARK = path.join(getImagesPath(), 'dark');
76-
77-
return {
78-
light: vscode.Uri.file(path.join(LIGHT, 'documents.svg')),
79-
dark: vscode.Uri.file(path.join(DARK, 'documents.svg')),
80-
};
81-
}
82-
83-
function getTooltip(type: string, documentCount: number | null): string {
84-
const typeString = type === CollectionType.view ? 'View' : 'Collection';
85-
if (documentCount !== null) {
86-
return `${typeString} Documents - ${documentCount}`;
87-
}
88-
return `${typeString} Documents`;
89-
}
90-
9170
export default class DocumentListTreeItem
9271
extends vscode.TreeItem
9372
implements TreeItemParent, vscode.TreeDataProvider<DocumentListTreeItem>
@@ -164,8 +143,8 @@ export default class DocumentListTreeItem
164143
this.description = formatDocCount(this._documentCount);
165144
}
166145

167-
this.iconPath = getIconPath();
168-
this.tooltip = getTooltip(type, cachedDocumentCount);
146+
this.iconPath = getDocumentsIconPath();
147+
this.tooltip = getDocumentsTooltip(type, cachedDocumentCount);
169148
}
170149

171150
getTreeItem(element: DocumentListTreeItem): DocumentListTreeItem {

src/explorer/documentListUtils.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as vscode from 'vscode';
2+
import numeral from 'numeral';
3+
import path from 'path';
4+
5+
import { getImagesPath } from '../extensionConstants';
6+
7+
export const CollectionType = {
8+
collection: 'collection',
9+
view: 'view',
10+
timeseries: 'timeseries',
11+
} as const;
12+
13+
export type CollectionType =
14+
(typeof CollectionType)[keyof typeof CollectionType];
15+
16+
export const formatDocCount = (count: number): string => {
17+
// We format the count (30000 -> 30k) and then display it uppercase (30K).
18+
return `${numeral(count).format('0a') as string}`.toUpperCase();
19+
};
20+
21+
export function getDocumentsIconPath(): { light: vscode.Uri; dark: vscode.Uri } {
22+
const LIGHT = path.join(getImagesPath(), 'light');
23+
const DARK = path.join(getImagesPath(), 'dark');
24+
25+
return {
26+
light: vscode.Uri.file(path.join(LIGHT, 'documents.svg')),
27+
dark: vscode.Uri.file(path.join(DARK, 'documents.svg')),
28+
};
29+
}
30+
31+
export function getDocumentsTooltip(
32+
type: string,
33+
documentCount: number | null,
34+
): string {
35+
const typeString = type === CollectionType.view ? 'View' : 'Collection';
36+
if (documentCount !== null) {
37+
return `${typeString} Documents - ${documentCount}`;
38+
}
39+
return `${typeString} Documents`;
40+
}
41+

0 commit comments

Comments
 (0)