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 ); }
QString ScriptEditorWidget::textUnderCursor() const { QString szWord; QTextCursor tc = textCursor(); if(tc.atBlockStart()) return QString(); tc.clearSelection(); tc.movePosition(QTextCursor::StartOfWord,QTextCursor::KeepAnchor); if(tc.atBlockStart()) { szWord.append(tc.selectedText()); tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor); szWord.append(tc.selectedText()); if(tc.atBlockEnd()){ return szWord; } tc.movePosition(QTextCursor::NextCharacter,QTextCursor::KeepAnchor); szWord.append(tc.selectedText()); if(szWord.right(1)!=".") szWord.chop(1); return szWord; } tc.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor); szWord=tc.selectedText(); if(szWord.left(1)==".") { tc.movePosition(QTextCursor::StartOfWord); tc.movePosition(QTextCursor::PreviousCharacter); tc.movePosition(QTextCursor::PreviousWord); tc.movePosition(QTextCursor::EndOfWord,QTextCursor::KeepAnchor,1); szWord.prepend(tc.selectedText()); } else szWord.remove(0,1); return szWord; }
static bool isBlockOnlySelection(QTextCursor cursor) { Q_ASSERT(cursor.hasSelection()); QTextCursor begin(cursor); begin.setPosition(begin.anchor()); if (begin.atBlockStart() && (cursor.atBlockStart() || cursor.atBlockEnd())) return true; else return false; }
void GenericCodeEditor::handleKeyRightParentheses(QKeyEvent *event, QTextCursor &textCursor) { if( !textCursor.atBlockEnd() ) { QTextCursor nextCharCursor( textCursor ); nextCharCursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor ); if( nextCharCursor.selectedText() == event->text() ) { // don't insert, just move moveCursor( QTextCursor::NextCharacter ); return; } } QPlainTextEdit::keyPressEvent( event ); }
bool TabSettings::tabShouldIndent(const QTextDocument *document, QTextCursor cursor, int *suggestedPosition) const { if (m_tabKeyBehavior == TabNeverIndents) return false; QTextCursor tc = cursor; if (suggestedPosition) *suggestedPosition = tc.position(); // At least suggest original position tc.movePosition(QTextCursor::StartOfLine); if (tc.atBlockEnd()) // cursor was on a blank line return true; if (document->characterAt(tc.position()).isSpace()) { tc.movePosition(QTextCursor::WordRight); if (tc.columnNumber() >= cursor.columnNumber()) { if (suggestedPosition) *suggestedPosition = tc.position(); // Suggest position after whitespace if (m_tabKeyBehavior == TabLeadingWhitespaceIndents) return true; } } return (m_tabKeyBehavior == TabAlwaysIndents); }
void SoTextEdit::keyPressEvent(QKeyEvent *input) { QTextCursor cursor = textCursor(); QString text; QStringList list; QTextBlock block; int position = 0; // int position_in_block = 0; int count; if (input->modifiers() != Qt::NoModifier && input->modifiers() != Qt::ShiftModifier) { input->ignore(); return; } switch (input->key()) { case Qt::Key_Shift: case Qt::Key_Control: case Qt::Key_unknown: input->ignore(); return; case Qt::Key_BraceLeft: cursor.insertText(input->text(), colorFormat(default_color)); std::cout << block_stack << std::endl; return; case Qt::Key_BraceRight: cursor.insertText("}"); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Return: emit returnPressed(); position = cursor.position(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count <= text.size(); count++) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } } cursor.setPosition(position); if (cursor.atBlockEnd()) { cursor.insertText(input->text()); position++; } else { cursor.setPosition(position + 1, QTextCursor::MoveAnchor); cursor.setPosition(position - 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); position++; } position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); return; case Qt::Key_Tab: cout << "tab" << endl; position = autoIndent(cursor); cursor.setPosition(position); setTextCursor(cursor); std::cout << "block_stack" << block_stack << std::endl; return; case Qt::Key_Left: if (cursor.atBlockStart()) { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); } else { cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); } setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Right: if (cursor.atEnd()) { return; } else if (cursor.atBlockEnd()) { cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } } cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; case Qt::Key_Down: block = cursor.block(); if ((block.blockNumber() + 1 ) == blockCount()) { return; } else { position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack++; } else if (text[count] == QChar('}')) { block_stack--; } count++; } cursor.setPosition(position); cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Up: block = cursor.block(); if (block.blockNumber() == 0) { return; } else { cursor.movePosition(QTextCursor::Up, QTextCursor::MoveAnchor); position = cursor.position(); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position); setTextCursor(cursor); std::cout << block_stack << std::endl; return; } case Qt::Key_Space: if (cursor.atBlockEnd()) { cursor.insertText(input->text()); setTextCursor(cursor); return; } else { position = cursor.position(); cursor.setPosition(position - 1, QTextCursor::MoveAnchor); cursor.setPosition(position + 1, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0] || text[1] == (*exception)[count][0]) { cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); cursor.insertText(input->text()); setTextCursor(cursor); return; } case Qt::Key_Delete: position = cursor.position(); cursor.deleteChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); cursor.setPosition(position); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); } } cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; case Qt::Key_Backspace: if (cursor.atStart()) { return; } else if (cursor.atBlockStart()) { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); text = cursor.selectedText(); std::cout << "debug_text " << qPrintable(text) << std::endl; count = 0; while (count < text.size()) { if (text[count] == QChar('{')) { block_stack--; } else if (text[count] == QChar('}')) { block_stack++; } count++; } cursor.setPosition(position, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } else { position = cursor.position() - 1; cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NoMove, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); text = cursor.selectedText(); for (count = 0; count < exception->count(); count++) { if (text[0] == (*exception)[count][0]) { cursor.setPosition(position - 1); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); break; } } if (count == exception->count()) { cursor.setPosition(position); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } } std::cout << block_stack <<std::endl; break; default: position = cursor.position() + 1; cursor.insertText(input->text()); cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); text = cursor.selectedText(); } qDebug() << text; if (text.isEmpty()) { return; } switch (checkType(text)) { case t_TYPE: cursor.insertText(text, colorFormat(type_color)); break; case t_KEYWORD: cursor.insertText(text, colorFormat(keyword_color)); break; case t_DEFAULT: cursor.insertText(text, colorFormat(default_color)); } cursor.setPosition(position); setTextCursor(cursor); return; }
/** * @brief Removes tabulation(s) at the cursor position. */ void TextEditorWidget::remove_tab () { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); // Check all selected lines. if (cursor.hasSelection()) { // Get the end block number dans set position to start. int start_pos = cursor.selectionStart(); cursor.setPosition(cursor.selectionEnd()); int block_number = cursor.blockNumber(); cursor.setPosition(start_pos); // Loop on each blocks. while (cursor.blockNumber() <= block_number) { cursor.movePosition(QTextCursor::StartOfLine); int last_pos = cursor.position(); // Remove first space(s) character(s) of this line. if (replace_tab_by_spaces) { // get the number of spaces to remove. do { cursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor); } while (cursor.selectedText().endsWith(" ") && !cursor.atBlockEnd() && cursor.selectedText().length() <= tab_length); int length = cursor.selectedText().length() - 1; // select and remove the space(s). if (length > 0) { cursor.movePosition(QTextCursor::StartOfLine); cursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor, length); cursor.removeSelectedText(); } } // Remove the first tab character of this line. else { cursor.movePosition( QTextCursor::NextCharacter, QTextCursor::KeepAnchor); if (cursor.selectedText() == "\t") { cursor.removeSelectedText(); } } cursor.setPosition(last_pos); cursor.movePosition(QTextCursor::NextBlock); } } else { // Remove previous space(s) character(s). if (replace_tab_by_spaces) { int pos = cursor.position(); // get space count before the cursor. do { cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); } while (cursor.selectedText().startsWith(" ")); int space_count = cursor.selectedText().length() - 1; // select and remove previous space(s). if (space_count > 0) { int length = space_count; if (space_count > tab_length) { length = space_count % tab_length; if (length == 0) { length = tab_length; } } cursor.setPosition(pos); cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, length); cursor.removeSelectedText(); } } // Remove previous tab character. else { cursor.movePosition( QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); if (cursor.selectedText() == "\t") { cursor.removeSelectedText(); } } } cursor.endEditBlock(); }