KDevelop API Documentation

editors/qeditor/qeditor_indenter.cpp

Go to the documentation of this file.
00001 /********************************************************************** 00002 ** Copyright (C) 2000 Trolltech AS. All rights reserved. 00003 ** 00004 ** This file is part of Qt Designer. 00005 ** 00006 ** This file may be distributed and/or modified under the terms of the 00007 ** GNU General Public License version 2 as published by the Free Software 00008 ** Foundation and appearing in the file COPYING included in the 00009 ** packaging of this file. 00010 ** 00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 ** 00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 00015 ** 00016 ** Contact info@trolltech.com if any conditions of this licensing are 00017 ** not clear to you. 00018 ** 00019 **********************************************************************/ 00020 00021 00022 /* 00023 * Copyright (C) 2002 Roberto Raggi (roberto@kdevelop.org) 00024 * 00025 * This program is free software; you can redistribute it and/or 00026 * modify it under the terms of the GNU General Public 00027 * License as published by the Free Software Foundation; either 00028 * version 2 of the License, or (at your option) any later version. 00029 * 00030 * This program is distributed in the hope that it will be useful, 00031 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00032 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00033 * General Public License for more details. 00034 * 00035 * You should have received a copy of the GNU General Public License 00036 * along with this program; see the file COPYING. If not, write to 00037 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00038 * Boston, MA 02111-1307, USA. 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
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:38:56 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003