Javascript 代码镜像首先查找函数

Javascript 代码镜像首先查找函数,javascript,codemirror,Javascript,Codemirror,我正在使用代码镜像为我教的一门课构建一些代码练习。我将其设置为“代码镜像”一次检索一行代码,突出显示当前行,以便学生可以在执行每个步骤时看到它 问题是我希望学生能够创建自己的函数,但我不能逐行阅读代码。我需要首先找到任何用户定义的函数。然后,如果用户调用该函数,我需要跳转到该函数,读取函数内的行,然后返回到用户调用该函数的位置,继续读取其余代码 有什么想法吗?我翻阅了手册,没有找到解决办法。提前谢谢 var editor = CodeMirror.fromTextArea(document.ge

我正在使用代码镜像为我教的一门课构建一些代码练习。我将其设置为“代码镜像”一次检索一行代码,突出显示当前行,以便学生可以在执行每个步骤时看到它

问题是我希望学生能够创建自己的函数,但我不能逐行阅读代码。我需要首先找到任何用户定义的函数。然后,如果用户调用该函数,我需要跳转到该函数,读取函数内的行,然后返回到用户调用该函数的位置,继续读取其余代码

有什么想法吗?我翻阅了手册,没有找到解决办法。提前谢谢

var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
            lineNumbers: true,
            mode: "javascript",
            matchBrackets: true
        });
        editor.setSize(400, 400);


$('#submit').click(function(){
      //read the next line of code each second
        myVar=setInterval(function(){getCode()},1000);
    });


    function getCode(){
     //only read lines that have code written in them
        var lines = editor.lineCount();

     //get all the code on that specific line
        var code = editor.getRange({'line': i, 'ch': 0}, {'line': i, 'ch': 255});


//highlight
        editor.removeLineClass(i-1, 'background', 'highlight');
        editor.addLineClass(i, 'background', 'highlight');

//evaluate code (this will be replaced with a library eventually for security reasons)
        eval(code);
        i++;

//stop once all lines have been read
        if(i > lines){
            clearInterval(myVar);
        } 

使用解析器获得代码的更抽象视图。由于学生可能正在编辑代码,并且可能在语法上无效,Acorn的容错解析器可能非常适合。看到和