Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix files shown as IDLE when opened with CustomEditor, add support for *.drawio files #61

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
340 changes: 166 additions & 174 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { Presence } from 'discord-rpc';
import { getConfig } from './config';
import {
debug,
DiagnosticSeverity,
env,
languages,
Selection,
TextDocument,
window,
workspace
} from 'vscode';
import { debug, DiagnosticSeverity, env, languages, window, commands, Uri } from 'vscode';
import {
CONFIG_KEYS,
DEBUGGING_IMAGE_KEY,
Expand All @@ -21,20 +12,23 @@ import {
VSCODE_IMAGE_KEY,
VSCODE_INSIDERS_IMAGE_KEY
} from './constants';
import { sep } from 'path';
import { dataClass } from './data';
import { isObject } from './helpers/isObject';
import { isExcluded } from './helpers/isExcluded';
import { getFileIcon, resolveFileIcon, toLower, toTitle, toUpper } from './helpers/resolveFileIcon';
import {
getFileIcon,
resolveFileIcon,
resolveFileIconByUri,
toLower,
toTitle,
toUpper
} from './helpers/resolveFileIcon';

let totalProblems = 0;

export function onDiagnosticsChange() {
const diagnostics = languages.getDiagnostics();

let counted = 0;

diagnostics.forEach((diagnostic) => {
languages.getDiagnostics().forEach((diagnostic) => {
if (diagnostic[1]) {
diagnostic[1].forEach((diagnostic) => {
if (
Expand All @@ -50,7 +44,18 @@ export function onDiagnosticsChange() {
totalProblems = counted;
}

export function activity(previous: Presence = {}, isViewing = false): Presence {
async function getFileURI() {
// temporary workaround to get the file URI of the current active CustomTextEditor
// https://github.com/microsoft/vscode/issues/3553#issuecomment-1098562676

const origClipboard = await env.clipboard.readText();
await commands.executeCommand('copyFilePath');
const filePath = await env.clipboard.readText();
await env.clipboard.writeText(origClipboard);
return Uri.file(filePath);
}
leonardssh marked this conversation as resolved.
Show resolved Hide resolved

export async function activity(previous: Presence = {}, isViewing = false): Promise<Presence> {
const config = getConfig();
const { appName } = env;

Expand Down Expand Up @@ -104,105 +109,104 @@ export function activity(previous: Presence = {}, isViewing = false): Presence {
smallImageText: defaultSmallImageText
};

if (window.activeTextEditor) {
const largeImageKey = resolveFileIcon(window.activeTextEditor.document);
const largeImageText = config[CONFIG_KEYS.LargeImage]
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(largeImageKey))
.replace(REPLACE_KEYS.LanguageTitleCase, toTitle(largeImageKey))
.replace(REPLACE_KEYS.LanguageUpperCase, toUpper(largeImageKey))
.padEnd(2, FAKE_EMPTY);

let isWorkspaceExcluded = false;
let workspaceExcludedText = 'No workspace ignore text provided.';

if (dataClass.workspaceFolder && 'uri' in dataClass.workspaceFolder) {
isWorkspaceExcluded = isExcluded(
config[CONFIG_KEYS.IgnoreWorkspaces],
dataClass.workspaceFolder.uri.fsPath
);
}

if (isWorkspaceExcluded && dataClass.workspaceFolder && dataClass.workspaceFolder.name) {
const ignoreWorkspacesText = config[CONFIG_KEYS.IgnoreWorkspacesText];

workspaceExcludedText = isObject(ignoreWorkspacesText)
? // @ts-ignore Element implicitly has an 'any' type because index expression is not of type 'number'.
ignoreWorkspacesText[dataClass.workspaceFolder.name]
: ignoreWorkspacesText
? ignoreWorkspacesText
: 'No workspace ignore text provided.';
}

presence = {
...presence,
details: removeDetails
? undefined
: isWorkspaceExcluded
? workspaceExcludedText
: details(
CONFIG_KEYS.DetailsIdling,
CONFIG_KEYS.DetailsViewing,
CONFIG_KEYS.DetailsEditing,
CONFIG_KEYS.DetailsDebugging,
isViewing
),
state: removeLowerDetails
? undefined
: isWorkspaceExcluded
? undefined
: details(
CONFIG_KEYS.LowerDetailsIdling,
CONFIG_KEYS.LowerDetailsViewing,
CONFIG_KEYS.LowerDetailsEditing,
CONFIG_KEYS.LowerDetailsDebugging,
isViewing
),
largeImageKey: getFileIcon(largeImageKey),
largeImageText
};

if (config[CONFIG_KEYS.ButtonEnabled] && dataClass.gitRemoteUrl) {
const gitRepo = dataClass.gitRemoteUrl.toString('https').replace(/\.git$/, '');
const gitOrg = dataClass.gitRemoteUrl.organization ?? dataClass.gitRemoteUrl.owner;
const fileURI = await getFileURI(); // obtain fileURI if CustomTextEditor is used
dataClass.setFileURI(fileURI);

const fileIcon = window.activeTextEditor
? resolveFileIcon(window.activeTextEditor.document)
: resolveFileIconByUri(fileURI);
const largeImageText = config[CONFIG_KEYS.LargeImage]
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(fileIcon))
.replace(REPLACE_KEYS.LanguageTitleCase, toTitle(fileIcon))
.replace(REPLACE_KEYS.LanguageUpperCase, toUpper(fileIcon))
.padEnd(2, FAKE_EMPTY);

let isWorkspaceExcluded = false;
let workspaceExcludedText = 'No workspace ignore text provided.';

if (dataClass.workspaceFolder && 'uri' in dataClass.workspaceFolder) {
isWorkspaceExcluded = isExcluded(
config[CONFIG_KEYS.IgnoreWorkspaces],
dataClass.workspaceFolder.uri.fsPath
);
}

const isRepositoryExcluded = isExcluded(
config[CONFIG_KEYS.IgnoreRepositories],
gitRepo
);
if (isWorkspaceExcluded && dataClass.workspaceFolder && dataClass.workspaceFolder.name) {
const ignoreWorkspacesText = config[CONFIG_KEYS.IgnoreWorkspacesText];

const isOrganizationExcluded = isExcluded(
config[CONFIG_KEYS.IgnoreOrganizations],
gitOrg
);
workspaceExcludedText = isObject(ignoreWorkspacesText)
? // @ts-ignore Element implicitly has an 'any' type because index expression is not of type 'number'.
ignoreWorkspacesText[dataClass.workspaceFolder.name]
: ignoreWorkspacesText
? ignoreWorkspacesText
: 'No workspace ignore text provided.';
}

const isNotExcluded =
!isRepositoryExcluded && !isWorkspaceExcluded && !isOrganizationExcluded;
presence = {
...presence,
details: removeDetails
? undefined
: isWorkspaceExcluded
? workspaceExcludedText
: details(
CONFIG_KEYS.DetailsIdling,
CONFIG_KEYS.DetailsViewing,
CONFIG_KEYS.DetailsEditing,
CONFIG_KEYS.DetailsDebugging,
isViewing,
fileIcon
),
state: removeLowerDetails
? undefined
: isWorkspaceExcluded
? undefined
: details(
CONFIG_KEYS.LowerDetailsIdling,
CONFIG_KEYS.LowerDetailsViewing,
CONFIG_KEYS.LowerDetailsEditing,
CONFIG_KEYS.LowerDetailsDebugging,
isViewing,
fileIcon
),
largeImageKey: getFileIcon(fileIcon),
largeImageText
};

if (gitRepo && config[CONFIG_KEYS.ButtonActiveLabel] && isNotExcluded) {
presence = {
...presence,
buttons: [
{
label: config[CONFIG_KEYS.ButtonActiveLabel],
url: gitRepo
}
]
};
} else if (
!gitRepo &&
config[CONFIG_KEYS.ButtonInactiveLabel] &&
config[CONFIG_KEYS.ButtonInactiveUrl]
) {
presence = {
...presence,
buttons: [
{
label: config[CONFIG_KEYS.ButtonInactiveLabel],
url: config[CONFIG_KEYS.ButtonInactiveUrl]
}
]
};
}
if (config[CONFIG_KEYS.ButtonEnabled] && dataClass.gitRemoteUrl) {
const gitRepo = dataClass.gitRemoteUrl.toString('https').replace(/\.git$/, '');
const gitOrg = dataClass.gitRemoteUrl.organization ?? dataClass.gitRemoteUrl.owner;

const isRepositoryExcluded = isExcluded(config[CONFIG_KEYS.IgnoreRepositories], gitRepo);

const isOrganizationExcluded = isExcluded(config[CONFIG_KEYS.IgnoreOrganizations], gitOrg);

const isNotExcluded =
!isRepositoryExcluded && !isWorkspaceExcluded && !isOrganizationExcluded;

if (gitRepo && config[CONFIG_KEYS.ButtonActiveLabel] && isNotExcluded) {
presence = {
...presence,
buttons: [
{
label: config[CONFIG_KEYS.ButtonActiveLabel],
url: gitRepo
}
]
};
} else if (
!gitRepo &&
config[CONFIG_KEYS.ButtonInactiveLabel] &&
config[CONFIG_KEYS.ButtonInactiveUrl]
) {
presence = {
...presence,
buttons: [
{
label: config[CONFIG_KEYS.ButtonInactiveLabel],
url: config[CONFIG_KEYS.ButtonInactiveUrl]
}
]
};
}
}

Expand All @@ -214,53 +218,36 @@ function details(
viewing: CONFIG_KEYS,
editing: CONFIG_KEYS,
debugging: CONFIG_KEYS,
isViewing: boolean
isViewing: boolean,
_fileIcon: string | undefined = undefined // idle if undefined
) {
const config = getConfig();

let raw = (config[idling] as string).replace(REPLACE_KEYS.Empty, FAKE_EMPTY);

const workspaceFolderName = dataClass.workspaceFolder
? dataClass.workspaceFolder.name
: config[CONFIG_KEYS.LowerDetailsNoWorkspaceFound].replace(REPLACE_KEYS.Empty, FAKE_EMPTY);
const workspaceName = dataClass.workspace
? dataClass.workspace.replace(REPLACE_KEYS.VSCodeWorkspace, EMPTY)
: workspaceFolderName;
const workspaceAndFolder = `${workspaceName}${
workspaceFolderName === FAKE_EMPTY ? '' : ` - ${workspaceFolderName}`
}`;
const problems = config[CONFIG_KEYS.ShowProblems]
? config[CONFIG_KEYS.ProblemsText].replace(
REPLACE_KEYS.ProblemsCount,
totalProblems.toString()
)
: '';
if (window.activeTextEditor) {
const noWorkspaceFound = config[CONFIG_KEYS.LowerDetailsNoWorkspaceFound].replace(
REPLACE_KEYS.Empty,
FAKE_EMPTY
);

const workspaceFolderName = dataClass.workspaceFolder
? dataClass.workspaceFolder.name
: noWorkspaceFound;
const workspaceName = dataClass.workspace
? dataClass.workspace.replace(REPLACE_KEYS.VSCodeWorkspace, EMPTY)
: workspaceFolderName;
const workspaceAndFolder = `${workspaceName}${
workspaceFolderName === FAKE_EMPTY ? '' : ` - ${workspaceFolderName}`
}`;

const fileIcon = resolveFileIcon(window.activeTextEditor.document);
const problems = config[CONFIG_KEYS.ShowProblems]
? config[CONFIG_KEYS.ProblemsText].replace(
REPLACE_KEYS.ProblemsCount,
totalProblems.toString()
)
: '';

raw = config[
debug.activeDebugSession ? debugging : isViewing ? viewing : editing
] as string;

if (dataClass.workspace) {
const name = dataClass.workspace;
const relativePath = workspace
.asRelativePath(window.activeTextEditor.document.fileName)
.split(sep);

relativePath.splice(-1, 1);
raw = raw.replace(REPLACE_KEYS.FullDirName, `${name}${sep}${relativePath.join(sep)}`);
}

raw = fileDetails(raw, window.activeTextEditor.document, window.activeTextEditor.selection);

raw = raw
const { document, selection } = window.activeTextEditor;
const fileIcon = resolveFileIcon(document);
raw = config[debug.activeDebugSession ? debugging : isViewing ? viewing : editing]
.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString())
.replace(REPLACE_KEYS.CurrentLine, (selection.active.line + 1).toLocaleString())
.replace(REPLACE_KEYS.CurrentColumn, (selection.active.character + 1).toLocaleString())
.replace(REPLACE_KEYS.FullDirName, dataClass.fullDirName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.FileName, dataClass.fileName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.DirName, dataClass.dirName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.Workspace, workspaceName)
Expand All @@ -277,27 +264,32 @@ function details(
: dataClass.gitRepoName ?? FAKE_EMPTY
)
.replace(REPLACE_KEYS.GitBranch, dataClass.gitBranchName ?? FAKE_EMPTY);
}

return raw;
}

function fileDetails(_raw: string, document: TextDocument, selection: Selection) {
let raw = _raw.slice();

if (raw.includes(REPLACE_KEYS.TotalLines)) {
raw = raw.replace(REPLACE_KEYS.TotalLines, document.lineCount.toLocaleString());
}

if (raw.includes(REPLACE_KEYS.CurrentLine)) {
raw = raw.replace(REPLACE_KEYS.CurrentLine, (selection.active.line + 1).toLocaleString());
}

if (raw.includes(REPLACE_KEYS.CurrentColumn)) {
raw = raw.replace(
REPLACE_KEYS.CurrentColumn,
(selection.active.character + 1).toLocaleString()
);
} else if (_fileIcon !== undefined) {
// since this file is now edited with a CustomTextEditor, the following limitations apply:
// - unable to distinguish between debugging, viewing or editing -> editing
// - unable to show TotalLines, CurrentLine and CurrentColumn -> zeroed / emptied

raw = config[editing]
.replace(REPLACE_KEYS.TotalLines, FAKE_EMPTY)
.replace(REPLACE_KEYS.CurrentLine, '0')
.replace(REPLACE_KEYS.CurrentColumn, '0')
leonardssh marked this conversation as resolved.
Show resolved Hide resolved
.replace(REPLACE_KEYS.FullDirName, dataClass.fullDirName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.FileName, dataClass.fileName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.DirName, dataClass.dirName ?? FAKE_EMPTY)
.replace(REPLACE_KEYS.Workspace, workspaceName)
.replace(REPLACE_KEYS.WorkspaceFolder, workspaceFolderName)
.replace(REPLACE_KEYS.WorkspaceAndFolder, workspaceAndFolder)
.replace(REPLACE_KEYS.LanguageLowerCase, toLower(_fileIcon))
.replace(REPLACE_KEYS.LanguageTitleCase, toTitle(_fileIcon))
.replace(REPLACE_KEYS.LanguageUpperCase, toUpper(_fileIcon))
.replace(REPLACE_KEYS.Problems, problems)
.replace(
REPLACE_KEYS.GitRepo,
dataClass.gitRemoteUrl
? dataClass.gitRemoteUrl.name
: dataClass.gitRepoName ?? FAKE_EMPTY
)
.replace(REPLACE_KEYS.GitBranch, dataClass.gitBranchName ?? FAKE_EMPTY);
}

return raw;
Expand Down
Loading