Search in sources :

Example 1 with TransformEditor

use of catdata.fql.decl.TransformEditor in project fql by CategoricalData.

the class FqlCodeEditor method vedit.

public void vedit() {
    FQLProgram init = tryParse(topArea.getText());
    if (init == null) {
        respArea.setText(toDisplay);
        return;
    }
    if (init.lines.isEmpty()) {
        return;
    }
    String which = null;
    int start = -1;
    int offs = topArea.getCaretPosition();
    int end = -1;
    int i = 0;
    int pos = 0;
    for (String k : init.lines.keySet()) {
        Integer v = init.lines.get(k);
        if (v < offs && v > start) {
            start = v;
            which = k;
            pos = i;
        }
        i++;
    }
    if (which == null) {
        throw new RuntimeException();
    }
    int j = 0;
    for (String k : init.lines.keySet()) {
        if (j == pos + 1) {
            end = init.lines.get(k);
            break;
        }
        j++;
    }
    if (end == -1) {
        end = topArea.getText().length();
    }
    InstExp ie = init.insts.get(which);
    TransExp te = init.transforms.get(which);
    if ((ie == null && te == null) || (ie != null && !(ie instanceof InstExp.Const)) || (te != null && !(te instanceof Const))) {
        respArea.setText("Cannot visually edit " + which + ": only constant instances or transforms are visually editable.");
        return;
    }
    try {
        if (ie != null) {
            InstExp.Const iec = (InstExp.Const) ie;
            InstExp.Const n = new InstanceEditor(which, iec.sig.toSig(init), iec).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "instance " + which + " = " + n + " : " + n.sig + "\n\n";
            topArea.replaceRange(newText, start, end);
        } else {
            Const iec = (Const) te;
            if (iec == null) {
                throw new RuntimeException("Anomaly: please report");
            }
            InstExp.Const s = (InstExp.Const) init.insts.get(iec.src);
            InstExp.Const t = (InstExp.Const) init.insts.get(iec.dst);
            Const n = new TransformEditor(which, init.insts.get(iec.src).type(init).toSig(init), iec, s, t).show(Color.black);
            if (n == null) {
                return;
            }
            String newText = "transform " + which + " = " + n + " : " + n.src + " -> " + n.dst + "\n\n";
            topArea.replaceRange(newText, start, end);
        }
    } catch (FQLException fe) {
        fe.printStackTrace();
        respArea.setText(fe.getLocalizedMessage());
    }
}
Also used : InstExp(catdata.fql.decl.InstExp) FQLException(catdata.fql.FQLException) TransExp(catdata.fql.decl.TransExp) Const(catdata.fql.decl.TransExp.Const) InstanceEditor(catdata.fql.decl.InstanceEditor) FQLProgram(catdata.fql.decl.FQLProgram) TransformEditor(catdata.fql.decl.TransformEditor)

Aggregations

FQLException (catdata.fql.FQLException)1 FQLProgram (catdata.fql.decl.FQLProgram)1 InstExp (catdata.fql.decl.InstExp)1 InstanceEditor (catdata.fql.decl.InstanceEditor)1 TransExp (catdata.fql.decl.TransExp)1 Const (catdata.fql.decl.TransExp.Const)1 TransformEditor (catdata.fql.decl.TransformEditor)1