[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jedit-cvs
Subject:    [ jEdit-commits ] SF.net SVN: jedit: [12952]
From:       voxmea () users ! sourceforge ! net
Date:       2008-06-28 2:44:57
Message-ID: E1KCQQr-0007KD-0U () sc8-pr-svn4 ! sourceforge ! net
[Download RAW message or body]

Revision: 12952
          http://jedit.svn.sourceforge.net/jedit/?rev=12952&view=rev
Author:   voxmea
Date:     2008-06-27 19:44:56 -0700 (Fri, 27 Jun 2008)

Log Message:
-----------
Fixed indent calculation bugs. Fixed the way the buffer listener is added and removed \
from a buffer.

Modified Paths:
--------------
    plugins/CommentContinuator/trunk/src/CommentContinuator.java
    plugins/CommentContinuator/trunk/src/CommentContinuatorPlugin.java

Modified: plugins/CommentContinuator/trunk/src/CommentContinuator.java
===================================================================
--- plugins/CommentContinuator/trunk/src/CommentContinuator.java	2008-06-28 01:18:40 \
                UTC (rev 12951)
+++ plugins/CommentContinuator/trunk/src/CommentContinuator.java	2008-06-28 02:44:56 \
UTC (rev 12952) @@ -115,15 +115,18 @@
                 prefix = commentStart.replaceAll(".", " ") + " ";
         } else {
             String nextLine = buffer.getLineText(line + 1);
-            prefix = get_prefix(nextLine) + " ";
-            // prefix already has 1 appended space, so subtract that from extra.
+            prefix = get_prefix(nextLine);
+            // If prefix is non-empty, add a space after the prefix. Otherwise,
+            // extra will take care of the necessary indent.
+            if (prefix.length() > 0)
+                prefix += " ";
             int extra = StandardUtilities.getLeadingWhiteSpace(nextLine) -
-                        StandardUtilities.getLeadingWhiteSpace(text) - 1;
+                        StandardUtilities.getLeadingWhiteSpace(text);
             if (extra > 0) {
                 char[] spaces = new char[extra];
                 for (int i = 0; i < extra; ++i)
                     spaces[i] = ' ';
-                prefix += new String(spaces);
+                prefix = new String(spaces) + prefix;
             }
         }
         // Need a leading prefix because formatParagraph skips existing
@@ -168,48 +171,50 @@
         ta.setCaretPosition(pos);
     }
 
-	protected static void formatParagraph(String text, int maxLineLength,
+    protected static void formatParagraph(String text, int maxLineLength,
                                           int tabSize, StringBuilder buf,
                                           String leadingPrefix, String prefix)
     {
         // align everything to paragraph's leading indent
-		int leadingWhitespaceCount = StandardUtilities.getLeadingWhiteSpace(text);
-		String leadingWhitespace = text.substring(0,leadingWhitespaceCount);
-		int leadingWhitespaceWidth = \
                StandardUtilities.getLeadingWhiteSpaceWidth(text,tabSize);
-        int prefixWidth = prefix.length();
-        int leadingWidth = leadingWhitespaceWidth + prefixWidth;
+        int leadingWhitespaceCount = StandardUtilities.getLeadingWhiteSpace(text);
+        String leadingWhitespace = text.substring(0,leadingWhitespaceCount);
+        int leadingWhitespaceWidth = \
StandardUtilities.getLeadingWhiteSpaceWidth(text,tabSize); +        int \
leadingFirstLineWidth = leadingWhitespaceWidth + leadingPrefix.length(); +        int \
leadingWidth = leadingWhitespaceWidth + prefix.length();  
-		buf.append(leadingWhitespace);
+        buf.append(leadingWhitespace);
         buf.append(leadingPrefix);
         
         String trimmed_prefix = prefix.trim();
         
-		int lineLength = leadingWhitespaceWidth + leadingPrefix.length();
-		StringTokenizer st = new StringTokenizer(text);
-		while(st.hasMoreTokens())
-		{
-			String word = st.nextToken();
+        int lineLength = leadingWhitespaceWidth + leadingPrefix.length();
+        StringTokenizer st = new StringTokenizer(text);
+        while(st.hasMoreTokens())
+        {
+            String word = st.nextToken();
             if (word.equals(trimmed_prefix))
                 continue;
-			if(lineLength == leadingWidth)
-			{
+            
+            if ((lineLength == leadingWidth) ||
+                (lineLength == leadingFirstLineWidth))
+            {
                 // Do nothing
-			}
-			else if(lineLength + word.length() + 1 > maxLineLength)
-			{
-				buf.append('\n');
-				buf.append(leadingWhitespace);
+            }
+            else if(lineLength + word.length() + 1 > maxLineLength)
+            {
+                buf.append('\n');
+                buf.append(leadingWhitespace);
                 buf.append(prefix);
-				lineLength = leadingWidth;
-			}
-			else
-			{
-				buf.append(' ');
-				lineLength++;
-			}
-			buf.append(word);
-			lineLength += word.length();
-		}
+                lineLength = leadingWidth;
+            }
+            else
+            {
+                buf.append(' ');
+                lineLength++;
+            }
+            buf.append(word);
+            lineLength += word.length();
+        }
     }
 
     public void contentInserted(JEditBuffer buffer, int startLine, int offset,
@@ -241,7 +246,7 @@
                 if ((commentEnd != null) && text.endsWith(commentEnd))
                     return;
                 
-                // Find the start of this lines comment char
+                // Find the start of this line's comment char
                 int pos = 0;
                 if (!text.trim().equals("")) {
                     while ((pos < (lineEnd - lineStart)) && 
@@ -264,8 +269,7 @@
                     // Only append leading space after the first line of the
                     // comment, after that idealIndentForLine takes care of
                     // things.
-                    boolean first_line = \
                text.substring(pos).startsWith(commentStart);
-                    if (first_line) {
+                    if (text.substring(pos).startsWith(commentStart)) {
                         for (int i = 0; i < (commentStart.length() - 1); ++i) {
                             sb.append(' ');
                         }
@@ -275,15 +279,10 @@
                     } else {
                         sb.append(get_prefix(buffer.getLineText(startLine)));
                     }
-                    // Hack for c style comments
-                    /*
-                    if (commentStart.equals("/*")) {
-                        sb.append('*');
-                    } else if (first_line) {
-                        sb.append(' ');
-                        sb.append(' ');
-                    }
-                    */
+                    
+                    // We schedule this for after indent runs, otherwise any of
+                    // the leading whitespace gets removed by, or incorporated
+                    // into, the indent.
                     final int nextLine = startLine + 1;
                     final JEditBuffer fb = buffer;
                     final String fs = sb.toString();
@@ -294,7 +293,7 @@
                             int indent = fb.getIdealIndentForLine(nextLine);
                             String text = fb.getLineText(nextLine);
                             
-                            // Automatically inserts a space for us on wrap
+                            // May automatically insert a space for us on wrap
                             String extra_insert = "";
                             if (!fs.trim().equals("")) {
                                 if (indent >= text.length()) {
@@ -311,4 +310,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}

Modified: plugins/CommentContinuator/trunk/src/CommentContinuatorPlugin.java
===================================================================
--- plugins/CommentContinuator/trunk/src/CommentContinuatorPlugin.java	2008-06-28 \
                01:18:40 UTC (rev 12951)
+++ plugins/CommentContinuator/trunk/src/CommentContinuatorPlugin.java	2008-06-28 \
02:44:56 UTC (rev 12952) @@ -30,6 +30,7 @@
 import org.gjt.sp.jedit.View;
 import org.gjt.sp.jedit.Buffer;
 import org.gjt.sp.jedit.TextUtilities;
+import org.gjt.sp.jedit.buffer.JEditBuffer;
 
 public class CommentContinuatorPlugin extends EBPlugin
 {
@@ -49,11 +50,12 @@
             if ((epu.getWhat() == EditPaneUpdate.BUFFER_CHANGED) ||
                 (epu.getWhat() == EditPaneUpdate.CREATED))
             {
-                if (buffer != null)
-                    buffer.addBufferListener(continuator);
-            } else {
-                if (buffer != null)
-                    buffer.removeBufferListener(continuator);
+                if (buffer != null) {
+                    if (!buffer.getBooleanProperty("commentcontinuator.enabled")) {
+                        buffer.setBooleanProperty("commentcontinuator.enabled", \
true); +                        buffer.addBufferListener(continuator);
+                    }
+                }
             }
         }
     }
@@ -63,14 +65,18 @@
         // Enumerate edit panes and add listener
         continuator = new CommentContinuator();
         for (View v : jEdit.getViews()) {
-            v.getEditPane().getBuffer().addBufferListener(continuator);
+            JEditBuffer buffer = v.getEditPane().getBuffer();
+            buffer.setBooleanProperty("commentcontinuator.enabled", true);
+            buffer.addBufferListener(continuator);
         }
     }
     
     public void stop()
     {
         for (View v : jEdit.getViews()) {
-            v.getEditPane().getBuffer().removeBufferListener(continuator);
+            JEditBuffer buffer = v.getEditPane().getBuffer();
+            buffer.setBooleanProperty("commentcontinuator.enabled", false);
+            buffer.removeBufferListener(continuator);
         }
     }
 }


This was sent by the SourceForge.net collaborative development platform, the world's \
largest Open Source development site.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
jEdit-CVS mailing list
jEdit-CVS@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jedit-cvs


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic