简体   繁体   中英

Is there a way to force NetBeans JavaScript Autocomplete?

I am developing some JavaScript to be used in a CMS I'm working on.

I have encapsulated my code like this:

(function(){
    var libraryName = {};
    ...code...
    window.libraryName = libraryName;
}())

Now when I add a subnamespace and try using it outside my declaration, the NetBeans (8.0.2) autocomplete function doesn't work. Like this:

(function(){
    var libraryName = {};
    libraryName.subSet = {
            showSomething: function(){}
    };
    window.libraryName = libraryName;
}())
libraryName.subSet.showSomething(); // This works
libraryName.subSet. // No current autocomplete even when pressing CTRL+space

I would like to know if there is some way to tell NetBeans how to autocomplete instead of it guessing.

Thanks

You can use Ctrl+K , the "hippie" code completion. It directly completes some matching result and if the completed item is not what you wanted, you can keep pressing Ctrl+K to get another autocompleted item (will replace the previously inserted one). Another thing, you can press Ctrl+Space 2 times to get "full" code completion (meaning pretty much everything from other objects/variables)

Update: There is another way using JSDoc, but it works only in Dev build of NetBeans and will be part of the next 8.1 release (you can download Dev builds from here ):

/**
 * @typedef libraryName
 * @property {Function} showSomething description
 * @property {someProp} foo description
 */

/**
 * @typedef someProp
 * @property {Date} day description
 * @property {Number} num description
 */
/**
 * @typedef libraryName.someProp2
 * @property {Date} day description
 * @property {Number} num description
 */

This way you'd have to create this "documentation" for your library and have it somewhere in JS file in your project (perhaps non-minified JS file of your library). With this @typedef feature, you can learn code completion pretty much anything even if it is not even in your code). Of course there are some issues yet to be fixed (it is a Dev build)...

I tried another approach which worked for me.

I copied my JavaScript file and removed the encapsulation. So I now have two files, the "real" one with the encapsulation and another "working" one that doesn't have the encapsulation. Now when I try using the autocomplete it works.

The downside for this is that you create noise since there is a file that isn't meant for the web app and you have to update it every time you update the original file. But it makes coding easier with the magic of autocomplete. When you load html you just don't reference the "working" file.

So, this would be my main.js file (in /js/main.js for instance)

(function(){
var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};
window.libraryName = libraryName;
}())

And a main.tmp.js file would be like this (in /tmp/main.tmp.js for instance)

var libraryName = {};
libraryName.subSet = {
        showSomething: function(){}
};

Now, when I do libraryName.subSet. it shows me the correct autocomplete with showSomething .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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