qeditor_indenter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "qeditor_indenter.h"
00043 #include "qeditor.h"
00044 #include <kdebug.h>
00045
00046 QEditorIndenter::QEditorIndenter( QEditor* ed )
00047 : m_editor( ed )
00048 {
00049 }
00050
00051 QEditorIndenter::~QEditorIndenter()
00052 {
00053 }
00054
00055 int QEditorIndenter::indentation( const QString &s )
00056 {
00057 if ( s.simplifyWhiteSpace().length() == 0 )
00058 return 0;
00059
00060 int tabwidth = m_editor->tabStop();
00061 int i = 0;
00062 int ind = 0;
00063 while ( i < (int)s.length() ) {
00064 QChar c = s.at( i );
00065 if ( c == ' ' ){
00066 ind++;
00067 } else if ( c == '\t' ){
00068 ind += tabwidth;
00069 } else {
00070 break;
00071 }
00072 ++i;
00073 }
00074 return ind;
00075 }
00076
00077 void QEditorIndenter::indentLine( QTextParagraph *p, int &oldIndent, int &newIndent )
00078 {
00079 QString indentString;
00080 indentString.fill( ' ', newIndent );
00081 indentString.append( "a" );
00082 tabify( indentString );
00083 indentString.remove( indentString.length() - 1, 1 );
00084 newIndent = indentString.length();
00085 oldIndent = 0;
00086 while ( p->length() > 0 && ( p->at( 0 )->c == ' ' || p->at( 0 )->c == '\t' ) ) {
00087 ++oldIndent;
00088 p->remove( 0, 1 );
00089 }
00090 if ( p->string()->length() == 0 )
00091 p->append( " " );
00092 if ( !indentString.isEmpty() )
00093 p->insert( 0, indentString );
00094 }
00095
00096 void QEditorIndenter::tabify( QString& s )
00097 {
00098 int i = 0;
00099 int tabSize = m_editor->tabStop();
00100 for ( ;; ) {
00101 for ( int j = i; j < (int)s.length(); ++j ) {
00102 if ( s[ j ] != ' ' && s[ j ] != '\t' ) {
00103 if ( j > i ) {
00104 QString t = s.mid( i, j - i );
00105 int spaces = 0;
00106 for ( int k = 0; k < (int)t.length(); ++k )
00107 spaces += ( t[ k ] == ' ' ? 1 : tabSize );
00108 s.remove( i, t.length() );
00109 int tabs = spaces / tabSize;
00110 spaces = spaces - ( tabSize * tabs );
00111 QString tmp;
00112 tmp.fill( ' ', spaces );
00113 if ( spaces > 0 )
00114 s.insert( i, tmp );
00115 tmp.fill( '\t', tabs );
00116 if ( tabs > 0 )
00117 s.insert( i, tmp );
00118 }
00119 break;
00120 }
00121 }
00122 i = s.find( '\n', i );
00123 if ( i == -1 )
00124 break;
00125 ++i;
00126 }
00127 }
00128
00129 void QEditorIndenter::indent( QTextDocument*, QTextParagraph* parag, int* oldIndent, int* newIndent )
00130 {
00131 int line = parag->paragId();
00132 QString text = m_editor->text( line );
00133
00134 int oi = indentation( text );
00135
00136 int ind = indentForLine( line );
00137
00138 indentLine( parag, oi, ind );
00139
00140 if( oldIndent ) *oldIndent = oi;
00141 if( newIndent ) *newIndent = ind;
00142 }
00143
00144 int QEditorIndenter::previousNonBlankLine( int line )
00145 {
00146 while( --line >=0 ){
00147 if( !editor()->text( line ).stripWhiteSpace().isEmpty() )
00148 break;
00149 }
00150 return line;
00151 }
00152
This file is part of the documentation for KDevelop Version 3.1.2.