int OpenedFile::insertLineBeforeBracket(const int start_line, const QString &new_line) { QTextCursor parsingCursor = textCursor(); QString new_text = new_line; parsingCursor.setPosition(0); while(parsingCursor.blockNumber() != start_line) { parsingCursor.movePosition(QTextCursor::Down); } while(!parsingCursor.atEnd()) { if(parsingCursor.block().text().contains('}')) break; parsingCursor.movePosition(QTextCursor::Down); } parsingCursor.movePosition(QTextCursor::StartOfLine); new_text.remove(".0000", Qt::CaseInsensitive); parsingCursor.insertText(new_text); parsingCursor.insertText("\n"); parsingCursor.movePosition(QTextCursor::Up); setTextCursor(parsingCursor); return parsingCursor.blockNumber(); }
void Widget::nextWord() { QTextCursor cur = m_text->textCursor(); cur.movePosition(QTextCursor::NextWord); cur.select(QTextCursor::WordUnderCursor); m_label->setText(cur.selectedText()); m_text->setTextCursor(cur); if (cur.atEnd()) stopReading(); }
bool TextEditEx::isAllSelected() { QTextCursor cur = textCursor(); if (!cur.hasSelection()) return false; const int start = cur.selectionStart(); const int end = cur.selectionEnd(); cur.setPosition(start); if (cur.atStart()) { cur.setPosition(end); return cur.atEnd(); } else if (cur.atEnd()) { cur.setPosition(start); return cur.atStart(); } return false; }
void QSerialTerminal::flushText(QString& text, QTextCursor& cursor) { if(text.size() == 0) { return; } if(!cursor.atEnd()) { cursor.clearSelection(); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, text.size()); } cursor.insertText(text); text.clear(); }
void Document::deleteTrailingSpaces() { QTextCursor cursor (textDocument()); cursor.beginEditBlock(); cursor.movePosition(QTextCursor::EndOfBlock); QTextDocument * doc = textDocument(); while( !cursor.atEnd() ) { while( (cursor.block().length() > 1) && doc->characterAt(cursor.position() - 1).isSpace()) cursor.deletePreviousChar(); cursor.movePosition(QTextCursor::NextBlock); cursor.movePosition(QTextCursor::EndOfBlock); } cursor.endEditBlock(); }
void Widget::startReading() { QPalette pal; pal.setColor(QPalette::Base, Qt::darkGray); pal.setColor(QPalette::Text, Qt::gray); m_text->setPalette(pal); statusBar()->showMessage("Start reading", 1000); QTextCursor cur = m_text->textCursor(); if (cur.atEnd()) cur.movePosition(QTextCursor::Start); cur.select(QTextCursor::WordUnderCursor); m_label->setText(cur.selectedText()); m_text->setTextCursor(cur); m_timer->start(); }
int OpenedFile::findLineNumber(const QString &str, const int start_line_number) { QTextCursor parsingCursor = textCursor(); parsingCursor.setPosition(0); while(parsingCursor.blockNumber() != start_line_number) { parsingCursor.movePosition(QTextCursor::Down); } while(!parsingCursor.atEnd()) { if(parsingCursor.block().text().contains(str)) return parsingCursor.blockNumber(); parsingCursor.movePosition(QTextCursor::Down); } return -1; }
void VBoxDbgConsoleOutput::appendText(const QString &rStr, bool fClearSelection) { Assert(m_hGUIThread == RTThreadNativeSelf()); if (rStr.isEmpty() || rStr.isNull() || !rStr.length()) return; /* * Insert all in one go and make sure it's visible. * * We need to move the cursor and unselect any selected text before * inserting anything, otherwise, text will disappear. */ QTextCursor Cursor = textCursor(); if (!fClearSelection && Cursor.hasSelection()) { QTextCursor SavedCursor = Cursor; Cursor.clearSelection(); Cursor.movePosition(QTextCursor::End); Cursor.insertText(rStr); setTextCursor(SavedCursor); } else { if (Cursor.hasSelection()) Cursor.clearSelection(); if (!Cursor.atEnd()) Cursor.movePosition(QTextCursor::End); Cursor.insertText(rStr); setTextCursor(Cursor); ensureCursorVisible(); } }
void TikzEditor::matchBrackets() { // clear previous bracket highlighting QList<QTextEdit::ExtraSelection> extraSelections; if (!isReadOnly()) { QTextEdit::ExtraSelection selection; selection.cursor = textCursor(); selection.cursor.clearSelection(); extraSelections.append(selection); } setExtraSelections(extraSelections); // find current matching brackets m_matchingBegin = -1; m_matchingEnd = -1; if (!m_showMatchingBrackets) return; m_plainText = toPlainText(); // QString matchText = simplifiedText(plainText); const QString matchText = m_plainText; const QTextCursor cursor = textCursor(); int pos = cursor.position(); if (pos == -1) return; else if (cursor.atEnd() || !QString("({[]})").contains(m_plainText.at(pos))) // if the cursor is not next to a bracket, then there is nothing to match, so return { if (pos <= 0 || !QString("({[]})").contains(m_plainText.at(--pos))) return; } // get corresponding opening/closing bracket and search direction QChar car = (!cursor.atEnd()) ? matchText.at(pos) : matchText.at(pos - 1); QChar matchCar; long inc = 1; if (car == '(') matchCar = ')'; else if (car == '{') matchCar = '}'; else if (car == '[') matchCar = ']'; else { inc = -1; if (car == ')') matchCar = '('; else if (car == '}') matchCar = '{'; else if (car == ']') matchCar = '['; else return; } // find location of the corresponding bracket m_matchingBegin = pos; int numOfMatchCharsToSkip = 0; do { if (matchText.at(pos) == car) numOfMatchCharsToSkip++; else if (matchText.at(pos) == matchCar) { numOfMatchCharsToSkip--; if (numOfMatchCharsToSkip == 0) { m_matchingEnd = pos; break; } } pos += inc; } while (pos >= 0 && pos < matchText.length()); if (m_matchingBegin > m_matchingEnd) qSwap(m_matchingBegin, m_matchingEnd); // if there is a match, then show it if (m_matchingBegin != -1) showMatchingBrackets(); }
bool CppDocumentationCommentHelper::handleKeyPressEvent(QKeyEvent *e) const { if (!m_settings.m_enableDoxygen && !m_settings.m_leadingAsterisks) return false; if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) { QTextCursor cursor = m_editorWidget->textCursor(); if (!m_editorWidget->autoCompleter()->isInComment(cursor)) return false; // We are interested on two particular cases: // 1) The cursor is right after a /**, /*!, /// or ///! and the user pressed enter. // If Doxygen is enabled we need to generate an entire comment block. // 2) The cursor is already in the middle of a multi-line comment and the user pressed // enter. If leading asterisk(s) is set we need to write a comment continuation // with those. if (m_settings.m_enableDoxygen && cursor.positionInBlock() >= 3) { const int pos = cursor.position(); if (isStartOfDoxygenComment(cursor)) { QTextDocument *textDocument = m_editorWidget->document(); DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument); // Check if we're already in a CppStyle Doxygen comment => continuation // Needs special handling since CppStyle does not have start and end markers if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB) && isCppStyleContinuation(cursor)) { return handleDoxygenCppStyleContinuation(cursor, e); } DoxygenGenerator doxygen; doxygen.setStyle(style); doxygen.setAddLeadingAsterisks(m_settings.m_leadingAsterisks); doxygen.setGenerateBrief(m_settings.m_generateBrief); doxygen.setStartComment(false); // Move until we reach any possibly meaningful content. while (textDocument->characterAt(cursor.position()).isSpace() && cursor.movePosition(QTextCursor::NextCharacter)) { } if (!cursor.atEnd()) { const QString &comment = doxygen.generate(cursor); if (!comment.isEmpty()) { cursor.beginEditBlock(); cursor.setPosition(pos); cursor.insertText(comment); cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); m_editorWidget->textDocument()->autoIndent(cursor); cursor.endEditBlock(); e->accept(); return true; } } } } // right after first doxygen comment return handleDoxygenContinuation(cursor, e, m_editorWidget->document(), m_settings.m_enableDoxygen, m_settings.m_leadingAsterisks); } return false; }
void Note::linkDialog() { QTextCursor textCursor = m_graphicsTextItem->textCursor(); bool gotUrl = false; if (textCursor.anchor() == textCursor.selectionStart()) { // the selection returns empty since we're between characters // so select one character forward or one character backward // to see whether we're in a url int wasAnchor = textCursor.anchor(); bool atEnd = textCursor.atEnd(); bool atStart = textCursor.atStart(); if (!atStart) { textCursor.setPosition(wasAnchor - 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } if (!gotUrl && !atEnd) { textCursor.setPosition(wasAnchor + 1, QTextCursor::KeepAnchor); QString html = textCursor.selection().toHtml(); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } textCursor.setPosition(wasAnchor, QTextCursor::MoveAnchor); } else { QString html = textCursor.selection().toHtml(); DebugDialog::debug(html); if (UrlTag.indexIn(html) >= 0) { gotUrl = true; } } LinkDialog ld; QString originalText; QString originalUrl; if (gotUrl) { originalUrl = UrlTag.cap(1); ld.setUrl(originalUrl); QString html = m_graphicsTextItem->toHtml(); // assumes html is in xml form QString errorStr; int errorLine; int errorColumn; QDomDocument domDocument; if (!domDocument.setContent(html, &errorStr, &errorLine, &errorColumn)) { return; } QDomElement root = domDocument.documentElement(); if (root.isNull()) { return; } if (root.tagName() != "html") { return; } DebugDialog::debug(html); QList<QDomElement> aElements; findA(root, aElements); foreach (QDomElement a, aElements) { // TODO: if multiple hrefs point to the same url this will only find the first one QString href = a.attribute("href"); if (href.isEmpty()) { href = a.attribute("HREF"); } if (href.compare(originalUrl) == 0) { QString text; if (TextUtils::findText(a, text)) { ld.setText(text); break; } else { return; } } } }
void BaseEditor::ensureAtTheLast() { QTextCursor tc = textCursor(); if(tc.atEnd()) verticalScrollBar()->setValue(verticalScrollBar()->maximum()); }
void CodeEditor::HighlightClosingTag() { if(textCursor().hasSelection()) return; QTextCursor parsingTextCursor = textCursor(); QTextCursor unHighlightingTextCursor = textCursor(); QString plain_text = toPlainText(); QString opening_tag; QStringList opened_tag_list; int opened_quote = 0, tag_delimiter_balance = 0, start_parse_position = 0; BlockData * data; if(!PreviousHighlightedOpeningTag.isNull()) { unHighlightingTextCursor.setPosition(PreviousHighlightedOpeningTag.x()); unHighlightingTextCursor.setPosition(PreviousHighlightedOpeningTag.y(), QTextCursor::KeepAnchor); unHighlightingTextCursor.block().setUserData(NULL); //unHighlightingTextCursor.mergeCharFormat(format); PreviousHighlightedOpeningTag.setX(0); PreviousHighlightedOpeningTag.setY(0); } if(!PreviousHighlightedClosingTag.isNull()) { unHighlightingTextCursor.setPosition(PreviousHighlightedClosingTag.x()); unHighlightingTextCursor.setPosition(PreviousHighlightedClosingTag.y(), QTextCursor::KeepAnchor); unHighlightingTextCursor.block().setUserData(NULL); //unHighlightingTextCursor.mergeCharFormat(format); PreviousHighlightedClosingTag.setX(0); PreviousHighlightedClosingTag.setY(0); } parsingTextCursor.movePosition(QTextCursor::StartOfWord); parsingTextCursor.movePosition(QTextCursor::Left); if(plain_text[parsingTextCursor.position()] != '<') return; // not a(n opening) tag parsingTextCursor.movePosition(QTextCursor::Right); do { parsingTextCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); } while (plain_text[parsingTextCursor.position()] != '>'); start_parse_position = parsingTextCursor.position(); opening_tag = parsingTextCursor.selectedText(); opening_tag = opening_tag.trimmed(); if(opening_tag.endsWith("/")) return; // closing tag is the opening one if(opening_tag.contains(' ')) { int index_space; index_space = opening_tag.indexOf(' '); opening_tag.chop(opening_tag.count() - index_space); } parsingTextCursor.setPosition(parsingTextCursor.selectionStart()); do { parsingTextCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); } while (plain_text[parsingTextCursor.position()] != '>' && plain_text[parsingTextCursor.position()] != ' '); data = new BlockData(); data->format.setFontUnderline(true); data->format.setFontOverline(true); parsingTextCursor.block().setUserData(data); PreviousHighlightedOpeningTag.setX(parsingTextCursor.selectionStart()); PreviousHighlightedOpeningTag.setY(parsingTextCursor.selectionEnd()); parsingTextCursor.setPosition(start_parse_position+1); while(!parsingTextCursor.atEnd()) { if( opened_quote == 0 && plain_text[parsingTextCursor.position()] == '"' && plain_text[parsingTextCursor.position()-1] != '\\') { opened_quote++; } else if(opened_quote > 0 && plain_text[parsingTextCursor.position()] == '"' && plain_text[parsingTextCursor.position()-1] != '\\') { opened_quote--; } if (opened_quote == 0) { if(plain_text[parsingTextCursor.position()] == '>') { tag_delimiter_balance--; } else if(plain_text[parsingTextCursor.position()] == '<') { tag_delimiter_balance++; } } if ( tag_delimiter_balance == 0 && parsingTextCursor.hasSelection() ) { QString tag_text = parsingTextCursor.selectedText().trimmed(); tag_text.remove('<'); if(!tag_text.contains("--")) { if(tag_text.startsWith('/')) { tag_text.remove('/'); if(opened_tag_list.empty()) { //found our tag, select it parsingTextCursor.movePosition(QTextCursor::PreviousWord); parsingTextCursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); data = new BlockData(); data->format.setFontUnderline(true); data->format.setFontOverline(true); parsingTextCursor.block().setUserData(data); PreviousHighlightedClosingTag.setX(parsingTextCursor.selectionStart()); PreviousHighlightedClosingTag.setY(parsingTextCursor.selectionEnd()); break; } else { opened_tag_list.removeOne(tag_text); } } else if(!tag_text.contains('/')) { if (tag_text.contains(' ')) { int first_space = tag_text.indexOf(' '); tag_text.chop(tag_text.count() - first_space); } opened_tag_list.append(tag_text); } // we don't need to test for <... /> since it won't influence the position of the closing tag } } parsingTextCursor.movePosition(QTextCursor::Right, tag_delimiter_balance > 0 ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor); } }
void ConsoleWidget::keyPressEvent(QKeyEvent* event) { switch(event->key()) { case Qt::Key_Tab: case Qt::Key_Backtab: event->accept(); { QTextCursor cursor = textCursor(); int begin = cursor.position(); int end = cursor.anchor(); if(end < begin) { int tmp = end; end = begin; begin = tmp; } cursor.setPosition(end); cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor); QString line = cursor.selectedText(); std::string command(line.toAscii().constData()); std::string inputCommand = command; console.completeConsoleCommand(command, event->key() == Qt::Key_Tab, begin == end); if(command == inputCommand) QApplication::beep(); else { QString newLine = command.c_str(); int newBegin = cursor.position(); int newEnd = newBegin + newLine.length(); cursor.insertText(newLine); /* int i = 0; for(int len = std::min(line.length(), newLine.length()); i < len && line.at(i) == newLine.at(i); ++i); newBegin += i; */ for(int i = command.length() - 2; i >= 0; --i) { char c = command[i]; if(c == ' ' || c == ':' || c == '.') { newBegin += i + 1; break; } } cursor.setPosition(newBegin); cursor.setPosition(newEnd, QTextCursor::KeepAnchor); setTextCursor(cursor); } } break; case Qt::Key_Return: case Qt::Key_Enter: if(event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)) { QTextCursor cursor = textCursor(); cursor.insertBlock(); setTextCursor(cursor); } else { event->accept(); { QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::StartOfBlock); cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); QString line = cursor.selectedText(); cursor.movePosition(QTextCursor::EndOfLine); if(cursor.atEnd()) cursor.insertText("\n"); cursor.movePosition(QTextCursor::NextBlock); setTextCursor(cursor); // save output for the case that the simulator crashes if(consoleView.loadAndSaveOutput) { QSettings& settings = RoboCupCtrl::application->getLayoutSettings(); settings.beginGroup(consoleView.fullName); settings.setValue("Output", toPlainText()); settings.endGroup(); output.clear(); } // execute the command console.executeConsoleCommand(line.toAscii().constData()); // stores unix like history entry history.removeAll(line); history.append(line); history_iter = history.end(); } } break; case Qt::Key_Right: { // avoid jumping to the next line when the right arrow key is used to accept suggestions from tab completion bool handled = false; if(event->modifiers() == 0) { QTextCursor cursor = textCursor(); int position = cursor.position(); if(position > cursor.anchor()) { cursor.movePosition(QTextCursor::EndOfBlock); if(cursor.position() == position) { handled = true; setTextCursor(cursor); } } } if(!handled) QTextEdit::keyPressEvent(event); } break; // History browsing keys case Qt::Key_Up: if((event->modifiers() & Qt::ControlModifier) && !history.isEmpty() && history_iter != history.begin()) { event->accept(); history_iter--; QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::End); cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); cursor.removeSelectedText(); cursor.insertText(*history_iter); setTextCursor(cursor); } else { QTextEdit::keyPressEvent(event); } break; case Qt::Key_Down: if(event->modifiers() & Qt::ControlModifier) { event->accept(); QTextCursor cursor = textCursor(); cursor.movePosition(QTextCursor::End); cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); cursor.removeSelectedText(); if(!history.isEmpty() && history_iter != history.end()) { history_iter++; if(history_iter != history.end()) { cursor.insertText(*history_iter); setTextCursor(cursor); } } } else { QTextEdit::keyPressEvent(event); } break; default: QTextEdit::keyPressEvent(event); if(event->matches(QKeySequence::Copy) || event->matches(QKeySequence::Cut)) emit pasteAvailable(canPaste()); break; } }
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; }
bool CodeEditor::checkXmlCorrectness(QString & error_message) { QTextCursor parsingTextCursor = textCursor(); int tag_delimiter_balance = 0; int opened_brace_counter = 0; int opened_quote = 0; QStringList opened_tag_list; QString plain_text = toPlainText(); bool isComentary = false; parsingTextCursor.setPosition(0); while(!parsingTextCursor.atEnd()) { if(isComentary) { if(plain_text[parsingTextCursor.position()] == '>') { if(plain_text[parsingTextCursor.position()-1] == '-'&&plain_text[parsingTextCursor.position()-2] == '-') { isComentary = false; parsingTextCursor.movePosition(QTextCursor::Right, tag_delimiter_balance > 0 ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor); continue; } } parsingTextCursor.movePosition(QTextCursor::Right, tag_delimiter_balance > 0 ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor); continue; } if( opened_quote == 0 && plain_text[parsingTextCursor.position()] == '"' && plain_text[parsingTextCursor.position()-1] != '\\') { opened_quote++; } else if(opened_quote > 0 && plain_text[parsingTextCursor.position()] == '"' && plain_text[parsingTextCursor.position()-1] != '\\') { opened_quote--; } if (opened_quote == 0) { if(plain_text[parsingTextCursor.position()] == '}') { opened_brace_counter--; } else if(plain_text[parsingTextCursor.position()] == '>') { tag_delimiter_balance--; } else if(plain_text[parsingTextCursor.position()] == '{') { opened_brace_counter++; } else if(plain_text[parsingTextCursor.position()] == '<') { if(toPlainText().count() > parsingTextCursor.position()+3 && plain_text[parsingTextCursor.position()+1] == '!' && plain_text[parsingTextCursor.position()+2] == '-' && plain_text[parsingTextCursor.position()+3] == '-') { isComentary = true; parsingTextCursor.movePosition(QTextCursor::Right, tag_delimiter_balance > 0 ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor, 3); continue; } if (opened_brace_counter > 0 && plain_text[parsingTextCursor.position() + 1] != '!') { error_message = "xml tag detected inside css class"; setTextCursor(parsingTextCursor); return false; } tag_delimiter_balance++; } } if(opened_brace_counter > 1 || tag_delimiter_balance > 1) { error_message = "found two opening character('<' or '{') in a row"; parsingTextCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); setTextCursor(parsingTextCursor); return false; } if(opened_brace_counter < 0|| tag_delimiter_balance < 0) { error_message = "closing character ('>' or '}') before any opening one"; parsingTextCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); setTextCursor(parsingTextCursor); return false; } if ( tag_delimiter_balance == 0 && parsingTextCursor.hasSelection() ) { QString tag_text = parsingTextCursor.selectedText().trimmed(); tag_text.remove('<'); if(!tag_text.contains("--")) { if( !tag_text.contains('/') ) { if ( tag_text.contains(' ') ) { int first_space = tag_text.indexOf(' '); tag_text.chop(tag_text.count() - first_space); } opened_tag_list.append(tag_text); } else { if(tag_text.startsWith('/')) { tag_text.remove('/'); if(!opened_tag_list.removeOne(tag_text)) { error_message = tag_text + " is closed without being opened"; setTextCursor(parsingTextCursor); return false; } } else { tag_text.remove('>'); tag_text = tag_text.trimmed(); if(!tag_text.endsWith('/')) // opening tag with source file behind a directory { if ( tag_text.contains(' ') ) { int first_space = tag_text.indexOf(' '); tag_text.chop(tag_text.count() - first_space); } opened_tag_list.append(tag_text); } } } } } // TODO: check l'ordre de fermeture parsingTextCursor.movePosition(QTextCursor::Right, tag_delimiter_balance > 0 ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor); } if(isComentary) { error_message = "Comment section is not closed"; return false; } opened_tag_list.removeAll( "!--" ); if(!opened_tag_list.isEmpty()) { error_message = opened_tag_list.first() + " is not closed"; } return opened_tag_list.isEmpty(); }
bool trySplitComment(TextEditor::TextEditorWidget *editorWidget, const CPlusPlus::Snapshot &snapshot) { const CommentsSettings &settings = CppToolsSettings::instance()->commentsSettings(); if (!settings.m_enableDoxygen && !settings.m_leadingAsterisks) return false; QTextCursor cursor = editorWidget->textCursor(); if (!CPlusPlus::MatchingText::isInCommentHelper(cursor)) return false; // We are interested on two particular cases: // 1) The cursor is right after a /**, /*!, /// or ///! and the user pressed enter. // If Doxygen is enabled we need to generate an entire comment block. // 2) The cursor is already in the middle of a multi-line comment and the user pressed // enter. If leading asterisk(s) is set we need to write a comment continuation // with those. if (settings.m_enableDoxygen && cursor.positionInBlock() >= 3) { const int pos = cursor.position(); if (isStartOfDoxygenComment(cursor)) { QTextDocument *textDocument = editorWidget->document(); DoxygenGenerator::DocumentationStyle style = doxygenStyle(cursor, textDocument); // Check if we're already in a CppStyle Doxygen comment => continuation // Needs special handling since CppStyle does not have start and end markers if ((style == DoxygenGenerator::CppStyleA || style == DoxygenGenerator::CppStyleB) && isCppStyleContinuation(cursor)) { return handleDoxygenCppStyleContinuation(cursor); } DoxygenGenerator doxygen; doxygen.setStyle(style); doxygen.setAddLeadingAsterisks(settings.m_leadingAsterisks); doxygen.setGenerateBrief(settings.m_generateBrief); doxygen.setStartComment(false); // Move until we reach any possibly meaningful content. while (textDocument->characterAt(cursor.position()).isSpace() && cursor.movePosition(QTextCursor::NextCharacter)) { } if (!cursor.atEnd()) { const QString &comment = doxygen.generate(cursor, snapshot, editorWidget->textDocument()->filePath()); if (!comment.isEmpty()) { cursor.beginEditBlock(); cursor.setPosition(pos); cursor.insertText(comment); cursor.setPosition(pos - 3, QTextCursor::KeepAnchor); editorWidget->textDocument()->autoIndent(cursor); cursor.endEditBlock(); return true; } } } } // right after first doxygen comment return handleDoxygenContinuation(cursor, editorWidget, settings.m_enableDoxygen, settings.m_leadingAsterisks); }