function (editor, currentPos) {
    return editor.getTokenAt(currentPos);
}

I am designing a code editor for a custom language. So, I want do autocompletion based on mode state. I want to get token before . like when we press CTRL + space during hello. Then, my method should give me hello so I can suggest some properties related to it. How can I do this?

2

There are 2 answers

2
Marijn On

The javascript-hint addon does something like this, continuing to get the previous token in a loop until it finds something that is not a property. You probably want code that looks similar.

0
hugoruscitti On

I have a similar issue, and i ends using a chunk of code to get the current line and then split'em to get the last token (and the . separator):

autocomplete(cm) {
   let full_line = cm.getValue();
   let end = cm.getCursor().ch;
   full_line = full_line.substr(0, end);
   let start = full_line.lastIndexOf(" ");

   if (start === -1) {
     start = 0;
   }

   let currentWord = full_line.substr(start, end - start);
   console.log(currentWord);

[...]

You can see the complete code in my ember-plugin: https://github.com/hugoruscitti/ember-cli-jsconsole/blob/master/tests/dummy/app/controllers/application.js#L5