Skip to content

Commit

Permalink
fix: suppress notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardssh committed Feb 11, 2022
1 parent e174acd commit e2d6002
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
},
"rpc.suppressNotifications": {
"type": "boolean",
"default": false,
"default": true,
"description": "Controls whether error messages should be shown to the user."
},
"rpc.prioritizeLanguagesOverExtensions": {
Expand Down
67 changes: 23 additions & 44 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export class Data implements DisposableLike {
);
}

// Getters

public get fileName(): string | undefined {
const v = this._file ? this._file.name + this._file.ext : undefined;
this.debug(4, `fileName(): ${v}`);
Expand Down Expand Up @@ -154,9 +152,6 @@ export class Data implements DisposableLike {
return this.eventEmitter.event(listener);
}

/**
* Gets called on extensions.onDidChange and initialisaion
*/
private ext(): void {
const ext = extensions.getExtension<GitExtension>('vscode.git');
this.debug(3, `ext(): ${ext ? 'Extension' : 'undefined'}`);
Expand All @@ -174,19 +169,14 @@ export class Data implements DisposableLike {
logInfo(`[data.ts] ext(): activate`);
void ext.activate();
}
}
// Changed to undefined
else if (!ext && this.gitExt) {
} else if (!ext && this.gitExt) {
this.debug(2, `[data.ts] ext(): Changed to undefined`);
this.gitExt = undefined;
this.api(false);
this.dispose(1);
}
}

/**
* Gets called on Extension<GitExtension>.exports.onDidChangeEnablement and this.ext()
*/
private api(e: boolean): void {
this.debug(2, `api(): ${e}`);
if (e) {
Expand Down Expand Up @@ -234,27 +224,21 @@ export class Data implements DisposableLike {
this.eventEmitter.fire();
}

// Helpers

private repo(): Repository | undefined {
if (!this.gitApi) {
return;
}

const repos = this.gitApi.repositories;

// If a file is open, return repo most likely containing the open file.
if (this._file) {
const testString = this._file.dir;
return (
repos
// filter out paths witch are longer than the file path; they can't by definition include them
.filter((v) => v.rootUri.fsPath.length <= testString.length)
// filter out paths wich don't match
.filter(
(v) => v.rootUri.fsPath === testString.substring(0, v.rootUri.fsPath.length)
)
// sort the results length (longest in front)
.sort((a, b) => b.rootUri.fsPath.length - a.rootUri.fsPath.length)
// get first element
.shift()
Expand All @@ -263,40 +247,35 @@ export class Data implements DisposableLike {

this.debug(3, `repo(): no file open`);

// else return the repo closesed to the root of the workspace of lowest index
return (
workspace.workspaceFolders
// array wrap to enable sorting by index
?.map((v) => [v])
// sorting by workspace index
.sort((a, b) => a[0].index - b[0].index)
// get first element (array wraped)
.shift()

?.map((workspace) =>
repos
// filter out paths witch are longer than the file path; they can't by definition include them
.filter((v) => v.rootUri.fsPath.length <= workspace.uri.fsPath.length)
// filter out paths wich don't match
.filter(
(v) =>
v.rootUri.fsPath ===
workspace.uri.fsPath.substring(0, v.rootUri.fsPath.length)
)
// sort the results length (shortest in front)
.sort((a, b) => a.rootUri.fsPath.length - b.rootUri.fsPath.length)
// get first element
.shift()
)
.shift()
);
if (!workspace.workspaceFolders) {
return undefined;
}

return workspace.workspaceFolders
.map((v) => [v])
.sort((a, b) => a[0].index - b[0].index)
.shift()
?.map((workspace) =>
repos
.filter((v) => v.rootUri.fsPath.length <= workspace.uri.fsPath.length)
.filter(
(v) =>
v.rootUri.fsPath ===
workspace.uri.fsPath.substring(0, v.rootUri.fsPath.length)
)
.sort((a, b) => a.rootUri.fsPath.length - b.rootUri.fsPath.length)
.shift()
)
.shift();
}

private remote() {
const remotes = this._repo?.state.remotes;

if (!remotes) {
return;
}

return remotes.find((v) => v.name === 'origin') ?? remotes[0];
}

Expand Down
14 changes: 12 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,24 @@ export const registerComamnds = (ctx: ExtensionContext) => {
await enable();

logInfo('Enabled Discord Rich Presence for this workspace.');
await window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');

if (!config[CONFIG_KEYS.SuppressNotifications]) {
await window.showInformationMessage(
'Enabled Discord Rich Presence for this workspace.'
);
}
});

const disableCommand = commands.registerCommand('rpc.disable', async () => {
await disable();

logInfo('Disabled Discord Rich Presence for this workspace.');
await window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');

if (!config[CONFIG_KEYS.SuppressNotifications]) {
await window.showInformationMessage(
'Disabled Discord Rich Presence for this workspace.'
);
}
});

const reconnectCommand = commands.registerCommand('rpc.reconnect', async () => {
Expand Down

0 comments on commit e2d6002

Please sign in to comment.