3

我正在尝试为 Python 语言的 Codemirror 设置自动完成功能。不幸的是,Codemirror 似乎只包含 Javascript 关键字完成所需的文件。

有没有人为 CodeMirror 构建了类似于JavaScript 版本的 Python 提示文件?

(编辑以供将来参考:CodeMirror Google Group 上类似问题的链接

4

5 回答 5

9

我是 Codemirror(1 和 2)的 Python 解析器的原作者。您是正确的,Python 解析器没有为自动完成提供足够的信息。当 Codemirror 2 出现时,我尝试将其构建到解析器中,但事实证明这对我当时的 JS 技能来说太难了。

我现在有更多的技能,但时间却少得多。也许有一天我会回到它。或者如果有人想接受它,我很乐意提供帮助。

于 2012-10-09T20:55:49.377 回答
3

添加python-hint.js、show-hint.js、show-hint.css。然后

var editor = CodeMirror.fromTextArea(your editor instance codes here;

editor.on('inputRead', function onChange(editor, input) {
    if (input.text[0] === ';' || input.text[0] === ' ' || input.text[0] === ":") {
        return;
    }
    editor.showHint({
        hint: CodeMirror.pythonHint
    });
});
于 2019-01-17T10:36:31.853 回答
2
< script >
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: {
            name: "python",
            version: 3,
            singleLineStringErrors: false
        },
        lineNumbers: true,
        indentUnit: 4,
        extraKeys: {
            "Ctrl-Space": "autocomplete"
        },
        matchBrackets: true
    });
CodeMirror.commands.autocomplete = function (cm) {
        CodeMirror.simpleHint(cm, CodeMirror.pythonHint);
    } 


 </script>
于 2020-01-13T12:14:38.897 回答
0

我使用基于 codemirror 3 中 pig-hint 的 js 启动 python 自动完成。

你可以python-hint.js 从这里得到。

要工作,您需要在 html 中:

  1. 包括simple-hint,jspython-hint.jssimple-hint.css加上codemirror.js

  2. 添加这个脚本:

    <script>
    CodeMirror.commands.autocomplete = function(cm) {
         CodeMirror.simpleHint(cm, CodeMirror.pythonHint);
       }
    </script>
    

python-hint.js是我今天创建的一个基本js,没有深入审查。

于 2013-01-04T08:41:25.880 回答
0

您也可以通过这种方式进行初始化,将 extraKeys 参数添加到 CodeMirror 初始化中:

CodeMirror(function(elt) {
        myTextArea.parentNode.replaceChild(elt, myTextArea);
    }, {
        mode: "python",
        lineNumbers: true,
        autofocus: true,
        extraKeys: {"Ctrl-Space": "autocomplete"}
    });
于 2019-05-07T08:28:56.683 回答