Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternjs for nodejs autocomplete

I am trying to print autocompletion of a js file using nodejs and tern. Ternjs has the worst documentation i have ever seen.

var tern = require("tern")

var ternServer = new tern.Server({})

var requestDetails = {
        "query": {
            "type": "completions",
            "file": "myfile.js",
            "end": {"line":0,"ch":3},
            "types":true,
            "includeKeywords":true,
            "sort":true,
            "guess":true,
            "docs":true,
            "urls":true,
            "origins":true,
            "lineCharPositions":true,
            "caseInsensitive":true
        },
        "files": [
            {
                "type": "full",
                "name": "myfile.js",
                "text": 'req'
            }
        ]
}

ternServer.request(requestDetails, function(error, success){
    console.log(success);
});

Its not working though if I use con it provided continue and const. But not after that. While in atom plugin it provided require module autocomplete. Am i missing something.

Also this is the .tern-project file

{
  "ecmaVersion": 6,
  "libs": [
    "browser",
    "jquery",
    "requirejs",
    "commonjs"
  ],
  "plugins": {
    "complete_strings": {
      "maxLength": 15
    },
    "node": {},
    "lint": {},
    "doc_comment": {
      "fullDocs": true,
      "strong": true
    }
  }
}
like image 818
Abhishek Sharma Avatar asked Aug 10 '16 12:08

Abhishek Sharma


2 Answers

The autocomplete libraries are not loaded when you start the server in this way. Simply defining them in the .tern_project file doesn't seem to work.

If you start the server using node_modules/tern/bin/tern, you'll get a port then you can successfully POST a request and get the completions that way.

curl -H "Content-Type:e": "completions","file": "myfile.js","end": {"line":0,"ch":3},"types":true,"includeKeywords":true,"sort":true,"guess":true,"docs":true,"urls":true,"origins":true,"lineCharPositions":true,"caseInsensitive":true},"files": [{"type": "full","name": "myfile.js","text": "req"}]}' http://localhost:[PORT]

If that doesn't work for you, you can manually add the def files like so.

var tern = require("tern");
var fs = require("fs");

var ternServer = new tern.Server({ "async": true, "defs": findDefs()})
var requestDetails = {
    "query": {
        "type": "completions",
        "file": "myfile.js",
        "end": { "line": 0, "ch": 3 },
        "types": true,
        "includeKeywords": true,
        "sort": true,
        "guess": true,
        "docs": true,
        "urls": true,
        "origins": true,
        "lineCharPositions": true,
        "caseInsensitive": true,
    },
    "files": [{
        "type": "full",
        "name": "myfile.js",
        "text": 'req'
    }]
}

ternServer.request(requestDetails, function(error, success) {
    console.log(success);
});

function findDefs() {
  var defs = [];
  defs.push(JSON.parse(fs.readFileSync("node_modules/tern/defs/ecmascript.json", "utf8")));
  defs.push(JSON.parse(fs.readFileSync("node_modules/tern/defs/browser.json", "utf8")));
  defs.push(JSON.parse(fs.readFileSync("node_modules/tern/defs/jquery.json", "utf8")));
  defs.push(JSON.parse(fs.readFileSync("node_modules/tern/defs/underscore.json", "utf8")));
  return defs;
}
like image 184
Larry Turtis Avatar answered Nov 18 '22 01:11

Larry Turtis


If you start the server using node_modules/tern/bin/tern, it will show a port then you can successfully POST a request and get the result.

curl -H "Content-Type:e": "completions","file": "myfile.js","end": {"line":0,"ch":3},"types":true,"includeKeywords":true,"sort":true,"guess":true,"docs":true,"urls":true,"origins":true,"lineCharPositions":true,"caseInsensitive":true},"files": [{"type": "full","name": "myfile.js","text": "req"}]}' http://localhost:[PORT]
like image 39
utpal ranjan Avatar answered Nov 18 '22 01:11

utpal ranjan