# PSI manipulation merges consecutive whitespace tokens

**URL:** <https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255>\
**Category:** IntelliJ Platform\
**Created:** [December 4, 2025, 6:01am UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255 "2025-12-04T06:01:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lemming](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/lemming/32/60_2.png) [@lemming](https://platform.jetbrains.com/u/lemming)\
**Post date:** [December 4, 2025, 6:01am UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255/1 "2025-12-04T06:01:16Z")

</div>

The language my plugin handles, Clojure, treats commas as whitespace, so a collection can be written as either `[a, b, c]` or `[a b c]`, those are essentially identical. However, I parse the commas as separate tokens, because it’s important to maintain them e.g. when formatting. This means that my PSI can have multiple contiguous whitespace tokens (e.g. in the case above, the tokens are `[` `a` `,` ` ` `b` `,` ` ` `c` `]`. This has worked for years with no problem. However, I’ve noticed an edge case - if I copy some of these tokens and move them in the PSI, e.g. I copy the `a` `,` ` ` and move them out of the vector, then when I use `addBefore` to add the two whitespace tokens they get merged into one in `CodeEditUtil#makePlaceHolderBetweenTokens`. I’m assuming the basic approach of having multiple different whitespace tokens is valid because Greg Shrago does the same in Clojure Kit. However while debugging, I can’t see any way to prevent those tokens being merged. Can anyone help with this?

---

<div class="post-metadata">

**Author:** ![jansorg](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/jansorg/32/52_2.png) [@jansorg](https://platform.jetbrains.com/u/jansorg)\
**Post date:** [December 4, 2025, 9:53am UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255/3 "2025-12-04T09:53:12Z")

</div>

If it’s merged by your formatter, have you checked if `WhiteSpaceFormattingStrategy` is involved?  
For a language, I’m using it to handle non-space whitespace characters, similar to your case.

---

<div class="post-metadata">

**Author:** ![lemming](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/lemming/32/60_2.png) [@lemming](https://platform.jetbrains.com/u/lemming)\
**Post date:** [December 4, 2025, 1:09pm UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255/4 "2025-12-04T13:09:10Z")

</div>

In this case, it’s not merged by the formatter, it’s when inserting two consecutive PSI elements which have IElementTypes of TokenType.WHITE\_SPACE. What is odd is that one of the tokens has a different IElementType, at least when lexing, so I’m wondering if I need to handle it in ASTFactory to give it a custom PSI type which returns that element type, or something like that. I’m guessing it currently gets a standard element TokenType.WHITE\_SPACE because its token type is in the set returned from getWhitespaceTokens in my ParserDefinition. But I haven’t debugged all the way through all this yet.

That said, that does look like a very useful EP, and one I could potentially use if I want to just treat all whitespace as a single token type, so thanks for that!

---

<div class="post-metadata">

**Author:** ![jansorg](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/jansorg/32/52_2.png) [@jansorg](https://platform.jetbrains.com/u/jansorg)\
**Post date:** [December 4, 2025, 1:52pm UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255/5 "2025-12-04T13:52:20Z")

</div>

Whitespace is tricky…

I haven’t had this case yet. Glancing at the method is seems to merge whitespace if left and right ASTNodes have type `TokenType.WHITE_SPACE`.

Unfortunately, a `PsiWhiteSpaceImpl` is AFAIK the only PSI whitespace element and always has type `WHITE_SPACE`, even if your lexer returns a different type for tokens. You can use custom whitespace types in the parser, but as soon as PSI is created such nodes are turned into `WHITE_SPACE` as far as I remember. You could detect such `PsiElement` whitespace by checking `text` or `textContains(',')`.

It _may_ be possible to lex `, ` in `a, b` as a single node of a custom whitespace token type (e.g. `COMMA_WHITESPACE`) instead of as two tokens `WHITE_SPACE WHITE_SPACE`. That would avoid the merging. The `WhiteSpaceFormattingStrategy` could then reduce too much whitespace around a comma to a smaller chunk.  
But this is just a thought, it may not work at all.

---

<div class="post-metadata">

**Author:** ![lemming](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/lemming/32/60_2.png) [@lemming](https://platform.jetbrains.com/u/lemming)\
**Post date:** [December 4, 2025, 8:45pm UTC](https://platform.jetbrains.com/t/psi-manipulation-merges-consecutive-whitespace-tokens/3255/6 "2025-12-04T20:45:10Z")

</div>

Yes, I’m starting to think that my best option is to lex the commas along with the rest of the whitespace into a single token, and then use `WhiteSpaceFormattingStrategy` to control how they appear in the resulting whitespace tokens after formatting. I’ll try that and report back.
