2016-08-10 15 views
15

Próbuję wydrukować autouzupełnianie pliku js przy użyciu nodejs i tern. Ternjs ma najgorszą dokumentację, jaką kiedykolwiek widziałem.Ternjs dla autouzupełniania nodejs

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

To nie działa, jeśli użyję con, pod warunkiem, że kontynuuję i const. Ale nie później. W dołączonej wtyczce atomowej wymaga autouzupełniania modułu. Czy czegoś brakuje?

Także to jest plik .tern-projekt

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

Warto dodać obsługę błędów. console.log (błąd) ;. Może dać ci wskazówkę, co się nie udało. – omer727

Odpowiedz

3

Biblioteki autouzupełniania nie są ładowane podczas uruchamiania serwera w ten sposób. Po prostu ich zdefiniowanie w pliku .tern_project nie działa.

Jeśli uruchomisz serwer za pomocą node_modules/tern/bin/tern, otrzymasz port, a następnie z powodzeniem możesz POST zażądać i uzyskać ukończenia w ten sposób.

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] 

Jeśli to nie działa, możesz ręcznie dodać pliki def, tak jak to.

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; 
} 
Powiązane problemy