Skip to content

Commit

Permalink
Merge pull request #91 from kortina/ak-find-files-fix
Browse files Browse the repository at this point in the history
improve perf by respecting search.exclude, files.excluce, AND .gitignore
  • Loading branch information
kortina authored Sep 22, 2020
2 parents 1c90f3f + 9bdf331 commit a4e63d4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-markdown-notes",
"displayName": "Markdown Notes",
"description": "Navigate notes with [[wiki-links]], backlinks, and #tags (like Bear, Roam, etc). Automatically create notes from new inline [[wiki-links]]. Use Peek Definition to preview linked notes.",
"version": "0.0.19",
"version": "0.0.20",
"publisher": "kortina",
"repository": {
"url": "https://github.com/kortina/vscode-markdown-notes.git",
Expand Down
8 changes: 5 additions & 3 deletions src/NoteWorkspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { basename, dirname, isAbsolute, join, normalize, relative } from 'path';
import { existsSync, writeFileSync } from 'fs';
import findNonIgnoredFiles from './findNonIgnoredFiles';
const GithubSlugger = require('github-slugger');
const SLUGGER = new GithubSlugger();

Expand Down Expand Up @@ -468,9 +469,10 @@ export class NoteWorkspace {

static async noteFiles(): Promise<Array<vscode.Uri>> {
let that = this;
let files = (await vscode.workspace.findFiles('**/*')).filter(
(f) => f.scheme == 'file' && f.path.match(that.rxFileExtensions())
);

// let files = await vscode.workspace.findFiles('**/*');
let files = await findNonIgnoredFiles('**/*');
files = files.filter((f) => f.scheme == 'file' && f.path.match(that.rxFileExtensions()));
this.noteFileCache = files;
return files;
}
Expand Down
7 changes: 3 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ export function activate(context: vscode.ExtensionContext) {
// For more information on how this works.
return {
extendMarkdownIt(md: any) {
return md.use(
pluginSettings()
)}
};
return md.use(pluginSettings());
},
};
}
69 changes: 69 additions & 0 deletions src/findNonIgnoredFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// "Here's an updated snippet which will return URIs of workspace files except
// for those ignored either by search.exclude or files.exclude or .gitignore."
// via:
// https://github.com/microsoft/vscode/issues/48674
import { workspace, Uri } from 'vscode';
import { exec, ExecException } from 'child_process';
// import applicationInsights from './telemetry';
import { join } from 'path';

export default async function findNonIgnoredFiles(
pattern: string,
checkGitIgnore = true
): Promise<Uri[]> {
const exclude = [
...Object.keys((await workspace.getConfiguration('search', null).get('exclude')) || {}),
...Object.keys((await workspace.getConfiguration('files', null).get('exclude')) || {}),
].join(',');

const uris = await workspace.findFiles(pattern, `{${exclude}}`);
if (!checkGitIgnore) {
return uris;
}
return filterGitIgnored(uris);
}

// TODO: https://github.com/Microsoft/vscode/blob/release/1.27/extensions/git/src/api/git.d.ts instead of git shell if possible
async function filterGitIgnored(uris: Uri[]): Promise<Uri[]> {
const workspaceRelativePaths = uris.map((uri) => workspace.asRelativePath(uri, false));
for (const workspaceDirectory of workspace.workspaceFolders!) {
const workspaceDirectoryPath = workspaceDirectory.uri.fsPath;
try {
const { stdout, stderr } = await new Promise<{ stdout: string; stderr: string }>(
(resolve, reject) => {
exec(
`git check-ignore ${workspaceRelativePaths.join(' ')}`,
{ cwd: workspaceDirectoryPath },
// https://git-scm.com/docs/git-check-ignore#_exit_status
(error: ExecException | null, stdout, stderr) => {
if (error && error.code !== 0 && error.code !== 1) {
reject(error);
return;
}

resolve({ stdout, stderr });
}
);
}
);

if (stderr) {
throw new Error(stderr);
}

for (const relativePath of stdout.split('\n')) {
const uri = Uri.file(
join(workspaceDirectoryPath, relativePath.slice(1, -1) /* Remove quotes */)
);
const index = uris.findIndex((u) => u.fsPath === uri.fsPath);
if (index > -1) {
uris.splice(index, 1);
}
}
} catch (error) {
console.error('findNonIgnoredFiles-git-exec-error', error);
// applicationInsights.sendTelemetryEvent('findNonIgnoredFiles-git-exec-error');
}
}
return uris;
}

0 comments on commit a4e63d4

Please sign in to comment.