Skip to content

Commit

Permalink
feat: support in doc setting LocalWords: (#485) (#486)
Browse files Browse the repository at this point in the history
* feat: support in doc setting `LocalWords:` (#485)

* fix: Update snapshots and packages

* ci: add integration alexiosc/megistos

* fix: support `LocalWord:` in doc settings

* Update InDocSettings.test.ts

* Update integration-test.yml

* ci: fix lint issue
  • Loading branch information
Jason3S committed Oct 31, 2020
1 parent 46e51f8 commit d5e0c87
Show file tree
Hide file tree
Showing 8 changed files with 4,069 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- php/php-src
- aws-amplify/docs
- Azure/azure-rest-api-specs
- alexiosc/megistos
- exonum/exonum
- bitjson/typescript-starter graphql/express-graphql graphql/graphql-relay-js
- graphql/graphql-js
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@
[submodule "integration-tests/repositories/php/php-src"]
path = integration-tests/repositories/php/php-src
url = https://github.com/php/php-src.git
[submodule "integration-tests/repositories/alexiosc/megistos"]
path = integration-tests/repositories/alexiosc/megistos
url = https://github.com/alexiosc/megistos.git
9 changes: 9 additions & 0 deletions integration-tests/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@
"**/*.{md,c,h,php}",
"*.{md}"
]
},
{
"path": "alexiosc/megistos",
"url": "https://github.com/alexiosc/megistos.git",
"args": [
"--config=../cspell.json",
"**/*.{md,c,h,html}",
"*.{md,c,h,html}"
]
}
]
}
3 changes: 3 additions & 0 deletions integration-tests/repositories/alexiosc/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dictionaries": ["cpp"]
}
1 change: 1 addition & 0 deletions integration-tests/repositories/alexiosc/megistos
Submodule megistos added at e800af
4,029 changes: 4,029 additions & 0 deletions integration-tests/snapshots/alexiosc/megistos/snapshot.txt

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions packages/cspell-lib/src/Settings/InDocSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ describe('Validate InDocSettings', () => {
'ignoreWords tooo faullts',
'language en-US',
'local',
'locale es-ES',
'local en, nl',
'dictionaries lorem-ipsum'
'dictionaries lorem-ipsum',
'LocalWords: one two three',
]);
});

Expand Down Expand Up @@ -53,7 +55,7 @@ describe('Validate InDocSettings', () => {
test('tests finding words to add to dictionary', () => {
const words = InDoc.internal.getWordsFromDocument(sampleCode);
// we match to the end of the line, so the */ is included.
expect(words).toEqual(['whiteberry', 'redberry', 'lightbrown']);
expect(words).toEqual(['whiteberry', 'redberry', 'lightbrown', 'one', 'two', 'three']);
expect(InDoc.getIgnoreWordsFromDocument('Hello')).toEqual([]);
});

Expand Down Expand Up @@ -84,7 +86,7 @@ describe('Validate InDocSettings', () => {
const ranges = TextRange.findMatchingRangesForPatterns(matches, sampleCode);
// console.log(ranges);
// console.log(replaceRangesWith(sampleCode, ranges));
expect(ranges.length).toBe(35);
expect(ranges.length).toBe(39);
});

test('test fetching the local for the text', () => {
Expand Down Expand Up @@ -122,9 +124,13 @@ const sampleCode = `
// weirdberry can be straange.
// cSpell:language en-US
// cspell:local
// cspell:locale es-ES
// cspell:local en, nl
// cspell:dictionaries lorem-ipsum
// LocalWords: one two three
// LocalWords:four five six
// localwords: seven eight nine
`;

// cspell:ignore againxx
Expand All @@ -134,3 +140,5 @@ const sampleText = `
# happydays arehere againxx
`;

// cspell:disableCompoundWords
// cspell:ignore localwords happydays arehere
22 changes: 12 additions & 10 deletions packages/cspell-lib/src/Settings/InDocSettings.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
import { Sequence } from 'gensequence';
import { genSequence, Sequence } from 'gensequence';
import * as Text from '../util/text';
import { CSpellUserSettings } from './CSpellSettingsDef';
import { mergeInDocSettings } from './CSpellSettingsServer';

// cspell:ignore gimuy
const regExMatchRegEx = /\/.*\/[gimuy]*/;
const regExInFileSetting = /(?:spell-?checker|cSpell)::?\s*(.*)/gi;
const regExInFileSettings = [
/(?:spell-?checker|cSpell)::?\s*(.*)/gi,
/(LocalWords:?\s+.*)/g,
];

export type CSpellUserSettingsKeys = keyof CSpellUserSettings;


export function getInDocumentSettings(text: string): CSpellUserSettings {
const settings = getPossibleInDocSettings(text)
.map(a => {
return a;
})
.map(a => a[1] || '')
.concatMap(a => parseSettingMatch(a))
.reduce((s, setting) => {
return mergeInDocSettings(s, setting);
}, { id: 'in-doc-settings' } as CSpellUserSettings);
return settings;
}

function parseSettingMatch(possibleSetting: string): CSpellUserSettings[] {
function parseSettingMatch(matchArray: RegExpMatchArray): CSpellUserSettings[] {
const [ , possibleSetting = '' ] = matchArray;
const settingParsers: [RegExp, (m: string) => CSpellUserSettings][] = [
[ /^(?:enable|disable)(?:allow)?CompoundWords/i, parseCompoundWords ],
[ /^words?\s/i , parseWords ],
[ /^ignore(?:words?)?\s/i, parseIgnoreWords ],
[ /^ignore_?Reg_?Exp\s+.+$/i, parseIgnoreRegExp ],
[ /^include_?Reg_?Exp\s+.+$/i, parseIncludeRegExp ],
[ /^(?:local|language)\s/i, parseLocal ],
[ /^(?:local|language)/i, parseLocal ],
[ /^locale?\s/i, parseLocal ],
[ /^language\s/i, parseLocal ],
[ /^dictionaries\s/i, parseDictionaries ],
[ /^LocalWords:/, parseWords ],
];

return settingParsers
Expand Down Expand Up @@ -90,7 +91,8 @@ function parseDictionaries(match: string): CSpellUserSettings {
}

function getPossibleInDocSettings(text: string): Sequence<RegExpExecArray> {
return Text.match(regExInFileSetting, text);
return genSequence(regExInFileSettings)
.concatMap(regexp => Text.match(regexp, text));
}

function getWordsFromDocument(text: string): string[] {
Expand Down

0 comments on commit d5e0c87

Please sign in to comment.