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 139802

Summary: Indent.reindent puts braces on other lines
Product: java Reporter: err <err>
Component: EditorAssignee: Dusan Balek <dbalek>
Status: VERIFIED FIXED    
Severity: blocker CC: issues, mslama, vstejskal
Priority: P3 Keywords: REGRESSION
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: DEFECT Exception Reporter:
Bug Depends on:    
Bug Blocks: 179047    

Description err 2008-07-12 22:21:16 UTC
According to the doc: "... indent only but does not do any other code beautification."

To reproduce using jVi (starting with clean userdir)

Do Tools > Options > JavaCode > Formatting
        Category: Alignment and Braces
        Tab: Braces Placement
for both ClassDeclaration and MethodDeclaration select "New Line". Leave Other as "Same Line".

Then reindent a method and notice that braces get moved around. More peculiar is that "if" braces gets moved around. Of
course no braces should be moved around, but its moving more braces than even a reformat would not move.

Here's a specific example, in jVi put the cursor on the line 'void foo', and enter the three character command "6==" to
reindent 6 lines. Expect nothing to happen after reindent, since indentation is correct. Notice that brace after Method
decl and brace after "if" are both moved. Then select the method with mouse and do Alt-Shft-F and the "if" brace gets
moved back up.


    void foo() {
        bar(1,2);
        if(sl.isEmpty()) {
            System.err.println("hello");
        }
    }

===== after reindent =====

    void foo()
    {
        bar(1,2);
        if(sl.isEmpty())
        {
            System.err.println("hello");
        }
    }

===== after reformat =====

    void foo()
    {
        bar(1, 2);
        if (sl.isEmpty()) {
            System.err.println("hello");
        }
    }


Product Version: NetBeans IDE Dev (Build 200807101204)
Java: 1.6.0_03; Java HotSpot(TM) Client VM 1.6.0_03-b05
System: Windows XP version 5.1 running on x86; Cp1252; en_US (nb)
Userdir: C:\Documents and Settings\erra\.netbeans\dev
Comment 1 Vitezslav Stejskal 2008-07-14 13:06:25 UTC
> Here's a specific example, in jVi put the cursor on the line 'void foo', and enter the three character command "6=="
> to reindent 6 lines.

How exactly is this implemented in jVi?


> Expect nothing to happen after reindent, since indentation is correct.

Depending on the above question this statement may not be true. If this is done by reformatting the 6 lines area the
action will apply all formatting rules. If jVi invokes Indent.reindent(startOffset, endOffset) than the statement is
correct and something's broken on our side.


> Notice that brace after Method decl and brace after "if" are both moved. Then select the method with mouse
> and do Alt-Shft-F and the "if" brace gets moved back up.

This is definitely weird.
Comment 2 err 2008-07-14 15:09:53 UTC
It uses Indent.reindent. BTW, before filing the bug I verified that this is the code that is being invoked, since I
couldn't believe it either. I just checked, things work fine on 6.1.

> How exactly is this implemented in jVi?

    @Override
    public void reindent(int line, int count) {
        if(getDoc() instanceof BaseDocument) {
            BaseDocument doc = (BaseDocument)getDoc();
            boolean keepAtomicLock = doc.isAtomicLock();
            if(keepAtomicLock)
                doc.atomicUnlock();
            Indent indent = Indent.get(doc);
            indent.lock();
            try {
                doc.atomicLock();
                try {
                    indent.reindent(getLineStartOffset(line),
                                    getLineEndOffset(line + count - 1));
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                } finally {
                    if(!keepAtomicLock)
                        doc.atomicUnlock();
                }
            } finally {
                indent.unlock();
            }
        } else {
            Util.vim_beep();
        }
    }
Comment 3 Jan Becicka 2008-07-21 17:14:03 UTC
It is not P2 imo.
Comment 4 err 2008-07-26 01:24:52 UTC
This is working now. I'll close it.
Comment 5 err 2008-07-27 23:02:34 UTC
Reopening. Following directions from a clean userdir still creates the problem.
(now get there by: Tools > Options > Editor > Formatting; Lang: Java, Category: Braces)

BTW, I initially filed against editor component. But does this implementation come under a "java" component?

Product Version: NetBeans IDE Dev (Build 200807251401)
Comment 6 err 2008-08-04 01:56:57 UTC
Part of the problem's explanation is that in createTask, see backtrace excerpt below, there is

            Lookup lookup = MimeLookup.getLookup(mimePath);
              ...
                IndentTask.Factory factory = lookup.lookup(IndentTask.Factory.class);

and factory is null when the mimePath is "MimePath[text/x-java]: RT: null". So later, since there are no tasks, at

        org.netbeans.modules.editor.indent.IndentImpl.reindent(IndentImpl.java:198)

it can't indent, so it decides to use the formatter.

org.netbeans.modules.editor.indent.TaskHandler$MimeItem.createTask(TaskHandler.java:482)
org.netbeans.modules.editor.indent.TaskHandler.addItem(TaskHandler.java:277)
org.netbeans.modules.editor.indent.TaskHandler.collectTasks(TaskHandler.java:150)
org.netbeans.modules.editor.indent.IndentImpl.indentLock(IndentImpl.java:131)
org.netbeans.modules.editor.indent.api.Indent.lock(Indent.java:113)
org.netbeans.modules.jvi.NbBuffer.reindent(NbBuffer.java:182)



Any hints on where to look for IndentTask for text/x-java? And then how to add it to the mime's lookup? Ironically, I
deleted the old, pre 6.5, source trees off of my system yesterday. Looking in there might have helped. I couldn't find
any IndentTask in editor.* or java.* in the 6.5 source.
Comment 7 David Strupl 2009-06-08 13:58:08 UTC
Marek, these are yours now ...
Comment 8 err 2009-12-03 13:57:00 UTC
In Comment #6 I discuss that this problem exists because there is no IndentTask registered for text/java; so the editor infrastructure is defaulting to using the format task.

Reindent is a common command in jVi/vim; there is a separate command for format. (I keep getting jVi bug reports about this problem).

Note that this used to work in 6.1. I vaguely remember that there was some infrastructure re-org that happened in 6.5, and I wonder if the problem is simply that indent was never hooked up for java when porting to the new infrastructure.

Reassigning to java editor, since that is probably where the issue is. If someone can point to where the indent code lives in the java editor (or say that the feature is no longer supported) I can take a look at it; a pointer to where the format is registered would also be helpful.
Comment 9 Vitezslav Stejskal 2009-12-03 23:42:39 UTC
(In reply to comment #8)
> Note that this used to work in 6.1. I vaguely remember that there was some
> infrastructure re-org that happened in 6.5, and I wonder if the problem is
> simply that indent was never hooked up for java when porting to the new
> infrastructure.
Oh yes :-).

> Reassigning to java editor, since that is probably where the issue is. If
> someone can point to where the indent code lives in the java editor (or say
> that the feature is no longer supported) I can take a look at it; a pointer to
> where the format is registered would also be helpful.
Somewhere around JavaFormatSupport and JavaFormatter in java.editor.lib module. AFAIK this is still using the old Syntax and should be converted to use lexer tokens and registered as IndentTask. Be warned though, it's ugly and a few people already declared war to it, but then withdrew.
Comment 10 Dusan Balek 2010-10-18 08:46:08 UTC
IndentTask for text/x-java implemented. See

http://hg.netbeans.org/jet-main/rev/62d7737905e6

So hopefully this issue gets fixed.
Comment 11 err 2010-11-21 13:47:20 UTC
Hooray.