2 Object.defineProperty(exports, "__esModule", { value: true });
3 const tslib_1 = require("tslib");
4 const coc_nvim_1 = require("coc.nvim");
5 const fs_1 = require("fs");
6 const path = tslib_1.__importStar(require("path"));
7 const errorHandler_1 = require("./errorHandler");
8 const utils_1 = require("./utils");
9 const ignore = require('ignore');
10 const nullIgnorer = { ignores: () => false };
12 * Create an ignore file handler. Will lazily read ignore files on a per-resource
13 * basis, and cache the contents until it changes.
15 function ignoreFileHandler(disposables) {
16 const ignorers = new Map();
17 disposables.push({ dispose: () => ignorers.clear() });
19 fileIsIgnored(filePath) {
20 const { ignorer, ignoreFilePath } = getIgnorerForFile(filePath);
21 return ignorer.ignores(path.relative(path.dirname(ignoreFilePath), filePath));
24 function getIgnorerForFile(fsPath) {
25 const absolutePath = getIgnorePathForFile(fsPath, utils_1.getConfig(coc_nvim_1.Uri.file(fsPath)).ignorePath);
27 return { ignoreFilePath: '', ignorer: nullIgnorer };
29 if (!ignorers.has(absolutePath)) {
30 loadIgnorer(coc_nvim_1.Uri.file(absolutePath));
32 if (!fs_1.existsSync(absolutePath)) {
33 // Don't log default value.
34 const ignorePath = utils_1.getConfig(coc_nvim_1.Uri.file(fsPath)).ignorePath;
35 if (ignorePath !== '.prettierignore') {
36 errorHandler_1.addToOutput(`Wrong prettier.ignorePath provided in your settings. The path (${ignorePath}) does not exist.`, 'Warning');
38 return { ignoreFilePath: '', ignorer: nullIgnorer };
41 ignoreFilePath: absolutePath,
42 ignorer: ignorers.get(absolutePath),
45 function loadIgnorer(ignoreUri) {
46 let ignorer = nullIgnorer;
47 if (!ignorers.has(ignoreUri.fsPath)) {
48 const fileWatcher = coc_nvim_1.workspace.createFileSystemWatcher(ignoreUri.fsPath);
49 disposables.push(fileWatcher);
50 fileWatcher.onDidCreate(loadIgnorer, null, disposables);
51 fileWatcher.onDidChange(loadIgnorer, null, disposables);
52 fileWatcher.onDidDelete(unloadIgnorer, null, disposables);
54 if (fs_1.existsSync(ignoreUri.fsPath)) {
55 const ignoreFileContents = fs_1.readFileSync(ignoreUri.fsPath, 'utf8');
56 ignorer = ignore().add(ignoreFileContents);
58 ignorers.set(ignoreUri.fsPath, ignorer);
60 function unloadIgnorer(ignoreUri) {
61 ignorers.set(ignoreUri.fsPath, nullIgnorer);
64 function getIgnorePathForFile(_filePath, ignorePath) {
65 // Configuration `prettier.ignorePath` is set to `null`
69 if (coc_nvim_1.workspace.workspaceFolder) {
70 const folder = coc_nvim_1.workspace.workspaceFolder;
71 return folder ? getPath(ignorePath, coc_nvim_1.Uri.parse(folder.uri).fsPath) : null;
75 function getPath(fsPath, relativeTo) {
76 return path.isAbsolute(fsPath) ? fsPath : path.join(relativeTo, fsPath);
78 exports.default = ignoreFileHandler;
79 //# sourceMappingURL=ignoreFileHandler.js.map