/*!
	\brief Completion callback
*/
void QCodeCompletionEngine::complete(const QDocumentCursor& c, const QString& trigger)
{
	#ifdef _QCODE_MODEL_
	// TODO :
	//	* use a more efficient design by avoiding deep copy of the data
	//	* only lex the requested part (stop at cursor or topmost frame required for proper class hierarchy)

	QDocumentCursor cc = c;
	cc.movePosition(1, QDocumentCursor::Start, QDocumentCursor::KeepAnchor);

	//qDebug("%s", qPrintable(cc.selectedText()));

	QCodeBuffer buffer(cc.selectedText());
	//QCodeBuffer buffer(c.document()->text());
	complete(&buffer, trigger);
	#else
	// remove unused argument warnings
	(void) c;
	(void) trigger;

	qWarning("From complete(QDocumentCursor, QString)");
	qWarning("QCodeCompletionEngine is not self-sufficient : subclasses should "
			"reimplement at least on of the complete() method...");
	#endif
}
Exemplo n.º 2
0
/*!
 \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;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
/*!
 \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

}
Exemplo n.º 6
0
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);
}