Skip to content

Commit

Permalink
Fix highlighting of keywords with special symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluralia committed Nov 25, 2021
1 parent ab17ff0 commit 88c7ddc
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion packages/langium-cli/src/generator/textmate-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,45 @@ function getControlKeywords(grammar: langium.Grammar, pack: LangiumConfig): Patt
const regex = /[A-Za-z]/;
const controlKeywords = collectKeywords(grammar).filter(kw => regex.test(kw));
const keywords = controlKeywords.map(escapeRegExp);
const groups = groupKeywords(keywords);
return {
'name': `keyword.control.${pack.languageId}`,
'match': `\\b(${keywords.join('|')})\\b`
'match': groups.join('|')
};
}

function groupKeywords(keywords: string[]): string[] {
const groups: {
letter: string[],
leftSpecial: string[],
rightSpecial: string[],
special: string[]
} = {letter: [], leftSpecial: [], rightSpecial: [], special: []};

keywords.forEach(keyword => {
if ((/[_a-zA-Z]/).test(keyword[0])) {
if ((/[_a-zA-Z]/).test(keyword[keyword.length - 1])) {
groups.letter.push(keyword);
} else {
groups.rightSpecial.push(keyword);
}
} else {
if ((/[_a-zA-Z]/).test(keyword[keyword.length - 1])) {
groups.leftSpecial.push(keyword);
} else {
groups.special.push(keyword);
}
}
});

const res = [];
if (groups.letter.length) res.push(`\\b(${groups.letter.join('|')})\\b`);
if (groups.leftSpecial.length) res.push(`\\B(${groups.leftSpecial.join('|')})\\b`);
if (groups.rightSpecial.length) res.push(`\\b(${groups.rightSpecial.join('|')})\\B`);
if (groups.special.length) res.push(`\\B(${groups.special.join('|')})\\B`);
return res;
}

function getStringPatterns(grammar: langium.Grammar, pack: LangiumConfig): Pattern[] {
const terminals = langium.stream(grammar.rules).filter(langium.isTerminalRule);
const stringTerminal = terminals.find(e => e.name.toLowerCase() === 'string');
Expand Down

0 comments on commit 88c7ddc

Please sign in to comment.