试图让JavaScript代码完成工作在netbeans与jsdoc

Trying to get JavaScript code completion to work in netbeans with jsdoc

本文关键字:工作 netbeans jsdoc JavaScript 代码      更新时间:2023-09-26

在我的app.js中,我有以下内容:

angular.module('app').controller('userList', 
  ['appSettings'
 ,function (/*@type {app.appSettings}*/appSettings) {  
   appSettings.<<== it shows a list here but nothing from autocomplete.js

在我的autocomplete.js中,我有以下内容(由JavaScript打印出我的服务及其成员生成):

var app={};
app.appSettings={};
app.appSettings.userFailMessages={};
app.appSettings.userFailMessages.invalidJson
  ="Invalid request, user sent is not valid json.";

NetBeans拒绝为我编写完整的appSettings,似乎不知道它是在autocomplete.js中定义的。也许我得到了我的js文档错误,但尝试了@var, @type和@param的组合组合没有成功。

当我输入app.appSettings.并从autocomplete.js给我一个列表时,它的代码完成了,但我想知道如何告诉NetBeans传递给函数的参数是app.appSettings。

也许我应该让自动完成包含构造函数而不是对象字面值,因为@type暗示了某种类型而不是实例。

是NetBeans 7.3.1

接近答案,要让NetBeans使用类型,您必须定义类型。然后,为了表明传递给angular模块(或任何函数)的参数是某种类型,我使用@param jsdoc

angular模块:

angular.module('app').controller('userList'
 , ['$scope','appRules','appSettings'
 ,/**
* @param {app.appRules} appRules
* @param {app.appSettings} appSettings
* */
 function ($scope,appRules,appSettings,$timeout) {
   //<== here both appRules and appSettings give suggestions
   //  from autocomplete

autocomplete.js(不包含在我的HTML文件中,但只是在那里的代码建议)

/*@typedef {Object} app*/
var app={};
app.appRules={};
app.appRules.userIsInRole=function (user,role){};
app.appRules.general={};
app.appRules.general.isEmpty=function (val){};
app.appRules.general.isEmail=function (val){};
app.appSettings={};
app.appSettings.userFailMessages={};
app.appSettings.userFailMessages.invalidJson
 ="Invalid request, user sent is not valid json.";
app.appSettings.userFailMessages.noPrivilege
 ="You do not have the privileges needed to change this user.";

我在控制台的一个包含我的应用程序的页面上运行了以下代码来生成autocomplete.js:

var inj;
function allServices(mod, r) {
  if (!r) {
    r = {};
    inj = angular.element(document.querySelector('[data-ng-app]')).injector().get;
  }
  angular.forEach(angular.module(mod).requires, function(m) {
    allServices(m, r)
  });
  angular.forEach(angular.module(mod)._invokeQueue, function(a) {
    try {
      r[a[2][0]] = inj(a[2][0]);
    } catch (e) {
    }
  });
  return r;
};
var output=[];
function addOutput(names,prop){
  if(names.length===1){
    output.push('var ');
  }
  output.push(names.join('.'));
  if(typeof prop === 'object'){
    output.push('={};'n');
    for(thing in prop){
      //TODO: no arrays or route paths
      if(/[0-9'/'']/.test(thing)){
        continue;
      }
      names.push(thing);
      addOutput(names,prop[thing]);
    }
  }else{
    output.push('=');
    output.push(
      (typeof prop === 'function')?
      prop.toString():
      JSON.stringify(prop)
    );
    output.push(';'n');
  }
  names.pop();
}
function createOutput(){
  allMyServices = allServices('app');
  addOutput(['app'],allMyServices);
  console.log(output.join(''));
}

createOutput();