2021-02-10 03:24:42 +00:00
|
|
|
import { basename } from 'path';
|
2021-04-10 12:42:28 +00:00
|
|
|
import { TextDocument, workspace, extensions, WorkspaceConfiguration } from 'vscode';
|
2021-02-10 03:24:42 +00:00
|
|
|
|
|
|
|
import { KNOWN_EXTENSIONS, KNOWN_LANGUAGES } from './constants';
|
2022-01-30 17:43:29 +00:00
|
|
|
import type { API, GitExtension } from './git';
|
2021-04-10 12:42:28 +00:00
|
|
|
import { log, LogLevel } from './logger';
|
2021-02-10 03:24:42 +00:00
|
|
|
|
2021-04-14 12:56:15 +00:00
|
|
|
let git: API | null | undefined;
|
|
|
|
|
2021-02-10 19:32:47 +00:00
|
|
|
type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
|
2021-02-10 03:24:42 +00:00
|
|
|
enabled: boolean;
|
2021-02-10 15:54:44 +00:00
|
|
|
detailsIdling: string;
|
|
|
|
detailsEditing: string;
|
|
|
|
detailsDebugging: string;
|
|
|
|
lowerDetailsIdling: string;
|
|
|
|
lowerDetailsEditing: string;
|
|
|
|
lowerDetailsDebugging: string;
|
|
|
|
lowerDetailsNoWorkspaceFound: string;
|
|
|
|
largeImageIdling: string;
|
|
|
|
largeImage: string;
|
|
|
|
smallImage: string;
|
|
|
|
suppressNotifications: boolean;
|
|
|
|
workspaceExcludePatterns: string[];
|
2021-02-10 17:32:13 +00:00
|
|
|
swapBigAndSmallImage: boolean;
|
2021-02-11 00:18:04 +00:00
|
|
|
removeDetails: boolean;
|
|
|
|
removeLowerDetails: boolean;
|
2021-02-26 19:01:36 +00:00
|
|
|
removeTimestamp: boolean;
|
2021-04-10 12:42:28 +00:00
|
|
|
removeRemoteRepository: boolean;
|
2021-04-10 13:17:11 +00:00
|
|
|
idleTimeout: number;
|
2021-02-10 03:24:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export function getConfig() {
|
2021-02-10 19:32:47 +00:00
|
|
|
return workspace.getConfiguration('discord') as WorkspaceExtensionConfiguration;
|
2021-02-10 03:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const toLower = (str: string) => str.toLocaleLowerCase();
|
|
|
|
|
|
|
|
export const toUpper = (str: string) => str.toLocaleUpperCase();
|
|
|
|
|
|
|
|
export const toTitle = (str: string) => toLower(str).replace(/^\w/, (c) => toUpper(c));
|
|
|
|
|
|
|
|
export function resolveFileIcon(document: TextDocument) {
|
|
|
|
const filename = basename(document.fileName);
|
|
|
|
const findKnownExtension = Object.keys(KNOWN_EXTENSIONS).find((key) => {
|
|
|
|
if (filename.endsWith(key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const match = /^\/(.*)\/([mgiy]+)$/.exec(key);
|
|
|
|
if (!match) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const regex = new RegExp(match[1], match[2]);
|
|
|
|
return regex.test(filename);
|
|
|
|
});
|
|
|
|
const findKnownLanguage = KNOWN_LANGUAGES.find((key) => key.language === document.languageId);
|
|
|
|
const fileIcon = findKnownExtension
|
|
|
|
? KNOWN_EXTENSIONS[findKnownExtension]
|
|
|
|
: findKnownLanguage
|
|
|
|
? findKnownLanguage.image
|
|
|
|
: null;
|
|
|
|
|
|
|
|
return typeof fileIcon === 'string' ? fileIcon : fileIcon?.image ?? 'text';
|
|
|
|
}
|
2021-04-10 12:42:28 +00:00
|
|
|
|
|
|
|
export async function getGit() {
|
2021-04-14 12:56:15 +00:00
|
|
|
if (git || git === null) {
|
|
|
|
return git;
|
|
|
|
}
|
|
|
|
|
2021-04-10 12:42:28 +00:00
|
|
|
try {
|
|
|
|
log(LogLevel.Debug, 'Loading git extension');
|
|
|
|
const gitExtension = extensions.getExtension<GitExtension>('vscode.git');
|
|
|
|
if (!gitExtension?.isActive) {
|
|
|
|
log(LogLevel.Trace, 'Git extension not activated, activating...');
|
|
|
|
await gitExtension?.activate();
|
|
|
|
}
|
|
|
|
git = gitExtension?.exports.getAPI(1);
|
|
|
|
} catch (error) {
|
2021-04-14 12:56:15 +00:00
|
|
|
git = null;
|
2021-04-10 12:42:28 +00:00
|
|
|
log(LogLevel.Error, `Failed to load git extension, is git installed?; ${error as string}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return git;
|
|
|
|
}
|