Skip to content

Commit 1961c5c

Browse files
committed
Add StreamParser.mergeTokens option
FEATURE: Stream parsers now support a `mergeTokens` option that can be used to turn off automatic merging of adjacent tokens. See https://discuss.codemirror.net/t/merging-adjacent-tokens-makes-completion-much-harder-when-it-merges-adjacent-bracket-tokens/9054
1 parent 509ea10 commit 1961c5c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/stream-parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ export interface StreamParser<State> {
4747
/// token name that exists as a property in this object, the
4848
/// corresponding tags will be assigned to the token.
4949
tokenTable?: {[name: string]: Tag | readonly Tag[]}
50+
/// By default, adjacent tokens of the same type are merged in the
51+
/// output tree. Set this to false to disable that.
52+
mergeTokens?: boolean
5053
}
5154

5255
function fullParser<State>(spec: StreamParser<State>): Required<StreamParser<State>> {
@@ -58,7 +61,8 @@ function fullParser<State>(spec: StreamParser<State>): Required<StreamParser<Sta
5861
copyState: spec.copyState || defaultCopyState,
5962
indent: spec.indent || (() => null),
6063
languageData: spec.languageData || {},
61-
tokenTable: spec.tokenTable || noTokens
64+
tokenTable: spec.tokenTable || noTokens,
65+
mergeTokens: spec.mergeTokens !== false
6266
}
6367
}
6468

@@ -288,7 +292,8 @@ class Parse<State> implements PartialParse {
288292
size += this.chunk.length - len0
289293
}
290294
let last = this.chunk.length - 4
291-
if (size == 4 && last >= 0 && this.chunk[last] == id && this.chunk[last + 2] == from)
295+
if (this.lang.streamParser.mergeTokens && size == 4 && last >= 0 &&
296+
this.chunk[last] == id && this.chunk[last + 2] == from)
292297
this.chunk[last + 2] = to
293298
else
294299
this.chunk.push(id, from, to, size)

0 commit comments

Comments
 (0)