QString LatexTables::getTableText(QDocumentCursor &cur){ int result=findNextToken(cur,QStringList(),false,true); if(result!=-2) return QString(); QString line=cur.line().text(); int i=line.indexOf("\\begin"); if(i>=0) cur.setColumnNumber(i); result=findNextToken(cur,QStringList(),true,false); if(result!=-2) return QString(); line=cur.line().text(); QRegExp rx("\\\\end\\{.*\\}"); i=rx.indexIn(line); if(i>=0) cur.setColumnNumber(i+rx.cap(0).length(),QDocumentCursor::KeepAnchor); QString res=cur.selectedText(); return res; }
int LatexTables::findNextToken(QDocumentCursor &cur,QStringList tokens,bool keepAnchor,bool backwards){ int pos=-1; int nextToken=-1; int offset=0; QDocumentCursor::MoveOperation mvNextLine= backwards ? QDocumentCursor::PreviousLine : QDocumentCursor::NextLine; QDocumentCursor::MoveOperation mvNextChar= backwards ? QDocumentCursor::Left : QDocumentCursor::Right; QDocumentCursor::MoveOperation mvStartOfLine= backwards ? QDocumentCursor::EndOfLine : QDocumentCursor::StartOfLine; QDocumentCursor::MoveFlag mvFlag= keepAnchor ? QDocumentCursor::KeepAnchor : QDocumentCursor::MoveAnchor; do{ QString line=cur.line().text(); if(backwards){ offset=line.length(); } line=LatexParser::cutComment(line); if(backwards){ offset=offset-line.length(); QString help; foreach(const QChar& elem,line) help.prepend(elem); line=help; } if(line.contains("\\end{")&&!backwards) { nextToken=-2; break; } if(line.contains("{nigeb\\")&&backwards) { nextToken=-2; break; } pos=-1; for(int i=0;i<tokens.count();i++){ QString elem=tokens.at(i); int colNumber= cur.columnNumber(); if(backwards) colNumber=line.length()+offset-colNumber ; int zw=line.indexOf(elem,colNumber); if(zw>-1) { if(pos>zw || pos==-1){ pos=zw; nextToken=i; } } } if(pos<0){ if(!backwards&&cur.lineNumber()>=cur.document()->lineCount()-1) break; if(backwards&&cur.lineNumber()<=0) break; cur.movePosition(1,mvNextLine,mvFlag); cur.movePosition(1,mvStartOfLine,mvFlag); } }while(pos<0); if(pos>-1) { cur.movePosition(1,mvStartOfLine,mvFlag); cur.movePosition(pos+tokens.at(nextToken).length()+offset,mvNextChar,mvFlag); } return nextToken; }
/*! \brief \fn CCompletion::getLastToken \param c \return QString */ QString CCompletion::getLastToken(const QDocumentCursor &c) { QString line = c.line().text(); QString Token = c.selectedText(); if (Token.isEmpty()) { if (line.size()>1) { int i = c.columnNumber()-1; while (QString("_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").contains(line.at(i).toUpper())) { Token = line.at(i)+Token; i--; if (i<0) break; } } } return Token; }
/*! \brief \fn CCompletion::complete \param c \param trigger */ void CCompletion::complete(const QDocumentCursor &c, const QString &trigger) { // test if there is selected text // etendre le texte selectionner au mot complet // si il a pour next char une ( alors afficher un calltips if ( (trigger == "(" ) || !c.selectedText().isEmpty()) { QStringList tips; //qDebug("fn %s", fn.constData()); QList<QCodeNode*> nodes = mainwindow->windowide->completionScan(editor()); tips = mainwindow->windowide->getProc(getLastToken(c)); if ( tips.count() ) { CDOxyItem * di = mainwindow->windowide->getDOxygenInfo(getLastToken(c).trimmed()); if (di) { tips[0] += QString("\n")+di->brief; if (di->params.count()) { for (int j=0;j<di->params.size();j++) { tips[0] += QString("\n")+di->params.at(j); } } if (!(di->returnTyp.isEmpty())) tips[0]+=QString("\nReturn value : ")+di->returnTyp; if (tips[0].right(1)=="\n") tips[0].chop(1); } QRect r = editor()->cursorRect(); QDocumentCursor cursor = editor()->cursor(); QDocumentLine line = cursor.line(); int hx = editor()->horizontalOffset(), cx = line.cursorToX(cursor.columnNumber()); QCallTip *ct = new QCallTip(editor()->viewport()); ct->move(cx - hx, r.y() + r.height()); ct->setTips(tips); ct->show(); ct->setFocus(); #ifdef TRACE_COMPLETION qDebug("parsing + scoping + search + pre-display : elapsed %i ms", time.elapsed()); #endif } } else { if ( pPopup && pPopup->editor() != editor() ) { delete pPopup; pPopup = 0; } if ( !pPopup ) { pPopup = new QCodeCompletionWidget(editor()); } pPopup->clear(); pPopup->setCursor(editor()->cursor()); QTime time; time.start(); QList<QCodeNode*> nodes = mainwindow->windowide->completionScan(editor()); pPopup->setPrefix(getLastToken(c)); pPopup->setCompletions(nodes); pPopup->update(); pPopup->popup(); } #if 1 #endif }
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); }