Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / lib / ignoreFileHandler.js
1 "use strict";
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 };
11 /**
12  * Create an ignore file handler. Will lazily read ignore files on a per-resource
13  * basis, and cache the contents until it changes.
14  */
15 function ignoreFileHandler(disposables) {
16     const ignorers = new Map();
17     disposables.push({ dispose: () => ignorers.clear() });
18     return {
19         fileIsIgnored(filePath) {
20             const { ignorer, ignoreFilePath } = getIgnorerForFile(filePath);
21             return ignorer.ignores(path.relative(path.dirname(ignoreFilePath), filePath));
22         },
23     };
24     function getIgnorerForFile(fsPath) {
25         const absolutePath = getIgnorePathForFile(fsPath, utils_1.getConfig(coc_nvim_1.Uri.file(fsPath)).ignorePath);
26         if (!absolutePath) {
27             return { ignoreFilePath: '', ignorer: nullIgnorer };
28         }
29         if (!ignorers.has(absolutePath)) {
30             loadIgnorer(coc_nvim_1.Uri.file(absolutePath));
31         }
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');
37             }
38             return { ignoreFilePath: '', ignorer: nullIgnorer };
39         }
40         return {
41             ignoreFilePath: absolutePath,
42             ignorer: ignorers.get(absolutePath),
43         };
44     }
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);
53         }
54         if (fs_1.existsSync(ignoreUri.fsPath)) {
55             const ignoreFileContents = fs_1.readFileSync(ignoreUri.fsPath, 'utf8');
56             ignorer = ignore().add(ignoreFileContents);
57         }
58         ignorers.set(ignoreUri.fsPath, ignorer);
59     }
60     function unloadIgnorer(ignoreUri) {
61         ignorers.set(ignoreUri.fsPath, nullIgnorer);
62     }
63 }
64 function getIgnorePathForFile(_filePath, ignorePath) {
65     // Configuration `prettier.ignorePath` is set to `null`
66     if (!ignorePath) {
67         return null;
68     }
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;
72     }
73     return null;
74 }
75 function getPath(fsPath, relativeTo) {
76     return path.isAbsolute(fsPath) ? fsPath : path.join(relativeTo, fsPath);
77 }
78 exports.default = ignoreFileHandler;
79 //# sourceMappingURL=ignoreFileHandler.js.map