How to integrate spellcheck in Ace Editor using custom mode

1,522 views
Skip to first unread message

sweety vgs

unread,
Jun 24, 2015, 7:04:58 AM6/24/15
to ace-d...@googlegroups.com

I'm new to ace-editor and I have used ace-rails-ap gem and I have included custom mode to validate my code, every line should end up with semicolon, If semicolon is not present in my query by mistake then the editor should give warning like "Missing Semicolon", by the way it should behave like spell check.


I tried with the following code:


editor.setOption("spellcheck", true)  but spellchecking is not working and I'm seeing a "misspelled option "spellcheck" message in the console.


How to integrate the above check to my custom mode

sweety vgs

unread,
Jun 26, 2015, 8:52:02 AM6/26/15
to ace-d...@googlegroups.com
Any input would be appreciated, Thank you in advance

Harutyun Amirjanyan

unread,
Jun 26, 2015, 9:27:38 AM6/26/15
to ace-d...@googlegroups.com
there is no spellcheck option 
you need to write something like https://github.com/ajaxorg/ace/blob/master/lib/ace/mode/json_worker.js
https://github.com/ajaxorg/ace/blob/master/lib/ace/mode/json.js#L73
that checks lines and sends anootations.​

if the only thing you need is to check for semicolons then do something like

lines = this.doc.getAllLines()
...loop over lines... {
    if (!/;\s*$/.test(lines[i])) {
        errors.push({
            row: i,
            column: lines[i].length,
            text: "Missing Semicolon",
            type: "error"
        });
    }
}
 should work

sweety vgs

unread,
Jun 29, 2015, 7:20:11 AM6/29/15
to ace-d...@googlegroups.com
Hi,

Thanks for the reply. I tried the following code and it has "X" mark appear for first line and enter into a new line then the "X" marker pointing to second line but it clears old annotations ie. first line annotation.
 

$('#editor').on('input', function() {
lines = editor.session.doc.getAllLines();
  for (var i = 0; i < lines.length; i++) {
    if (!/;\s*$/.test(lines[i])) {
     editor.session.setAnnotations([{
          row: i,
          column: lines[i],
          text: "Missing semicolon ;",
          type: "error"
     }]);
    }
  }
  });
editor.session.setOption("useWorker", false)

So, I also addededitor.session.setOption("useWorker", false)but it couldn't resolve it.

Any helps are greatly appreciated as I have spent a day while trying to sort this out.

Harutyun Amirjanyan

unread,
Jun 29, 2015, 7:46:31 AM6/29/15
to ace-d...@googlegroups.com
see the code in json worker, you need to collect errors in an array, and then call setAnnotations with whole array

sweety vgs

unread,
Jul 1, 2015, 6:56:17 AM7/1/15
to ace-d...@googlegroups.com
I tried according to json_worker.js

Added the following script in my custom mode

(function() {
    this.lineCommentStart = "--";
    this.blockComment = {start: "->", end: "<-"};

    this.createWorker = function(session) {
        var worker = new WorkerClient(["ace"], "ace/mode/json_worker", "JsonWorker");
        worker.attachToDocument(session.getDocument());

        worker.on("errors", function(e) {
            session.setAnnotations(e.data);
        });

        worker.on("annotate", function(e) {
            session.setAnnotations(e.data);
        });

        worker.on("terminate", function() {
            session.clearAnnotations();
        });

        return worker;
    };
}).call(Mode.prototype);


var lines = editor.session.doc.getAllLines()
var errors = [];

for (var i = 0; i < lines.length; i++){
    if (!/;\s*$/.test(lines[i])) {
        errors.push({
            row: i,
            column: lines[i].length,
            text: "Missing Semicolon",
            type: "error"
        });
    }
}
 
Now, ace editor shows the X marker for the first line always and if I enter into the second new line it doesn't show the 'X' marker and also if I add the "Semi colon" at the end of the first line, the "X" marker is not getting removed.

Kindly provide the sample code snippet which can be used for custom mode,  so that it would be helpful to get this out.
Reply all
Reply to author
Forward
0 new messages