00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "python_indent.h"
00022
#include "qeditor.h"
00023
#include "paragdata.h"
00024
#include <kdebug.h>
00025
00026 PythonIndent::PythonIndent(
QEditor* ed )
00027 :
QEditorIndenter( ed ),
00028 rxLineEndedWithAColon( "^[^#]*:\\s*(#.*)?$" ),
00029 rxStopExecutionStmt( "^\\s*(break|continue|raise|return|pass)\\b.*" ),
00030 rxHeaderKeyword( "^\\s*(elif|else|except|finaly)\\b.*" ),
00031 rxOneLinerStmt( "^\\s*(for|if|try)\\b.*" )
00032 {
00033 }
00034
00035 PythonIndent::~PythonIndent()
00036 {
00037 }
00038
00039 int PythonIndent::indentForLine(
int line )
00040 {
00041
if( line == 0 )
00042
return 0;
00043
00044
int prevLine = QMAX( 0, previousNonBlankLine( line ) );
00045
int sw = 4;
00046
00047
QString lineText =
editor()->text( line );
00048
QString prevLineText =
editor()->text( prevLine );
00049
00050
int lineInd = indentation( lineText );
00051
int prevLineInd = indentation( prevLineText );
00052
00053
int extraInd = 0;
00054
00055
ParagData* data = (
ParagData*)
editor()->
document()->paragAt( prevLine )->extraData();
00056
if( data ){
00057
QValueList<Symbol> symbolList = data->
symbolList();
00058
QValueList<Symbol>::Iterator it = symbolList.begin();
00059
while( it != symbolList.end() ){
00060
const Symbol& sym = *it;
00061 ++it;
00062
00063
if ( sym.
type() == Symbol::Left )
00064 extraInd += 4;
00065
else if( sym.
type() == Symbol::Right )
00066 extraInd -= 4;
00067 }
00068 }
00069
00070
if(
rxLineEndedWithAColon.exactMatch(prevLineText) ){
00071
return QMAX( prevLineInd + sw + extraInd, 0 );
00072
00073 }
else if(
rxStopExecutionStmt.exactMatch(prevLineText) ){
00074
return QMAX( prevLineInd + sw + extraInd, 0 );
00075
00076 }
else if(
rxHeaderKeyword.exactMatch(lineText) ){
00077
00078
if(
rxOneLinerStmt.exactMatch(prevLineText) )
00079
return QMAX( prevLineInd + extraInd, 0 );
00080
00081
return QMAX( prevLineInd - sw + extraInd, 0 );
00082
00083 }
else
00084
return QMAX( prevLineInd + extraInd, 0 );
00085 }
00086