Bug 481692 - Formatter fails with IllegalArgumentException "length must be >= 0"
Summary: Formatter fails with IllegalArgumentException "length must be >= 0"
Status: NEW
Alias: None
Product: Xtend
Classification: Tools
Component: Core (show other bugs)
Version: 2.9.0   Edit
Hardware: PC Linux
: P3 normal with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 521909 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-11-09 04:35 EST by Jesper Eskilson CLA
Modified: 2017-09-06 05:18 EDT (History)
3 users (show)

See Also:


Attachments
Source code triggering the formatting error. (240 bytes, application/octet-stream)
2015-11-09 04:35 EST, Jesper Eskilson CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jesper Eskilson CLA 2015-11-09 04:35:21 EST
Created attachment 257816 [details]
Source code triggering the formatting error.

I get a illegal argument exception when formatting a particular piece of source code:

package com.iar.jsonrpc

class FormattingBug {
    def foo()
    {      
        val x = 2
        '''
        // Formatting is ok if the blank line after the IF below is removed.
        «IF x > 0»

        «ENDIF»
        '''
    }
}

(I've attached the source file also.)

The exception is:

java.lang.IllegalArgumentException: length must be >= 0
	at com.google.common.base.Preconditions.checkArgument(Preconditions.java:93)
	at org.eclipse.xtext.formatting2.regionaccess.internal.TextReplacement.<init>(TextReplacement.java:24)
	at org.eclipse.xtext.formatting2.regionaccess.internal.TextRegionRewriter.createReplacement(TextRegionRewriter.java:79)
	at org.eclipse.xtext.formatting2.regionaccess.internal.AbstractTextSegment.replaceWith(AbstractTextSegment.java:117)
	at org.eclipse.xtext.formatting2.internal.WhitespaceReplacer.createReplacements(WhitespaceReplacer.java:92)
	at org.eclipse.xtext.formatting2.internal.FormattableDocument.createReplacements(FormattableDocument.java:143)
	at org.eclipse.xtext.formatting2.internal.FormattableDocument.renderToTextReplacements(FormattableDocument.java:281)
	at org.eclipse.xtext.formatting2.AbstractFormatter2.format(AbstractFormatter2.java:270)
	at org.eclipse.xtext.ui.editor.formatting2.ContentFormatter.exec(ContentFormatter.java:90)
	at org.eclipse.xtext.ui.editor.formatting2.ContentFormatter$FormattingUnitOfWork.exec(ContentFormatter.java:57)
	at org.eclipse.xtext.ui.editor.formatting2.ContentFormatter$FormattingUnitOfWork.exec(ContentFormatter.java:1)
	at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:121)
	at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:520)
	at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.priorityReadOnly(XtextDocument.java:485)
	at org.eclipse.xtext.ui.editor.model.XtextDocument.priorityReadOnly(XtextDocument.java:142)
	at org.eclipse.xtext.ui.editor.formatting2.ContentFormatter.format(ContentFormatter.java:103)
	at org.eclipse.jface.text.source.SourceViewer.doOperation(SourceViewer.java:1023)
Comment 1 Jesper Eskilson CLA 2015-11-09 04:41:44 EST
Some more detail: the culprit seem to be the empty line after the IF. If I replace the empty line with a line containing spaces (so that the amount of space aligns with the IF), then formatting works.

Hope this helps in figuring out the problem.
Comment 2 Christian Dietrich CLA 2016-09-12 00:35:00 EDT
can be reproduced with 2.11

problem is in RichStringFormatter.format(RichString richString, IFormattableDocument doc) 

(length is < 0 since rootIndentLenght > line.indentLength

@Mortiz:

Should that line be formatted at all?

fixing the length to zero would result in

			«IF x > 0»
						
			«ENDIF»

not formatting would result in

			«IF x > 0»
			
			«ENDIF»

or should the emply line be kept
Comment 3 Moritz Eysholdt CLA 2016-09-12 04:46:30 EDT
the empty line should be kept. The general rule is that the formatter must only (and should) change whitespace that does not impact the template's output. That's generally *not* the white-space highlighted as "grey-space", but other whitespace and also whitespace inside « ».
Comment 4 Karsten Thoms CLA 2017-09-06 05:18:23 EDT
*** Bug 521909 has been marked as a duplicate of this bug. ***