gpt4 book ai didi

javascript - 用于 nodejs 自动完成的 Ternjs

转载 作者:IT老高 更新时间:2023-10-28 23:03:38 24 4
gpt4 key购买 nike

我正在尝试使用 nodejs 和 tern 打印 js 文件的自动完成。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);
});

如果我使用 con,它提供了 continue 和 const,但它不起作用。但不是在那之后。在 atom 插件中,它提供了 require 模块自动完成功能。我是不是错过了什么。

这也是 .tern-project 文件

{
"ecmaVersion": 6,
"libs": [
"browser",
"jquery",
"requirejs",
"commonjs"
],
"plugins": {
"complete_strings": {
"maxLength": 15
},
"node": {},
"lint": {},
"doc_comment": {
"fullDocs": true,
"strong": true
}
}
}

最佳答案

以这种方式启动服务器时,不会加载自动完成库。简单地在 .tern_project 文件中定义它们似乎不起作用。

如果您使用 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]

如果这对您不起作用,您可以像这样手动添加 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;
}

关于javascript - 用于 nodejs 自动完成的 Ternjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com