Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将运行时定义的变量、函数、类添加到CodeMirror自动完成列表中,并在JS中从代码中删除时删除它们_Javascript_Autocomplete_Codemirror - Fatal编程技术网

Javascript 如何将运行时定义的变量、函数、类添加到CodeMirror自动完成列表中,并在JS中从代码中删除时删除它们

Javascript 如何将运行时定义的变量、函数、类添加到CodeMirror自动完成列表中,并在JS中从代码中删除时删除它们,javascript,autocomplete,codemirror,Javascript,Autocomplete,Codemirror,我正在为我的在线python代码编辑器项目使用这个库。它可以自动完成。但我想将用户定义的变量、函数和类添加到自动完成列表中,并在运行时从编辑器中删除定义时将它们从列表中删除。我还想在没有JQuery的vanillaJS中实现这一点 var editor = CodeMirror.fromTextArea(myTextarea, { mode: { name: "python", version: 3, sin

我正在为我的在线python代码编辑器项目使用这个库。它可以自动完成。但我想将用户定义的变量、函数和类添加到自动完成列表中,并在运行时从编辑器中删除定义时将它们从列表中删除。我还想在没有JQuery的vanillaJS中实现这一点

var editor = CodeMirror.fromTextArea(myTextarea, {
        mode: {
            name: "python",
            version: 3,
            singleLineStringErrors: false
        },
        lineNumbers: true,
        indentUnit: 4,
        extraKeys: {
            "Ctrl-Space": "autocomplete",
            "Ctrl-/": "toggleComment",
            "Alt-E": runCode,
            "Alt-C": clearOutput

        },
        matchBrackets: true,
        autoCloseBrackets: true
    });

最后我发现CodeMirror的anyword-hint.js可以用来完成这个任务。 因此,我结合了&anyword-hint.js的功能。 (我使用npm安装CodeMirror,因为我正在处理一个NodeJs项目)

(35-37行)因为我在我的
showallphints()
函数中设置了
completeSingle:false

我知道
getAllHints(e)
函数可以进行更多优化,并且存在一些问题,例如无法过滤掉变量、函数和类名。 这意味着提示列表包括变量、函数、类名,还包括以前键入的字符串、数字等。 然而,这正是我所期望的。所以我决定在优化之前发布一个答案。
欢迎提供任何优化提示、建议和更好的答案。

请向我们展示您的代码。@DJ9114抱歉。我已经用代码更新了这个问题
<script src="node_modules/codemirror/lib/codemirror.js"></script>
<script src="node_modules/codemirror/mode/python/python.js"></script>

<script src="python-hint.js"></script>
<!-- this script not contains in CodeMirror. I have added manually -->


<script src="node_modules/codemirror/addon/hint/anyword-hint.js"></script>
<script src="node_modules/codemirror/addon/hint/show-hint.js"></script>


<link rel="stylesheet" href="node_modules/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="node_modules/codemirror/addon/hint/show-hint.css">

 <script>
    function getAllHints(e) {
        var hints = CodeMirror.pythonHint(e);
        var anyHints = CodeMirror.hint.anyword(e);

        anyHints.list.forEach(function(hint) {
            if (hints.list.indexOf(hint) == -1)
                hints.list.push(hint);
        })

        hints.list.sort();

        if (hints) {
            CodeMirror.on(hints, "pick", function(word) {
                if (word.charAt(word.length - 1) == ')')
                    editor.execCommand("goCharLeft");
            });
        }
        return hints;
    }

    function showAllHints() {
        editor.showHint({
            hint: getAllHints,
            completeSingle: false
        });
    }

    var editor = CodeMirror.fromTextArea(document.getElementById("editor"), {
        mode: {
            name: "python",
            version: 3
        },
        lineNumbers: true,
        indentUnit: 4
    });

    editor.on('inputRead', function onChange(editor, input) {
        if (input.text[0] === ' ' || input.text[0] === ":" || input.text[0] === ")") {
            return;
        }
        showAllHints();
    });
</script>
if(completionList.length == 1) {
  completionList.push(" ");
}