📄 source-entry.js
字号:
/**
* @public
* @constructor
* @param <String> source
*/
function SourceEntry(source){
var timestamp = new Date().getTime();
this.initialize(source);
this.parse();
this.docEntries = [];
this.docIds = [];
this.docMap = {};
this.filedocs = [];
for(var i=0;i<this.partitions.length;i++){
var p = this.partitions[i];
if(p.type == 'document'){
this.docEntries.push(new DocEntry(this,p.begin,p.end));
}
}
for(var i=0;i<this.docEntries.length;i++){
var doc = this.docEntries[i];
if(doc.isFiledoc()){
this.filedocs.push(doc);
}else{
var id = doc.getId();
this.docMap[id] = doc;
this.docIds.push(id);
}
}
this.timeSpent = new Date().getTime()-timestamp;
//$log.info(this.timeSpent);
}
SourceEntry.sourceEntryMap = {};
SourceEntry.require = function(file,pkg){
if(pkg){
file = $JSI.Package.require(pkg).scriptBase+file;
}
var source = this.sourceEntryMap[file];
if(!source){
var text = $JSI.loadText(file);
source = this.sourceEntryMap[file] = new SourceEntry(text);
}
return source;
}
SourceEntry.prototype = new ECMAParser();
SourceEntry.prototype.getDescription = function(id){
if(!("_description" in this)){
var infos = [];
for(var i = 0;i<this.filedocs.length;i++){
infos.push(this.filedocs[i].description || '');
}
this._description = infos.join('\r\n');
if(infos.length == 0 && this.partitions.length){
var t = this.partitions[0];
if(t.type == "comment"){
this._description = t.value.replace(/^\s*\/\//,'');
}else if(t.type == "muti-comment"){
this._description = t.value.replace(/(?:^\s*\/\*)|(?:\*\/\s*$)/g,'').replace(/^\s*\*\s?/gm,'');
}
}
}
return this._description;
}
SourceEntry.prototype.getDocEntry = function(id){
//$log.info(id,this.docMap[id],this.docMap);
return this.docMap[id];
}
SourceEntry.prototype.getTopDocEntries = function(id){
var rtv = [];
for(var i = 0;i<this.docEntries.length;i++){
if(this.docEntries[i].isTop()){
rtv.push(this.docEntries[i]);
}
}
return rtv;
}
function DocEntry(sourceEntry,start,end){
this.sourceEntry = sourceEntry;
this.source = sourceEntry.source;
this.start = start;
this.end = end;
this.tagAttributes = {'constructor':null};
this.sourceAttributes = {'constructor':null};
this.parseSource();
this.parseDoc();
}
DocEntry.prototype.getStaticMemberMap = function(){
if(!this.staticMenberMap){
var id = this.getId();
var ids = this.sourceEntry.docIds;
this.staticMenberMap = {};
var mp = new RegExp("^"+id.replace(/([\$\.])/g,'\\$1')+"\\.([^\\.]+)$")
for(var i=0;i<ids.length;i++){
if(mp.test(ids[i])){
//alert(ids[i])
this.staticMenberMap[ids[i].replace(mp,"$1")] = this.sourceEntry.getDocEntry(ids[i]);
}
}
}
return this.staticMenberMap
}
DocEntry.prototype.getInstanceMemberMap = function(){
if(!this.instanceMemberMap){
var id = this.getId();
var ids = this.sourceEntry.docIds;
this.instanceMemberMap = {};
var mp = new RegExp("^"+id.replace(/([\$\.])/g,'\\$1')+"\\.prototype\\.([^\\.]+)$")
//alert(mp.source)
for(var i=0;i<ids.length;i++){
if(mp.test(ids[i])){
//alert(ids[i])
this.instanceMemberMap[ids[i].replace(mp,"$1")] = this.sourceEntry.getDocEntry(ids[i]);
}
}
}
return this.instanceMemberMap
}
DocEntry.prototype.toString = function(){
var buf = "sourceId="+this.sourceAttributes.name+"\n";
buf += "description="+this.description+"\n";
for(var key in this.tagAttributes){
buf+= "@"+key+":"+this.tagAttributes[key]+"\n";
}
return buf+"\n\n\n";
}
DocEntry.prototype.parseDoc = function(){
this.doc = this.source.substring(this.start,this.end);
var pp = /^\s*\*\s*(?:@([\w\d\-_]+))?\s*((?:(?:{@)|[^@])*)$/gm;
var m = null;
while(m = pp.exec(this.doc)){
var part = m[0];
var tag = m[1];
var content = m[2];
if(content){
content = content.replace(/(?:\s*\*\/\s*$)|(?:^\s*\*\s?)/gm,'');
}
if(tag){
switch(tag){
case 'public':
case 'private':
case 'protected':
this.processTag("access",tag);
break;
default:
this.processTag(tag,content);
}
}else{
this.description = content;
}
}
};
DocEntry.prototype.tagAlias = function(){
var list = [accessTag,flagTag,valueTag,valuesTag];
var rtv = {'constructor':null};
for(var i = 0;i<list.length;i++){
for(var n in list[i]){
var info = list[i][n];
if(info.alias){
for(var j = 0;j<info.alias.length;j++){
rtv[info.alias[j]] = n;
}
}
}
}
return rtv;
}();
DocEntry.prototype.processTag = function(tag,value){
if(this.tagAlias[tag]){
tag = this.tagAlias[tag];
}
var values = this.tagAttributes[tag];
if(values == null){
values = this.tagAttributes[tag] = [];
}
values.push(value);
}
var FUN_1 = /^\s*function\s+[\w\$]+\s*\([\w\$,\s]*\)/;
var FUN_2 = /^\s*function\s*\([\w\$,\s]*\)/;
var OBJ_1 = /^\s*var\s+[\w\$]+\s*=/;
var OBJ_2 = /^\s*[\w\$\.]+\s*=/;
var OBJ_3 = /^\s*[\w\$]+\s*:/;
DocEntry.prototype.parseSource = function(){
var source = this.source.substr(this.end);
var name,type,instance;
if(FUN_1.test(source)){
var p = source.indexOf("function");
var p1 = source.indexOf("(");
var p2 = source.indexOf(")");
name = source.substring(p+"function".length,p1).replace(/\s*/g,'');
type = "function";
}else{
if(OBJ_1.test(source)){//var xxx
var p = source.indexOf("=");
name = source.substring(source.indexOf("var")+3,p).replace(/\s*/g,'');
}else if(OBJ_2.test(source)){
var p = source.indexOf("=");
name = source.substring(0,p).replace(/\s*/g,'');
}else if(OBJ_3.test(source)){
var p = source.indexOf(":");
name = source.substring(0,p).replace(/\s*/g,'');
}
source = source.substr(p+1);
if(FUN_2.test(source)){
type = "function";
}else if(/^\s*(\[|\{|(new\s+))/.test(source) ){
type = "object";
}
}
if(name && name.length != (name = name.replace(/^this\./,'')).length){
this.sourceAttributes.instanceAttribute = true;
}
this.sourceAttributes.name = name;
this.sourceAttributes['typeof'] = type;
};
DocEntry.prototype.getAttribute = function(key){
var values = this.tagAttributes[key];
if(values){
return values[values.length-1];
}else{
return null;
}
};
DocEntry.prototype.getAttributeType = function(){
return this.getAttribute('attributeType');
}
DocEntry.prototype.getExtend = function(){
var e =this.getAttribute('extend');
if(e){
return e.replace(/^\s*([\w\.]+)[\s\S]*$/,'$1');
}
};
DocEntry.prototype.isConstructor = function(){
//$log.info(this.tagAttributes['constructor']);
return this.tagAttributes['constructor'] != null;
};
DocEntry.prototype.getInstanceof = function(){
return this.getAttribute('instanceof');
};
DocEntry.prototype.getTypeof = function(){
return this.getAttribute('typeof');
};
DocEntry.prototype.getAccess = function(){
return this.getAttribute('access');
};
DocEntry.prototype.getOwner = function(){
var o = this.getAttribute('owner');
if(o){
return o.replace(/([\w\d\.\$\_]*)[\s\S]*$/,'$1');
}else{
return null;
}
};
DocEntry.prototype.getParams = function(){
return this.tagAttributes["param"];;
}
DocEntry.prototype.getDescription = function(){
return this.description;
}
DocEntry.prototype.getArguments = function(){
return this.getAttribute('arguments');
}
DocEntry.prototype.getReturn = function(){
return this.getAttribute('return');
}
DocEntry.prototype.getReturnType = function(){
return this.getAttribute('returnType');
}
DocEntry.prototype.getStatic = function(){
return this.tagAttributes['static'] && true;
};
DocEntry.prototype.getName = function(){
var n = this.getAttribute('name');
if(n){
return n;
}
return this.sourceAttributes['name'];
};
DocEntry.prototype.isFileoverview = function(){
return this.getAttribute('fileoverview') && true;
};
DocEntry.prototype.getId = function(){
if(this.id){
return this.id;
}
var name = this.getName();
if(!name){
return null;
}
var owner = this.getOwner();
if(this.sourceAttributes.instanceAtribute){
//TODO...
/*
if(!owner){
if(this.owner == undefined){
var des = this.sourceEntry.docEntries;
for(var i = des.length-1;i>=0;i--){
var doc = des[i];
if(doc == this){
for(i--;i>=0;i--){
var doc = des[i];
if(doc.isConstructor()){
owner = this.owner = doc.getId();
}else{
owner = doc.getOwner();
if(owner){
this.owner = owner;
}else{
doc.getId();
owner = this.owner = doc.owner;
}
}
}
}
}
}
}
*/
}
if(owner){
// || !(this.getConstructor() || /^\s*'?"?function/.test(this.getType()))
if(!this.sourceAttributes.instanceAtribute && this.getStatic()){
this.id = owner+"."+name;
}else{
this.id = owner+".prototype."+name;
}
return this.id;
}
return this.id = name;;
}
DocEntry.prototype.isTop = function(){
if(this.isConstructor()){
return true;
}else{
var id = this.getId();
return this.getOwner()==null&&id!=null && id.indexOf('.')<0;
}
}
DocEntry.prototype.isFiledoc = function(){
return this.isFileoverview() || this.getId() == null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -