void ScCodeEditor::moveToPreviousToken( QTextCursor & cursor, QTextCursor::MoveMode mode ) { if (cursor.atBlockStart()) { cursor.movePosition( QTextCursor::PreviousCharacter, mode ); return; } QTextBlock block( cursor.block() ); QString blockText = block.text(); int positionInBlock = cursor.position() - block.position() - 1; // skip whitespace while (positionInBlock > 0 && blockText[positionInBlock].isSpace()) --positionInBlock; cursor.setPosition(positionInBlock + block.position(), mode); if (positionInBlock == 0) return; // go to beginning of token or beginning of word TokenIterator tokenIt( block, positionInBlock ); if (tokenIt.isValid()) { cursor.setPosition( tokenIt.position(), mode ); } else { int pos = positionInBlock; if (blockText[pos].isLetterOrNumber()) { while (pos > 0 && blockText[pos-1].isLetterOrNumber()) --pos; } cursor.setPosition( pos + block.position(), mode ); } }
void ScCodeEditor::moveToNextToken( QTextCursor & cursor, QTextCursor::MoveMode mode ) { if (cursor.atBlockEnd()) { cursor.movePosition( QTextCursor::NextCharacter, mode ); return; } QTextBlock block( cursor.block() ); QString blockText = block.text(); int positionInBlock = cursor.position() - block.position(); // go to end of token or end of word TokenIterator tokenIt( block, positionInBlock ); if (tokenIt.isValid()) positionInBlock = tokenIt->positionInBlock + tokenIt->length; else { int pos = positionInBlock; if (blockText[pos].isLetterOrNumber()) { ++pos; while (pos < blockText.size() && blockText[pos].isLetterOrNumber()) ++pos; } else { ++pos; } positionInBlock = pos; } // skip whitespace while (positionInBlock < blockText.size() && blockText[positionInBlock].isSpace()) ++positionInBlock; cursor.setPosition( positionInBlock + block.position(), mode ); }
void appendTypesString( const std::string &typesString, std::vector<const OSG::ReflexiveContainerType *> &types ) { const FieldContainerType *pType; string_token_iterator tokenIt(typesString, ", "); string_token_iterator tokenEnd; for(; tokenIt != tokenEnd; ++tokenIt) { pType = FieldContainerFactory::the()->findType((*tokenIt).c_str()); if(pType) types.push_back(pType); } }
void ApplesoftRetokenizer::retokenizeLinesForFormatting() { QVector<ApplesoftLine> retLines; foreach(ApplesoftLine line, m_retokenized_lines) { int indentlevel = 1; // quint16 linenum = line.linenum; bool firstToken = true; ApplesoftToken previousToken; QMutableVectorIterator<ApplesoftToken> tokenIt(line.tokens); while (tokenIt.hasNext()) { ApplesoftToken token = tokenIt.next(); bool isFlowTarget = false; QString tokenstr = token.getRawPrintableString(); if (firstToken) { if (!tokenstr.startsWith(" ")) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtLeadingSpaceTokenValue); tokenIt.remove(); tokenIt.insert(tmptoken); tokenIt.insert(token); } firstToken = false; } quint16 preTokenId = previousToken.getTokenId(); if (preTokenId == ApplesoftToken::ASGoto || preTokenId == ApplesoftToken::ASGosub || preTokenId == ApplesoftToken::ASThen) { isFlowTarget = false; if (preTokenId == ApplesoftToken::ASGoto || preTokenId == ApplesoftToken::ASGosub) { isFlowTarget = true; } else if (preTokenId == ApplesoftToken::ASThen && token.getTokenId() == ApplesoftToken::IntegerTokenVal) { isFlowTarget = true; } if (isFlowTarget) { QPair<quint16,quint16> pair; pair.first = line.linenum; pair.second = token.getWordValue(); m_flowTargets.append(pair); ApplesoftToken tmptoken(ApplesoftToken::OptFmtFlagFlowTargetNextTokenValue); tokenIt.remove(); tokenIt.insert(tmptoken); tokenIt.insert(token); } } if (token.getTokenId() == ApplesoftToken::ASReturn) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtReturnLineBreakTokenValue); tokenIt.insert(tmptoken); } if (token.getTokenId() == ':') { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentLineBreakTokenValue); tokenIt.insert(tmptoken); for (int ind = 0; ind < indentlevel; ind++) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentTabTokenValue); tokenIt.insert(tmptoken); } if (!tokenIt.peekNext().getRawPrintableString().startsWith(" ")) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentSpaceTokenValue); tokenIt.insert(tmptoken); } } if (token.getTokenId() == ApplesoftToken::ASThen) { indentlevel++; if (tokenIt.peekNext().getTokenId() != ApplesoftToken::IntegerTokenVal) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentLineBreakTokenValue); tokenIt.insert(tmptoken); for (int ind = 0; ind < indentlevel; ind++) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentTabTokenValue); tokenIt.insert(tmptoken); } if (!tokenIt.peekNext().getRawPrintableString().startsWith(" ")) { ApplesoftToken tmptoken(ApplesoftToken::OptFmtIndentSpaceTokenValue); tokenIt.insert(tmptoken); } } } previousToken = token; } retLines.append(line); }