Un-template-ize SharedTools::Indenter which was only used once.

Reviewed-by: Roberto Raggi
This commit is contained in:
hjk
2010-06-04 12:00:20 +02:00
parent 424b9724d6
commit e5afb64f49
4 changed files with 37 additions and 67 deletions

View File

@@ -1482,14 +1482,13 @@ bool CPPEditor::isInComment(const QTextCursor &cursor) const
} }
// Indent a code line based on previous // Indent a code line based on previous
template <class Iterator>
static void indentCPPBlock(const CPPEditor::TabSettings &ts, static void indentCPPBlock(const CPPEditor::TabSettings &ts,
const QTextBlock &block, const QTextBlock &block,
const Iterator &programBegin, const TextEditor::TextBlockIterator &programBegin,
const Iterator &programEnd, const TextEditor::TextBlockIterator &programEnd,
QChar typedChar) QChar typedChar)
{ {
typedef typename SharedTools::Indenter<Iterator> Indenter; typedef SharedTools::Indenter Indenter;
Indenter &indenter = Indenter::instance(); Indenter &indenter = Indenter::instance();
indenter.setIndentSize(ts.m_indentSize); indenter.setIndentSize(ts.m_indentSize);
indenter.setTabSize(ts.m_tabSize); indenter.setTabSize(ts.m_tabSize);

View File

@@ -27,9 +27,6 @@
** **
**************************************************************************/ **************************************************************************/
#ifndef INDENTER_C
#define INDENTER_C
/* /*
This file is a self-contained interactive indenter for C++ and Qt This file is a self-contained interactive indenter for C++ and Qt
Script. Script.
@@ -69,6 +66,8 @@
string literals are removed beforehand. string literals are removed beforehand.
*/ */
#include "indenter.h"
namespace SharedTools { namespace SharedTools {
/* qmake ignore Q_OBJECT */ /* qmake ignore Q_OBJECT */
@@ -105,8 +104,7 @@ namespace {
enum { ppCommentOffset = 1 }; enum { ppCommentOffset = 1 };
} }
template <class Iterator> Indenter::Indenter() :
Indenter<Iterator>::Indenter() :
ppHardwareTabSize(8), ppHardwareTabSize(8),
ppIndentSize(4), ppIndentSize(4),
ppIndentBraces(false), ppIndentBraces(false),
@@ -119,40 +117,34 @@ Indenter<Iterator>::Indenter() :
{ {
} }
template <class Iterator> Indenter::~Indenter()
Indenter<Iterator>::~Indenter()
{ {
delete yyLinizerState; delete yyLinizerState;
} }
template <class Iterator> Indenter &Indenter::instance()
Indenter<Iterator> &Indenter<Iterator>::instance()
{ {
static Indenter rc; static Indenter rc;
return rc; return rc;
} }
template <class Iterator> void Indenter::setIndentSize(int size)
void Indenter<Iterator>::setIndentSize(int size)
{ {
ppIndentSize = size; ppIndentSize = size;
ppContinuationIndentSize = 2 * size; ppContinuationIndentSize = 2 * size;
} }
template <class Iterator> void Indenter::setTabSize(int size )
void Indenter<Iterator>::setTabSize(int size )
{ {
ppHardwareTabSize = size; ppHardwareTabSize = size;
} }
template <class Iterator> void Indenter::setIndentBraces(bool indent)
void Indenter<Iterator>::setIndentBraces(bool indent)
{ {
ppIndentBraces = indent; ppIndentBraces = indent;
} }
template <class Iterator> void Indenter::setDoubleIndentBlocks(bool indent)
void Indenter<Iterator>::setDoubleIndentBlocks(bool indent)
{ {
ppDoubleIndentBlocks = indent; ppDoubleIndentBlocks = indent;
} }
@@ -161,8 +153,7 @@ void Indenter<Iterator>::setDoubleIndentBlocks(bool indent)
Returns the first non-space character in the string t, or Returns the first non-space character in the string t, or
QChar::null if the string is made only of white space. QChar::null if the string is made only of white space.
*/ */
template <class Iterator> QChar Indenter::firstNonWhiteSpace( const QString& t )
QChar Indenter<Iterator>::firstNonWhiteSpace( const QString& t )
{ {
if (const int len = t.length()) if (const int len = t.length())
for ( int i = 0; i < len; i++) for ( int i = 0; i < len; i++)
@@ -175,8 +166,7 @@ QChar Indenter<Iterator>::firstNonWhiteSpace( const QString& t )
Returns true if string t is made only of white space; otherwise Returns true if string t is made only of white space; otherwise
returns false. returns false.
*/ */
template <class Iterator> bool Indenter::isOnlyWhiteSpace( const QString& t )
bool Indenter<Iterator>::isOnlyWhiteSpace( const QString& t )
{ {
return t.isEmpty() || firstNonWhiteSpace( t ).isNull(); return t.isEmpty() || firstNonWhiteSpace( t ).isNull();
} }
@@ -186,8 +176,7 @@ bool Indenter<Iterator>::isOnlyWhiteSpace( const QString& t )
index. Column numbers and index are identical for strings that don't index. Column numbers and index are identical for strings that don't
contain '\t's. contain '\t's.
*/ */
template <class Iterator> int Indenter::columnForIndex( const QString& t, int index ) const
int Indenter<Iterator>::columnForIndex( const QString& t, int index ) const
{ {
int col = 0; int col = 0;
if ( index > t.length() ) if ( index > t.length() )
@@ -208,8 +197,7 @@ int Indenter<Iterator>::columnForIndex( const QString& t, int index ) const
/* /*
Returns the indentation size of string t. Returns the indentation size of string t.
*/ */
template <class Iterator> int Indenter::indentOfLine( const QString& t ) const
int Indenter<Iterator>::indentOfLine( const QString& t ) const
{ {
return columnForIndex( t, t.indexOf(firstNonWhiteSpace(t)) ); return columnForIndex( t, t.indexOf(firstNonWhiteSpace(t)) );
} }
@@ -230,8 +218,7 @@ static inline void eraseChar( QString& t, int k, QChar ch )
Removes some nefast constructs from a code line and returns the Removes some nefast constructs from a code line and returns the
resulting line. resulting line.
*/ */
template <class Iterator> QString Indenter::trimmedCodeLine( const QString& t )
QString Indenter<Iterator>::trimmedCodeLine( const QString& t )
{ {
QString trimmed = t; QString trimmed = t;
int k; int k;
@@ -348,8 +335,7 @@ static inline bool okay( QChar typedIn, QChar okayCh )
accordingly. yyLine is cleaned from comments and other damageable accordingly. yyLine is cleaned from comments and other damageable
constructs. Empty lines are skipped. constructs. Empty lines are skipped.
*/ */
template <class Iterator> bool Indenter::readLine()
bool Indenter<Iterator>::readLine()
{ {
int k; int k;
@@ -457,8 +443,7 @@ bool Indenter<Iterator>::readLine()
Resets the linizer to its initial state, with yyLine containing the Resets the linizer to its initial state, with yyLine containing the
line above the bottom line of the program. line above the bottom line of the program.
*/ */
template <class Iterator> void Indenter::startLinizer()
void Indenter<Iterator>::startLinizer()
{ {
yyLinizerState->braceDepth = 0; yyLinizerState->braceDepth = 0;
yyLinizerState->inCComment = false; yyLinizerState->inCComment = false;
@@ -479,8 +464,7 @@ void Indenter<Iterator>::startLinizer()
potentially the whole line) is part of a C-style comment; otherwise potentially the whole line) is part of a C-style comment; otherwise
returns false. returns false.
*/ */
template <class Iterator> bool Indenter::bottomLineStartsInCComment()
bool Indenter<Iterator>::bottomLineStartsInCComment()
{ {
/* /*
We could use the linizer here, but that would slow us down We could use the linizer here, but that would slow us down
@@ -515,8 +499,7 @@ bool Indenter<Iterator>::bottomLineStartsInCComment()
Essentially, we're trying to align against some text on the previous Essentially, we're trying to align against some text on the previous
line. line.
*/ */
template <class Iterator> int Indenter::indentWhenBottomLineStartsInCComment() const
int Indenter<Iterator>::indentWhenBottomLineStartsInCComment() const
{ {
int k = yyLine->lastIndexOf(m_constants.m_slashAster ); int k = yyLine->lastIndexOf(m_constants.m_slashAster );
if ( k == -1 ) { if ( k == -1 ) {
@@ -560,8 +543,7 @@ int Indenter<Iterator>::indentWhenBottomLineStartsInCComment() const
if ( x ) if ( x )
y; y;
*/ */
template <class Iterator> bool Indenter::matchBracelessControlStatement()
bool Indenter<Iterator>::matchBracelessControlStatement()
{ {
int delimDepth = 0; int delimDepth = 0;
@@ -649,8 +631,7 @@ bool Indenter<Iterator>::matchBracelessControlStatement()
f + // unfinished continuation line f + // unfinished continuation line
g; // continuation line g; // continuation line
*/ */
template <class Iterator> bool Indenter::isUnfinishedLine()
bool Indenter<Iterator>::isUnfinishedLine()
{ {
bool unf = false; bool unf = false;
@@ -699,8 +680,7 @@ bool Indenter<Iterator>::isUnfinishedLine()
Returns true if yyLine is a continuation line; otherwise returns Returns true if yyLine is a continuation line; otherwise returns
false. false.
*/ */
template <class Iterator> bool Indenter::isContinuationLine()
bool Indenter<Iterator>::isContinuationLine()
{ {
YY_SAVE(); YY_SAVE();
const bool cont = readLine() && isUnfinishedLine(); const bool cont = readLine() && isUnfinishedLine();
@@ -716,8 +696,7 @@ bool Indenter<Iterator>::isContinuationLine()
or other bracked left opened on a previous line, or some interesting or other bracked left opened on a previous line, or some interesting
operator such as '='. operator such as '='.
*/ */
template <class Iterator> int Indenter::indentForContinuationLine()
int Indenter<Iterator>::indentForContinuationLine()
{ {
int braceDepth = 0; int braceDepth = 0;
int delimDepth = 0; int delimDepth = 0;
@@ -953,8 +932,7 @@ int Indenter<Iterator>::indentForContinuationLine()
people with irregular indentation schemes. A hook line near at hand people with irregular indentation schemes. A hook line near at hand
is much more reliable than a remote one. is much more reliable than a remote one.
*/ */
template <class Iterator> int Indenter::indentForStandaloneLine()
int Indenter<Iterator>::indentForStandaloneLine()
{ {
const QChar semicolon = QLatin1Char(';'); const QChar semicolon = QLatin1Char(';');
const QChar openingBrace = QLatin1Char('{'); const QChar openingBrace = QLatin1Char('{');
@@ -1049,8 +1027,7 @@ int Indenter<Iterator>::indentForStandaloneLine()
slighly more liberal if typedIn is always null. The user might be slighly more liberal if typedIn is always null. The user might be
annoyed by the liberal behavior. annoyed by the liberal behavior.
*/ */
template <class Iterator> int Indenter::indentForBottomLine(const Iterator &current,
int Indenter<Iterator>::indentForBottomLine(const Iterator &current,
const Iterator &programBegin, const Iterator &programBegin,
const Iterator &programEnd, const Iterator &programEnd,
QChar typedIn ) QChar typedIn )
@@ -1146,8 +1123,3 @@ int Indenter<Iterator>::indentForBottomLine(const Iterator &current,
} }
} // namespace SharedTools } // namespace SharedTools
#undef YY_SAVE
#undef YY_RESTORE
#endif // INDENTER_C

View File

@@ -30,6 +30,9 @@
#ifndef INDENTER_H #ifndef INDENTER_H
#define INDENTER_H #define INDENTER_H
#include "texteditor/basetexteditor.h"
#include "texteditor/textblockiterator.h"
#include <QtCore/QString> #include <QtCore/QString>
#include <QtCore/QStringList> #include <QtCore/QStringList>
@@ -62,14 +65,13 @@ struct Constants {
* given as a list of strings, with the bottom line being the line to * given as a list of strings, with the bottom line being the line to
* indent. The actual program might contain extra lines, but those are * indent. The actual program might contain extra lines, but those are
* uninteresting and not passed over to us. */ * uninteresting and not passed over to us. */
template <class Iterator>
struct LinizerState { struct LinizerState {
QString line; QString line;
int braceDepth; int braceDepth;
bool leftBraceFollows; bool leftBraceFollows;
Iterator iter; TextEditor::TextBlockIterator iter;
bool inCComment; bool inCComment;
bool pendingRightBrace; bool pendingRightBrace;
}; };
@@ -79,8 +81,8 @@ struct LinizerState {
* of a sequence of code lines represented as QString. * of a sequence of code lines represented as QString.
* When setting the parameters, be careful to * When setting the parameters, be careful to
* specify the correct template parameters (best use a typedef). */ * specify the correct template parameters (best use a typedef). */
template <class Iterator>
class Indenter { class Indenter {
typedef TextEditor::TextBlockIterator Iterator;
Indenter(const Indenter&); Indenter(const Indenter&);
Indenter &operator=(const Indenter&); Indenter &operator=(const Indenter&);
Indenter(); Indenter();
@@ -131,7 +133,7 @@ private:
Iterator yyProgramBegin; Iterator yyProgramBegin;
Iterator yyProgramEnd; Iterator yyProgramEnd;
typedef typename IndenterInternal::LinizerState<Iterator> LinizerState; typedef IndenterInternal::LinizerState LinizerState;
LinizerState *yyLinizerState ; LinizerState *yyLinizerState ;
@@ -142,6 +144,4 @@ private:
}; };
} }
#include "indenter_impl.h"
#endif // INDENTER_H #endif // INDENTER_H

View File

@@ -1,5 +1,4 @@
INCLUDEPATH *= $$PWD INCLUDEPATH *= $$PWD
SOURCES += $$PWD/constants.cpp SOURCES += $$PWD/constants.cpp $$PWD/indenter.cpp
HEADERS += $$PWD/indenter.h \ HEADERS += $$PWD/indenter.h
$$PWD/indenter_impl.h