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

List:       kwrite-devel
Subject:    [patch] Ability to force indent with spaces in alignment from indent
From:       "Robin Pedersen" <robinpeder () gmail ! com>
Date:       2008-05-27 22:10:39
Message-ID: op.ubt2j1ew9lgty9 () lenobin
[Download RAW message or body]

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
["align.diff" (align.diff)]

Index: utils/kateautoindent.h
===================================================================
--- utils/kateautoindent.h	(revision 813423)
+++ 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 813423)
+++ 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 = qMax (0, align - length);
 
   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();
 
@@ -197,8 +199,11 @@
     return;
   }
 
+  int align = m_script->global("align").toInt32();
+  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/data/ruby.js
===================================================================
--- script/data/ruby.js	(revision 813506)
+++ script/data/ruby.js	(working copy)
@@ -304,7 +304,8 @@
     var hasComma = testAtEnd(prevStmt, /,\s*/g);
     if (anch.ch != '{' && !isLastCodeColumn(anch.line, anch.column)) {
       // TODO This is alignment, should force using spaces instead of tabs:
-      return document.toVirtualColumn(anch.line, anch.column) + (hasComma ? 1 : 0);
+      align = document.toVirtualColumn(anch.line, anch.column) + (hasComma ? 1 : 0);
+      return prevStmtInd;
     } else {
       return document.firstVirtualColumn(anch.line) + ((anch.line == prevStmt.end || \
hasComma) ? indentWidth : 0);  }
Index: script/kateindentscript.cpp
===================================================================
--- script/kateindentscript.cpp	(revision 813423)
+++ script/kateindentscript.cpp	(working copy)
@@ -36,7 +36,7 @@
     return m_triggerCharacters;
 
   m_triggerCharactersSet = true;
-  
+
   m_triggerCharacters = global("triggerCharacters").toString();
 
   kDebug( 13050 ) << "trigger chars: '" << m_triggerCharacters << "'";
@@ -55,6 +55,10 @@
   if(!indentFunction.isValid()) {
     return -2;
   }
+
+  // reset alignment property
+  m_engine->globalObject().setProperty("align", QScriptValue(m_engine, 0));
+
   // add the arguments that we are going to pass to the function
   QScriptValueList arguments;
   arguments << QScriptValue(m_engine, position.line());



_______________________________________________
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