summaryrefslogtreecommitdiffstats
path: root/kate/scripts/script-indent-c-newline.js
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitce4a32fe52ef09d8f5ff1dd22c001110902b60a2 (patch)
tree5ac38a06f3dde268dc7927dc155896926aaf7012 /kate/scripts/script-indent-c-newline.js
downloadtdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.tar.gz
tdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kate/scripts/script-indent-c-newline.js')
-rw-r--r--kate/scripts/script-indent-c-newline.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/kate/scripts/script-indent-c-newline.js b/kate/scripts/script-indent-c-newline.js
new file mode 100644
index 000000000..ade711a3e
--- /dev/null
+++ b/kate/scripts/script-indent-c-newline.js
@@ -0,0 +1,113 @@
+var tabWidth = 4;
+var spaceIndent = true;
+var indentWidth = 4;
+
+var strIndentCharacters = " ";
+var strIndentFiller = "";
+
+var intStartLine = view.cursorLine();
+var intStartColumn = view.cursorColumn();
+
+var strTextLine = document.textLine( intStartLine );
+var strPrevLine = document.textLine( intStartLine - 1 );
+
+var addIndent = "";
+
+function firstNonSpace( _text )
+{
+ for( _i=0; _i < _text.length; _i++ )
+ {
+ _ch = _text.charAt( _i );
+ if( _ch != ' ' && _ch != '\t' )
+ return _i;
+ }
+
+ return -1;
+}
+
+function lastNonSpace( _text )
+{
+ for( _i=_text.length - 1; _i >= 0; _i-- )
+ {
+ _ch = _text.charAt( _i );
+ if( _ch != ' ' && _ch != '\t' )
+ return _i;
+ }
+
+ return -1;
+}
+
+
+// if previous line ends with a '{' increase indent level
+// if ( prevLine.search( /{\s*$/ ) != -1 )
+// {
+// if ( spaceIndent )
+// addIndent = " ";
+// else
+// addIndent = "\t";
+// }
+// else
+{
+ var intCurrentLine = intStartLine;
+ var openParenCount = 0;
+ var openBraceCount = 0;
+
+ label_while:
+ while ( intCurrentLine > 0 )
+ {
+ intCurrentLine--;
+
+ strCurrentLine = document.textLine( intCurrentLine );
+ intLastChar = lastNonSpace( strCurrentLine );
+ intFirstChar = firstNonSpace( strCurrentLine ) ;
+
+ if ( strCurrentLine.search( /\/\// ) == -1 )
+ {
+
+ // look through line backwards for interesting characters
+ for( intCurrentChar = intLastChar; intCurrentChar >= intFirstChar; --intCurrentChar )
+ {
+ ch = strCurrentLine.charAt( intCurrentChar );
+ switch( ch )
+ {
+ case '(': case '[':
+ if( ++openParenCount > 0 )
+ break label_while; //return calcIndentInBracket( begin, cur, pos );
+ break;
+ case ')': case ']': openParenCount--; break;
+ case '{':
+ if( ++openBraceCount > 0 )
+ break label_while; //return calcIndentInBrace( begin, cur, pos );
+ break;
+ case '}': openBraceCount--; lookingForScopeKeywords = false; break;
+ case ';':
+ if( openParenCount == 0 )
+ lookingForScopeKeywords = false;
+ break;
+ }
+ }
+ }
+ }
+
+ strIndentFiller += strCurrentLine.match(/^\s+/);
+ if ( strIndentFiller == "null" )
+ strIndentFiller = "";
+
+ debug( "line: " + intCurrentLine);
+ debug( openParenCount + ", " + openBraceCount);
+
+ while( openParenCount > 0 )
+ {
+ openParenCount--;
+ strIndentFiller += strIndentCharacters;
+ }
+
+ while( openBraceCount > 0 )
+ {
+ openBraceCount--;
+ strIndentFiller += strIndentCharacters;
+ }
+}
+
+document.insertText( intStartLine, 0, strIndentFiller );
+view.setCursorPositionReal( intStartLine, document.textLine( intStartLine ).length );