Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 313133 - [Formatter/Comments] Comments sometimes have wrong indentation
Summary: [Formatter/Comments] Comments sometimes have wrong indentation
Status: RESOLVED WONTFIX
Alias: None
Product: TMF
Classification: Modeling
Component: Xtext (show other bugs)
Version: 1.0.0   Edit
Hardware: PC Mac OS X - Carbon (unsup.)
: P3 normal with 9 votes (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 280066 406152 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-05-17 09:38 EDT by Moritz Eysholdt CLA
Modified: 2016-07-20 11:36 EDT (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Eysholdt CLA 2010-05-17 09:38:25 EDT
If comments are right behind a place where the level of indentation is increased/decreased, the comments have the wrong level of indentation.

Example:

hugo1 {
// comment1
  blablabla
}


This is because formatting instructions are typically matched at the transition from one token to the next, i.e. the next token has to be matched to actually find the instruction. In the example above the change in indentation will be noticed by the formatter when it reaches "blablabla".

When formatting comments, the formatter would need to do a lookahead to the next semantic token. This would also improve the possibilities to merge formatting instructions that are triggered by a comment with the instructions related to the surrounding semantic tokens.
Comment 1 Moritz Eysholdt CLA 2010-05-17 09:43:04 EDT
*** Bug 280066 has been marked as a duplicate of this bug. ***
Comment 2 Moritz Eysholdt CLA 2010-05-18 09:30:38 EDT
In MWE2 I observed that ML_COMMENTs following a SL_COMMENT have no indentation at all.
Comment 3 Moritz Eysholdt CLA 2013-05-27 04:36:07 EDT
*** Bug 406152 has been marked as a duplicate of this bug. ***
Comment 4 Alexander Fichtinger CLA 2014-01-24 02:38:42 EST
We also observed this problem (on Windows, it's not a Mac specific problem apparently).

Examples:

Example 1:

FUNCTION_BLOCK FUB_LEDCylon
/*
     * The LEDs are shifted back and forth.
     */
	VAR_INPUT
		Trigger : BOOL;
	END_VAR
END_FUNCTION_BLOCK



Example 2:

PROGRAM RaspberryPiPiface

	VAR
	/* 1st digital input of the PiFace */
		DInput1 : BOOL;
		/* 2nd digital input of the PiFace */
		DInput2 : BOOL;
	END_VAR
END_PROGRAM
Comment 5 Moritz CLA 2014-01-27 09:06:19 EST
We had the same problem when providing formatting-support for an XPath-based grammar.

Our workaround looks as follows:

We subclassed Line.
We subclassed FormattingConfigBasedStream to override createLine(List<LineEntry> initialEntries, int leftover) to return subclass of line and override createLineEntry(7 parameters.

The latter method looks like this:

    @Override
    public LineEntry createLineEntry(EObject grammarElement,
                                     String value,
                                     boolean isHidden,
                                     Set<ElementLocator> beforeLocators,
                                     String leadingWS,
                                     int indent,
                                     ParserRule hiddenTokenDefition)
    {
        afterComment = inComment;
        inComment = isComment(grammarElement);
        lastIndent = indent;
        return super.createLineEntry(grammarElement,
                                     value,
                                     isHidden,
                                     beforeLocators,
                                     leadingWS,
                                     indent,
                                     hiddenTokenDefition);
    }

In the subclass of "Line" we access the members "afterComment" and "inComment" in "add(..)": 

        @Override
        public Line add(LineEntry lineEntry)
            throws IOException
        {
            if (afterComment)
            {
                // take the indention of the entry added last and correct this line
                indent = getIndentation(lastIndent);
            }

            afterComment = inComment;
            return super.add(lineEntry);
        }

This seems to work well except if multi-line comment follows multi-line comment. Might not be a general solution though.
Comment 6 Markus Duft CLA 2014-08-27 07:16:47 EDT
Hey. The proposed workaround does not work for us. I implemented it quite similar, but comments in the first line after a { are not indented. Actually the bad thing is, that indentation of the first comment line is /removed/ on format. thus it looks like this:

persistent XX {
/**
	 * asd
	 */
	data-category XX;

that's not cool :( any other ideas on how to fix that or work around the issue?
Comment 7 Henrik Rentz-Reichert CLA 2015-04-16 08:20:16 EDT
The workaround sounds complicated...

Given that this is a standard situation which lets the formatter fail: do you think that you can provide a fix?
Comment 8 Michael Wittner CLA 2015-04-16 09:14:26 EDT
I would also be very interested in getting this issue fixed. For those users of our DSL that invoke the formatter this will most probably be the first issue they will discover...
Comment 9 Markus Duft CLA 2015-04-16 09:15:57 EDT
I already also voted on this a while ago, and actually this bug is preventing us from enabling a "format on save" feature company wide to get uniform formats in our (multiple hundreds) DSL files :(
Comment 10 Moritz Eysholdt CLA 2015-04-16 09:23:08 EDT
We're currently building a completely new formatting infrastructure for Xtext. An alpha version has already been release with Xtext 2.8.0:
http://www.eclipse.org/Xtext/releasenotes.html#new-formatter-provisional

The new infrastructure fixes this and several other problems with the formatter.

We'd rather get the new infrastructure great and API-stable then invest time into the hard-to-maintain code base of the old formatter.
Comment 11 Karsten Thoms CLA 2016-07-20 11:36:11 EDT
The new formatter API was released in the meantime, and the old API is now deprecated.