admin管理员组

文章数量:1323509

I am trying to print autopletion 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": "pletions",
            "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 autoplete. Am i missing something.

Also this is the .tern-project file

{
  "ecmaVersion": 6,
  "libs": [
    "browser",
    "jquery",
    "requirejs",
    "monjs"
  ],
  "plugins": {
    "plete_strings": {
      "maxLength": 15
    },
    "node": {},
    "lint": {},
    "doc_ment": {
      "fullDocs": true,
      "strong": true
    }
  }
}

I am trying to print autopletion 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": "pletions",
            "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 autoplete. Am i missing something.

Also this is the .tern-project file

{
  "ecmaVersion": 6,
  "libs": [
    "browser",
    "jquery",
    "requirejs",
    "monjs"
  ],
  "plugins": {
    "plete_strings": {
      "maxLength": 15
    },
    "node": {},
    "lint": {},
    "doc_ment": {
      "fullDocs": true,
      "strong": true
    }
  }
}
Share edited Aug 23, 2016 at 3:33 Abhishek Sharma asked Aug 10, 2016 at 12:17 Abhishek SharmaAbhishek Sharma 3,3401 gold badge22 silver badges38 bronze badges 1
  • worth adding error handling. console.log(error);. It can give you a clue to what failed. – omer727 Commented Oct 15, 2016 at 19:53
Add a ment  | 

2 Answers 2

Reset to default 3

The autoplete 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 pletions that way.

curl -H "Content-Type:e": "pletions","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": "pletions",
        "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": "pletions","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]

本文标签: javascriptTernjs for nodejs autocompleteStack Overflow