ternjs / tern_for_vim

Tern plugin for Vim
MIT License
1.83k stars 100 forks source link

Omnicompletion not working as expected #41

Closed derekleverenz closed 11 years ago

derekleverenz commented 11 years ago

As quick preface, I am not sure if this is a bug -- I may simply be expecting something from tern/tern_fo_vim that it isn't meant to do (also this might not be specific to vim)

Anyway, I have a large javascript project where individual files create objects and attach them to a global namespace. Here is a simplistic example of the structure (in concatenated form, as it would be in a browser, but it works the same if each part is in separate files in vim+tern_for_vim):

// file1.js
window.Namespace = {};

// file2.js
(function (Namespace) {
    "use strict";

    var ObjectOne = function () {
        this.val1 = 10;
        this.f1 =  function (arg1, arg2) {
            return "something";
        };
        this.f2 = function () {
            return 5;
        };
        this.f3 = function () {
            return new Array();
        };
    };

    Namespace.ObjectOne = ObjectOne;
})(window.Namespace);

// file3.js
(function (Namespace) {
    "use strict";

    // make an ObjectOne
    var objOne = new Namespace.ObjectOne();

    // this works (outputs "something" to console)
    console.log(objOne.f1());

})(window.Namespace);

if run in a browser, this works as expected. However, tern's onmicompletion doesn't seem to be know about the properties (val1, f1, ...) of objectOne unless they are called within the file2.js function, and even then doesn't know any type information:

screen shot 2013-10-03 at 11 12 11 am

In the above example, I expect that val1 and f2 also appear as autocomletion options, and ideally also that those have type information.

Am I simply misunderstanding functionality, or is this a real issue? It seems like there might be some issue with introspecting the way I am doing namespaces.

derekleverenz commented 11 years ago

As an addendum, this also seems to prevent jumping to definition. I'm not sure if this issue should be here or in the main tern project

marijnh commented 11 years ago

If I paste your example into http://ternjs.net/doc/demo.html , it works fine.

Most likely, the problem is that you aren't loading the browser environment, and thus window isn't bound to the top scope. Create a .tern-project file to tell tern which environment to load. For example:

{"libs": ["browser"]}
derekleverenz commented 11 years ago

Oh thanks, I did forget that. I had that in my main project but I think there was a problem with the way my project was configured, I was able to fix it though. Thanks!