Skip to content

Commit

Permalink
v0.3.0 includeFolders (glob patterns), fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
rioj7 committed May 15, 2021
1 parent ae3b488 commit 95de272
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -23,16 +23,20 @@ This means that files that are open in a tab will be closed if they meet the cri

# Extension Settings

* `commandOnAllFiles.includeFileExtensions`: Only files with file extensions in this list will be opened and formatted.<br/>Defaults to `[]`.
* `commandOnAllFiles.excludeFolders`: These folders will be skipped when looking for files to format.<br/>Defaults to `["node_modules", "out", ".vscode-test", "media", ".git"]`
* `commandOnAllFiles.includeFileExtensions`: Only files with file extensions in this list will be processed.<br/>Defaults to `[]`.
* `commandOnAllFiles.excludeFolders`: These folders will be skipped when looking for files to process.<br/>Can contain workspacefolder (base)names to exclude certain Multi Root Workspaces.<br/>Defaults to `["node_modules", "out", ".vscode-test", "media", ".git"]`
* `commandOnAllFiles.includeFolders`: An array of [Glob Patterns](https://code.visualstudio.com/api/references/vscode-api#GlobPattern) describing folders that will determine which files will be processed.<br/>There is no need to use `**` at the start of the Glob Pattern.<br/>If no Glob Pattern defined all files with a matching extension are processed.
* `commandOnAllFiles.commands`: Here we describe the command to apply together with possible overrides of `includeFileExtensions` and `excludeFolders`. The key of a command is its description and the properties of its value object are:
* `command`: the command to apply
* `includeFileExtensions`: override `commandOnAllFiles.includeFileExtensions` if defined
* `excludeFolders`: override `commandOnAllFiles.excludeFolders` if defined
* `includeFolders`: override `commandOnAllFiles.includeFolders` if defined
* `label`, `description`, `detail`: when `applyOnWorkspace` is called from the command palette it shows a QuickPick list. These 3 properties (`strings`) are used in the construction of the [QuickPickItem](https://code.visualstudio.com/api/references/vscode-api#QuickPickItem). The default value for `label` is the key name of the command. In the 3 properties you can [use icons](https://microsoft.github.io/vscode-codicons/dist/codicon.html) with the `$(<name>)`-syntax.

No matter what the value of `commandOnAllFiles.excludeFolders` is the `".git"` entry will always be added. This to prevent that if you make a mistake in the configuration you could corrupt your Source Control Repository.

To prevent an incorrect directory match in the `includeFolders` glob patterns always include the separator `/`. Using `["/src/"]` prevents a match on directory `src-test`.

# Example

An example of how to configure the extension to add `Hello` to the end of all `.txt` files in the Workspace. We need the extension [multi-command](https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command) because the have to perform a sequence of commands.
Expand Down Expand Up @@ -71,6 +75,11 @@ In `keybindings.json`:
}
```

# Release Notes

### v0.3.0 `includeFolders` : array with glob patterns
### v0.2.0 QuickPick list

# TODO

* Find a way to get a list of file URI's that have a tab so only close files not currently open
30 changes: 28 additions & 2 deletions extension.js
Expand Up @@ -11,19 +11,33 @@ function activate(context) {

let includeExtensions;
let excludeFolders;
let includeFolders;
let unableToApply;
var recentlyUsedCommandOnAllFiles = [];

async function applyOnWorkspace(uri, command) {
const stat = await vscode.workspace.fs.stat(uri);
if ( isStatType(stat, vscode.FileType.Directory) && !excludeFolders.includes(path.basename(uri.fsPath))) {
if (isStatType(stat, vscode.FileType.Directory)) {
if (excludeFolders.includes(path.basename(uri.fsPath))) { return; }
const files = await vscode.workspace.fs.readDirectory(uri);
for (const file of files) {
await applyOnWorkspace(vscode.Uri.joinPath(uri, file[0]), command);
}
return;
}
if ( isStatType(stat, vscode.FileType.File) && includeExtensions.includes(path.extname(uri.fsPath))) {
if (includeFolders && includeFolders.length > 0) {
let found = false;
let path = uri.path;
for (const incFolderRE of includeFolders) {
incFolderRE.lastIndex = 0;
if (incFolderRE.test(path)) {
found = true;
break;
}
}
if (!found) { return; }
}
try {
await vscode.window.showTextDocument(uri);
await vscode.commands.executeCommand(command);
Expand Down Expand Up @@ -90,8 +104,20 @@ function activate(context) {
return val;
};
includeExtensions = getConfigProperty('includeFileExtensions', []);
excludeFolders = getConfigProperty('excludeFolders', []);
excludeFolders = getConfigProperty('excludeFolders', []).concat(); // make shallow copy of configuration array
excludeFolders.push('.git'); // Never traverse this
let globRE = /\.|\*\*|\*|\?|\{([^}]+)\}|\[!/g;
includeFolders = getConfigProperty('includeFolders', []).map( glob => {
globRE.lastIndex = 0;
return new RegExp(glob.replace(globRE, (m, p1) => {
if (m === '.') return '\\.';
if (m === '*') return '[^/]+';
if (m === '?') return '[^/]';
if (m === '**') return '.*';
if (m.startsWith('{')) return `(${p1.replaceAll(',','|')})`;
if (m === '[!') return '[^';
}));
});
unableToApply = [];
let command = getProperty(argsCommand, 'command');
for (const folder of folders) {
Expand Down
14 changes: 13 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Apply one or more commands to all files in the Workspace",
"publisher": "rioj7",
"license": "MIT",
"version": "0.2.1",
"version": "0.3.0",
"engines": {"vscode": "^1.47.0"},
"categories": ["Other"],
"keywords": ["command","files","all","multi-root ready"],
Expand Down Expand Up @@ -37,6 +37,13 @@
"default": ["node_modules", "out", ".vscode-test", "media", ".git"],
"description": "List of folders to exclude."
},
"commandOnAllFiles.includeFolders": {
"type": "array",
"scope": "resource",
"items": { "type": "string" },
"default": [],
"description": "List of glob patterns of folders to use."
},
"commandOnAllFiles.commands": {
"type": "object",
"scope": "resource",
Expand All @@ -61,6 +68,11 @@
"items": { "type": "string" },
"description": "List of folders to exclude."
},
"includeFolders": {
"type": "array",
"items": { "type": "string" },
"description": "List of glob patterns of folders to use."
},
"label": {
"type": "string",
"description": "(Optional) Label to use in the QuickPick list for the command commandOnAllFiles.applyOnWorkspace"
Expand Down

0 comments on commit 95de272

Please sign in to comment.