Skip to content

Commit

Permalink
fix: Creating a new task when no task is activated, asks to keep files
Browse files Browse the repository at this point in the history
  • Loading branch information
marlomgirardi committed Oct 8, 2022
1 parent a75756a commit 1f186c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Starting at `v1.0.0` this project will follow [Semantic Versioning](https://semv
## Unreleased
- Upgrade all dependencies
- Update icons to use [vscode-codicon](https://microsoft.github.io/vscode-codicons/dist/codicon.html).
- Replace `TextDocument` by `Tab` and `TabGroup`.
- Replace `TextDocument` by `Tab` and `TabGroup`.

## 0.2.0 - 2021-05-21

Expand Down
13 changes: 3 additions & 10 deletions src/KeepContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { commands, StatusBarAlignment, StatusBarItem, Uri, ViewColumn, window, w
import { ContextTreeDataProvider } from './ContextTreeDataProvider';
import { ContextTreeItem } from './ContextTreeItem';
import GitProvider from './GitProvider';
import { createTask, getRealFileName, taskInputBox } from './utils';
import { createTask, getAllOpenedFiles, taskInputBox } from './utils';
import State from './State';
import { QuickPickTask } from './QuickPickTask';
import { clearStatusBar, updateStatusBar } from './statusbar';
Expand Down Expand Up @@ -107,9 +107,7 @@ export default class KeepContext {
return;
}

const fileNames = workspace.textDocuments
.map(getRealFileName)
.filter((value) => typeof value === 'string') as string[];
const fileNames = getAllOpenedFiles();

if (!this.state.activeTask && fileNames.length > 0) {
keepFilesOpened = await window
Expand All @@ -118,12 +116,7 @@ export default class KeepContext {
}

if (keepFilesOpened) {
task.files = fileNames.reduce((acc: string[], fileName: string) => {
if (!acc.includes(fileName)) {
acc = [...acc, fileName];
}
return acc;
}, []);
task.files = fileNames;
}

this.state.addTask(task);
Expand Down
12 changes: 2 additions & 10 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import KeepContext from './KeepContext';
import State from './State';
import { isTabSupported, KeepContextTabInput } from './utils';
import { getAllOpenedFiles, isTabSupported } from './utils';

export function activate(context: vscode.ExtensionContext): void {
State.setupState(context.workspaceState);
Expand All @@ -24,15 +24,7 @@ export function activate(context: vscode.ExtensionContext): void {
const hasChanged = tabEvent.changed.some((tab) => !tab.isPreview && isTabSupported(tab));

if ((hasClosed || hasOpened || hasChanged) && keepContext.isTaskActive()) {
const files = [
...new Set(
vscode.window.tabGroups.all
.flatMap((group) => group.tabs)
.filter((tab) => !tab.isPreview && isTabSupported(tab))
.map(({ input }) => (input as KeepContextTabInput).uri.fsPath),
),
];

const files = getAllOpenedFiles();
keepContext.updateFileList(files);
}
});
Expand Down
39 changes: 16 additions & 23 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tab, TabInputCustom, TabInputNotebook, TabInputText, TextDocument, window } from 'vscode';
import { Tab, TabInputCustom, TabInputNotebook, TabInputText, window } from 'vscode';
import Task from './Task';

/**
Expand Down Expand Up @@ -58,32 +58,25 @@ export function taskInputBox(
});
}

/**
* Some times we receive a document with `uri.schema` equals `git` instead of `file`.
* To avoid bugs, they should be treated as valid.
*
* @param document Text Document from VSCode.
* @deprecated Will be removed once the `TextDocument` is replaced by `Tab`
*/
export function getRealFileName(document: TextDocument): string | false {
let fileName: string | false = false;

if (document.uri.scheme === 'file') {
fileName = document.fileName;
}

if (document.uri.scheme === 'git') {
fileName = document.fileName.replace(/\.git$/, '');
}

return fileName;
}
/** As the typescript do not evaluate `isTabSupported` and I don't want to spread it through the code. This will help */
type KeepContextTabInput = TabInputText | TabInputCustom | TabInputNotebook;

export function isTabSupported(tab: Tab) {
return (
tab.input instanceof TabInputText || tab.input instanceof TabInputCustom || tab.input instanceof TabInputNotebook
);
}

/** As the typescript do not evaluate `isTabSupported` and I don't want to spread it through the code. This will help */
export type KeepContextTabInput = TabInputText | TabInputCustom | TabInputNotebook;
/**
* Get a list of opened files supported by KeepContext.
*/
export function getAllOpenedFiles() {
return [
...new Set(
window.tabGroups.all
.flatMap((group) => group.tabs)
.filter((tab) => !tab.isPreview && isTabSupported(tab))
.map(({ input }) => (input as KeepContextTabInput).uri.fsPath),
),
];
}

0 comments on commit 1f186c2

Please sign in to comment.