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 116231 - HTML indentation fails for some embedded scenarios
Summary: HTML indentation fails for some embedded scenarios
Status: RESOLVED FIXED
Alias: None
Product: web
Classification: Unclassified
Component: HTML Editor (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Torbjorn Norbye
URL:
Keywords:
: 116235 (view as bug list)
Depends on: 120491
Blocks: 104929
  Show dependency tree
 
Reported: 2007-09-21 00:46 UTC by Torbjorn Norbye
Modified: 2009-05-18 10:47 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Torbjorn Norbye 2007-09-21 00:46:56 UTC
RHTML formatting no longer works for a lot of typical RHTML pages. This is due to some changes in the way HTML
indentation works, and I believe I can work around most of them, but I've encountered two blocking problems.

The following RHTML fragment will no longer indent:

<tr class="<%= article.status %>">
<td></td>
</tr>

In Beta 1, it gets indented as expected:

<tr class="<%= article.status %>">
  <td></td>
</tr>

However, with trunk bits (and Ruby formatting disabled, so it's just the HTML indenter that operates on the buffer), it
doesn't realign the inner <td></td> based on the outer <tr>. In fact, it seems not to modify any formatting at all.

The problem appears to be the embedded RHTML token within the <tr> tag, inside the class attribute. If I remove it,
everything works okay. However, this is unfortunately a -very- common idiom in RHTML files, so we have to support it -
and it worked before.

It also takes very little to upset the editor; inserting newlines in some places will post exception popups:

javax.swing.text.BadLocationException: Invalid offset=-1 not within <0, 60>
	at org.netbeans.editor.Utilities.checkOffsetValid(Utilities.java:1309)
	at org.netbeans.editor.Utilities.getLineOffset(Utilities.java:591)
	at
org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.calcIndents_processOpeningTag(TagBasedLexerFormatter.java:163)
	at org.netbeans.modules.editor.structure.formatting.TagBasedLexerFormatter.reformat(TagBasedLexerFormatter.java:281)
	at org.netbeans.modules.html.editor.indent.HtmlIndentTask.reindent(HtmlIndentTask.java:50)
	at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:350)
	at org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:213)
	
This means RHTML editing, not just formatting, is hosed at the moment which is why I'm making this a P1.

Note that there is one key difference between the lexing in Beta 1 and the trunk: The language embedding sections are no
longer using joinSections for LanguageEmbedding.create. I turned them off recently because something changed in the
lexer which was causing problems for indentation.  But you can re-enable it using this patch in case this helps
(possibly html should have joinSections=true and Ruby have joinSections=false):

Index: ruby/rhtml/src/org/netbeans/modules/ruby/rhtml/lexer/api/RhtmlTokenId.java
===================================================================
RCS file: /cvs/ruby/rhtml/src/org/netbeans/modules/ruby/rhtml/lexer/api/RhtmlTokenId.java,v
retrieving revision 1.4
diff -u -r1.4 RhtmlTokenId.java
--- ruby/rhtml/src/org/netbeans/modules/ruby/rhtml/lexer/api/RhtmlTokenId.java  6 Sep 2007 19:17:01 -0000  1.4
+++ ruby/rhtml/src/org/netbeans/modules/ruby/rhtml/lexer/api/RhtmlTokenId.java  20 Sep 2007 23:44:11 -0000
@@ -94,10 +94,10 @@
                                   LanguagePath languagePath, InputAttributes inputAttributes) {
             switch(token.id()) {
                 case HTML:
-                    return LanguageEmbedding.create(HTMLTokenId.language(), 0, 0, false);
+                    return LanguageEmbedding.create(HTMLTokenId.language(), 0, 0, true);
                 case RUBY_EXPR:
                 case RUBY:
-                    return LanguageEmbedding.create(RubyTokenId.language(), 0, 0, false);
+                    return LanguageEmbedding.create(RubyTokenId.language(), 0, 0, true);
                 default:
                     return null;
             }
Comment 1 Torbjorn Norbye 2007-09-21 01:53:52 UTC
*** Issue 116235 has been marked as a duplicate of this issue. ***
Comment 2 Tomasz Slota 2007-09-21 14:30:21 UTC
Formatting is broken by exception thrown when there is a language embedded *inside a tag*. This can be also reproduced in JSP with the following code:

<tr class="<%= page %>">
<td></td>
</tr>

I will probably need support from lexer to fix this. I suggest downgrading to P2.

Comment 3 Torbjorn Norbye 2007-09-21 16:26:16 UTC
The reason I made it a P1 was the exception popup which occurs whenever you try to edit the file. If that can be
resolved (and the exception is coming from TagBasedLexerFormatter), then the rest of the issue can be a P2 - as long as
that doesn't mean it won't get done for 6.0 :)

Is there anything I can do to help? Do you need a new issue filed against lexer or are you already talking to Mila?

As a fallback plan for 6.0 (if we don't get this done), perhaps we can keep the old HTML formatter around, and in the
HtmlIndentTask, invoke it whenever we see that the outermost language has mimetype "application/x-httpd-eruby". But this
is definitely only an emergency solution.

Unfortunately, I think I have to make other changes to the RHTML indenter too (after the above bug is fixed) to address
the fact that <% %> sections are no longer indented by the HTML indenter, and so on.  So, hopefully this bug won't be
fixed at the very last minute before the deadline since I need additional time :)

Here's a patch which restores RHTML formatting for now as described above (it -does- expose a lexer bug on some source
files)

Index: html/editor/src/org/netbeans/modules/html/editor/indent/HtmlIndentTask.java
===================================================================
RCS file: /cvs/html/editor/src/org/netbeans/modules/html/editor/indent/HtmlIndentTask.java,v
retrieving revision 1.2
diff -u -r1.2 HtmlIndentTask.java
--- html/editor/src/org/netbeans/modules/html/editor/indent/HtmlIndentTask.java 17 Sep 2007 15:40:18 -0000 1.2
+++ html/editor/src/org/netbeans/modules/html/editor/indent/HtmlIndentTask.java 21 Sep 2007 15:25:10 -0000
@@ -47,6 +47,15 @@
     }
 
     public void reindent() throws BadLocationException {
+        // Workaround for http://www.netbeans.org/issues/show_bug.cgi?id=116231
+        String mimeType = NbEditorUtilities.getMimeType(context.document());
+        if (mimeType != null && mimeType.equals("application/x-httpd-eruby")) { // NOI18N
+            new org.netbeans.editor.ext.html.HTMLFormatter(HTMLKit.class).
+                    reformat((BaseDocument) context.document(), 
+                       context.startOffset(), context.endOffset(), true);;
+            return;
+        }
+
         getFormatter().reformat((BaseDocument) context.document(), context.startOffset(), context.endOffset(), true);
     }
 
Comment 4 Tomasz Slota 2007-09-21 16:53:49 UTC
OK, the error window didn't pop up with my config. I already worked around the missing lexer infrastructure and am very close to having this issue fixed, it 
will not happen today though.
Comment 5 Tomasz Slota 2007-09-24 12:58:24 UTC
Fixed.

Checking in web/jspsyntax/src/org/netbeans/modules/web/core/syntax/formatting/JSPLexerFormatter.java;
/cvs/web/jspsyntax/src/org/netbeans/modules/web/core/syntax/formatting/JSPLexerFormatter.java,v  <--  JSPLexerFormatter.java
new revision: 1.4; previous revision: 1.3
done
Checking in html/editor/lib/src/org/netbeans/editor/ext/html/HTMLLexerFormatter.java;
/cvs/html/editor/lib/src/org/netbeans/editor/ext/html/HTMLLexerFormatter.java,v  <--  HTMLLexerFormatter.java
new revision: 1.6; previous revision: 1.5
done
Checking in xml/tageditorsupport/src/org/netbeans/modules/editor/structure/formatting/TagBasedLexerFormatter.java;
/cvs/xml/tageditorsupport/src/org/netbeans/modules/editor/structure/formatting/TagBasedLexerFormatter.java,v  <--  TagBasedLexerFormatter.java
new revision: 1.13; previous revision: 1.12
done
Comment 6 arrix 2007-09-26 14:48:50 UTC
I updated my netbeans as soon as this got fixed. However, there are still some small problems with indentation.
1. extra indent before closing tag after formatting
2. after pressing enter, extra indent inserted before the line under the cursor

I've been trying to simplify the case but it played hide-and-seek. Here is as far as I can get:

<div>
  <%if session[:user]%>
  <%end%>
</div>

After Alt+Shift+F

<div>
  <%if session[:user]%>
    <%end%>
  </div>

Or

<div>
  <%if session[:user]%>
  <%end%>
  <div>$cursor is here</div>
</div>

After pressing enter
<div>
  <%if session[:user]%>
  <%end%>
  <div>
   </div>
   </div>

Netbeans is my favorite ruby IDE. Thank you for the great work! I hope I'm providing helpful information.
Comment 7 Tomasz Slota 2007-09-26 15:33:51 UTC
Thank you for your feedback arrix. The HTML formatting/indentation has been undergoing massive changes, hopefully the situation is stabilized now. I've just 
committed a few fixes in the new line line indentation area, they are not present in any build yet.

Tor please have a look at the RHTML-specific issues, if there are problems please file separate bugs. 
Comment 8 Torbjorn Norbye 2007-10-31 21:51:29 UTC
I examined the specific scenario which is still reproducible:

<div>
  <%if session[:user]%>
  <%end%>
</div>


It turns out that the JRuby lexer is incorrectly passing the second "]" as an identifier token rather than an RBRACKET,
which breaks the formattter. I added a similar workaround to one I have for a few "[" identifiers instead of LBRACKETs
and formatting now passes this test. 

I believe everything else should work in RHTML indentation now (and in particular the two previous broken code fragments
listed in this bug report), so closing this older issue as fixed.