How to use cspell-lib - 10 common examples

To help you get started, we’ve selected a few cspell-lib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github streetsidesoftware / vscode-spell-checker / packages / _server / src / server.ts View on Github external
[key in keyof Api.ServerMethodRequestResult]: (param: Api.ServerRequestMethodRequests[key]) => RequestResult;
};

const notifyMethodNames: Api.NotifyServerMethodConstants = {
    onConfigChange: 'onConfigChange',
    registerConfigurationFile: 'registerConfigurationFile',
};

const tds = CSpell;

const defaultCheckLimit = Validator.defaultCheckLimit;

// Turn off the spell checker by default. The setting files should have it set.
// This prevents the spell checker from running too soon.
const defaultSettings: CSpellUserSettings = {
    ...CSpell.mergeSettings(getDefaultSettings(), CSpell.getGlobalSettings()),
    checkLimit: defaultCheckLimit,
    enabled: false,
};
const defaultDebounce = 50;

function run() {
    // debounce buffer
    const validationRequestStream = new ReplaySubject(1);
    const triggerUpdateConfig = new ReplaySubject(1);
    const triggerValidateAll = new ReplaySubject(1);
    const validationByDoc = new Map();
    const blockValidation = new Map();
    let isValidationBusy = false;
    const disposables: Disposable[] = [];

    const requestMethodApi: RequestMethodApi = {
github streetsidesoftware / cspell / packages / cspell / src / application.ts View on Github external
async function run(): Promise {

        header();

        const configFiles = (await globP(cfg.configGlob, cfg.configGlobOptions)).filter(util.uniqueFn());
        cfg.info(`Config Files Found:\n    ${configFiles.join('\n    ')}\n`, MessageTypes.Info);
        const config = cspell.readSettingsFiles(configFiles);
        const configInfo: ConfigInfo = { source: configFiles.join(' || '), config };
        // Get Exclusions from the config files.
        const { root } = cfg;
        const globOptions = { root, cwd: root };
        const exclusionGlobs = extractGlobExcludesFromConfig(root, configInfo.source, configInfo.config).concat(cfg.excludes);
        const files = filterFiles(await findFiles(cfg.files, globOptions), exclusionGlobs);

        return processFiles(fileLoader(files), configInfo);
    }
github streetsidesoftware / vscode-spell-checker / packages / _server / src / validator.ts View on Github external
export async function validateTextDocumentAsync(textDocument: TextDocument, options: CSpellUserSettings): Promise> {
    const { diagnosticLevel = DiagnosticSeverity.Information.toString() } = options;
    const severity = diagSeverityMap.get(diagnosticLevel.toLowerCase()) || DiagnosticSeverity.Information;
    const limit = (options.checkLimit || defaultCheckLimit) * 1024;
    const text = textDocument.getText().slice(0, limit);
    const diags = genSequence(await validateText(text, options))
        // Convert the offset into a position
        .map(offsetWord => ({...offsetWord, position: textDocument.positionAt(offsetWord.offset) }))
        // Calculate the range
        .map(word => ({
            ...word,
            range: {
                start: word.position,
                end: ({...word.position, character: word.position.character + word.text.length })
            }
        }))
        // Convert it to a Diagnostic
        .map(({text, range}) => ({
            severity,
            range: range,
            message: `"${text}": Unknown word.`,
            source: diagSource
github streetsidesoftware / vscode-spell-checker / packages / _server / src / SuggestionsGenerator.ts View on Github external
async genSuggestions(doc: DocInfo, word: string): Promise {
        const { settings, dictionary } = await this.getSettings(doc);
        const { numSuggestions = defaultNumSuggestions } = settings;

        if (word.length > maxWordLengthForSuggestions) {
            return [];
        }
        const numSugs = word.length > wordLengthForLimitingSuggestions ? maxNumberOfSuggestionsForLongWords : numSuggestions;
        const options: SuggestOptions = {
            numChanges: maxEdits,
            numSuggestions: numSugs,
            // Turn off compound suggestions for now until it works a bit better.
            compoundMethod: CompoundWordsMethod.NONE,
            ignoreCase: !settings.caseSensitive,
        };
        return dictionary.suggest(word, options).map(s => ({...s, word: s.word.replace(regexJoinedWords, '')}));
    }
github streetsidesoftware / cspell / packages / cspell / src / application.ts View on Github external
function calcFinalConfigInfo(
    configInfo: ConfigInfo,
    settingsFromCommandLine: cspell.CSpellUserSettings,
    filename: string,
    text: string
): FileConfigInfo {
    const ext = path.extname(filename);
    const fileSettings = cspell.calcOverrideSettings(configInfo.config, path.resolve(filename));
    const settings = cspell.mergeSettings(cspell.getDefaultSettings(), cspell.getGlobalSettings(), fileSettings, settingsFromCommandLine);
    const languageIds = settings.languageId ? [settings.languageId] : cspell.getLanguagesForExt(ext);
    const config = cspell.constructSettingsForText(settings, text, languageIds);
    return {configInfo: {...configInfo, config}, filename, text, languageIds};
}
github streetsidesoftware / vscode-spell-checker / packages / _server / src / documentSettings.ts View on Github external
private async fetchUriSettings(uri: string): Promise {
        log('Start fetchUriSettings:', uri);
        const folder = await this.findMatchingFolder(uri);
        const folderSettings = await this.fetchSettingsForUri(folder.uri);
        const spellSettings = CSpell.mergeSettings(this.defaultSettings, this.importedSettings(), folderSettings.settings);
        const fileUri = Uri.parse(uri);
        const fileSettings = CSpell.calcOverrideSettings(spellSettings, fileUri.fsPath);
        log('Finish fetchUriSettings:', uri);
        return fileSettings;
    }
github streetsidesoftware / cspell / packages / cspell / src / application.ts View on Github external
export async function trace(words: string[], options: TraceOptions): Promise {
    const configGlob = options.config || defaultConfigGlob;
    const configGlobOptions = options.config ? {} : defaultConfigGlobOptions;

    const configFiles = (await globP(configGlob, configGlobOptions)).filter(util.uniqueFn());
    const config = cspell.mergeSettings(cspell.getDefaultSettings(), cspell.getGlobalSettings(), cspell.readSettingsFiles(configFiles));
    const results = await traceWords(words, config);
    return results;
}
github streetsidesoftware / vscode-spell-checker / packages / _server / src / server.ts View on Github external
async function getBaseSettings(doc: TextDocument) {
        const settings = await getActiveSettings(doc);
        return {...CSpell.mergeSettings(defaultSettings, settings), enabledLanguageIds: settings.enabledLanguageIds};
    }
github streetsidesoftware / vscode-spell-checker / packages / _server / src / documentSettings.ts View on Github external
private resolveConfigImports(config: CSpellUserSettings, folderUri: string): CSpellUserSettings {
        const uriFsPath = Uri.parse(folderUri).fsPath;
        const imports = typeof config.import === 'string' ? [config.import] : config.import || [];
        if (!imports.length) {
            return config;
        }
        const importAbsPath = imports.map(file => path.resolve(file, uriFsPath));
        return CSpell.mergeSettings(CSpell.readSettingsFiles(importAbsPath), config);
    }
}
github streetsidesoftware / vscode-spell-checker / packages / _server / src / documentSettings.ts View on Github external
function readSettingsFiles(paths: string[]) {
    log(`readSettingsFiles:`, paths);
    const existingPaths = paths.filter(filename => fs.existsSync(filename));
    return CSpell.readSettingsFiles(existingPaths);
}