Skip to content

Commit

Permalink
feat: 🎸 ignore document format if .bladeignore exists
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Feb 3, 2021
1 parent 88f459a commit 93e0785
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const vscode = require("vscode");
const path = require("path");
var findConfig = require("find-config");
const fs = require("fs");
const ignore = require("ignore");
const { Range, Position } = vscode;
const vsctmModule = getCoreNodeModule("vscode-textmate");
const onigurumaModule = getCoreNodeModule("vscode-oniguruma");
Expand All @@ -24,6 +28,10 @@ function activate(context) {
context.subscriptions.push(
vscode.languages.registerDocumentFormattingEditProvider("blade", {
provideDocumentFormattingEdits(document) {
if (shouldIgnore(document.uri.fsPath)) {
return document;
}

const extConfig = vscode.workspace.getConfiguration(
"bladeFormatter.format"
);
Expand Down Expand Up @@ -94,6 +102,24 @@ function getCoreNodeModule(moduleName) {
return null;
}

function shouldIgnore(filepath) {
const ignoreFilename = ".bladeignore";

try {
const ignoreFilePath = findConfig(ignoreFilename, {
cwd: path.dirname(filepath),
});
const ignoreFileContent = fs.readFileSync(ignoreFilePath).toString();
const ig = ignore().add(ignoreFileContent);

return vscode.workspace.workspaceFolders.find((folder) => {
return ig.ignores(path.relative(folder.uri.fsPath, filepath));
});
} catch (err) {
return false;
}
}

module.exports = {
activate,
deactivate,
Expand Down

0 comments on commit 93e0785

Please sign in to comment.