Skip to content

Commit

Permalink
fix: clean up hanging memory and declare no affiliation to microsoft …
Browse files Browse the repository at this point in the history
…or vscode
  • Loading branch information
ZanzyTHEbar committed Jul 12, 2024
1 parent afb4a89 commit c4d9ddf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Navigate to your desired directory and execute following commands in the termina

```bash
git clone https://github.com/ZanzyTHEbar/vscode-nautilus.git
cd vscode-nautilus/vscode-workspaces
cd vscode-nautilus/gnome-extension

make && make pack && make install
```
Expand Down
2 changes: 1 addition & 1 deletion gnome-extension/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "VSCode Workspaces",
"description": "A VSCode/Codium Workspace management tool-set for GNOME",
"description": "A VSCode/Codium Workspace management tool-set for GNOME - This extension is not affiliated, funded, or in any way associated with Microsoft and vscode software.",
"uuid": "[email protected]",
"url": "https://github.com/ZanzyTHEbar/vscode-nautilus",
"settings-schema": "org.gnome.shell.extensions.vscode-workspaces",
Expand Down
42 changes: 22 additions & 20 deletions gnome-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export default class VSCodeWorkspacesExtension extends Extension {
GLib.get_home_dir(),
'.config/Code/User/workspaceStorage',
]);
_workspaces: Set<Workspace> = new Set();
_recentWorkspaces: Set<RecentWorkspace> = new Set();
_workspaces: Set<Workspace> | null = new Set();
_recentWorkspaces: Set<RecentWorkspace> | null = new Set();

// TODO: Implement notifications
//_messageTray: MessageTray.MessageTray | null = null;
Expand Down Expand Up @@ -101,6 +101,8 @@ export default class VSCodeWorkspacesExtension extends Extension {
this._indicator = undefined;
}
this.gsettings = undefined;
this._recentWorkspaces = null;
this._workspaces = null;

this._log(`VSCode Workspaces Extension disabled`);
}
Expand Down Expand Up @@ -138,7 +140,7 @@ export default class VSCodeWorkspacesExtension extends Extension {
_loadRecentWorkspaces() {
this._getRecentWorkspaces();

if (this._recentWorkspaces.size === 0) {
if (this._recentWorkspaces?.size === 0) {
this._log('No recent workspaces found');
return;
}
Expand All @@ -161,7 +163,7 @@ export default class VSCodeWorkspacesExtension extends Extension {

// Create the PopupMenu for the ComboBox items

this._recentWorkspaces.forEach(workspace => {
this._recentWorkspaces?.forEach(workspace => {
const item = new PopupMenu.PopupMenuItem(workspace.name);

const trashIcon = new St.Icon({
Expand Down Expand Up @@ -264,7 +266,7 @@ export default class VSCodeWorkspacesExtension extends Extension {

callback(newWorkspace);
// Check if a workspace with the same uri exists
const workspaceExists = Array.from(this._workspaces).some(workspace => {
const workspaceExists = Array.from(this._workspaces!).some(workspace => {
return workspace.uri === workspaceURI;
});

Expand All @@ -274,12 +276,12 @@ export default class VSCodeWorkspacesExtension extends Extension {
}

// use a cache to avoid reprocessing the same directory/file
if (this._workspaces.has(newWorkspace)) {
if (this._workspaces?.has(newWorkspace)) {
this._log(`Workspace already exists: ${newWorkspace}`);
continue;
}

this._workspaces.add(newWorkspace);
this._workspaces?.add(newWorkspace);
} catch (error) {
logError(error as object, 'Failed to parse workspace.json');
continue;
Expand Down Expand Up @@ -319,7 +321,7 @@ export default class VSCodeWorkspacesExtension extends Extension {
this._log(
`Workspace does not exist and will be removed from the list: ${pathToWorkspace.get_path()}`
);
this._workspaces.delete(workspace);
this._workspaces?.delete(workspace);
return;
}

Expand Down Expand Up @@ -382,7 +384,7 @@ export default class VSCodeWorkspacesExtension extends Extension {
this._log(`Found .code-workspace file in ${workspace.uri}`);

// Check if a workspace with the same uri as the workspaceFile exists
const workspaceFileExists = Array.from(this._workspaces).some(_workspace => {
const workspaceFileExists = Array.from(this._workspaces!).some(_workspace => {
return _workspace.uri === workspaceFile.get_uri();
});

Expand All @@ -392,24 +394,24 @@ export default class VSCodeWorkspacesExtension extends Extension {

this._log(`Workspace already exists in recent workspaces - Removing directory workspace in favour of code-workspace file: ${workspaceFile.get_uri()}`);

this._workspaces.delete(workspace);
this._workspaces?.delete(workspace);

// remove the directory workspace from the cache
const recentWorkspaceObject = Array.from(this._recentWorkspaces).find(
const recentWorkspaceObject = Array.from(this._recentWorkspaces!).find(
recentWorkspace => recentWorkspace.path === workspace.uri
);

recentWorkspaceObject?.softRemove();

this._recentWorkspaces = new Set(
Array.from(this._recentWorkspaces).filter(
Array.from(this._recentWorkspaces!).filter(
recentWorkspace => recentWorkspace.path !== workspace.uri
)
);
});

// sort the workspace files by access time
this._workspaces = new Set(Array.from(this._workspaces).sort((a, b) => {
this._workspaces = new Set(Array.from(this._workspaces!).sort((a, b) => {

const aInfo = Gio.File.new_for_uri(a.uri).query_info('standard::*,unix::atime', Gio.FileQueryInfoFlags.NONE, null);
const bInfo = Gio.File.new_for_uri(b.uri).query_info('standard::*,unix::atime', Gio.FileQueryInfoFlags.NONE, null);
Expand Down Expand Up @@ -437,7 +439,7 @@ export default class VSCodeWorkspacesExtension extends Extension {

this._recentWorkspaces = new Set(
Array.from(this._workspaces).map(workspace => {
let workspaceName = GLib.path_get_basename(workspace.uri);
let workspaceName = GLib.path_get_basename(workspace?.uri);

// if there is`.code-workspace` included in the workspaceName, remove it
if (workspaceName.endsWith('.code-workspace')) {
Expand All @@ -450,10 +452,10 @@ export default class VSCodeWorkspacesExtension extends Extension {
softRemove: () => {
this._log(`Moving Workspace to Trash: ${workspaceName}`);
// Purge from the recent workspaces
this._workspaces.delete(workspace);
this._workspaces?.delete(workspace);
// Purge from the cache
this._recentWorkspaces = new Set(
Array.from(this._recentWorkspaces).filter(
Array.from(this._recentWorkspaces!).filter(
recentWorkspace => recentWorkspace.path !== workspace.uri
)
);
Expand All @@ -474,10 +476,10 @@ export default class VSCodeWorkspacesExtension extends Extension {
removeWorkspaceItem: () => {
this._log(`Removing workspace: ${workspaceName}`);
// Purge from the recent workspaces
this._workspaces.delete(workspace);
this._workspaces?.delete(workspace);
// Purge from the cache
this._recentWorkspaces = new Set(
Array.from(this._recentWorkspaces).filter(
Array.from(this._recentWorkspaces!).filter(
recentWorkspace => recentWorkspace.path !== workspace.uri
)
);
Expand Down Expand Up @@ -617,8 +619,8 @@ export default class VSCodeWorkspacesExtension extends Extension {
);

// Purge the cache
this._workspaces.clear();
this._recentWorkspaces.clear();
this._workspaces?.clear();
this._recentWorkspaces?.clear();

// Refresh the menu to reflect the changes

Expand Down
20 changes: 11 additions & 9 deletions gnome-extension/src/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import {
gettext as _,
} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

export default class GnomeRectanglePreferences extends ExtensionPreferences {
_settings?: Gio.Settings;

export default class VSCodeWorkspacesPreferences extends ExtensionPreferences {
fillPreferencesWindow(window: Adw.PreferencesWindow) {
this._settings = this.getSettings();
const _settings = this.getSettings();

const page = new Adw.PreferencesPage({
title: _('General'),
Expand Down Expand Up @@ -75,34 +73,34 @@ export default class GnomeRectanglePreferences extends ExtensionPreferences {
refreshIntervalGroup.add(refreshGroupEntry);

// Bind settings
this._settings!.bind(
_settings.bind(
'new-window',
newWindowSwitch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
this._settings!.bind(
_settings.bind(
'vscode-location',
vscodeLocation,
'text',
Gio.SettingsBindFlags.DEFAULT
);

this._settings!.bind(
_settings.bind(
'debug',
debug,
'active',
Gio.SettingsBindFlags.DEFAULT
);

this._settings!.bind(
_settings.bind(
'prefer-workspace-file',
preferWorkspaceFile,
'active',
Gio.SettingsBindFlags.DEFAULT
);

this._settings!.bind(
_settings.bind(
'refresh-interval',
refreshGroupEntry,
'value',
Expand All @@ -112,5 +110,9 @@ export default class GnomeRectanglePreferences extends ExtensionPreferences {
// Show the window
// Add the page to the window
window.add(page);

window.connect('close-request', () => {
_settings.apply();
});
}
}

0 comments on commit c4d9ddf

Please sign in to comment.