diff --git a/lib/token.cpp b/lib/token.cpp index 29c3c13a8..850b61474 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -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); } } diff --git a/lib/token.h b/lib/token.h index 64fe2e6b9..14037ad92 100644 --- a/lib/token.h +++ b/lib/token.h @@ -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;