Skip to content

Commit

Permalink
feat: Support enableFileTypes (#439)
Browse files Browse the repository at this point in the history
* feat: Support enableFileTypes 

* refactor: fix spelling of `locale`

* fix: Make sure the settings viewer shows the right values

Update the settings viewer to show the correct language ids.

* fix: Only update VS Code settings if cspell.json doesn't exist
  • Loading branch information
Jason3S authored Feb 22, 2020
1 parent 29b8fb6 commit 2fde3bc
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 241 deletions.
4 changes: 2 additions & 2 deletions packages/_integrationTests/src/ExtensionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export interface ExtensionApi {
disableCurrentLanguage(): Thenable<void>;
addWordToUserDictionary(word: string): Thenable<void>;
addWordToWorkspaceDictionary(word: string, uri?: string | null): Thenable<void>;
// enableLocal(target: ConfigTarget, local: string): Thenable<void>;
// disableLocal(target: ConfigTarget, local: string): Thenable<void>;
// enableLocale(target: ConfigTarget, locale: string): Thenable<void>;
// disableLocale(target: ConfigTarget, locale: string): Thenable<void>;
updateSettings(): boolean;
// cSpellClient(): CSpellClient;
}
4 changes: 2 additions & 2 deletions packages/_integrationTests/src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ const apiSignature: Api = {
// cSpellClient: 'cSpellClient',
disableCurrentLanguage: 'disableCurrentLanguage',
disableLanguageId: 'disableLanguageId',
// disableLocal: 'disableLocal',
// disableLocale: 'disableLocale',
enableCurrentLanguage: 'enableCurrentLanguage',
enableLanguageId: 'enableLanguageId',
// enableLocal: 'enableLocal',
// enableLocale: 'enableLocale',
registerConfig: 'registerConfig',
triggerGetSettings: 'triggerGetSettings',
updateSettings: 'updateSettings',
Expand Down
32 changes: 30 additions & 2 deletions packages/_server/src/documentSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export class DocumentSettings {
log('getUriSettings:', uri);
const r = uri
? await this.fetchUriSettings(uri)
: CSpell.mergeSettings(this.defaultSettings, this.importedSettings());
: applyEnableFiletypes(
extractEnableFiletypes(this.defaultSettings, this.importedSettings()),
CSpell.mergeSettings(this.defaultSettings, this.importedSettings())
);
return r;
}

Expand Down Expand Up @@ -122,7 +125,10 @@ export class DocumentSettings {
private async fetchUriSettings(uri: string): Promise<CSpellUserSettings> {
log('Start fetchUriSettings:', uri);
const folderSettings = await this.fetchSettingsForUri(uri);
const spellSettings = CSpell.mergeSettings(this.defaultSettings, this.importedSettings(), folderSettings.settings);
const importedSettings = this.importedSettings();
const mergedSettings = CSpell.mergeSettings(this.defaultSettings, importedSettings, folderSettings.settings);
const enabledFiletypes = extractEnableFiletypes(this.defaultSettings, importedSettings, folderSettings.settings);
const spellSettings = applyEnableFiletypes(enabledFiletypes, mergedSettings);
const fileUri = Uri.parse(uri);
const fileSettings = CSpell.calcOverrideSettings(spellSettings, fileUri.fsPath);
log('Finish fetchUriSettings:', uri);
Expand Down Expand Up @@ -276,6 +282,28 @@ export function doesUriMatchAnyScheme(uri: string, schemes: string[]): boolean {
return schemes.findIndex(v => v === schema) >= 0;
}

function extractEnableFiletypes(...settings: CSpellUserSettings[]): string[] {
return settings
.map(({ enableFiletypes = [] }) => enableFiletypes)
.reduce((acc, next) => acc.concat(next), []);
}

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)
}
});
return enabled.size ? { ...rest, enabledLanguageIds: [...enabled] } : { ...rest };
}

const correctRegExMap = new Map([
['/"""(.*?\\n?)+?"""/g', '/(""")[^\\1]*?\\1/g'],
["/'''(.*?\\n?)+?'''/g", "/(''')[^\\1]*?\\1/g"],
Expand Down
16 changes: 8 additions & 8 deletions packages/_settingsViewer/src/api/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
isSelectFileMessage,
SelectFolderMessage,
isSelectFolderMessage,
EnableLocalMessage,
isEnableLocalMessage,
EnableLocaleMessage,
isEnableLocaleMessage,
} from './message';
import { sampleSettings } from '../test/samples/sampleSettings';

Expand Down Expand Up @@ -97,12 +97,12 @@ describe('Validate Messages', () => {
expect(isSelectFolderMessage(msgConfigurationChangeMessage)).toBe(false);
});

test('isEnableLocalMessage', () => {
const msg: EnableLocalMessage = {
command: 'EnableLocalMessage', value: { target: 'folder', uri: 'uri', local: 'en', enable: true }
test('isEnableLocaleMessage', () => {
const msg: EnableLocaleMessage = {
command: 'EnableLocaleMessage', value: { target: 'folder', uri: 'uri', locale: 'en', enable: true }
};
expect(isEnableLocalMessage({ command: 'SelectFolderMessage' })).toBe(false);
expect(isEnableLocalMessage({ command: 'EnableLocalMessage' })).toBe(false);
expect(isEnableLocalMessage(msg)).toBe(true);
expect(isEnableLocaleMessage({ command: 'SelectFolderMessage' })).toBe(false);
expect(isEnableLocaleMessage({ command: 'EnableLocaleMessage' })).toBe(false);
expect(isEnableLocaleMessage(msg)).toBe(true);
});
});
12 changes: 6 additions & 6 deletions packages/_settingsViewer/src/api/message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Settings, ConfigTarget } from './settings';
export type Commands = 'ConfigurationChangeMessage'
| 'EnableLanguageIdMessage'
| 'EnableLocalMessage'
| 'EnableLocaleMessage'
| 'RequestConfigurationMessage'
| 'SelectFileMessage'
| 'SelectFolderMessage'
Expand All @@ -15,7 +15,7 @@ export interface Message {
export type Messages =
ConfigurationChangeMessage
| EnableLanguageIdMessage
| EnableLocalMessage
| EnableLocaleMessage
| RequestConfigurationMessage
| SelectFileMessage
| SelectFolderMessage
Expand Down Expand Up @@ -77,19 +77,19 @@ export interface EnableLanguageIdMessage extends Message {
};
}

export interface EnableLocalMessage extends Message {
command: 'EnableLocalMessage';
export interface EnableLocaleMessage extends Message {
command: 'EnableLocaleMessage';
value: {
target: ConfigTarget;
uri: string | undefined;
local: string;
locale: string;
enable: boolean;
};
}

export const isConfigurationChangeMessage = isA<ConfigurationChangeMessage>('ConfigurationChangeMessage', [['value', isObject]]);
export const isEnableLanguageIdMessage = isA<EnableLanguageIdMessage>('EnableLanguageIdMessage', [['value', isObject]]);
export const isEnableLocalMessage = isA<EnableLocalMessage>('EnableLocalMessage', [['value', isObject]]);
export const isEnableLocaleMessage = isA<EnableLocaleMessage>('EnableLocaleMessage', [['value', isObject]]);
export const isRequestConfigurationMessage = isA<RequestConfigurationMessage>('RequestConfigurationMessage', []);
export const isSelectFileMessage = isA<SelectFileMessage>('SelectFileMessage', [['value', isString]]);
export const isSelectFolderMessage = isA<SelectFolderMessage>('SelectFolderMessage', [['value', isString]]);
Expand Down
9 changes: 4 additions & 5 deletions packages/_settingsViewer/src/api/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface Settings {
activeFolderUri?: string;
}

export type LocalId = string;
export type LocalList = LocalId[];
export type LocaleId = string;
export type LocaleList = LocaleId[];

export type FileType = string;
export type FileTypeList = FileType[];
Expand All @@ -32,14 +32,14 @@ export interface Configs extends SettingByConfigTarget<Config> {

export interface DictionaryEntry {
name: string;
locals: LocalList;
locales: LocaleList;
languageIds: FileTypeList;
description?: string;
}

export interface Config {
inherited: { [key in keyof Config]?: ConfigSource };
locals: Inherited<LocalList>;
locales: Inherited<LocaleList>;
languageIdsEnabled: Inherited<FileTypeList>;
}

Expand All @@ -48,4 +48,3 @@ export interface FileConfig extends TextDocument {
fileEnabled: boolean | undefined;
dictionaries: DictionaryEntry[];
}

4 changes: 2 additions & 2 deletions packages/_settingsViewer/src/iso639-1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const regExReplace = /^([a-z]{2})[-_]?([a-z]{0,2})$/i;
export function normalizeCode(code: string) {
return code.replace(regExReplace, (match: string, p1: string, p2: string) => {
const lang = p1.toLowerCase();
const local = p2.toUpperCase();
return local ? `${lang}-${local}` : lang;
const locale = p2.toUpperCase();
return locale ? `${lang}-${locale}` : lang;
});
}

Expand Down
3 changes: 1 addition & 2 deletions packages/_settingsViewer/src/iso639-1/languageCodes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const codes: string[][] = [
// ['code', 'language'[', ''local']],
// ['code', 'language'[', ''locale']],
['af', 'Afrikaans'],
['af-NA', 'Afrikaans', 'Namibia'],
['af-ZA', 'Afrikaans', 'South Africa'],
Expand Down Expand Up @@ -531,4 +531,3 @@ export const codes: string[][] = [
['zu', 'Zulu'],
['zu-ZA', 'Zulu', 'South Africa'],
];

68 changes: 34 additions & 34 deletions packages/_settingsViewer/src/test/samples/sampleSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ const languageIdsWorkspace = languageIdsA;
const knownLanguageIds = languageIdsA.concat(languageIdsB).concat(['jsonc', 'perl', 'sh', 'bash', 'avro']).filter(uniqueFilter()).sort();

const dictionaries: DictionaryEntry[] = [
{ name: 'companies', locals: [], languageIds: [], description: 'Company names dictionary for cspell.' },
{ name: 'css', locals: [], languageIds: ['javascriptreact', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php', 'css', 'less', 'scss'], description: 'CSS Keywords.' },
{ name: 'csharp', locals: [], languageIds: ['csharp'], description: 'C# Keywords and common library functions.' },
{ name: 'dotnet', locals: [], languageIds: ['csharp'], description: '.Net keywords.' },
{ name: 'filetypes', locals: [], languageIds: [], description: 'List of file types.' },
{ name: 'fonts', locals: [], languageIds: ['javascriptreact', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php', 'css', 'less', 'scss'], description: 'List of fonts.' },
{ name: 'html', locals: [], languageIds: ['javascriptreact', 'typescriptreact', 'markdown', 'asciidoc', 'html', 'pug', 'jade', 'handlebars', 'php'], description: 'HTML keywords.' },
{ name: 'misc', locals: [], languageIds: [], description: 'List of miscellaneous terms.' },
{ name: 'node', locals: [], languageIds: ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'json'], description: 'List of NodeJS terms.' },
{ name: 'npm', locals: [], languageIds: ['csharp', 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'markdown', 'asciidoc', 'html', 'pug', 'jade', 'json', 'php'], description: 'List of Top 500 NPM packages.' },
{ name: 'powershell', locals: [], languageIds: [], description: 'Powershell Keywords.' },
{ name: 'softwareTerms', locals: [], languageIds: [], description: 'Common Software Terms.' },
{ name: 'typescript', locals: [], languageIds: ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php'], description: 'JavaScript and Typescript terms.' },
{ name: 'cpp', locals: [], languageIds: ['c', 'cpp'], description: 'C/C++ Keywords and common library functions.' },
{ name: 'django', locals: [], languageIds: ['html', 'python'], description: 'List of Python Django Framework keywords.' },
{ name: 'elixir', locals: [], languageIds: ['elixir'], description: 'Elixir dictionary for cspell.' },
{ name: 'en_us', locals: ['en', 'en-US'], languageIds: [], description: 'American English Dictionary' },
{ name: 'en-gb', locals: ['en-GB'], languageIds: [], description: 'British English Dictionary' },
{ name: 'fullstack', locals: [], languageIds: ['php', 'javascript'], description: 'Common words encountered during fullstack development' },
{ name: 'golang', locals: [], languageIds: ['go'], description: 'Go Language Dictionary' },
{ name: 'java', locals: [], languageIds: ['java'], description: 'Java dictionary for cspell.' },
{ name: 'latex', locals: [], languageIds: ['latex'], description: 'LaTeX dictionary' },
{ name: 'lorem-ipsum', locals: ['lorem', 'lorem-ipsum'], languageIds: [], description: 'Lorem-ipsum dictionary for cspell.' },
{ name: 'php', locals: [], languageIds: ['php'], description: 'Php dictionary for cspell.' },
{ name: 'python', locals: [], languageIds: ['python'], description: 'Python Keyword Dictionary' },
{ name: 'rust', locals: [], languageIds: ['rust'], description: 'Rust Keyword Dictionary' },
{ name: 'scala', locals: [], languageIds: ['scala'], description: 'Scala dictionary for cspell.' },
{ name: 'cs-cz', locals: ['cs'], languageIds: [], description: 'Czech dictionary for cspell.' }
{ name: 'companies', locales: [], languageIds: [], description: 'Company names dictionary for cspell.' },
{ name: 'css', locales: [], languageIds: ['javascriptreact', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php', 'css', 'less', 'scss'], description: 'CSS Keywords.' },
{ name: 'csharp', locales: [], languageIds: ['csharp'], description: 'C# Keywords and common library functions.' },
{ name: 'dotnet', locales: [], languageIds: ['csharp'], description: '.Net keywords.' },
{ name: 'filetypes', locales: [], languageIds: [], description: 'List of file types.' },
{ name: 'fonts', locales: [], languageIds: ['javascriptreact', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php', 'css', 'less', 'scss'], description: 'List of fonts.' },
{ name: 'html', locales: [], languageIds: ['javascriptreact', 'typescriptreact', 'markdown', 'asciidoc', 'html', 'pug', 'jade', 'handlebars', 'php'], description: 'HTML keywords.' },
{ name: 'misc', locales: [], languageIds: [], description: 'List of miscellaneous terms.' },
{ name: 'node', locales: [], languageIds: ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'json'], description: 'List of NodeJS terms.' },
{ name: 'npm', locales: [], languageIds: ['csharp', 'javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'markdown', 'asciidoc', 'html', 'pug', 'jade', 'json', 'php'], description: 'List of Top 500 NPM packages.' },
{ name: 'powershell', locales: [], languageIds: [], description: 'Powershell Keywords.' },
{ name: 'softwareTerms', locales: [], languageIds: [], description: 'Common Software Terms.' },
{ name: 'typescript', locales: [], languageIds: ['javascript', 'javascriptreact', 'typescript', 'typescriptreact', 'html', 'pug', 'jade', 'handlebars', 'php'], description: 'JavaScript and Typescript terms.' },
{ name: 'cpp', locales: [], languageIds: ['c', 'cpp'], description: 'C/C++ Keywords and common library functions.' },
{ name: 'django', locales: [], languageIds: ['html', 'python'], description: 'List of Python Django Framework keywords.' },
{ name: 'elixir', locales: [], languageIds: ['elixir'], description: 'Elixir dictionary for cspell.' },
{ name: 'en_us', locales: ['en', 'en-US'], languageIds: [], description: 'American English Dictionary' },
{ name: 'en-gb', locales: ['en-GB'], languageIds: [], description: 'British English Dictionary' },
{ name: 'fullstack', locales: [], languageIds: ['php', 'javascript'], description: 'Common words encountered during fullstack development' },
{ name: 'golang', locales: [], languageIds: ['go'], description: 'Go Language Dictionary' },
{ name: 'java', locales: [], languageIds: ['java'], description: 'Java dictionary for cspell.' },
{ name: 'latex', locales: [], languageIds: ['latex'], description: 'LaTeX dictionary' },
{ name: 'lorem-ipsum', locales: ['lorem', 'lorem-ipsum'], languageIds: [], description: 'Lorem-ipsum dictionary for cspell.' },
{ name: 'php', locales: [], languageIds: ['php'], description: 'Php dictionary for cspell.' },
{ name: 'python', locales: [], languageIds: ['python'], description: 'Python Keyword Dictionary' },
{ name: 'rust', locales: [], languageIds: ['rust'], description: 'Rust Keyword Dictionary' },
{ name: 'scala', locales: [], languageIds: ['scala'], description: 'Scala dictionary for cspell.' },
{ name: 'cs-cz', locales: ['cs'], languageIds: [], description: 'Czech dictionary for cspell.' }
];


const _sampleSettings: Settings = {
dictionaries,
knownLanguageIds,
configs: {
user: { locals: ['en'], languageIdsEnabled: languageIdsUser, inherited: {} },
workspace: { locals: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: {} },
folder: { locals: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: { locals: 'workspace', languageIdsEnabled: 'workspace' } },
user: { locales: ['en'], languageIdsEnabled: languageIdsUser, inherited: {} },
workspace: { locales: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: {} },
folder: { locales: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: { locales: 'workspace', languageIdsEnabled: 'workspace' } },
file: {
uri: 'file:///Users/cspell/projects/vscode-cspell-dict-extensions/extensions/dutch/src/extension.ts',
fileName: 'vscode-cspell-dict-extensions/extensions/dutch/src/extension.ts',
Expand All @@ -77,9 +77,9 @@ const _sampleSettingsSingleFolder: Settings = {
dictionaries,
knownLanguageIds,
configs: {
user: { locals: ['en'], languageIdsEnabled: languageIdsUser, inherited: {} },
workspace: { locals: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: {} },
folder: { locals: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: { locals: 'workspace', languageIdsEnabled: 'workspace' } },
user: { locales: ['en'], languageIdsEnabled: languageIdsUser, inherited: {} },
workspace: { locales: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: {} },
folder: { locales: ['en', 'da'], languageIdsEnabled: languageIdsWorkspace, inherited: { locales: 'workspace', languageIdsEnabled: 'workspace' } },
file: {
uri: 'file:///Users/cspell/projects/vscode-cspell-dict-extensions/extensions/dutch/src/extension.ts',
fileName: 'vscode-cspell-dict-extensions/extensions/dutch/src/extension.ts',
Expand Down
Loading

0 comments on commit 2fde3bc

Please sign in to comment.