Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"default": true,
"description": "Prompt the user when removing all scratchpads"
},
"scratchpads.removeEmptyOnClose": {
"type": "boolean",
"default": false,
"description": "Automatically remove a scratchpad file when it is closed empty"
},
"scratchpads.promptForFilename": {
"type": "boolean",
"default": false,
Expand Down
1 change: 1 addition & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const CONFIG_DEFAULT_FILETYPE = 'defaultFiletype';
export const CONFIG_FILE_PREFIX = 'filePrefix';
export const CONFIG_PROMPT_FOR_FILENAME = 'promptForFilename';
export const CONFIG_PROMPT_FOR_REMOVAL = 'promptForRemoval';
export const CONFIG_REMOVE_EMPTY_ON_CLOSE = 'removeEmptyOnClose';
export const CONFIG_RENAME_WITH_EXTENSION = 'renameWithExtension';
export const CONFIG_SCRATCHPADS_FOLDER = 'scratchpadsFolder';
export const CONFIG_SHOW_IN_EXPLORER = 'showInExplorer';
Expand Down
27 changes: 27 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { Config } from './config';
import { CONFIG_REMOVE_EMPTY_ON_CLOSE } from './consts';
import { FiletypesManager } from './filetypes.manager';
import { ScratchpadTreeProvider } from './scratchpad-tree-provider';
import { ScratchpadsManager } from './scratchpads.manager';
Expand Down Expand Up @@ -101,6 +102,32 @@ export async function activate(context: vscode.ExtensionContext) {
Config.recalculatePaths();
treeViewProvider.refreshOnConfigChange();
});

context.subscriptions.push(
vscode.window.tabGroups.onDidChangeTabs((event) => {
if (!Config.getExtensionConfiguration(CONFIG_REMOVE_EMPTY_ON_CLOSE)) {
return;
}

for (const tab of event.closed) {
if (!(tab.input instanceof vscode.TabInputText)) {
continue;
}

const filePath = tab.input.uri.fsPath;

if (!Utils.isScratchpadFile(filePath)) {
continue;
}

try {
Utils.deleteFileIfEmpty(filePath);
} catch (error) {
console.error('[Scratchpads] removeEmptyOnClose error:', error);
}
}
}),
);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/scratchpads.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,7 @@ export class ScratchpadsManager {
* @param {TextEditor} editor The tab to inspect
*/
private isScratchpadEditor(editor?: vscode.TextEditor) {
if (editor) {
const editorPath = path.dirname(editor.document.fileName);
return editorPath === Config.projectScratchpadsPath;
}

return false;
return editor !== undefined && Utils.isScratchpadFile(editor.document.fileName);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ export default class Utils {
});
}

/**
* Check if the given file path belongs to the scratchpads folder
* @param filePath Full path to the file
*/
public static isScratchpadFile(filePath: string): boolean {
return path.dirname(filePath) === Config.projectScratchpadsPath;
}

/**
* Delete a file if its content is empty or whitespace-only
* @param filePath Full path to the file
*/
public static deleteFileIfEmpty(filePath: string): void {
if (fs.existsSync(filePath) && fs.readFileSync(filePath, 'utf8').trim() === '') {
fs.unlinkSync(filePath);
}
}

/**
* Check if file exists and prompt for overwrite if needed
* @param filePath Path to check
Expand Down