Skip to content

Commit

Permalink
feat: Support multiple languageId overrides. (#4348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Mar 21, 2023
1 parent 0ae4396 commit c69f8c4
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 39 deletions.
31 changes: 17 additions & 14 deletions cspell.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -913,17 +913,7 @@
"description": "List of regular expression patterns or defined pattern names to match for spell checking.\n\nIf this property is defined, only text matching the included patterns will be checked.\n\nWhile you can create your own patterns, you can also leverage several patterns that are [built-in to CSpell](https://github.com/streetsidesoftware/cspell/blob/main/packages/cspell-lib/src/Settings/DefaultSettings.ts#L22)."
},
"languageId": {
"anyOf": [
{
"$ref": "#/definitions/LanguageId"
},
{
"items": {
"$ref": "#/definitions/LanguageIdSingle"
},
"type": "array"
}
],
"$ref": "#/definitions/MatchingFileType",
"description": "The language id. Ex: \"typescript\", \"html\", or \"php\". \"*\" -- will match all languages."
},
"local": {
Expand Down Expand Up @@ -998,6 +988,19 @@
"description": "This is a written language locale like: 'en', 'en-GB', 'fr', 'es', 'de', etc.",
"type": "string"
},
"MatchingFileType": {
"anyOf": [
{
"$ref": "#/definitions/LanguageId"
},
{
"items": {
"$ref": "#/definitions/LanguageId"
},
"type": "array"
}
]
},
"OverrideSettings": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -1096,8 +1099,8 @@
"description": "Sets the locale."
},
"languageId": {
"$ref": "#/definitions/LanguageId",
"description": "Sets the programming language id."
"$ref": "#/definitions/MatchingFileType",
"description": "Sets the programming language id to match file type."
},
"languageSettings": {
"description": "Additional settings for individual languages.\n\nSee [Language Settings](https://cspell.org/configuration/language-settings/) for more details.",
Expand Down Expand Up @@ -1583,7 +1586,7 @@
"description": "Current active spelling language. This specifies the language locale to use in choosing the general dictionary.\n\nFor example:\n\n- \"en-GB\" for British English.\n- \"en,nl\" to enable both English and Dutch."
},
"languageId": {
"$ref": "#/definitions/LanguageId",
"$ref": "#/definitions/MatchingFileType",
"description": "Forces the spell checker to assume a give language id. Used mainly as an Override."
},
"languageSettings": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"pub-grad": "pnpm run pre-pub && lerna publish --conventional-commits --conventional-graduate --sync-workspace-lock",
"pub-next": "pnpm run pre-pub && lerna publish --conventional-commits --conventional-prerelease --sync-workspace-lock --dist-tag next",
"test-watch": "pnpm -r run --parallel test-watch",
"test": "pnpm -r --stream run test && pnpm run test-schema",
"test": "pnpm -r --stream --workspace-concurrency=1 run test && pnpm run test-schema",
"test:update-snapshots": "pnpm run -r test:update-snapshot",
"test:bin": "pnpm test:bin-spell && pnpm test:bin-spell-cache-content && pnpm test:bin-spell-cache-metadata && pnpm test:bin-trace && pnpm test:bin-check && pnpm test:bin-spell-yarn",
"test:bin-check": "node ./bin.js check README.md",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-lib/src/lib/Settings/LanguageSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function calcSettingsForLanguageId(
baseSettings: CSpellUserSettings,
languageId: LanguageId[] | LanguageId
): CSpellUserSettings {
const langIds: string[] = ['*'].concat(languageId instanceof Array ? languageId : [languageId]);
const langIds: string[] = ['*', ...normalizeLanguageId(languageId)];
const langSettings = langIds.reduce((settings, languageId) => {
return calcUserSettingsForLanguage(settings, languageId);
}, baseSettings);
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell-lib/src/lib/textValidation/checkText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import { validateText } from './validator.js';
* @deprecated
*/
export async function checkText(text: string, settings: CSpellUserSettings): Promise<CheckTextInfo> {
const languageId = settings.languageId || 'plaintext';
const doc = clean({
uri: '',
text,
languageId: settings.languageId || 'plaintext',
languageId: Array.isArray(languageId) ? languageId.join(',') : languageId,
locale: settings.language,
});
return checkTextDocument(doc, { noConfigSearch: true }, { loadDefaultConfiguration: false, ...settings });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { opConcatMap, opTake, pipe, toArray } from '@cspell/cspell-pipe/sync';
import * as Trie from 'cspell-trie-lib';
import { importTrie, isCircular, iteratorTrieWords, serializeTrie } from 'cspell-trie-lib';
import * as fsp from 'fs/promises';
import { uniqueFilter } from 'hunspell-reader/dist/util';
import { uniqueFilter } from 'hunspell-reader';
import * as path from 'path';

import { spyOnConsole } from '../test/console';
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-tools/src/compiler/wordListParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { opCombine, opCombine as opPipe, type Operator, opFilter, opMap } from '@cspell/cspell-pipe/sync';
import { createDictionaryLineParser } from 'cspell-trie-lib';
import { uniqueFilter } from 'hunspell-reader/dist/util';
import { uniqueFilter } from 'hunspell-reader';

import type { CompileOptions } from './CompileOptions';
import { legacyLineToWords } from './legacyLineToWords';
Expand Down
31 changes: 17 additions & 14 deletions packages/cspell-types/cspell.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -913,17 +913,7 @@
"description": "List of regular expression patterns or defined pattern names to match for spell checking.\n\nIf this property is defined, only text matching the included patterns will be checked.\n\nWhile you can create your own patterns, you can also leverage several patterns that are [built-in to CSpell](https://github.com/streetsidesoftware/cspell/blob/main/packages/cspell-lib/src/Settings/DefaultSettings.ts#L22)."
},
"languageId": {
"anyOf": [
{
"$ref": "#/definitions/LanguageId"
},
{
"items": {
"$ref": "#/definitions/LanguageIdSingle"
},
"type": "array"
}
],
"$ref": "#/definitions/MatchingFileType",
"description": "The language id. Ex: \"typescript\", \"html\", or \"php\". \"*\" -- will match all languages."
},
"local": {
Expand Down Expand Up @@ -998,6 +988,19 @@
"description": "This is a written language locale like: 'en', 'en-GB', 'fr', 'es', 'de', etc.",
"type": "string"
},
"MatchingFileType": {
"anyOf": [
{
"$ref": "#/definitions/LanguageId"
},
{
"items": {
"$ref": "#/definitions/LanguageId"
},
"type": "array"
}
]
},
"OverrideSettings": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -1096,8 +1099,8 @@
"description": "Sets the locale."
},
"languageId": {
"$ref": "#/definitions/LanguageId",
"description": "Sets the programming language id."
"$ref": "#/definitions/MatchingFileType",
"description": "Sets the programming language id to match file type."
},
"languageSettings": {
"description": "Additional settings for individual languages.\n\nSee [Language Settings](https://cspell.org/configuration/language-settings/) for more details.",
Expand Down Expand Up @@ -1583,7 +1586,7 @@
"description": "Current active spelling language. This specifies the language locale to use in choosing the general dictionary.\n\nFor example:\n\n- \"en-GB\" for British English.\n- \"en,nl\" to enable both English and Dutch."
},
"languageId": {
"$ref": "#/definitions/LanguageId",
"$ref": "#/definitions/MatchingFileType",
"description": "Forces the spell checker to assume a give language id. Used mainly as an Override."
},
"languageSettings": {
Expand Down
12 changes: 7 additions & 5 deletions packages/cspell-types/src/CSpellSettingsDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export interface Settings extends ReportingConfiguration, BaseSetting, PnPSettin
languageSettings?: LanguageSetting[];

/** Forces the spell checker to assume a give language id. Used mainly as an Override. */
languageId?: LanguageId;
languageId?: MatchingFileType;

/**
* By default, the bundled dictionary configurations are loaded. Explicitly setting this to `false`
Expand Down Expand Up @@ -394,8 +394,8 @@ export interface LegacySettings {
}

export interface OverrideSettings extends Settings, OverrideFilterFields {
/** Sets the programming language id. */
languageId?: LanguageId;
/** Sets the programming language id to match file type. */
languageId?: MatchingFileType;

/** Sets the locale. */
language?: LocaleId;
Expand Down Expand Up @@ -536,14 +536,14 @@ export interface LanguageSettingFilterFields

export interface LanguageSettingFilterFieldsPreferred {
/** The language id. Ex: "typescript", "html", or "php". "*" -- will match all languages. */
languageId: LanguageId | LanguageIdSingle[];
languageId: MatchingFileType;
/** The locale filter, matches against the language. This can be a comma separated list. "*" will match all locales. */
locale?: LocaleId | LocaleId[];
}

export interface LanguageSettingFilterFieldsDeprecated {
/** The language id. Ex: "typescript", "html", or "php". "*" -- will match all languages. */
languageId: LanguageId | LanguageIdSingle[];
languageId: MatchingFileType;
/**
* Deprecated - The locale filter, matches against the language. This can be a comma separated list. "*" will match all locales.
* @deprecated true
Expand Down Expand Up @@ -665,6 +665,8 @@ export type LanguageIdMultipleNeg = string;

export type LanguageId = LanguageIdSingle | LanguageIdMultiple | LanguageIdMultipleNeg;

export type MatchingFileType = LanguageId | LanguageId[];

/**
* A File System Path. Relative paths are relative to the configuration file.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/cspell-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export type {
LegacySettings,
LocaleId,
LocalId,
MatchingFileType,
MergeSource,
OverrideFilterFields,
OverrideSettings,
Expand Down
6 changes: 5 additions & 1 deletion packages/cspell/src/app/util/fileHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ export function calcFinalConfigInfo(
fileSettings,
settingsFromCommandLine
);
const languageIds = settings.languageId ? [settings.languageId] : cspell.getLanguageIdsForBaseFilename(basename);
const languageIds = settings.languageId
? Array.isArray(settings.languageId)
? settings.languageId
: [settings.languageId]
: cspell.getLanguageIdsForBaseFilename(basename);
const config = cspell.constructSettingsForText(settings, text, languageIds);
return {
configInfo: { ...configInfo, config },
Expand Down
1 change: 1 addition & 0 deletions packages/hunspell-reader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export {
type WordInfo,
} from './IterableHunspellReader';
export { IterableHunspellReader as HunspellReader } from './IterableHunspellReader';
export { uniqueFilter } from './util';

0 comments on commit c69f8c4

Please sign in to comment.