Как реализовать зеркальное отображение кода, чтобы показать подсказку без CTRL + SPACE - PullRequest
0 голосов
/ 17 мая 2018

JavaScript:

        $http.get("/getApexBody", config).then(function(response) {
            document.getElementById("saveBtn").disabled = false;
            document.getElementById("cleanBtn").disabled = false;
            $scope.apexClassWrapper = response.data;
            $('#loaderImage').hide();
            if (globalEditor1) {
                globalEditor1.toTextArea();
            }
            setTimeout(function(test) {
                CodeMirror.commands.autocomplete = function(cm) {
                    cm.showHint({
                        hint: CodeMirror.hint.auto
                    });
                };
                var editor = CodeMirror.fromTextArea(document.getElementById('apexBody'), {
                    lineNumbers: true,
                    matchBrackets: true,
                    extraKeys: {
                        "Ctrl-Space": "autocomplete"
                    },
                    gutters: ["CodeMirror-lint-markers"],
                    lint: true,
                    mode: "text/x-apex"
                });
                globalEditor1 = $('.CodeMirror')[0].CodeMirror;
            }), 2000
        });

Это мой JS-файл, ctrl-space работает нормально, но мне нужно, чтобы реализовать автозаполнение без каких-либо привязок клавиш.

Я даже пробовал это:

        $http.get("/getApexBody", config).then(function(response) {
            document.getElementById("saveBtn").disabled = false;
            document.getElementById("cleanBtn").disabled = false;
            $scope.apexClassWrapper = response.data;
            $('#loaderImage').hide();
            if (globalEditor1) {
                globalEditor1.toTextArea();
            }
            setTimeout(function(test) {
                /* CodeMirror.commands.autocomplete = function(cm) {
                    cm.showHint({
                        hint: CodeMirror.hint.auto
                    });
                };*/
                var editor = CodeMirror.fromTextArea(document.getElementById('apexBody'), {
                    lineNumbers: true,
                    matchBrackets: true,
                    /*extraKeys: {
                        "Ctrl-Space": "autocomplete"
                    },*/
                    gutters: ["CodeMirror-lint-markers"],
                    lint: true,
                    mode: "text/x-apex"
                });
                editor.on('inputRead', function onChange(editor, input) {
                    if (input.text[0] === ';' || input.text[0] === ' ') {
                        return;
                    }
                    CodeMirror.commands.autocomplete = function(editor) {
                        editor.showHint({
                            hint: CodeMirror.hint.auto
                        });
                    };
                });
                globalEditor1 = $('.CodeMirror')[0].CodeMirror;
            }), 2000
        });

Но это не работает.Есть что-то, чего я здесь не хватает?Как мне показать живые подсказки о завершении с codemirror?

Я использовал show-hints.js и немного изменил его, чтобы он работал для "."тоже.Пожалуйста, помогите.

Ответы [ 2 ]

0 голосов
/ 26 января 2019

Используйте эту функцию для автозаполнения codeMirror без CTRL + Пробел .

Установить completeSingle в false в show-hint.js

editor.on("inputRead", function(instance) {
    if (instance.state.completionActive) {
            return;
    }
    var cur = instance.getCursor();
    var token = instance.getTokenAt(cur);
    if (token.type && token.type != "comment") {
            CodeMirror.commands.autocomplete(instance);
    }
});
0 голосов
/ 17 мая 2018
    $http.get("/getApexBody", config).then(function(response) {
            document.getElementById("saveBtn").disabled = false;
            document.getElementById("cleanBtn").disabled = false;
            $scope.apexClassWrapper = response.data;
            $('#loaderImage').hide();
            if (globalEditor1) {
                globalEditor1.toTextArea();
            }
            setTimeout(function(test) {
                /*CodeMirror.commands.autocomplete = function(cm) {
                    cm.showHint({
                        hint: CodeMirror.hint.auto
                    });
                };*/
                var editor = CodeMirror.fromTextArea(document.getElementById('apexBody'), {
                    lineNumbers: true,
                    matchBrackets: true,
                    styleActiveLine: true,
                    extraKeys: {
                        ".": function(editor) {
                            setTimeout(function() {
                                editor.execCommand("autocomplete");
                            }, 100);
                            throw CodeMirror.Pass; // tell CodeMirror we didn't handle the key
                        }
                    },
                    gutters: ["CodeMirror-lint-markers"],
                    lint: true,
                    mode: "text/x-apex"
                });
                editor.on('inputRead', function onChange(editor, input) {
                    if (input.text[0] === ';' || input.text[0] === ' ') {
                        return;
                    }
                    //CodeMirror.commands.autocomplete = function(editor) {
                    editor.showHint({
                        hint: CodeMirror.hint.auto
                    });
                    //};
                });
                globalEditor1 = $('.CodeMirror')[0].CodeMirror;
            }), 2000
        });

Это работает, но после ввода "." Он дает методы этой конкретной переменной, но после ввода нескольких подходящих слов он снова начинает показывать подсказки из исходного набора слов.

, например,: isBatch и isAbort - это два метода класса System.

Когда я начинаю набирать Sy ... Система появляется, затем я набираю ".", И эти два метода отображаются isBatch иisAbort, но когда я набираю isA вместо отображения isAbort, он снова начинает показывать подсказки из полного списка слов.

Есть ли способ избежать этого тоже?

...