Ajax php linter

My php-lint addon code :

(function(mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(require("../../lib/codemirror"));
  else if (typeof define == "function" && define.amd) // AMD
    define(["../../lib/codemirror"], mod);
  else // Plain browser env
    mod(CodeMirror);
})(function(CodeMirror) {
"use strict";

    CodeMirror.registerHelper("lint", "xml", function (text , options , cm){
        var found = [];
        if (cm.doc.mode.name != 'php' || typeof $ == 'undefined') return;
            $.post('index.php', {
                'operation' : 'lint',
                'content' : text
            })
            .done(function(results){
                if(!results) return;
                for(var i = 0 ; i < results.length ; i++){
                    var r = results[i];
                    r.from = CodeMirror.Pos(r.from - 1 , 1);
                    r.to = CodeMirror.Pos(r.to - 1 , 1);
                }                
                found = results;
            });
        return found;
    });    
});

The problem is the helper function return “found” varibale before ajax request done !

My solution is by modifying function “startLinting” in “lint.js” file to be like :

function startLinting(cm) {
    var state = cm.state.lint, options = state.options;
    var passOptions = options.options || options; // Support deprecated passing of `options` property in options
    var getAnnotations = options.getAnnotations || cm.getHelper(CodeMirror.Pos(0, 0), "lint");
    if (!getAnnotations) return;
    if (options.async || getAnnotations.async) {
        lintAsync(cm, getAnnotations, passOptions)
     } else {
    var annotations =  getAnnotations(cm.getValue(), passOptions, cm);
    if(annotations && annotations.length) 
        updateLinting(cm, annotations);
    else
        getAnnotations(cm.getValue(), passOptions, cm , updateLinting);
    }
}

The new addon code :

(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
        mod(require("../../lib/codemirror"));
    else if (typeof define == "function" && define.amd) // AMD
        define(["../../lib/codemirror"], mod);
    else // Plain browser env
        mod(CodeMirror);
    })(function(CodeMirror) {
    "use strict";
    
    CodeMirror.registerHelper("lint", "xml", function(text , options , cm , lintUpdater){
        if (cm.doc.mode.name != 'php' || !lintUpdater || typeof $ == 'undefined') return;
            $.post('index.php', {
                'operation' : 'lint',
                'content' : text
            })
            .done(function(results){
                if(!results) return;
                for(var i = 0 ; i < results.length ; i++){
                    var r = results[i];
                    r.from = CodeMirror.Pos(r.from - 1 , 1);
                    r.to = CodeMirror.Pos(r.to - 1 , 1);
                }                
                lintUpdater(cm, results);
            });
        });
    });

Is there any solution else ?
Thanks in advance and sorry for bad english

You can indicate that a linter function is asynchronous by giving it an async property of true (i.e. CodeMirror.lint.xml.async = true). This changes the parameters it expects to (text, callback, options). It should call callback with the array of warnings.

1 Like

Thanks a lot , i’ll try that