Skip to content

Commit

Permalink
fix: Support case sensitive file types (#1047)
Browse files Browse the repository at this point in the history
Fix: #1043
  • Loading branch information
Jason3S authored Jul 15, 2021
1 parent 9ad82b3 commit 933fb99
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
33 changes: 27 additions & 6 deletions packages/_server/src/config/documentSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import * as cspell from 'cspell-lib';
import { getDefaultSettings, Pattern } from 'cspell-lib';
import * as os from 'os';
import * as Path from 'path';
import { mocked } from 'ts-jest/utils';
import { DocumentSettings, isUriAllowed, isUriBlocked, debugExports, correctBadSettings, ExcludedByMatch } from './documentSettings';
import { Connection, WorkspaceFolder } from 'vscode-languageserver/node';
import { getWorkspaceFolders, getConfiguration } from './vscode.config';
import * as Path from 'path';
import { URI as Uri } from 'vscode-uri';
import * as cspell from 'cspell-lib';
import { Pattern, getDefaultSettings } from 'cspell-lib';
import { CSpellUserSettings } from '../config/cspellConfig';
import * as os from 'os';
import { escapeRegExp } from './../utils/util';
import {
correctBadSettings,
debugExports,
DocumentSettings,
ExcludedByMatch,
isUriAllowed,
isUriBlocked,
__testing__,
} from './documentSettings';
import { getConfiguration, getWorkspaceFolders } from './vscode.config';

jest.mock('vscode-languageserver/node');
jest.mock('./vscode.config');
Expand Down Expand Up @@ -161,6 +169,19 @@ describe('Validate DocumentSettings', () => {
expect(settings.enabledLanguageIds).toEqual(expect.arrayContaining(['php', 'json', 'pug']));
});

test('applyEnableFiletypes', () => {
const settings: CSpellUserSettings = {
enabledLanguageIds: ['typescript', 'markdown', 'plaintext', 'json'],
enableFiletypes: ['!json', '!!!javascript'],
};
const enabled = __testing__.extractEnableFiletypes(settings, {
enableFiletypes: ['typescript', '!plaintext', 'FreeFormFortran', '!!json', '!!javascript'],
});
const r = __testing__.applyEnableFiletypes(enabled, settings);
// cspell:ignore freeformfortran
expect(r.enabledLanguageIds).toEqual(['typescript', 'markdown', 'FreeFormFortran', 'json']);
});

test('isExcludedBy', async () => {
const mockFolders: WorkspaceFolder[] = [workspaceFolderServer];
mockGetWorkspaceFolders.mockReturnValue(Promise.resolve(mockFolders));
Expand Down
34 changes: 22 additions & 12 deletions packages/_server/src/config/documentSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,26 @@ function extractEnableFiletypes(...settings: CSpellUserSettings[]): string[] {
function applyEnableFiletypes(enableFiletypes: string[], settings: CSpellUserSettings): CSpellUserSettings {
const { enableFiletypes: _, enabledLanguageIds = [], ...rest } = settings;
const enabled = new Set(enabledLanguageIds);
enableFiletypes
.filter((a) => !!a)
.map((a) => a.toLowerCase())
.forEach((lang) => {
if (lang[0] === '!') {
enabled.delete(lang.slice(1));
} else {
enabled.add(lang);
}
});
normalizeEnableFiletypes(enableFiletypes).forEach((lang) => {
if (lang[0] === '!') {
enabled.delete(lang.slice(1));
} else {
enabled.add(lang);
}
});
return enabled.size || settings.enabledLanguageIds !== undefined ? { ...rest, enabledLanguageIds: [...enabled] } : { ...rest };
}

function normalizeEnableFiletypes(enableFiletypes: string[]): string[] {
const ids = enableFiletypes
.map((id) => id.replace(/!/g, '~')) // Use ~ for better sorting
.sort()
.map((id) => id.replace(/~/g, '!')) // Restore the !
.map((id) => id.replace(/^(!!)+/, '')); // Remove extra !! pairs

return ids;
}

function _matchingFoldersForUri(folders: WorkspaceFolder[], docUri: string): WorkspaceFolder[] {
return folders.filter(({ uri }) => docUri.startsWith(uri)).sort((a, b) => b.uri.length - a.uri.length);
}
Expand Down Expand Up @@ -483,7 +490,7 @@ const regExIsOwnedByExtension = /\bstreetsidesoftware\.code-spell-checker\b/;
* @param settings - finalized settings
* @returns array of Settings
*/
function extractCSpellFileConfigurations(settings: CSpellUserSettings): CSpellSettingsWithFileSource[] {
export function extractCSpellFileConfigurations(settings: CSpellUserSettings): CSpellSettingsWithFileSource[] {
const sources = getSources(settings);
const configs = sources
.filter(isCSpellSettingsWithFileSource)
Expand All @@ -499,7 +506,7 @@ function extractCSpellFileConfigurations(settings: CSpellUserSettings): CSpellSe
* @param settings - finalized settings
* @returns
*/
function extractTargetDictionaries(settings: CSpellUserSettings): DictionaryDefinitionCustom[] {
export function extractTargetDictionaries(settings: CSpellUserSettings): DictionaryDefinitionCustom[] {
const { dictionaries = [], dictionaryDefinitions = [] } = settings;
const defs = new Map(dictionaryDefinitions.map((d) => [d.name, d]));
const activeDicts = dictionaries.map((name) => defs.get(name)).filter(isDefined);
Expand All @@ -522,4 +529,7 @@ function isDefined<T>(t: T | undefined): t is T {

export const __testing__ = {
extractTargetDictionaries,
extractEnableFiletypes,
normalizeEnableFiletypes,
applyEnableFiletypes,
};
15 changes: 12 additions & 3 deletions packages/client/src/infoViewer/infoView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,10 @@ function extractViewerConfigFromConfig(

function applyEnableFiletypesToEnabledLanguageIds(
languageIds: string[] | undefined = [],
enabledFiletypes: string[] | undefined = []
enableFiletypes: string[] | undefined = []
): string[] {
const ids = new Set(languageIds);
enabledFiletypes
.filter((a) => !!a)
normalizeEnableFiletypes(enableFiletypes)
.map((lang) => ({ enable: lang[0] !== '!', lang: lang.replace('!', '') }))
.forEach(({ enable, lang }) => {
if (enable) {
Expand All @@ -351,6 +350,16 @@ function extractViewerConfigFromConfig(
return [...ids];
}

function normalizeEnableFiletypes(enableFiletypes: string[]): string[] {
const ids = enableFiletypes
.map((id) => id.replace(/!/g, '~')) // Use ~ for better sorting
.sort()
.map((id) => id.replace(/~/g, '!')) // Restore the !
.map((id) => id.replace(/^(!!)+/, '')); // Remove extra !! pairs

return ids;
}

function inspectKeyToOrder(a: InspectKeys): number {
return configOrderRev.get(a) || 0;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ export function enableLocaleForTarget(
* @param currentValues the value to update.
*/
function updateEnableFiletypes(languageId: string, enable: boolean, currentValues: string[] | undefined) {
const values = new Set((currentValues || []).map((v) => v.toLowerCase()));
languageId = languageId.toLowerCase();
const values = new Set(currentValues || []);
const disabledLangId = '!' + languageId;
if (enable) {
if (values.has(disabledLangId)) {
Expand Down
16 changes: 16 additions & 0 deletions samples/code/example.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PROGRAM Triangle
IMPLICIT NONE
REAL :: a, b, c, Area
PRINT *, 'Welcome, please enter the&
&lengths of the 3 sides.'
READ *, a, b, c
PRINT *, 'Triangle''s area: ', Area(a,b,c)
END PROGRAM Triangle
FUNCTION Area(x,y,z)
IMPLICIT NONE
REAL :: Area ! function type
REAL, INTENT( IN ) :: x, y, z
REAL :: theta, height
theta = ACOS((x**2+y**2-z**2)/(2.0*x*y))
height = x*SIN(theta); Area = 0.5*y*height
END FUNCTION Area
9 changes: 9 additions & 0 deletions samples/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "0.2",
"enableFiletypes": [
"FortranFreeForm",
"php",
"ruby"
],
"language": "en"
}

0 comments on commit 933fb99

Please sign in to comment.