Add a 'prepend' bool option to Token::InsertToken.

If it's true, add a new token before this. Except when this is the first one in the list, because there's not a way to update the list.front (I noticed some Token functions can potentially delete the list.front(), I don't know which effects does it cause).
This commit is contained in:
Edoardo Prezioso 2012-11-20 02:58:19 +01:00
parent fdeacbaec6
commit e0c10efdce
2 changed files with 27 additions and 9 deletions

View File

@ -898,9 +898,14 @@ const Token *Token::findmatch(const Token *tok, const char pattern[], const Toke
return 0;
}
void Token::insertToken(const std::string &tokenStr)
void Token::insertToken(const std::string &tokenStr, bool prepend)
{
Token *newToken;
//TODO: Find a solution for the first token on the list
if (prepend && !this->previous())
return;
if (_str.empty())
newToken = this;
else
@ -911,14 +916,25 @@ void Token::insertToken(const std::string &tokenStr)
newToken->_progressValue = _progressValue;
if (newToken != this) {
if (this->next()) {
newToken->next(this->next());
newToken->next()->previous(newToken);
} else if (tokensBack) {
*tokensBack = newToken;
if (prepend) {
/*if (this->previous())*/ {
newToken->previous(this->previous());
newToken->previous()->next(newToken);
} /*else if (tokensFront?) {
*tokensFront? = newToken;
}*/
this->previous(newToken);
newToken->next(this);
} else {
if (this->next()) {
newToken->next(this->next());
newToken->next()->previous(newToken);
} else if (tokensBack) {
*tokensBack = newToken;
}
this->next(newToken);
newToken->previous(this);
}
this->next(newToken);
newToken->previous(this);
}
}

View File

@ -293,8 +293,10 @@ public:
* Insert new token after this token. This function will handle
* relations between next and previous token also.
* @param tokenStr String for the new token.
* @param prepend Insert the new token before this token when it's not
* the first one on the tokens list.
*/
void insertToken(const std::string &tokenStr);
void insertToken(const std::string &tokenStr, const bool prepend=false);
Token *previous() const {
return _previous;