void qceEqual(const QDocumentCursor& c, const QDocumentCursor& expected, const QString& message){ QEQUAL2(c.hasSelection(),expected.hasSelection(), " got-total: "+cur2str(c)+" expected-total: "+cur2str(expected)+" more: "+message); QEQUAL2(c.anchorLineNumber(),expected.anchorLineNumber(), " got-total: "+cur2str(c)+" expected-total: "+cur2str(expected)+" more: "+message); QEQUAL2(c.anchorColumnNumber(),expected.anchorColumnNumber(), " got-total: "+cur2str(c)+" expected-total: "+cur2str(expected)+" more: "+message); QEQUAL2(c.lineNumber(),expected.lineNumber(), " got-total: "+cur2str(c)+" expected-total: "+cur2str(expected)+" more: "+message); QEQUAL2(c.columnNumber(),expected.columnNumber(), " got-total: "+cur2str(c)+" expected-total: "+cur2str(expected)+" more: "+message); }
bool QSnippetBinding::keyPressEvent(QKeyEvent *event, QEditor *editor) { /* if ( event->modifiers() & Qt::ControlModifier ) { for ( int i = 0; i < qMin(10, m->snippetCount()); ++i ) { if ( event->key() == (Qt::Key_F1 + i) ) { m->snippet(i)->insert(editor); return true; } } } */ if ( (event->modifiers() & Qt::AltModifier) && (event->key() == Qt::Key_Space || event->text() == " ") ) { QDocumentCursor c = editor->cursor(); //c.select(QDocumentCursor::SelectWord); if ( !c.hasSelection() ) { c.movePosition(1, QDocumentCursor::PreviousWord, QDocumentCursor::KeepAnchor); editor->setCursor(c); } QString s = c.selectedText(); for ( int i = 0; i < m_manager->snippetCount(); ++i ) { QSnippet *snip = m_manager->snippet(i); if ( snip->name() == s ) { snip->insert(editor); return true; } } } return QEditorInputBinding::keyPressEvent(event, editor); }
void LatexTables::alignTableCols(QDocumentCursor &cur){ QString text = getTableText(cur); if (!cur.hasSelection()) return; QString indentation = cur.selectionStart().line().indentation(); // split off \begin and \end parts int index = text.indexOf("\\begin{")+6; int cellsStart; QList<CommandArgument> args = getCommandOptions(text, index, &cellsStart); if (args.count() < 2) return; QString tableType = args.at(0).value; // assume alignment in second arg except for the following environments (which have it in the third one) QString alignment; if (tabularNames.contains(tableType)) { alignment = args.at(1).value; } else if (tabularNamesWithOneOption.contains(tableType)) { if (args.count()<3) alignment = ""; // incomplete definition -> fall back to defaults else alignment = args.at(2).value; } else return; // not a registered table environment int cellsEnd = text.indexOf("\\end{"+tableType); if (cellsEnd<0) return; QString beginPart = text.left(cellsStart); QString endPart = text.mid(cellsEnd); LatexTableModel ltm; ltm.setContent(text.mid(cellsStart, cellsEnd-cellsStart)); QStringList l_defs=splitColDef(alignment); simplifyColDefs(l_defs); QStringList content(ltm.getAlignedLines(l_defs)); QString result = beginPart + '\n'; for (int i=0; i<content.count(); i++) { result.append(indentation + content.at(i)); } result.append(indentation + endPart); cur.replaceSelectedText(result); }
QString cur2str(const QDocumentCursor &c){ if (c.hasSelection()) return QString("%1|%2|%3|%4").arg(c.anchorLineNumber()).arg(c.anchorColumnNumber()).arg(c.lineNumber()).arg(c.columnNumber()); return QString("%1|%2").arg(c.lineNumber()).arg(c.columnNumber()); }
void CompletionWord::insertAt(QEditor* editor, QDocumentCursor cursor){ QString savedSelection; int multilines=shownWord.count('\n'); QVector<QDocumentLine> documentlines; if (cursor.hasSelection()) { savedSelection=cursor.selectedText(); cursor.removeSelectedText(); } QDocumentCursor selector=cursor; int curStart=cursor.columnNumber(); QDocumentLine curLine=cursor.line(); cursor.insertText(shownWord); if (multilines) { documentlines.resize(multilines+1); documentlines[0]=curLine; for (int i=1;i<documentlines.count()-1;i++) documentlines[i]=documentlines[i-1].next(); //todo: optimize } if (QDocument::formatFactory()) for (int i=0;i<descriptiveParts.size();i++) { QFormatRange fr(descriptiveParts[i].first+curStart,descriptiveParts[i].second,QDocument::formatFactory()->id("temporaryCodeCompletion")); if (multilines) { QString temp= shownWord; temp.truncate(descriptiveParts[i].first); int linetoadd=temp.count('\n'); if (linetoadd==0) curLine.addOverlay(fr); else if (linetoadd<documentlines.size()) { fr.offset=temp.size()-temp.lastIndexOf('\n')-1; documentlines[linetoadd].addOverlay(fr); } } else curLine.addOverlay(fr); } //place cursor/add \end int selectFrom=-1; int selectTo=-1; int deltaLine=0; if (shownWord.startsWith("\\begin")&&!multilines) { //int curColumnNumber=cursor.columnNumber(); QString indent=curLine.indentation(); int p=shownWord.indexOf("{"); QString content="content..."; if (editor->flag(QEditor::AutoIndent)){ cursor.insertText( "\n"+indent+"\t"+content+"\n"+indent+"\\end"+shownWord.mid(p,shownWord.indexOf("}")-p+1)); indent+="\t"; } else cursor.insertText( "\n"+indent+content+"\n"+indent+"\\end"+shownWord.mid(p,shownWord.indexOf("}")-p+1)); if (QDocument::formatFactory()) for (int i=0;i<descriptiveParts.size();i++) curLine.next().addOverlay(QFormatRange(indent.size(),content.size(),QDocument::formatFactory()->id("temporaryCodeCompletion"))); if (cursorPos==-1) { deltaLine=1; selectFrom=indent.length(); selectTo=indent.length()+content.size(); } else { selectFrom=anchorPos+curStart; selectTo=cursorPos+curStart; } } else if (cursorPos>-1) { if (multilines) { //todo: add support for selected multilines QString temp= shownWord; temp.truncate(cursorPos); deltaLine=temp.count('\n'); if (!deltaLine) { selectFrom=anchorPos+curStart; selectTo=cursorPos+curStart; } else { selectTo=temp.size()-temp.lastIndexOf('\n')-1; selectFrom=anchorPos-cursorPos+selectTo; } } else { selectFrom=anchorPos+curStart; selectTo=cursorPos+curStart; } } else editor->setCursor(cursor); //place after insertion if (selectFrom!=-1){ if (deltaLine>0) selector.movePosition(deltaLine,QDocumentCursor::Down,QDocumentCursor::MoveAnchor); selector.setColumnNumber(selectFrom); if (selectTo>selectFrom) selector.movePosition(selectTo-selectFrom,QDocumentCursor::Right,QDocumentCursor::KeepAnchor); else if (selectTo<selectFrom) selector.movePosition(selectFrom-selectTo,QDocumentCursor::Left,QDocumentCursor::KeepAnchor); editor->setCursor(selector); } if (!savedSelection.isEmpty() && cursorPos>0) editor->cursor().insertText(savedSelection); }