Skip to content

Commit 81643ae

Browse files
committed
feat: Take search.exclude and file.exclude settings into account when caching workspace #116
1 parent 1b34e2b commit 81643ae

File tree

4 files changed

+134
-39
lines changed

4 files changed

+134
-39
lines changed

src/extension.ts

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export const activate = async (context: vscode.ExtensionContext) => {
3434

3535
context.subscriptions.push(
3636
...commands,
37+
vscode.workspace.onDidChangeConfiguration(async (configChangeEvent) => {
38+
if (configChangeEvent.affectsConfiguration('search.exclude')) {
39+
await cacheWorkspace();
40+
}
41+
}),
3742
vscode.window.createTreeView('memo.backlinksPanel', {
3843
treeDataProvider: backlinksTreeDataProvider,
3944
showCollapseAll: true,

src/test/testUtils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,12 @@ const getDefaultConfigProperties = (): {
116116
return require('../../package.json').contributes.configuration.properties;
117117
};
118118

119-
export const updateConfigProperty = async (property: string, value: unknown) => {
120-
await workspace.getConfiguration().update(property, value, ConfigurationTarget.Workspace);
119+
export const updateConfigProperty = async (
120+
property: string,
121+
value: unknown,
122+
target = ConfigurationTarget.Workspace,
123+
) => {
124+
await workspace.getConfiguration().update(property, value, target);
121125
};
122126

123127
export const updateMemoConfigProperty = async (property: string, value: unknown) =>

src/utils/utils.spec.ts

+96-31
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,58 @@
1-
import { window, Selection, workspace, Range, Position, Uri } from 'vscode';
1+
import { Position, Range, Selection, Uri, window, workspace } from 'vscode';
22
import fs from 'fs';
33
import path from 'path';
44

55
import {
6-
createFile,
7-
rndName,
86
cleanWorkspace,
97
closeEditorsAndCleanWorkspace,
8+
createFile,
109
openTextDocument,
10+
rndName,
1111
toPlainObject,
12+
updateConfigProperty,
1213
} from '../test/testUtils';
14+
import * as utils from './utils';
1315
import {
16+
addCachedRefs,
17+
cacheRefs,
18+
cacheUris,
19+
cacheWorkspace,
20+
cleanWorkspaceCache,
1421
containsImageExt,
1522
containsMarkdownExt,
1623
containsOtherKnownExts,
1724
containsUnknownExt,
18-
fsPathToRef,
19-
trimLeadingSlash,
20-
trimTrailingSlash,
21-
trimSlashes,
22-
isLongRef,
23-
normalizeSlashes,
24-
getWorkspaceFolder,
25-
getWorkspaceCache,
26-
getMemoConfigProperty,
27-
matchAll,
28-
cacheWorkspace,
29-
cacheUris,
30-
cacheRefs,
31-
addCachedRefs,
32-
removeCachedRefs,
33-
cleanWorkspaceCache,
34-
getRefUriUnderCursor,
35-
getReferenceAtPosition,
25+
ensureDirectoryExists,
3626
escapeForRegExp,
27+
extractDanglingRefs,
3728
extractEmbedRefs,
38-
parseRef,
39-
replaceRefs,
29+
extractExt,
30+
findAllUrisWithUnknownExts,
31+
findDanglingRefsByFsPath,
32+
findFilesByExts,
33+
findNonIgnoredFiles,
4034
findReferences,
35+
findUriByRef,
36+
fsPathToRef,
37+
getConfigProperty,
4138
getFileUrlForMarkdownPreview,
4239
getImgUrlForMarkdownPreview,
40+
getMemoConfigProperty,
41+
getReferenceAtPosition,
42+
getRefUriUnderCursor,
43+
getWorkspaceCache,
44+
getWorkspaceFolder,
45+
isLongRef,
4346
isUncPath,
44-
findFilesByExts,
45-
findAllUrisWithUnknownExts,
46-
extractExt,
47-
findUriByRef,
48-
ensureDirectoryExists,
49-
extractDanglingRefs,
50-
findDanglingRefsByFsPath,
47+
matchAll,
48+
normalizeSlashes,
49+
parseRef,
50+
removeCachedRefs,
51+
replaceRefs,
52+
trimLeadingSlash,
53+
trimSlashes,
54+
trimTrailingSlash,
5155
} from './utils';
52-
import * as utils from './utils';
5356

5457
describe('containsImageExt()', () => {
5558
test.each(['png', 'jpg', 'jpeg', 'gif'])(
@@ -1534,3 +1537,65 @@ describe('findDanglingRefsByFsPath()', () => {
15341537
expect(await findDanglingRefsByFsPath(getWorkspaceCache().markdownUris)).toEqual({});
15351538
});
15361539
});
1540+
1541+
describe('findNonIgnoredFiles()', () => {
1542+
beforeEach(closeEditorsAndCleanWorkspace);
1543+
1544+
afterEach(closeEditorsAndCleanWorkspace);
1545+
1546+
it('should find non-ignored files', async () => {
1547+
const prevConfig = getConfigProperty('search.exclude', {});
1548+
// eslint-disable-next-line @typescript-eslint/naming-convention
1549+
await updateConfigProperty('search.exclude', { '**/search-ignored': true });
1550+
1551+
const allowedName = rndName();
1552+
const ignoredName = rndName();
1553+
1554+
await createFile(`${allowedName}.md`);
1555+
await createFile(`search-ignored/some-package/${ignoredName}.md`);
1556+
1557+
const files = await findNonIgnoredFiles('**/*.md');
1558+
1559+
expect(files).toHaveLength(1);
1560+
expect(path.basename(files[0].fsPath)).toBe(`${allowedName}.md`);
1561+
1562+
await updateConfigProperty('search.exclude', prevConfig);
1563+
});
1564+
1565+
describe('when exclude param passed explicitly', () => {
1566+
it('should find non-ignored files', async () => {
1567+
const allowedName = rndName();
1568+
const ignoredName = rndName();
1569+
1570+
await createFile(`${allowedName}.md`);
1571+
await createFile(`search-ignored/some-package/${ignoredName}.md`);
1572+
1573+
const files = await findNonIgnoredFiles('**/*.md', '**/search-ignored');
1574+
1575+
expect(files).toHaveLength(1);
1576+
expect(path.basename(files[0].fsPath)).toBe(`${allowedName}.md`);
1577+
});
1578+
});
1579+
1580+
describe('when exclude param passed explicitly and search.exclude set', () => {
1581+
it('should find non-ignored files', async () => {
1582+
const prevConfig = getConfigProperty('search.exclude', {});
1583+
// eslint-disable-next-line @typescript-eslint/naming-convention
1584+
await updateConfigProperty('search.exclude', { '**/search-ignored': true });
1585+
1586+
const allowedName = rndName();
1587+
const ignoredName = rndName();
1588+
1589+
await createFile(`${allowedName}.md`);
1590+
await createFile(`search-ignored/some-package/${ignoredName}.md`);
1591+
await createFile(`search-ignored-2/some-package/${ignoredName}.md`);
1592+
1593+
const files = await findNonIgnoredFiles('**/*.md', '**/search-ignored-2');
1594+
1595+
expect(files).toHaveLength(1);
1596+
expect(path.basename(files[0].fsPath)).toBe(`${allowedName}.md`);
1597+
1598+
await updateConfigProperty('search.exclude', prevConfig);
1599+
});
1600+
});
1601+
});

src/utils/utils.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import vscode, { workspace } from 'vscode';
1+
import vscode, { CancellationToken, GlobPattern, Uri, workspace } from 'vscode';
22
import path from 'path';
33
import { sort as sortPaths } from 'cross-path-sort';
44
import fs from 'fs';
@@ -158,9 +158,9 @@ const workspaceCache: WorkspaceCache = {
158158
export const getWorkspaceCache = (): WorkspaceCache => workspaceCache;
159159

160160
export const cacheUris = async () => {
161-
const markdownUris = await vscode.workspace.findFiles('**/*.md');
162-
const imageUris = await vscode.workspace.findFiles(`**/*.{${imageExts.join(',')}}`);
163-
const otherUris = await vscode.workspace.findFiles(`**/*.{${otherExts.join(',')}}`);
161+
const markdownUris = await findNonIgnoredFiles('**/*.md');
162+
const imageUris = await findNonIgnoredFiles(`**/*.{${imageExts.join(',')}}`);
163+
const otherUris = await findNonIgnoredFiles(`**/*.{${otherExts.join(',')}}`);
164164

165165
workspaceCache.markdownUris = sortPaths(markdownUris, { pathKey: 'path', shallowFirst: true });
166166
workspaceCache.imageUris = sortPaths(imageUris, { pathKey: 'path', shallowFirst: true });
@@ -230,8 +230,12 @@ export const cleanWorkspaceCache = () => {
230230
export const getWorkspaceFolder = (): string | undefined =>
231231
vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0].uri.fsPath;
232232

233+
export function getConfigProperty<T>(property: string, fallback: T): T {
234+
return vscode.workspace.getConfiguration().get(property, fallback);
235+
}
236+
233237
export function getMemoConfigProperty<T>(property: string, fallback: T): T {
234-
return vscode.workspace.getConfiguration().get(`memo.${property}`, fallback);
238+
return getConfigProperty(`memo.${property}`, fallback);
235239
}
236240

237241
export const matchAll = (pattern: RegExp, text: string): Array<RegExpMatchArray> => {
@@ -345,7 +349,7 @@ const uncPathRegex = /^[\\\/]{2,}[^\\\/]+[\\\/]+[^\\\/]+/;
345349
export const isUncPath = (path: string): boolean => uncPathRegex.test(path);
346350

347351
export const findFilesByExts = async (exts: string[]): Promise<vscode.Uri[]> =>
348-
await workspace.findFiles(`**/*.{${exts.join(',')}}`);
352+
await findNonIgnoredFiles(`**/*.{${exts.join(',')}}`);
349353

350354
export const findAllUrisWithUnknownExts = async (uris: vscode.Uri[]) => {
351355
const unknownExts = Array.from(
@@ -472,3 +476,20 @@ export const replaceRefs = ({
472476

473477
return updatedOnce ? nextContent : null;
474478
};
479+
480+
export const findNonIgnoredFiles = async (
481+
include: GlobPattern,
482+
excludeParam?: string | null,
483+
maxResults?: number,
484+
token?: CancellationToken,
485+
): Promise<Uri[]> => {
486+
const exclude = [
487+
...Object.keys(getConfigProperty('search.exclude', {})),
488+
...Object.keys(getConfigProperty('file.exclude', {})),
489+
...[excludeParam],
490+
].join(',');
491+
492+
const files = await workspace.findFiles(include, `{${exclude}}`, maxResults, token);
493+
494+
return files;
495+
};

0 commit comments

Comments
 (0)