Skip to content

Creating Tern Plugins

angelozerr edited this page May 12, 2015 · 10 revisions

Tern Toolings are for helping with extending Tern IDE, and not for Tern IDE users.

See https://github.com/marijnh/tern/issues/443
https://github.com/angelozerr/tern-jug tern plugin example

Tern (Server) Plugin

Server plugins gives you the capability to generate a simple tern plugin.

Tern (Server) Plugin generation

Open the wizard selection (Ctrl+N) and select Tern / Server plugin :

Server plugin

Click on Next button and choose as container a project which has tern nature (Configure / Convert to Tern project...)

Server plugin

After click on Finish button, a tern plugin is generated in the project root folder :

Server plugin

Server Plugin using

At this step, our tern plugin is generated inside root project and looks like this :

(function(mod) {
  if (typeof exports == "object" && typeof module == "object") { // CommonJS
    var path = require("path"), ternDir = path.resolve(process.argv[1], "..");
    return mod(require(ternDir + "/../lib/tern"), require(ternDir + "/../lib/tern"));
  }
  if (typeof define == "function" && define.amd) // AMD
    return define([ "tern/lib/infer", "tern/lib/tern" ], mod);
  mod(tern, tern);
})(function(infer, tern) {
  "use strict";

  tern.registerPlugin("mylibrary", function(server, options) {

    return {
      defs : defs
    };
  });
  
  var defs = {
	  "!name": "mylibrary",
	  "!define": {
	    "point": {
	      "x": "number",
	      "y": "number"
	    }
	  },
	  "MyConstructor": {
	    "!type": "fn(arg: string)",
	    "staticFunction": "fn() -> bool",
	    "prototype": {
	      "property": "[number]",
	      "clone": "fn() -> +MyConstructor",
	      "getPoint": "fn(i: number) -> point"
	    }
	  },
	  "someOtherGlobal": "string"
  }

});

Now we will use it.

Enable loading local plugin

By default, the JS files which are hosted in the root project, cannot be used as tern plugin. To use our generated tern plugin, we must enable loading local plugin :

Server plugin

Close the project properties page, reopen it, go to Tern / Modules and check your generated tern plugin :

Server plugin

Create a JS file and test the generated tern plugin :

Server plugin

Some information to rewrite ...

This wiki is to summarize knowledge.
Please create issues for particular problems.

Tern plugins and JSON Tern definition are generally references as modules inside tern.java project. #63

TODO see #19 #20 #43 #125 and marijnh/tern#443

Since 0.7.0 plugin list is not hard-coded and you can use your own tern repository.

Clone this wiki locally