Skip to content

Commit 9223cf9

Browse files
refactor(gen-tm): deduplicate callee-token detection from #58 (#60)
Post-merge cleanup for #58: the callee-token predicate was copy-pasted in the continuation-bracket call variant and the contextualScopes call-args region — extract findCalleeToken() so the two derived call regions cannot drift apart. Also removes the always-true `(o !== '(' || true)` leftover in the ctx-pair filter and fixes the mis-indented ctx-call-args entry. No behavior change: zero `npm run gen` drift, env-spec-regressions 28/28, highlighter gates 10/10.
1 parent 8d3335f commit 9223cf9

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

src/gen-tm.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,17 @@ function classifyToken(token: TokenDecl, opts?: { explicitScope?: boolean }): {
596596
return { scope: 'variable.other' };
597597
}
598598

599+
// The grammar's CALLEE token: one scoped as a function name whose pattern is gated on a
600+
// following `(` (e.g. env-spec FUNCTION_NAME, `name(?=\s*\()`). Shared by the two derived
601+
// call-argument regions (the contextualScopes call-args region and the lineComment
602+
// continuation-bracket call variant), which must agree on what a callee looks like.
603+
function findCalleeToken(grammar: CstGrammar): TokenDecl | undefined {
604+
return grammar.tokens.find((t) => {
605+
const sc = t.scope ?? classifyToken(t).scope;
606+
return (sc.startsWith('variable.function') || sc.startsWith('entity.name.function')) && tokenPatternSource(t).includes('\\(');
607+
});
608+
}
609+
599610
/**
600611
* The repository keys of the grammar's COMMENT token entries, in declaration
601612
* order. A token is a comment iff its emitted scope (the `@scope` override, else
@@ -5081,10 +5092,7 @@ export function generateTmLanguage(grammar: CstGrammar): TmGrammar {
50815092
// interior that treats line-start `#` as a comment, eat the `# )` closer, and run
50825093
// away past the comment block. Same interior as the paren construct, begin consumes
50835094
// the callee (scoped like the grammar's callee token). Tried before $self.
5084-
const calleeForCont = grammar.tokens.find((t) => {
5085-
const sc = t.scope ?? classifyToken(t).scope;
5086-
return (sc.startsWith('variable.function') || sc.startsWith('entity.name.function')) && tokenPatternSource(t).includes('\\(');
5087-
});
5095+
const calleeForCont = findCalleeToken(grammar);
50885096
const hasParenPair = contBrackets.some(([open]) => open === '(');
50895097
const contCallInclude = (calleeForCont && hasParenPair) ? [{ include: `#${key}-rich-cont-call` }] : [];
50905098
if (contBrackets.length) {
@@ -7584,7 +7592,7 @@ export function generateTmLanguage(grammar: CstGrammar): TmGrammar {
75847592
const allRuleLits = new Set<string>();
75857593
for (const r of grammar.rules) for (const lit of collectLiterals(r.body)) allRuleLits.add(lit);
75867594
const ctxPairs = ([['(', ')'], ['[', ']'], ['{', '}']] as [string, string][])
7587-
.filter(([o, c]) => allRuleLits.has(o) && allRuleLits.has(c) && (o !== '(' || true));
7595+
.filter(([o, c]) => allRuleLits.has(o) && allRuleLits.has(c));
75887596
const pairKeyOf = (open: string) => `ctx-pair-${[...open].map((ch) => ch.charCodeAt(0).toString(16)).join('')}`;
75897597
const pairIncludes = ctxPairs.filter(([o]) => o !== '(').map(([o]) => ({ include: `#${pairKeyOf(o)}` }));
75907598
for (const [open, close] of ctxPairs) {
@@ -7601,22 +7609,19 @@ export function generateTmLanguage(grammar: CstGrammar): TmGrammar {
76017609
// gated on a following `(` — e.g. env-spec FUNCTION_NAME), else the identifier pattern.
76027610
// The generic ident fallback can resolve to a placeholder token in indentation grammars,
76037611
// so a never-matching callee skips the region entirely rather than emitting a dead rule.
7604-
const calleeTok = grammar.tokens.find((t) => {
7605-
const sc = t.scope ?? classifyToken(t).scope;
7606-
return (sc.startsWith('variable.function') || sc.startsWith('entity.name.function')) && tokenPatternSource(t).includes('\\(');
7607-
});
7612+
const calleeTok = findCalleeToken(grammar);
76087613
const calleeScope = calleeTok?.scope ?? 'entity.name.function';
76097614
const calleePattern = calleeTok ? tokenPatternSource(calleeTok) : identPattern;
76107615
if (!calleePattern.includes('(?!)')) {
76117616
repository['ctx-call-args'] = {
7612-
begin: `(${calleePattern})\\s*(\\()`,
7613-
beginCaptures: {
7614-
'1': { name: `${calleeScope}.${langName}` },
7615-
'2': { name: `${getScope(scopeOverrides, '(') ?? 'punctuation.section.parens.begin'}.${langName}` },
7616-
},
7617-
end: '\\)',
7618-
endCaptures: { '0': { name: `${getScope(scopeOverrides, ')') ?? 'punctuation.section.parens.end'}.${langName}` } },
7619-
patterns: [...ctxOverrideIncludes, ...pairIncludes, { include: '$self' }],
7617+
begin: `(${calleePattern})\\s*(\\()`,
7618+
beginCaptures: {
7619+
'1': { name: `${calleeScope}.${langName}` },
7620+
'2': { name: `${getScope(scopeOverrides, '(') ?? 'punctuation.section.parens.begin'}.${langName}` },
7621+
},
7622+
end: '\\)',
7623+
endCaptures: { '0': { name: `${getScope(scopeOverrides, ')') ?? 'punctuation.section.parens.end'}.${langName}` } },
7624+
patterns: [...ctxOverrideIncludes, ...pairIncludes, { include: '$self' }],
76207625
};
76217626
topPatterns.push({ include: '#ctx-call-args' });
76227627
}

0 commit comments

Comments
 (0)