Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add highlighting for kewords with special symbols #312

Merged
merged 2 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
grammar <%= LanguageName %>

Model:
entry Model:
(persons+=Person | greetings+=Greeting)*;

Person:
'person' name=ID;

Greeting:
'Hello' person=[Person|ID] '!';
'Hello' person=[Person:ID] '!';

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;
Expand Down
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 (/\w/.test(keyword[0])) {
if (/\w/.test(keyword[keyword.length - 1])) {
groups.letter.push(keyword);
} else {
groups.rightSpecial.push(keyword);
}
} else {
if ((/\w/).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