├── .gitignore ├── .gitattributes ├── .vscode ├── extensions.json ├── settings.json ├── tasks.json └── launch.json ├── documentation └── menu.png ├── .vscodeignore ├── CHANGELOG.md ├── tsconfig.json ├── README.md ├── .eslintrc.js ├── LICENSE ├── package.json └── src └── extension.ts /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | node_modules 3 | .vscode-test/ 4 | *.vsix 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behavior to automatically normalize line endings. 2 | * text=auto 3 | 4 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint" 4 | ] 5 | } -------------------------------------------------------------------------------- /documentation/menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mjbvz/vscode-folder-source-actions/HEAD/documentation/menu.png -------------------------------------------------------------------------------- /.vscodeignore: -------------------------------------------------------------------------------- 1 | .vscode/** 2 | .vscode-test/** 3 | out/test/** 4 | out/**/*.map 5 | src/** 6 | .gitignore 7 | tsconfig.json 8 | vsc-extension-quickstart.md 9 | tslint.json -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## 0.2.1 — Feb 17, 2021 4 | - Fix actions not running. This requires VS Code 1.54+. There is not a good approach to backporting the fix to work on older versions of VS Code. 5 | 6 | ## 0.2.0 — March 7, 2019 7 | - Add fix all in folder. 8 | 9 | ## 0.0.3 — July 11, 2018 10 | - Execute actions one at a time. 11 | 12 | ## 0.0.1 — July 11, 2018 13 | - Initial release -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to overwrite default and user settings. 2 | { 3 | "files.exclude": { 4 | "out": false // set this to true to hide the "out" folder with the compiled JS files 5 | }, 6 | "search.exclude": { 7 | "out": true // set this to false to include "out" folder in search results 8 | }, 9 | // Turn off tsc task auto detection since we have the necessary tasks as npm scripts 10 | "typescript.tsc.autoDetect": "off" 11 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "target": "es2018", 5 | "outDir": "out", 6 | "lib": [ 7 | "es2018" 8 | ], 9 | "sourceMap": true, 10 | "rootDir": "src", 11 | /* Strict Type-Checking Option */ 12 | "strict": true, /* enable all strict type-checking options */ 13 | "noUnusedLocals": true 14 | }, 15 | "exclude": [ 16 | "node_modules", 17 | ".vscode-test" 18 | ] 19 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VS Code Folder Source Actions 2 | 3 | Adds a context menu that allows you to apply VS Code's organize imports code action to all files in a folder: 4 | 5 | ![Organize imports in folder context menu](https://github.com/mjbvz/vscode-folder-source-actions/raw/master/documentation/menu.png?raw=true) 6 | 7 | > **Important** — Requires VS Code 1.32+ 8 | 9 | ## Supported languages 10 | 11 | - VS Code's built-in JavaScript and Typescript support. 12 | - Any extension that returns `source.organizeImports` code actions. -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | // See https://go.microsoft.com/fwlink/?LinkId=733558 2 | // for the documentation about the tasks.json format 3 | { 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "type": "npm", 8 | "script": "watch", 9 | "problemMatcher": "$tsc-watch", 10 | "isBackground": true, 11 | "presentation": { 12 | "reveal": "never" 13 | }, 14 | "group": { 15 | "kind": "build", 16 | "isDefault": true 17 | } 18 | } 19 | ] 20 | } -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | /**@type {import('eslint').Linter.Config} */ 2 | // eslint-disable-next-line no-undef 3 | module.exports = { 4 | root: true, 5 | parser: '@typescript-eslint/parser', 6 | plugins: [ 7 | '@typescript-eslint', 8 | ], 9 | extends: [ 10 | 'eslint:recommended', 11 | 'plugin:@typescript-eslint/recommended', 12 | ], 13 | rules: { 14 | 'semi': [2, "always"], 15 | '@typescript-eslint/no-unused-vars': 0, 16 | '@typescript-eslint/no-explicit-any': 0, 17 | '@typescript-eslint/explicit-module-boundary-types': 0, 18 | '@typescript-eslint/no-non-null-assertion': 0, 19 | } 20 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (C) 2018 Matt Bierner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | // A launch configuration that compiles the extension and then opens it inside a new window 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | { 6 | "version": "0.2.0", 7 | "configurations": [ 8 | { 9 | "name": "Extension", 10 | "type": "extensionHost", 11 | "request": "launch", 12 | "runtimeExecutable": "${execPath}", 13 | "args": [ 14 | "--extensionDevelopmentPath=${workspaceFolder}" 15 | ], 16 | "outFiles": [ 17 | "${workspaceFolder}/out/**/*.js" 18 | ], 19 | "preLaunchTask": "npm: watch" 20 | }, 21 | { 22 | "name": "Extension Tests", 23 | "type": "extensionHost", 24 | "request": "launch", 25 | "runtimeExecutable": "${execPath}", 26 | "args": [ 27 | "--extensionDevelopmentPath=${workspaceFolder}", 28 | "--extensionTestsPath=${workspaceFolder}/out/test" 29 | ], 30 | "outFiles": [ 31 | "${workspaceFolder}/out/test/**/*.js" 32 | ], 33 | "preLaunchTask": "npm: watch" 34 | } 35 | ] 36 | } 37 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "folder-source-actions", 3 | "private": true, 4 | "displayName": "Folder source actions", 5 | "description": "Apply organize imports and fix all to all files in a folder", 6 | "version": "0.2.1", 7 | "publisher": "bierner", 8 | "engines": { 9 | "vscode": "^1.32.0" 10 | }, 11 | "repository": { 12 | "url": "https://github.com/mjbvz/vscode-folder-source-actions.git", 13 | "type": "git" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/mjbvz/vscode-folder-source-actions/issues" 17 | }, 18 | "keywords": [ 19 | "imports", 20 | "organize", 21 | "sort", 22 | "javascript", 23 | "typescript" 24 | ], 25 | "categories": [ 26 | "Other" 27 | ], 28 | "activationEvents": [ 29 | "onCommand:folderSourceActions.organizeImports", 30 | "onCommand:folderSourceActions.fixAll" 31 | ], 32 | "main": "./out/extension", 33 | "contributes": { 34 | "commands": [ 35 | { 36 | "command": "folderSourceActions.organizeImports", 37 | "title": "Organize Imports in Folder" 38 | }, 39 | { 40 | "command": "folderSourceActions.fixAll", 41 | "title": "Fix All in Folder" 42 | } 43 | ], 44 | "menus": { 45 | "explorer/context": [ 46 | { 47 | "command": "folderSourceActions.organizeImports", 48 | "when": "explorerResourceIsFolder" 49 | }, 50 | { 51 | "command": "folderSourceActions.fixAll", 52 | "when": "explorerResourceIsFolder" 53 | } 54 | ], 55 | "commandPalette": [ 56 | { 57 | "command": "folderSourceActions.organizeImports", 58 | "when": "false" 59 | }, 60 | { 61 | "command": "folderSourceActions.fixAll", 62 | "when": "false" 63 | } 64 | ] 65 | } 66 | }, 67 | "scripts": { 68 | "vscode:prepublish": "npm run compile", 69 | "compile": "tsc -p ./", 70 | "watch": "tsc -watch -p ./", 71 | "lint": "eslint . --ext .ts,.tsx" 72 | }, 73 | "devDependencies": { 74 | "@types/node": "^12.20.1", 75 | "@types/vscode": "^1.53.0", 76 | "@typescript-eslint/eslint-plugin": "^4.15.0", 77 | "@typescript-eslint/parser": "^4.15.0", 78 | "eslint": "^7.20.0", 79 | "typescript": "^4.1.5" 80 | } 81 | } -------------------------------------------------------------------------------- /src/extension.ts: -------------------------------------------------------------------------------- 1 | import * as vscode from 'vscode'; 2 | 3 | const fakeWholeDocumentRange = new vscode.Range(0, 0, 99999, 0); 4 | 5 | export function activate(context: vscode.ExtensionContext) { 6 | context.subscriptions.push( 7 | vscode.commands.registerCommand('folderSourceActions.organizeImports', 8 | createFolderSourceAction( 9 | vscode.CodeActionKind.SourceOrganizeImports, 10 | "Organizing Imports in Folder" 11 | ))); 12 | 13 | context.subscriptions.push( 14 | vscode.commands.registerCommand('folderSourceActions.fixAll', 15 | createFolderSourceAction( 16 | vscode.CodeActionKind.SourceFixAll, 17 | "Fixing All in Folder" 18 | ))); 19 | } 20 | 21 | function createFolderSourceAction( 22 | kind: vscode.CodeActionKind, 23 | progressLabel: string, 24 | ) { 25 | return function (dir: vscode.Uri) { 26 | return vscode.window.withProgress( 27 | { 28 | location: vscode.ProgressLocation.Window, 29 | title: progressLabel 30 | }, 31 | async () => { 32 | const files = await getPotentialFilesForSourceAction(dir); 33 | return Promise.all(files 34 | .map(file => getSourceActionForFile(kind, file)) 35 | .map(action => action.then(tryApplyCodeAction))); 36 | }); 37 | }; 38 | } 39 | 40 | async function getPotentialFilesForSourceAction( 41 | dir: vscode.Uri 42 | ): Promise> { 43 | return vscode.workspace.findFiles( 44 | { base: dir.fsPath, pattern: '**/*' }, 45 | '**/node_modules/**'); 46 | } 47 | 48 | async function getSourceActionForFile( 49 | kind: vscode.CodeActionKind, 50 | file: vscode.Uri 51 | ): Promise { 52 | try { 53 | const allActions = (await getAllCodeActionsForFile(file, kind)) || []; 54 | return allActions.find(actionFilter(kind)); 55 | } catch { 56 | // noop 57 | } 58 | return undefined; 59 | } 60 | 61 | function getAllCodeActionsForFile(file: vscode.Uri, kind: vscode.CodeActionKind): Thenable | undefined> { 62 | // We need make sure VS Code knows about the file before trying to request code actions 63 | return vscode.workspace.openTextDocument(file).then(doc => 64 | vscode.commands.executeCommand('vscode.executeCodeActionProvider', 65 | file, 66 | fakeWholeDocumentRange, 67 | kind.value)); 68 | } 69 | 70 | function actionFilter(targetKind: vscode.CodeActionKind) { 71 | return function (action: vscode.CodeAction): boolean { 72 | return action && !!action.kind && targetKind.contains(action.kind); 73 | }; 74 | } 75 | 76 | async function tryApplyCodeAction( 77 | action: vscode.CodeAction | undefined 78 | ) { 79 | if (!action) { 80 | return; 81 | } 82 | 83 | if (action.edit && action.edit.size > 0) { 84 | await vscode.workspace.applyEdit(action.edit); 85 | } 86 | if (action.command) { 87 | await vscode.commands.executeCommand(action.command.command, ...(action.command.arguments || [])); 88 | } 89 | } --------------------------------------------------------------------------------