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

List:       kwrite-devel
Subject:    Re: [patch] Ability to force indent with spaces in alignment from
From:       "Robin Pedersen" <robinpeder () gmail ! com>
Date:       2008-05-29 12:16:08
Message-ID: op.ubw0c4ld9lgty9 () lenobin
[Download RAW message or body]

This is an updated patch. The functionality is the same, but the  
implementation and the scripting API is different:

Instead of returning the alignment as a global variable, the script's  
indent function can either return only the indent amount like before, or  
an array with the indent amount and the alignment. An example is given in  
the ruby indenter included in the patch.

Please let me know what you think.

On Wed, 28 May 2008 00:10:39 +0200, Robin Pedersen <robinpeder@gmail.com>  
wrote:

> This patch makes it possible in an indent script to align code with  
> spaces, even if tabs are normally used to indent.
>
> Example situation where this is useful:
>>   >   foobar("Hello, world",
>>   >   ......."How are you?")
>
> Notice how the second line is aligned under the opening parenthesis in  
> the first line. Currently, the kate indenter does this instead:
>>   >   foobar("Hello, world",
>>   >   >   ..."How are you?")
>
> If the tab width is changed from four to eight, you get this:
>>       >       foobar("Hello, world",
>>       >       >       ..."How are you?")
>
> The second line is no longer aligned.
>
> The result is that if you're using tabs to indent, you can't change the  
> tab width, which is the whole point of using tabs in the first place. If  
> all alignment is done like this, and tabs are only used for the part  
> that is actually indenting, team members can edit the same file using  
> different tab width without problems with unaligned code.
>
> The scripting API implemented by the patch works like this: The script  
> returns the indent width /without/ the extra spaces for alignment.  
> Before returning, the script updates a global variable named "align",  
> setting it to the column where the alignment should be. If the value of  
> the align variable is greater than the returned indent with, the  
> difference is added as spaces, otherwise it's ignored.
>
> This is backwards compatible. Indent scripts that don't use this feature  
> work like before.
>
> I haven't found a way to keep the alignment when indenting manually  
> (e.g. ctrl+i), but I'm not sure if it's needed.
>
> What do you think? I'm not sure if this is the best way to do it, but  
> I'd really like to have this feature.
>



-- 
Robin Pedersen
["align2.diff" (align2.diff)]

Index: utils/kateautoindent.h
===================================================================
--- utils/kateautoindent.h	(revision 814071)
+++ utils/kateautoindent.h	(working copy)
@@ -96,9 +96,10 @@
      * Produces a string with the proper indentation characters for its length.
      *
      * @param length The length of the indention in characters.
+     * @param align Length of alignment, ignored if less of equal to length
      * @return A QString representing @p length characters (factoring in tabs and \
                spaces)
      */
-    QString tabString (int length) const;
+    QString tabString (int length, int align) const;
 
     /**
      * Change the indent of the specified line by the number of levels
@@ -111,7 +112,7 @@
      * \param relative is the change a relative change to the current indent level \
                or should
      * the indent of the given line be set to the given indentation level
      */
-    bool doIndent ( KateView *view, int line, int change, bool relative, bool \
keepExtraSpaces = false ); +    bool doIndent ( KateView *view, int line, int change, \
bool relative, bool keepExtraSpaces = false, int align = 0 );  
     /**
      * Reuse the indent of the previous line
Index: utils/kateautoindent.cpp
===================================================================
--- utils/kateautoindent.cpp	(revision 814071)
+++ utils/kateautoindent.cpp	(working copy)
@@ -96,24 +96,25 @@
 {
 }
 
-QString KateAutoIndent::tabString (int length) const
+QString KateAutoIndent::tabString (int length, int align) const
 {
   QString s;
   length = qMin (length, 256); // sanity check for large values of pos
+  align = qBound (0, align - length, 256);
 
   if (!useSpaces)
   {
     s.append (QString (length / tabWidth, '\t'));
     length = length % tabWidth;
   }
-  s.append (QString (length, ' '));
+  s.append (QString (length + align, ' '));
 
   return s;
 }
 
-bool KateAutoIndent::doIndent ( KateView *view, int line, int change, bool relative, \
bool keepExtraSpaces ) +bool KateAutoIndent::doIndent ( KateView *view, int line, int \
change, bool relative, bool keepExtraSpaces, int align )  {
-  kDebug (13060) << "doIndent: line: " << line << " change: " << change << " \
relative: " << relative; +  kDebug (13060) << "doIndent: line: " << line << " change: \
" << change << " relative: " << relative << " align: " << align;  
   KateTextLine::Ptr textline = doc->plainKateTextLine(line);
 
@@ -129,6 +130,7 @@
   // for relative change, check for extra spaces
   if (relative)
   {
+    align = 0;
     extraSpaces = indentDepth % indentWidth;
 
     indentDepth += change;
@@ -149,7 +151,7 @@
   if (indentDepth < 0)
     indentDepth = 0;
 
-  QString indentString = tabString (indentDepth);
+  QString indentString = tabString (indentDepth, align);
 
   int first_char = textline->firstChar();
 
@@ -182,7 +184,8 @@
 
 void KateAutoIndent::scriptIndent (KateView *view, const KTextEditor::Cursor \
&position, QChar typedChar)  {
-  int newIndentInChars = m_script->indent (view, position, typedChar, indentWidth);
+  QPair<int, int> result = m_script->indent (view, position, typedChar, \
indentWidth); +  int newIndentInChars = result.first;
 
   // handle negative values special
   if (newIndentInChars < -1)
@@ -197,8 +200,12 @@
     return;
   }
 
+  int align = result.second;
+  if (align > 0)
+    kDebug (13060) << "Align: " << align;
+
   // we got a positive or zero indent to use...
-  doIndent (view, position.line(), newIndentInChars, false);
+  doIndent (view, position.line(), newIndentInChars, false, false, align);
 }
 
 void KateAutoIndent::setMode (const QString &_name)
Index: script/kateindentscript.h
===================================================================
--- script/kateindentscript.h	(revision 814071)
+++ script/kateindentscript.h	(working copy)
@@ -22,6 +22,8 @@
 #include "katescript.h"
 #include "kateview.h"
 
+#include <QtCore/QPair>
+
 class KateScriptDocument;
 
 /**
@@ -33,7 +35,12 @@
     KateIndentScript(const QString &url, const KateScriptInformation &information);
 
     const QString &triggerCharacters();
-    int indent(KateView* view, const KTextEditor::Cursor& position, QChar \
typedCharacter, +
+    /**
+     * Returns a pair where the first value is the indent amount, and the second
+     * value is the alignment.
+     */
+    QPair<int, int> indent(KateView* view, const KTextEditor::Cursor& position, \
QChar typedCharacter,  int indentWidth);
 
   private:
Index: script/data/ruby.js
===================================================================
--- script/data/ruby.js	(revision 814122)
+++ script/data/ruby.js	(working copy)
@@ -310,7 +310,8 @@
         if (nextCol > 0 && !isCommentAttr(anch.line, nextCol))
           anch.column = nextCol;
       }
-      return document.toVirtualColumn(anch.line, anch.column);
+      // Keep indent of previous statement, while aligning to the anchor column
+      return [prevStmtInd, document.toVirtualColumn(anch.line, anch.column)];
     } else {
       return document.firstVirtualColumn(anch.line) + (shouldIndent ? indentWidth : \
0);  }
Index: script/kateindentscript.cpp
===================================================================
--- script/kateindentscript.cpp	(revision 814071)
+++ script/kateindentscript.cpp	(working copy)
@@ -44,17 +44,18 @@
   return m_triggerCharacters;
 }
 
-int KateIndentScript::indent(KateView* view, const KTextEditor::Cursor& position,
+QPair<int, int> KateIndentScript::indent(KateView* view, const KTextEditor::Cursor& \
position,  QChar typedCharacter, int indentWidth)
 {
   // if it hasn't loaded or we can't load, return
   if(!setView(view))
-    return -2;
+    return qMakePair(-2,-2);
 
   QScriptValue indentFunction = function("indent");
   if(!indentFunction.isValid()) {
-    return -2;
+    return qMakePair(-2,-2);
   }
+
   // add the arguments that we are going to pass to the function
   QScriptValueList arguments;
   arguments << QScriptValue(m_engine, position.line());
@@ -65,14 +66,21 @@
   // error during the calling?
   if(m_engine->hasUncaughtException()) {
     displayBacktrace(result, "Error calling indent()");
-    return -2;
+    return qMakePair(-2,-2);
   }
-  int indentAmount = result.toInt32 ();
+  int indentAmount = -2;
+  int alignAmount = -2;
+  if (result.isArray()) {
+    indentAmount = result.property(0).toInt32();
+    alignAmount = result.property(1).toInt32();
+  } else {
+    indentAmount = result.toInt32();
+  }
   if(m_engine->hasUncaughtException()) {
     displayBacktrace(QScriptValue(), "Bad return type (must be integer)");
-    return -2;
+    return qMakePair(-2,-2);
   }
-  return indentAmount;
+  return qMakePair(indentAmount, alignAmount);
 }
 
 // kate: space-indent on; indent-width 2; replace-tabs on;



_______________________________________________
KWrite-Devel mailing list
KWrite-Devel@kde.org
https://mail.kde.org/mailman/listinfo/kwrite-devel


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

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