Codemirror中的Python自动完成功能?

时间:2012-07-09 18:57:05

标签: python autocomplete codemirror

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

是否有人为CodeMirror构建类似于JavaScript Version

的Python提示文件

(编辑以供将来参考:link to similar question on CodeMirror Google Group

5 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分: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>

答案 2 :(得分:1)

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

您可以获得python-hint.js from here

要工作,你需要在你的html:

  1. 包括simple-hint,jspython-hint.jssimple-hint.csscodemirror.js

  2. 添加此脚本:

    <script>
    CodeMirror.commands.autocomplete = function(cm) {
         CodeMirror.simpleHint(cm, CodeMirror.pythonHint);
       }
    </script>
    
  3. python-hint.js是我今天创建的基本版本,未经深入审核。

答案 3 :(得分:1)

添加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
    });
});

答案 4 :(得分:0)

您还可以通过以下方式进行初始化,向CodeMirror初始化添加extraKeys参数:

CodeMirror(function(elt) {
        myTextArea.parentNode.replaceChild(elt, myTextArea);
    }, {
        mode: "python",
        lineNumbers: true,
        autofocus: true,
        extraKeys: {"Ctrl-Space": "autocomplete"}
    });