Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/lodash-4.17.19
Browse files Browse the repository at this point in the history
  • Loading branch information
haifengkao committed Oct 7, 2020
2 parents b28675f + ef96194 commit 733aa66
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ The project root is identified by:

Out of the box it knows about git, darcs, mercurial, bazaar, and subversion, but you can configure it to look for anything.

The workspace will not be opened if the file has been associated with any existing workspace.
The workspace will not be opened if the file has been associated with any existing workspace.

# Features
Auto-open: automatically add folders to the current workspace if there are opened files without corresponding folders. If there are no opened workspaces, a new one will be created.
Auto add folder: automatically add folders to the current workspace if there are opened files without corresponding folders. If there are no opened workspaces, a new one will be created.

Auto-close: automatically remove the folder when there are no opened files in it. The last one won't be removed. It can be disabled in the setting.
Auto remove folder: automatically remove the folder when there are no opened files in it. The last one won't be removed. It can be disabled in the setting.

Auto close VSCode: close the last editor will also close VSCode. default is false, need to open it in the extension setting

# Known Issues
For nodejs projects, some vscode plugins will open `package.json` and `tsconfig.json` automatically. In such a case, the auto-close feature will fail to function because these files cannot be closed by the user.
- For nodejs projects, some vscode plugins will open `package.json` and `tsconfig.json` automatically. In such a case, the auto-remove feature will fail to function because these files cannot be closed by the user.
- Sometimes auto-remove will fail to remove the unused folder. We are waiting for VS Code to support [better editor API](https://github.com/microsoft/vscode/issues/15178) to solve it.

# Debug
All logs will be shown in `Help`->`Toggle Developer Tools`

## Release Notes

### 0.0.4
- Automatcally close vscode when all editors are closed

### 0.0.3
- Automatcally remove folder if all files belong to the folder are closed

Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type": "git",
"url": "https://github.com/haifengkao/AlwaysOpenWorkspace.git"
},
"version": "0.0.3",
"version": "0.0.7",
"engines": {
"vscode": "^1.40.0"
},
Expand Down Expand Up @@ -46,10 +46,23 @@
"default": true,
"scope": "window"
},
"AlwaysOpenWorkspace.autoCloseVSCode": {
"type": "boolean",
"description": "Auto close VSCode when all editors are closed",
"default": false,
"scope": "window"
},
"AlwaysOpenWorkspace.rootFolders": {
"type": "array",
"description": "Identify a workspace folder by the following files or directories. Directories must have a trailing slash. To work correctly with git submodules place .git before .git/.",
"default": [".git", ".git/", "_darcs/", ".hg/", ".bzr/", ".svn/"],
"default": [
".git",
".git/",
"_darcs/",
".hg/",
".bzr/",
".svn/"
],
"scope": "window",
"items": {
"type": "string"
Expand Down
53 changes: 40 additions & 13 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,41 @@ import * as path from 'path';
import { TextDocument, Uri } from 'vscode';

var loggingEnabled = false;
var shouldAutoCloseVsCodeWhenAllEditorsAreClosed = false;

export function dumpInConsole(...args: any[]) {
if (loggingEnabled) {
console.log(...args);
}
}

function shouldCloseVSCode(): boolean {
dumpInConsole("should close", userDocuments())

if (!shouldAutoCloseVsCodeWhenAllEditorsAreClosed) { return false; }

return userDocuments().length <= 0;
}

export function userDocuments(): Array<Uri>
{
// TODO: textDocuements do not return all opened editors. need another API for this task
{
// TODO: textDocuements do not return all opened editors. need another API for this task
// https://github.com/microsoft/vscode/issues/15178
return vscode.workspace.textDocuments.map(doc => doc.uri).filter(uri => uri.scheme == 'file');
}

export function closeDeadFolders()
{
{
const isAutoClose: boolean | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.autoClose");
if (!isAutoClose) { return; }

const docUris = userDocuments()
if (docUris.length <= 0) { return; } // don't close the last workspace

const liveWorkspaces = new Set(docUris.map(function(fileUrl) {
const liveWorkspaces = new Set(docUris.map(function(fileUrl) {
return vscode.workspace.getWorkspaceFolder(fileUrl);
}).filter(x => x != null)
);
);

const liveWorkspacesArray = Array.from(liveWorkspaces);
const allWorkspaces = vscode.workspace.workspaceFolders || [];
Expand All @@ -56,11 +65,16 @@ export function activate(context: vscode.ExtensionContext) {
loggingEnabled = logEnabled
}

const shouldAutoCloseVSCode: boolean | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.autoCloseVSCode");
if (shouldAutoCloseVSCode != undefined) {
shouldAutoCloseVsCodeWhenAllEditorsAreClosed = shouldAutoCloseVSCode;
}

async function didOpenTextDocument(textDocument: TextDocument): Promise<any> {
const isEnabled: boolean | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.enabled");
const logEnabled: boolean | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.loggingEnabled");
const rootFolders: string[] | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.rootFolders");

// if enabled is not defined or false then don't do anything...
if (logEnabled != undefined) {
loggingEnabled = logEnabled
Expand All @@ -72,7 +86,7 @@ export function activate(context: vscode.ExtensionContext) {
return;
}

if (!rootFolders) {
if (!rootFolders) {
dumpInConsole("AlwaysOpenWorkspace rootFolders are not set");
return;
}
Expand All @@ -84,13 +98,13 @@ export function activate(context: vscode.ExtensionContext) {

dumpInConsole("AlwaysOpenWorkspace didOpen: ", fileUrl, filePath);
const workspace = vscode.workspace.getWorkspaceFolder(fileUrl)
if (workspace) {
if (workspace) {
dumpInConsole("AlwaysOpenWorkspace has workspace just return: ", filePath);
return;
return;
} // we had the workspace, nothing to do

const dirname = path.dirname(filePath)
for (var name of rootFolders) {
for (var name of rootFolders) {
const typeValue = (name.slice(-1) == "/") ? 'directory' : 'file';
const folderPath = await findUp(name, {type: typeValue, cwd: dirname});
if (folderPath) {
Expand All @@ -99,13 +113,12 @@ export function activate(context: vscode.ExtensionContext) {
dumpInConsole("AlwaysOpenWorkspace find folder: ", folderPath, parentUrl);
vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders.length : 0, null, { uri: parentUrl, name: parent})
return; // finished
} else {
} else {
//dumpInConsole("AlwaysOpenWorkspace failed to find: ", name, folderPath);
}
}
}
const disposable = vscode.workspace.onDidOpenTextDocument(didOpenTextDocument);
vscode.workspace.textDocuments.forEach(didOpenTextDocument);

const disposable2 = vscode.workspace.onDidCloseTextDocument((textDocument) => {
const isEnabled: boolean | undefined = vscode.workspace.getConfiguration().get("AlwaysOpenWorkspace.enabled");
Expand All @@ -115,15 +128,29 @@ export function activate(context: vscode.ExtensionContext) {
}

const fileUrl = textDocument.uri;
const filePath = fileUrl.fsPath;
if (fileUrl.scheme != 'file') { return; } // it might be git or output scheme, not a real file

const workspace = vscode.workspace.getWorkspaceFolder(fileUrl)
if (!workspace) { return; } // it might be workspace.json. vscode will open and close it from time to time. just ignore it

const filePath = fileUrl.fsPath;
dumpInConsole("AlwaysOpenWorkspace didClose: ", fileUrl, filePath);
closeDeadFolders();

if (shouldCloseVSCode()) {
// the last window, just quit
dumpInConsole("!!!!AlwaysOpenWorkspace quit: ");
vscode.commands.executeCommand('workbench.action.quit');
}

});

vscode.workspace.textDocuments.forEach(didOpenTextDocument); // open the folders for the initial file which are remembered by vscode
closeDeadFolders(); // close the initial folders which are remembered by vscode in previous sessions

context.subscriptions.push(disposable);
context.subscriptions.push(disposable2);

}

// this method is called when your extension is deactivated
Expand Down

0 comments on commit 733aa66

Please sign in to comment.