简体   繁体   English

用于 nodejs 自动完成的 Ternjs

[英]Ternjs for nodejs autocomplete

I am trying to print autocompletion of a js file using nodejs and tern.我正在尝试使用 nodejs 和 tern 打印 js 文件的自动完成。 Ternjs has the worst documentation i have ever seen. Ternjs 有我见过的最糟糕的文档。

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.虽然如果我使用 con 它提供了 continue 和 const,但它不起作用。 But not after that.但在那之后就没有了。 While in atom plugin it provided require module autocomplete.在 atom 插件中,它提供了 require 模块自动完成功能。 Am i missing something.我是不是错过了什么。

Also this is the .tern-project file这也是 .tern-project 文件

{
  "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.简单地在 .tern_project 文件中定义它们似乎不起作用。

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.如果您使用 node_modules/tern/bin/tern 启动服务器,您将获得一个端口,然后您可以成功发布请求并以这种方式获得完成。

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.如果这对您不起作用,您可以像这样手动添加 def 文件。

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;
}

如果您使用 node_modules/tern/bin/tern 启动服务器,它将显示一个端口,然后您就可以成功 POST 请求并获得结果。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM