<commit_before>function wsLocation() {
    return window.location.href.replace(/^([a-z]+)\:/i, "ws:");
}

var ignoreChange = false;
function handleChange(editor, change) {
    if(ignoreChange) { return; }
    var msgs = doc.replace(change.from, change.to, change.text);
    if(msgs.length > 0) {
        sock.send(JSON.stringify({ actions: msgs }));
    }
}

function refreshEditor() {
    ignoreChange = true;
    // fixme: try to preserve cursor position and selection better if other users insert lines etc
    var sel = cm.somethingSelected();
    var selStart;
    var selEnd;
    var cursor = cm.getCursor();
    if(sel) {
        selStart = cm.getCursor(start);
        selEnd   = cm.getCursor(end);
    }
    cm.setValue(doc.currentVersion());
    cm.setCursor(cursor);
    if(sel) {
        cm.setSelection(selStart,selEnd);
    }
    ignoreChange = false;
}

doc = new Document();

cm = CodeMirror.fromTextArea($('#editor')[0],
                             { mode:         'text/x-haskell'
                               , lineWrapping: true
                               , lineNumbers:  true
                               , fixedGutter:  true
                               , onChange:     handleChange
                             }
                            );
sock = new WebSocket(wsLocation());
sock.onmessage = function(evt) {
    evtJson(evt,function(msg) {
        if(msg.time) {
            updateTimestamp(msg.time);
        }
        var refresh = false;
        var r;
        if(msg.actions) {
            for(var i=0;i<msg.actions.length;i++) {
                var a = msg.actions[i];
                if(a.action === "doc") {
                    doc.setDocument(a.doc);
                    refresh = true;
                } else if(a.action === "clientid") {
                    setClientId(a.clientId);
                } else if(a.action === "insert") {
                    r = doc.applyOp(a);
                    if(r) { refresh = true; }
                } else if(a.action === "remove") {
                    r = doc.applyOp(a);
                    if(r) { refresh = true; }
                }
            }
        }
        if(msg.refreshoutput) {
            // refreshOutput();
            // alert('need to refresh output');
            outputit();
        }
        if(refresh) {
            refreshEditor();
        }
    });
}

function makeResultSlot(expr) {
    var slot = $('<div><span class="prompt">hint&gt;</span> <span class="expr">empty expr</span><div class="result">...</div></div>');
    slot.find('.expr').text(expr);
    return slot;
}

function fillInResultSlot(slot, res) {
    var node = slot.find('.result');
    if (res.error) {
        node.text(res.error);
    } else {
        node.text('');
        node.append(res.result.result);
    }
}

function formatResult (res) {
    var slot = makeResultSlot(res.expr);
    fillInResultSlot(slot, res);
    return slot;
}

function scrollToBottom(elem) { // pass in the id of the element you want scrolled to bottom
    var elem = document.getElementById(elem);
    elem.scrollTop = elem.scrollHeight;
}
function load() {
    $.get('/loader', function(res) {
        if ("" + res === "Main,Helper") {
            $("#editormessages").text("");
        } else if (typeof res === "string") {
            var em = $("#editormessages");
            em.empty();
            var lines = res.split("\n");
            for (var i = 0; i < lines.length; ++i) {
                console.log('line ' + i + ': ' + lines[i]);
                em.append(lines[i]);
                if (lines[i+1])
                    em.append($('<br>'));
            }
        } else { // just paranoia
            $("#editormessages").text("" + res);
        }
    });
}

function outputit(){
    $.ajax({
        type: "GET",
        url: "/results",
        success: function(results) {
            // clear the output area
            $("#output").empty();
            // map formatResult over the results
            for (var i = 0; i < results.length; i++) {
                $("#output").append(formatResult(results[i]));
            }
            scrollToBottom('output');
        }
    }); // end ajax call
    return false;
}

$(function () {

    $("#load").click(function() {
        load();
        $("#expr").select();
        $("#expr").focus();
        return false;
    });

    function evalit() {
        var expr = $("#expr").val();
        var slot = makeResultSlot(expr);
        $("#output").append(slot);
        load();
        $.ajax({
            type: "GET",
            url: "/eval",
            data: {expr: expr},
            success: function(res) {
                // XXX This is probably pointless now that each evaluation
                // causes the server to send out a refreshoutput message. 
                // But maybe it still gets you a result display with lower
                // latency (one less round trip, and not rewriting the whole
                // output history) so I'm not nuking this yet.
                fillInResultSlot(slot, res);
                scrollToBottom('output');
            }
        }); // end ajax call
        $("#expr").select();
        return false;
    }


    $("#evalit").click(evalit);

    $("#outputit").click(outputit);
});<commit_msg>fix race on load/eval, by adding an extra round trip to wait for load. which kind of sucks.<commit_after>function wsLocation() {
    return window.location.href.replace(/^([a-z]+)\:/i, "ws:");
}

var ignoreChange = false;
function handleChange(editor, change) {
    if(ignoreChange) { return; }
    var msgs = doc.replace(change.from, change.to, change.text);
    if(msgs.length > 0) {
        sock.send(JSON.stringify({ actions: msgs }));
    }
}

function refreshEditor() {
    ignoreChange = true;
    // fixme: try to preserve cursor position and selection better if other users insert lines etc
    var sel = cm.somethingSelected();
    var selStart;
    var selEnd;
    var cursor = cm.getCursor();
    if(sel) {
        selStart = cm.getCursor(start);
        selEnd   = cm.getCursor(end);
    }
    cm.setValue(doc.currentVersion());
    cm.setCursor(cursor);
    if(sel) {
        cm.setSelection(selStart,selEnd);
    }
    ignoreChange = false;
}

doc = new Document();

cm = CodeMirror.fromTextArea($('#editor')[0],
                             { mode:         'text/x-haskell'
                               , lineWrapping: true
                               , lineNumbers:  true
                               , fixedGutter:  true
                               , onChange:     handleChange
                             }
                            );
sock = new WebSocket(wsLocation());
sock.onmessage = function(evt) {
    evtJson(evt,function(msg) {
        if(msg.time) {
            updateTimestamp(msg.time);
        }
        var refresh = false;
        var r;
        if(msg.actions) {
            for(var i=0;i<msg.actions.length;i++) {
                var a = msg.actions[i];
                if(a.action === "doc") {
                    doc.setDocument(a.doc);
                    refresh = true;
                } else if(a.action === "clientid") {
                    setClientId(a.clientId);
                } else if(a.action === "insert") {
                    r = doc.applyOp(a);
                    if(r) { refresh = true; }
                } else if(a.action === "remove") {
                    r = doc.applyOp(a);
                    if(r) { refresh = true; }
                }
            }
        }
        if(msg.refreshoutput) {
            // refreshOutput();
            // alert('need to refresh output');
            outputit();
        }
        if(refresh) {
            refreshEditor();
        }
    });
}

function makeResultSlot(expr) {
    var slot = $('<div><span class="prompt">hint&gt;</span> <span class="expr">empty expr</span><div class="result">...</div></div>');
    slot.find('.expr').text(expr);
    return slot;
}

function fillInResultSlot(slot, res) {
    var node = slot.find('.result');
    if (res.error) {
        node.text(res.error);
    } else {
        node.text('');
        node.append(res.result.result);
    }
}

function formatResult (res) {
    var slot = makeResultSlot(res.expr);
    fillInResultSlot(slot, res);
    return slot;
}

function scrollToBottom(elem) { // pass in the id of the element you want scrolled to bottom
    var elem = document.getElementById(elem);
    elem.scrollTop = elem.scrollHeight;
}
function load(success) {
    $.get('/loader', function(res) {
        if ("" + res === "Main,Helper") {
            $("#editormessages").text("");
        } else if (typeof res === "string") {
            var em = $("#editormessages");
            em.empty();
            var lines = res.split("\n");
            for (var i = 0; i < lines.length; ++i) {
                console.log('line ' + i + ': ' + lines[i]);
                em.append(lines[i]);
                if (lines[i+1])
                    em.append($('<br>'));
            }
        } else { // just paranoia
            $("#editormessages").text("" + res);
        }
        if (success) success();
    });
}

function outputit(){
    $.ajax({
        type: "GET",
        url: "/results",
        success: function(results) {
            // clear the output area
            $("#output").empty();
            // map formatResult over the results
            for (var i = 0; i < results.length; i++) {
                $("#output").append(formatResult(results[i]));
            }
            scrollToBottom('output');
        }
    }); // end ajax call
    return false;
}

$(function () {

    $("#load").click(function() {
        load();
        $("#expr").select();
        $("#expr").focus();
        return false;
    });

    function evalit() {
        var expr = $("#expr").val();
        var slot = makeResultSlot(expr);
        $("#output").append(slot);
        load(function() {
            $.ajax({
                type: "GET",
                url: "/eval",
                data: {expr: expr},
                success: function(res) {
                    // XXX This is probably pointless now that each evaluation
                    // causes the server to send out a refreshoutput message. 
                    // But maybe it still gets you a result display with lower
                    // latency (one less round trip, and not rewriting the whole
                    // output history) so I'm not nuking this yet.
                    fillInResultSlot(slot, res);
                    scrollToBottom('output');
                }
            });
        });
        $("#expr").select();
        return false;
    }


    $("#evalit").click(evalit);

    $("#outputit").click(outputit);
});
{'fraction_non_alphanumeric': 0.1500186011904762, 'fraction_numerical': 0.0011160714285714285, 'mean_word_length': 1.5602380952380952, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2082959', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2969, 'n_words': 701}