spotless icon indicating copy to clipboard operation
spotless copied to clipboard

maven replace regex replacement leading whitespace

Open QarthO opened this issue 2 months ago • 4 comments

I'm doing some replaceRegex rules and I'm attempting to replace some text with { } however the leading whitespace gets trimmed and doesn't get added.

I've tried numerous work arounds using unicode, cdata, escape codes, and they all get trimmed (or the formatter just breaks)

How the heck do i add a leading space in the replacement text?

 <replaceRegex>
    <name>collapseEmptyBlocks</name>
    <searchRegex>(?s)[\s\r\n]*\{[\s\r\n]*\}</searchRegex>
    <replacement> {}</replacement> <!-- why spotless remove the leading whitespace???  -->
</replaceRegex>

QarthO avatar Oct 15 '25 12:10 QarthO

the problem here is XML, not Spotless. Use a character entity

<replacement>&#32;{}</replacement>

nedtwigg avatar Oct 15 '25 19:10 nedtwigg

I've tried that and the formatter breaks completely and no longer formats files, (but no errors)

QarthO avatar Oct 15 '25 19:10 QarthO

This was the work around I had to do for other who come across something similar

<replaceRegex>
    <name>collapseEmptyBlocks</name>
    <searchRegex>(?s)[\s\r\n]*\{[\s\r\n]*\}</searchRegex>
    <replacement>__FORMATTER_SPACE__ { }</replacement>
</replaceRegex>
<replaceRegex>
    <name>collapseEmptyBlocks</name>
    <searchRegex>__FORMATTER_SPACE__</searchRegex>
    <replacement></replacement>
</replaceRegex>

QarthO avatar Oct 15 '25 19:10 QarthO

hmm, not sure then

nedtwigg avatar Oct 15 '25 19:10 nedtwigg