UNPKG

cspell-lib

Version:

A library of useful functions used across various cspell tools.

36 lines 2.19 kB
import * as path from 'node:path'; import { getLanguagesForBasename } from '../fileTypes.js'; import { calcOverrideSettings, getDefaultSettings, getGlobalSettingsAsync, mergeSettings } from '../Settings/index.js'; import { combineTextAndLanguageSettings } from '../Settings/TextDocumentSettings.js'; import { uriToFilePath } from '../util/Uri.js'; /** * Combines all relevant setting values into a final configuration to be used for spell checking. * It applies any overrides and appropriate language settings by taking into account the document type (languageId) * the locale (natural language) and any in document settings. * * Note: this method will not search for configuration files. Configuration files should already be merged into `settings`. * It is NOT necessary to include the cspell defaultSettings or globalSettings. They will be applied within this function. * @param document - The document to be spell checked. Note: if the URI doesn't have a path, overrides cannot be applied. * `locale` - if defined will be used unless it is overridden by an in-document setting. * `languageId` - if defined will be used to select appropriate file type dictionaries. * @param settings - The near final settings. Should already be the combination of all configuration files. */ export async function determineTextDocumentSettings(doc, settings) { const filename = uriToFilePath(doc.uri); const settingsWithDefaults = mergeSettings(await getDefaultSettings(settings.loadDefaultConfiguration ?? true), await getGlobalSettingsAsync(), settings); const fileSettings = calcOverrideSettings(settingsWithDefaults, filename); const languageIds = fileSettings?.languageId?.length ? fileSettings.languageId : doc.languageId ? doc.languageId : getLanguageForFilename(filename); if (doc.locale) { fileSettings.language = doc.locale; } return combineTextAndLanguageSettings(fileSettings, doc.text, languageIds); } function getLanguageForFilename(filename) { const basename = path.basename(filename); return getLanguagesForBasename(basename); } //# sourceMappingURL=determineTextDocumentSettings.js.map