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
}
}
}
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;
}
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With