This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 178512 - XML Indent.reindent only affects first line
Summary: XML Indent.reindent only affects first line
Status: RESOLVED FIXED
Alias: None
Product: xml
Classification: Unclassified
Component: Text-Edit (show other bugs)
Version: 6.x
Hardware: PC Windows XP
: P2 normal (vote)
Assignee: _ alexpetrov
URL:
Keywords:
Depends on:
Blocks: 179047
  Show dependency tree
 
Reported: 2009-12-11 14:02 UTC by err
Modified: 2010-04-17 08:19 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
screenshot_XML_before_Format (103.51 KB, image/png)
2010-03-19 21:39 UTC, _ alexpetrov
Details
screenshot_XML_after_Format.png (104.21 KB, image/png)
2010-03-19 21:40 UTC, _ alexpetrov
Details
XML file (899 bytes, text/plain)
2010-03-19 21:41 UTC, _ alexpetrov
Details
XML schema (3.02 KB, text/plain)
2010-03-19 21:43 UTC, _ alexpetrov
Details
XML file (899 bytes, text/plain)
2010-03-19 21:53 UTC, _ alexpetrov
Details
XML schema (3.02 KB, text/plain)
2010-03-19 21:54 UTC, _ alexpetrov
Details
screen shot: only_Format_item_is_provided.PNG (44.13 KB, image/png)
2010-04-13 14:44 UTC, _ alexpetrov
Details
test module (10.69 KB, application/octet-stream)
2010-04-13 17:22 UTC, err
Details
Showing the IDE with the two flags (34.92 KB, image/png)
2010-04-14 17:01 UTC, err
Details

Note You need to log in before you can comment on or make changes to this bug.
Description err 2009-12-11 14:02:36 UTC
Seems like bug is in editor.structure module, which looks like an edit infrastructure thing (otherwise would have filed against xml editor) but perhaps this should be assigned elsewhere. In response to a jVi user's command to reindent several lines in an xml file, only the first line of the requested range gets reindented. The following code in TagBasedLexerFormatter.java looks like the problem (see below for relevent stack trace).

    public void process(Context context) throws BadLocationException{
        if (context.isIndent()){
            enterPressed(context);
        } else {
            reformat(context);
        }
    }

The above code assumes that isIndent() implies that only one line is affected. As an experiment I modified the code as

    public void process(Context context) throws BadLocationException{
        if (context.isIndent()){
            BaseDocument doc = (BaseDocument) context.document();
            int firstLine = Utilities.getLineOffset(doc, context.startOffset());
            int lastLine = Utilities.getLineOffset(doc, context.endOffset());

            if(firstLine == lastLine) {
                enterPressed(context);
            } else {
                reformat(context);
            }
        } else {
            reformat(context);
        }
    }

and this affects all the lines in the range. I did not thoroughly test this.

NOTE: there does not seem to be a function to reindent multiple lines in the XMLIndentTask, but that is an XML issue, not an infrastructure issue, the workaround code shown above defaults to format when multiple lines are requested. Although supporting a true indent is desirable, the main indent infrastructure currently defaults to format if no indent is registered, and this is better than nothing; see Bug 139802 -  Indent.reindent puts braces on other lines.

org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.process(TagBasedLexerFormatter.java:151)
org.netbeans.modules.xml.text.indent.XMLIndentTask.reindent(XMLIndentTask.java:81)
org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:545)
org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:314)
org.netbeans.modules.editor.indent.IndentImpl.reindent(IndentImpl.java:232)
org.netbeans.modules.editor.indent.api.Indent.reindent(Indent.java:168)
org.netbeans.modules.jvi.NbBuffer.reindent(NbBuffer.java:191)

Product Version: NetBeans IDE 6.8 (Build 200912041610)
Java: 1.6.0_12; Java HotSpot(TM) Client VM 11.2-b01
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)
Comment 1 Vitezslav Stejskal 2009-12-15 03:21:22 UTC
(In reply to comment #0)
> Seems like bug is in editor.structure module, which looks like an edit
> infrastructure thing (otherwise would have filed against xml editor)

Unfortunately XML folks sneaked their stuff to the editor packages some time ago and since then it's been causing confusion about where to assign defects in this code. Anyway, I'm passing this to xml/editor now.
Comment 2 err 2009-12-15 06:43:18 UTC
I was just looking at indent API and noticed that there is a way to specify that you want a single line indented as opposed to a range.

     void 	reindent(int offset)
          Correct indentation on a single line determined by the given offset.
          Typically it is called after newline ...

     void 	reindent(int startOffset, int endOffset)
          Correct indentation of all lines in the given offset range.
Comment 3 _ alexpetrov 2010-03-19 21:39:08 UTC
I've checked formatting of a XML file by the method org.netbeans.modules.xml.text.indent.XMLIndentTask.reindent() on:
Product Version: NetBeans IDE Dev (Build 201003170201)
Java: 1.6.0_14; Java HotSpot(TM) Client VM 14.0-b16
System: Linux version 2.6.18-8.el5 running on i386

XML file before formatting  - see the attached "screenshot_XML_before_Format.png",
 XML file after using of pop-up menu item "Format" - see the attached "screenshot_XML_after_Format.png".
The appropriate XML and XSD files - in the attachment.

Please provide more info: XML file, which was used for formatting, steps for bug reproducing,...
Comment 4 _ alexpetrov 2010-03-19 21:39:58 UTC
Created attachment 95467 [details]
screenshot_XML_before_Format
Comment 5 _ alexpetrov 2010-03-19 21:40:42 UTC
Created attachment 95468 [details]
screenshot_XML_after_Format.png
Comment 6 _ alexpetrov 2010-03-19 21:41:30 UTC
Created attachment 95469 [details]
XML file
Comment 7 _ alexpetrov 2010-03-19 21:43:03 UTC
Created attachment 95470 [details]
XML schema
Comment 8 _ alexpetrov 2010-03-19 21:53:48 UTC
Created attachment 95473 [details]
XML file
Comment 9 _ alexpetrov 2010-03-19 21:54:40 UTC
Created attachment 95474 [details]
XML schema
Comment 10 err 2010-03-27 22:51:52 UTC
jVi supports both a reformat and a reindent command, just like netbeans, see
        org.netbeans.modules.editor.indent.api.Reformat
        org.netbeans.modules.editor.indent.api.Indent
It looks like TagBasedlexerFormatter treats any indent request (note indent, not reformat) as a single line reformat request by calling enterPressed as discussed in the description to this bug report. As you note, the reformat request works, which I have verified. Expected behavior is to fall back to reformat if there is no reindent available.

Below are the stack traces for reformat and reindent


    reformat.reformat(getLineStartOffset(line), getLineEndOffset(line + count - 1));

org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.reformat(TagBasedLexerFormatter.java:159)
org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.process(TagBasedLexerFormatter.java:154)
^^^^^^^ diverges
org.netbeans.modules.xml.text.indent.XMLIndentTask.reindent(XMLIndentTask.java:81)
org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:545)
org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:314)
^^^^^^^ same stack for three frames
org.netbeans.modules.editor.indent.IndentImpl.reformat(IndentImpl.java:317)
org.netbeans.modules.editor.indent.api.Reformat.reformat(Reformat.java:151)
org.netbeans.modules.jvi.impl.NbBuffer.reformat(NbBuffer.java:224)


    indent.reindent(getLineStartOffset(line), getLineEndOffset(line + count - 1));

org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.enterPressed(TagBasedLexerFormatter.java:555)
org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.process(TagBasedLexerFormatter.java:152)
^^^^^^^ diverges
org.netbeans.modules.xml.text.indent.XMLIndentTask.reindent(XMLIndentTask.java:81)
org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:545)
org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:314)
^^^^^^^ same stack for three frames
org.netbeans.modules.editor.indent.IndentImpl.reindent(IndentImpl.java:256)
org.netbeans.modules.editor.indent.api.Indent.reindent(Indent.java:168)


Product Version: NetBeans IDE Dev (Build 201003270201)
Java: 1.6.0_18; Java HotSpot(TM) Client VM 16.0-b13
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)
Userdir: C:\Documents and Settings\erra\.netbeans\dev
Comment 11 _ alexpetrov 2010-04-13 14:44:52 UTC
Created attachment 97212 [details]
screen shot: only_Format_item_is_provided.PNG
Comment 12 _ alexpetrov 2010-04-13 14:45:52 UTC
I understand all java code placed above.

But, please, describe wrong NB IDE behaviour.
Please, tell me what's wrong and how should I reproduce this wrong NB IDE behaviour?

Now I see only "Format" item in a pop-up menu, which is available for .xml files (see the attached screenshot "only_Format_item_is_provided.PNG").
And it works properly as you mentioned above.
Unfortunately I didn't find the following fragment of code ".reindent(getLineStartOffset(line)", which you provided, in any .java file of the trunk 

repository.
How should I invoke "org.netbeans.modules.editor.indent.api.Indent.reindent()"? 
Just press the key "Enter"?
In my opinion this action should have effect only on one (current) line of an edited .xml file.
Or press the key "Enter" when several lines are selected?

Please, give me more info without using of java code.
Comment 13 err 2010-04-13 17:18:41 UTC
> describe wrong NB IDE behaviour.

I support the jVi plugin (a vim clone) which uses the public API org.netbeans.modules.editor.indent.api.Indent. This API has a bug when working on an xml file. Sorry for the confusion.

I didn't find any unit tests for TagBasedLexerFormatter in editor.structure so I am attaching tiny module source that demonstartes the bug. If you build/install this module, you see two flags in the toolbar, they have tooltips "Indent" and "Reformat". (If you want to recreate the bug using the jVi plugin I can provide instructions)

- Open you're test file, po.xml
- With the mouse select a big chunk of the xml
- Click the Indent action flag

Observe that at most one line is change

The method being executed is output to stderr with System.err.println

Repeat the test with the Reformat action flag.

Note, when Indent runs it does

        if(start == end)
            indent.reindent(start);
        else
            indent.reindent(start, end);

so both indent APIs can be tried with the test.
Comment 14 err 2010-04-13 17:22:10 UTC
Created attachment 97239 [details]
test module
Comment 15 Sergey Lunegov 2010-04-14 09:26:47 UTC
Nikita, please take a look.
Comment 16 Nikita Krjukov 2010-04-14 15:25:04 UTC
Thanks for your simple test module. I believe it should help us. 
But we need get a reliable way of reproducing the bug first.  
I tried to reproduce the problem but failed. See my steps: 

-- Prepared pure trunk repository (6.9 current development version)
-- Unpacked the attached "indent-format" module inside of repository
-- Opened the module inside of NetBeans and tried building it. It was build only after recreation of all dependencies because of changes in modules versions. 
-- Run NetBeans with the new module and convinced that module is installed with Tools-->Plugins-->Installed
-- Opened a Java project and added there the attached XML file. 

PROBLEMS:
-- I don't see the mentioned "Indent" and "Reformat" buttons in toolbar. 
-- Do you mean main toolbar or editor's toolbar? 
-- Probably the demo module sources aren't suitable to latest NetBeans sources.
-- It's still not clear what Indent means in context of this issue.
-- I can do Indent/Reindent with Tab/Shift+Tab buttons so I suppose some other feature is implied here. 

So I think we still need a little bit more details to reproduce and understand the issue.
Comment 17 err 2010-04-14 17:00:18 UTC
Not sure why your steps didn't work, perhaps it is

    > -- Unpacked the attached "indent-format" module inside of repository

I just tried it with what I originally attached using the daily build I downloaded this morning



> -- Do you mean main toolbar or editor's toolbar? 

Main toolbar, see attached png

> -- Probably the demo module sources aren't suitable to latest NetBeans sources.

They are suitable, see steps below

> -- It's still not clear what Indent means in context of this issue.

In the javadoc for org.netbeans.modules.editor.indent.api.Indent it says

         reindent(int startOffset, int endOffset)
         Correct indentation of all lines in the given offset range.

There is a separate method to operate on only a single line

The workaround fix I provided in the original description, might help clarify things as well. Note this is a workaround, though it is similar to the one used by the java indenter when java indent was broken a few releases ago, the javadoc for the Indent class says

        means fixing of the line's indent only but does
        not do any other code beautification.



Not sure why your step didn't work. Maybe it is the

> -- Unpacked the attached "indent-format" module inside of repository

I just treat it like a plugin, not something built as part of the IDE.

Using todays daily build and the zip I originally attached, here is what I just did. Note, no need to change any depndencies.

- unzip indent-format to somewhere by itself
- start up todays version of the IDE
- open the indent-format module just unzipped
- Hit the RUN button
- A second IDE is brought up with the module installed
Observe the two flags. I the attached png, they are in the lower left corner


Product Version: NetBeans IDE Dev (Build 201004140201)
Java: 1.6.0_18; Java HotSpot(TM) Client VM 16.0-b13
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)
Userdir: C:\Documents and Settings\erra\.netbeans\dev
Comment 18 err 2010-04-14 17:01:38 UTC
Created attachment 97380 [details]
Showing the IDE with the two flags
Comment 19 Nikita Krjukov 2010-04-15 17:25:29 UTC
I managed to reproduce the issue just like you described and with some other tricks I even debugged it. It was my mistake that I tried putting your module to our repository. 

Now I see that the functionality declared with API org.netbeans.modules.editor.indent.api.Indent isn't implemented for XML files. The only degenerated case with single line is supported. You can see it at least from the name of method TagBasedLexerFormatter.enterPressed(). It looks like it implies to be called when user create a new line in XML. 

In your suggestion I see simple replacement of Indention by Formatting. But I suppose you can do it from your code explicitly. If so, you have a kind of workaround and the issue isn't so important like P2. 

On the other hand, your suggestion looks reasonable. Though I need some time to test if it can affect something else. 

Regarding adding honest support of Indention for XML, I doubt we can implement such functionality now. So it can be reason for waiving the issue if we won't agree about the workaround.
Comment 20 Nikita Krjukov 2010-04-15 17:43:38 UTC
It's not clear what is the use-case of using Indention. Could you explain how it works in your module? Otherwise it's difficult to check if your patch is safe.
Comment 21 err 2010-04-15 18:26:52 UTC
> In your suggestion I see simple replacement of Indention by Formatting.

Yes, only when multiple lines are selected, otherwise enterPressed is used.

> But I suppose you can do it from your code explicitly. If so, you have
> a kind of workaround and the issue isn't so important like P2. 

Not easily. jVi is a generic editor base on vi/vim. It has no idea what kind of file is being editted. Most of the jVi code can run standalone; there is a netbeans module that interfaces  jVi to the IDE.

> On the other hand, your suggestion looks reasonable. Though I need some
> time to test if it can affect something else. 

What does the user do to invoke the enterPressed(context) to indent one line in the standard IDE xml editor?

I think that the suggested patch does not change current behavior of standard editor at all since I'm guessing that standard editor never calls 
        reindent(int startOffset, int endOffset)
for multiple lines and only uses "reindent(int startOffset)"

> It's not clear what is the use-case of using Indention.

The vi/vim editor support both a format and an indent command. They are defined by vim the same as indent/format in netbeans. I can refer you to the documentation for vim if you want to look in more detail.

> Could you explain how it works in your module?

Select several lines and invoke indent. If you are planning to download jVi and try it, I can provide more detailed instructions.

> Otherwise it's difficult to check if your patch is safe.

Safe for jVi? When I proposed the patch a few months ago I tried it with jVi.


If you are curious, see http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=2802 . Download from sourceforge. Thousands of people use it with NetBeans.
Comment 22 Nikita Krjukov 2010-04-16 14:13:07 UTC
Fixed in trunk http://hg.netbeans.org/main/rev/34715c738aa8
The fix is a kind of workaround. It replaces indention with formatting in cases when more then 1 line of text have to be indented automatically. 

I check all cases of calling the Indent.reindent() method (in trunk) and found that in most cases only one line is affected. The only case when reindent() is called for several lines is the case like javadoc generation, when a new text has to be indented automatically. 

I've seen the similar cases in the following modules: 
-- cnd.completion
-- html.editor
-- javadoc
-- maven.jaxws
-- web.jsf.editor 

In all cases (except web.jsf.editor) it doesn't relate to xml indention. 

Another reason why the change is safe is that the indention and formatting of XML do quite the same now. The only difference is the indention affects only first line. 

For honest fix (not a workaround) it is necessary to implement Indention functionality for XML.
Comment 23 Quality Engineering 2010-04-17 08:19:16 UTC
Integrated into 'main-golden', will be available in build *201004170515* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main/rev/34715c738aa8
User: supernikita@netbeans.org
Log: #178512 - XML Indent.reindent affects only first line