示例#1
0
/** context sensitive completion
 *  take current line, give list of completions (both atoms and files)
 *  thanks to Jan for crafting a proper interface wrapping SWI-Prolog available facilities
 */
QString Completion::initialize(int promptPosition, QTextCursor c, QStringList &strings) {
    QString rets;
    SwiPrologEngine::in_thread _int;
    try {
        int p = c.position();
        Q_ASSERT(p >= promptPosition);

        c.setPosition(promptPosition, c.KeepAnchor);
        QString left = c.selectedText();
        if (left.length()) {
            PlString Before(left.toStdWString().data());

            c.setPosition(p);
            c.movePosition(c.EndOfLine, c.KeepAnchor);
            QString after = c.selectedText();
            PlString After(after.toStdWString().data());

            PlTerm Completions, Delete, word;
            if (PlCall("prolog", "complete_input", PlTermv(Before, After, Delete, Completions)))
                for (PlTail l(Completions); l.next(word); )
                    strings.append(t2w(word));

            c.setPosition(p);
            rets = t2w(Delete);
        }
    }
    catch(PlException e) {
        qDebug() << t2w(e);
    }
    catch(...) {
        qDebug() << "...";
    }

    return rets;
}
/** issue a query filling the model storage
 *  this will change when I will learn how to call SWI-Prolog completion interface
 */
void Completion::initialize(QStringList &strings) {

    SwiPrologEngine::in_thread _int;
    try {
        PlTerm p,m,a,l,v;
        PlQuery q("setof",
            PlTermv(p,
                quv(m,
                    quv(a,
                        join(PlCompound("current_predicate", mod(m, arith(p, a))),
                            neg(C("sub_atom", PlTermv(p, zero, one, _V, A("$"))))
                ))),
            l));
        if (q.next_solution())
            for (PlTail x(l); x.next(v); )
                strings.append(CCP(v));
    }
    catch(PlException e) {
        qDebug() << CCP(e);
    }
}
// Call the arbitrary command
int SWIPLContainer::call(const QString &command)
{
    return PlCall("call", PlTermv(PlCompound(toPlString(command))));
}