Skip to content

Commit

Permalink
feat: add prioritizeLanguagesOverExtensions config property
Browse files Browse the repository at this point in the history
Controls whether languages should be prioritized over extensions when matching a file name.

fix #46
  • Loading branch information
leonardssh committed Jan 5, 2022
1 parent 8ece5fa commit 99b1bc1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@
"type": "boolean",
"default": false,
"description": "Controls whether error messages should be shown to the user."
},
"rpc.prioritizeLanguagesOverExtensions": {
"type": "boolean",
"default": false,
"description": "Controls whether languages should be prioritized over extensions when matching a file name."
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type WorkspaceExtensionConfiguration = WorkspaceConfiguration & {
ignoreRepositories: string[];
ignoreOrganizations: string[];
suppressNotifications: boolean;
prioritizeLanguagesOverExtensions: boolean;
};

export function getConfig(): WorkspaceExtensionConfiguration {
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export const enum CONFIG_KEYS {
ButtonInactiveUrl = 'buttonInactiveUrl',
IgnoreRepositories = 'ignoreRepositories',
IgnoreOrganizations = 'ignoreOrganizations',
SuppressNotifications = 'suppressNotifications'
SuppressNotifications = 'suppressNotifications',
PrioritizeLanguagesOverExtensions = 'prioritizeLanguagesOverExtensions'
}

export const enum REPLACE_KEYS {
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { basename } from 'path';
import { TextDocument } from 'vscode';
import { KNOWN_EXTENSIONS, KNOWN_LANGUAGES } from './constants';
import { getConfig } from './config';
import { CONFIG_KEYS, KNOWN_EXTENSIONS, KNOWN_LANGUAGES } from './constants';

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 config = getConfig();
const filename = basename(document.fileName);
const findKnownExtension = Object.keys(KNOWN_EXTENSIONS).find((key) => {
if (filename.endsWith(key)) {
Expand All @@ -22,9 +24,13 @@ export function resolveFileIcon(document: TextDocument) {
return regex.test(filename);
});

const areLanguagesPrioritized = config[CONFIG_KEYS.PrioritizeLanguagesOverExtensions];
const findKnownLanguage = KNOWN_LANGUAGES.find((key) => key.language === document.languageId);

const fileIcon = findKnownExtension ? KNOWN_EXTENSIONS[findKnownExtension] : findKnownLanguage ? findKnownLanguage.image : null;
const knownExtension = findKnownExtension ? KNOWN_EXTENSIONS[findKnownExtension] : findKnownLanguage ? findKnownLanguage.image : null;
const knownLanguage = findKnownLanguage ? findKnownLanguage.image : knownExtension;

const fileIcon = areLanguagesPrioritized ? knownLanguage : knownExtension;

return typeof fileIcon === 'string' ? fileIcon : fileIcon?.image ?? 'text';
}

0 comments on commit 99b1bc1

Please sign in to comment.