Bug 436490 - [content assist][templates] Leading comments break indentation in Template Proposals preview popup
Summary: [content assist][templates] Leading comments break indentation in Template Pr...
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 minor (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks: 429433
  Show dependency tree
 
Reported: 2014-06-03 12:47 EDT by Andreas Sewe CLA
Modified: 2022-10-31 10:36 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Sewe CLA 2014-06-03 12:47:33 EDT
The following template (the default GridData template prefixed with its description as a comment) is indented incorrectly in the Content Assist preview popup

// new GridData for a Control
${gridData:newType(org.eclipse.swt.layout.GridData)} ${layoutData:newName(org.eclipse.swt.layout.GridData)} = new ${gridData}(SWT.${horizontal:link(FILL, BEGINNING, CENTER, END)}, SWT.${vertical:link(FILL, TOP, CENTER, BOTTOM)}, ${hex:link(true, false)}, ${vex:link(false, true)});
${layoutData}.widthHint= ${width:link(SWT.DEFAULT)};
${layoutData}.heightHint= ${height:link(SWT.DEFAULT)};
${parent:var(org.eclipse.swt.widgets.Control)}.setLayoutData(${layoutData});
${imp:import(org.eclipse.swt.SWT)}${cursor}

Here's what the preview looks like:

// new GridData for a Control
org.eclipse.swt.layout.GridData gridData = new org.eclipse.swt.layout.GridData(
		SWT.FILL, SWT.FILL, true, false);
		gridData.widthHint = SWT.DEFAULT;
		gridData.heightHint = SWT.DEFAULT;
		parent.setLayoutData(gridData);

Interestingly, this bug only manifests itself for certain comments; neither the comment's length nor the comment's number of words alone triggers the bug. In particular, a leading comment of

// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9

is totally fine, despite it being longer than "new GridData for a Control" and containing more "words".

Anyway, I think the problem is located in the vicinity of org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal.getAdditionalProposalInfo(), but I haven't looked much closer than that yet.
Comment 1 Andreas Sewe CLA 2014-06-04 04:36:38 EDT
Further analysis:

Regardless of the comment's contents, the TemplateBuffer that goes into IndentUtil.indentLines(..) in TemplateProposal.getAdditionalProposalInfo() always has the same shape, i.e., leading whitespace:

// comment
\torg.eclipse.swt.layout.GridData gridData = new org.eclipse.swt.layout.GridData(
\t\t\tSWT.FILL, SWT.FILL, true, false);
\tgridData.widthHint = SWT.DEFAULT;
\tgridData.heightHint = SWT.DEFAULT;
\tparent.setLayoutData(gridData);

In other words, the Document that goes into IndentUtil.indentLines(..) is the same.

However, JavaIndenter.computeIndentation(..) computes, for a non-problematic comment like "new GridData", a result of null, "", "\t\t", "", "", "", and "" for the 7 lines of the template. For a problematic comment like "new GridData for a Control" it computes null, "", "\t\t", "\t\t", "\t\t", "\t\t", "\t\t" instead.

As JavaIndenter is stateful it stands to reason that while indenting the first three lines its state diverges. As to why that is, I have no clue yet (but I hope this analysis helps in tracking down the problem).
Comment 2 Dani Megert CLA 2014-06-04 05:15:56 EDT
This was already wrong in 3.8. The indentation is correct when inserting the proposal.

Note that (the) wrong indentation can also be seen without adding a comment:

org.eclipse.swt.layout.GridData gridData = new org.eclipse.swt.layout.GridData(
		SWT.FILL, SWT.FILL, true, false);
gridData.widthHint = SWT.DEFAULT;
gridData.heightHint = SWT.DEFAULT;
text.setLayoutData(gridData);
Comment 3 Andreas Sewe CLA 2014-06-04 05:25:47 EDT
(In reply to Dani Megert from comment #2)
> This was already wrong in 3.8. The indentation is correct when inserting the
> proposal.

I know. It's just the preview popup where it looks awkward.

> Note that (the) wrong indentation can also be seen without adding a comment:
> 
> org.eclipse.swt.layout.GridData gridData = new
> org.eclipse.swt.layout.GridData(
> 		SWT.FILL, SWT.FILL, true, false);
> gridData.widthHint = SWT.DEFAULT;
> gridData.heightHint = SWT.DEFAULT;
> text.setLayoutData(gridData);

That is something I cannot reproduce *unless* you consider the third line to be incorrectly indented. All *other& lines like the ones starting with "gridData." are not indented in the preview but rather shown flushed left. *That* behavior changes only if I add the comment.

FWIW, note that the bug also occurs if the first line is

  "new GridData for a Control";

rather than

  // new GridData for a Control

so the bug is not tied to the "commentness" of the first line.

Anyway, thanks for taking care of this.
Comment 4 Daniel Sokolowski CLA 2015-02-15 23:36:13 EST
Also effected by this issue: my template is as follows but using empty lines also breaks the indentation.

```
//
//
System.debug('}}}'); 
try { System.debug(JSON.serialize(${target})); } catch(Exception e) { System.debug('Couldn\'t JSON serialize: ' + ${target}); }
System.debug('{{{');
//
//
```
Comment 5 Daniel Sokolowski CLA 2015-02-15 23:37:25 EST
s/effected/affected
Comment 6 Eclipse Genie CLA 2020-10-13 18:47:27 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 7 Eclipse Genie CLA 2022-10-31 10:36:53 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.