Skip to content

Commit

Permalink
整理了搜索范围
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwang committed Dec 23, 2019
1 parent bcd16b8 commit 8a184fc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 31 deletions.
16 changes: 10 additions & 6 deletions src/code/server/codeDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export class CodeDefinition {
}
// 根据VSCode提供的符号位置查询查号名
let symbRet = astContainer.searchDocSymbolfromPosition(info.position);
//全局查找定义
// 全局查找定义
if (symbRet != undefined && symbRet['sybinfo'] != undefined) {
let symbolInfo = symbRet['sybinfo'];
let containerList = symbRet['container'];
//先做一次普通搜索,如果有结果,就以普通搜索结果优先
let symbInstance = this.commonSearch(uri, symbolInfo.name, Tools.SearchMode.ExactlyEqual);
let symbInstance = this.directSearch(uri, symbolInfo.name, Tools.SearchMode.ExactlyEqual);
if(isArray(symbInstance) && symbInstance.length > 0 ){
// 已经搜到了结果
}else{
Expand All @@ -58,7 +58,7 @@ export class CodeDefinition {
let retLoc = Location.create(finalRetSymbols['containerURI'], finalRetSymbols['location'].range);
return retLoc;
} else {
//没找到符号,判断require文件的情况
// 没找到符号,判断require文件的情况
let reqFileName = astContainer.searchDocRequireFileNameFromPosition(info.position);
let uri = Tools.transFileNameToUri(reqFileName);
if(uri.length > 0){
Expand All @@ -68,9 +68,13 @@ export class CodeDefinition {
}
}

private static commonSearch(uri, symbolStr, method){
//做一次普通搜索
return CodeSymbol.searchSymbolforGlobalDefinition(uri, symbolStr, method);
// 直接搜索,其逻辑应该是搜索本文件的所有变量 -> 引用文件的全局变量 -> 所有文件的全局变量
private static directSearch(uri, symbolStr, method){
let ret = CodeSymbol.searchSymbolinDoc(uri, symbolStr, method) || [];
if(ret.length === 0){
ret = CodeSymbol.searchSymbolforGlobalDefinition(uri, symbolStr, method, Tools.SearchRange.GlobalSymbols) || [];
}
return ret;
}

//-----------------------------------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions src/code/server/codeSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ export class CodeSymbol {

// 搜索全局变量的定义,查找顺序是本文件,引用树,全局
// 不优先搜全局,不搜预制
public static searchSymbolforGlobalDefinition (uri:string, symbolStr: string, searchMethod: Tools.SearchMode = Tools.SearchMode.ExactlyEqual, searchRange: Tools.SearchRange = Tools.SearchRange.AllSymbols): Tools.SymbolInformation[] {
public static searchSymbolforGlobalDefinition (uri:string, symbolStr: string, searchMethod: Tools.SearchMode = Tools.SearchMode.ExactlyEqual, searchRange: Tools.SearchRange = Tools.SearchRange.GlobalSymbols): Tools.SymbolInformation[] {
if (symbolStr === '' || uri === '' ) {
return [];
}

let retSymbols: Tools.SymbolInformation[] = [];
//搜索顺序 用户 > 系统
CodeSymbol.alreadySearchList = new Object(); // 记录已经搜索过的文件。避免重复搜索耗时
let preS = this.recursiveSearchRequireTree(uri, symbolStr, searchMethod);
let preS = this.recursiveSearchRequireTree(uri, symbolStr, searchMethod, searchRange);
if(preS){
retSymbols = retSymbols.concat(preS);
}
Expand All @@ -250,7 +250,7 @@ export class CodeSymbol {
let retSymbols: Tools.SymbolInformation[] = [];
//搜索顺序 用户 > 系统
CodeSymbol.alreadySearchList = new Object();
let preS = this.recursiveSearchRequireTree(uri, symbolStr, searchMethod);
let preS = this.recursiveSearchRequireTree(uri, symbolStr, searchMethod, searchRange);
if(preS){
retSymbols = retSymbols.concat(preS);
}
Expand Down Expand Up @@ -389,7 +389,7 @@ export class CodeSymbol {
// @fileName 文件名
// @symbolStr 符号名
// @uri
private static recursiveSearchRequireTree(uri: string, symbolStr, searchMethod :Tools.SearchMode, isFirstEntry:boolean = true){
private static recursiveSearchRequireTree(uri: string, symbolStr, searchMethod :Tools.SearchMode, searchRange:Tools.SearchRange = Tools.SearchRange.AllSymbols, isFirstEntry:boolean = true){
if(!uri || uri === ''){
return [];
}
Expand Down Expand Up @@ -436,7 +436,7 @@ export class CodeSymbol {
// 在引用树上搜索符号,搜索的原则为优先搜索最近的定义,即先搜本文件,然后逆序搜索require的文件,再逆序搜索reference
// 分析自身文件的符号. 本文件,要查找所有符号,引用文件,仅查找global符号。这里要求符号分析分清楚局部和全局符号
let docS = this.docSymbolMap.get(uri);
let retSymbols = docS.searchMatchSymbal(symbolStr, searchMethod, Tools.SearchRange.AllSymbols);
let retSymbols = docS.searchMatchSymbal(symbolStr, searchMethod, searchRange);
if(retSymbols.length > 0){
//找到了,查找全部符号,压入数组
retSymbArray = retSymbArray.concat(retSymbols);
Expand All @@ -445,7 +445,7 @@ export class CodeSymbol {
let reqFiles = docProcessor.getRequiresArray();
for(let idx = reqFiles.length -1; idx >= 0; idx--){
let newuri = Tools.transFileNameToUri(reqFiles[idx]['reqName']);
let retSymbols = this.recursiveSearchRequireTree(newuri, symbolStr, searchMethod, false);
let retSymbols = this.recursiveSearchRequireTree(newuri, symbolStr, searchMethod, searchRange, false);
if(retSymbols != null && retSymbols.length > 0){
retSymbArray = retSymbArray.concat(retSymbols);
}
Expand All @@ -454,7 +454,7 @@ export class CodeSymbol {
let refFiles = docProcessor.getReferencesArray();
for(let idx = refFiles.length -1; idx >= 0; idx--){
let newuri = refFiles[idx];
let retSymbols = this.recursiveSearchRequireTree(newuri, symbolStr, searchMethod, false);
let retSymbols = this.recursiveSearchRequireTree(newuri, symbolStr, searchMethod, searchRange, false);
if (retSymbols != null && retSymbols.length > 0) {
retSymbArray = retSymbArray.concat(retSymbols);
}
Expand Down
54 changes: 36 additions & 18 deletions src/code/server/typeInfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class TypeInfer {
// 循环查tag for definition
private static recursiveSearchTagForDefinition(element, uri, searchPrefix, tailListCache, isStripping = true){
// 找到tag对应的符号。 searchTag函数的本意是找到 findTagRetSymbArray[key] 这个符号的所有对应tag符号,以便继续的合并搜索
let findoutArr = this.searchTag( element, uri ) || [];
let findoutArr = this.searchTag( element, uri, 0 ) || [];
for (const value of findoutArr) {
let uri = value.containerURI;
//[尝试]向上补全, 所以补全这边的 tailListCache 要拷贝
Expand All @@ -56,7 +56,7 @@ export class TypeInfer {
// 循环查tag for conpletion
private static recursiveSearchTagForCompletion(element, uri, searchPrefix, tailListCache, isStripping = true){
// 找到tag对应的符号。 searchTag函数的本意是找到 findTagRetSymbArray[key] 这个符号的所有对应tag符号,以便继续的合并搜索
let findoutArr = this.searchTag( element, uri ) || [];
let findoutArr = this.searchTag( element, uri, 1 ) || [];
if (findoutArr.length > this.maxSymbolCount) findoutArr.length = this.maxSymbolCount;
for (const value of findoutArr) {
let uri = value.containerURI;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class TypeInfer {
tailListCache.push(searchPrefixArray.pop());
let SCHName = searchPrefixArray.join('.');
// 先搜索本文件,如果找不到再搜索调用树
let findTagRetSymbArray = this.searchMethod(uri, SCHName); // 这里的搜索范围?
let findTagRetSymbArray = this.searchMethodforDef(uri, SCHName); // 这里的搜索范围?
// 没找到,继续pop循环
if(!findTagRetSymbArray || findTagRetSymbArray.length == 0) continue;
// 找到了。遍历查tag(循环)
Expand All @@ -100,7 +100,7 @@ export class TypeInfer {
// 从 tailListCache 取出变量忘 searchPrefix上拼接,并查找结果有没有符号,符号有没有tag
let temptailCache = tailListCache.concat();
let newName = searchPrefix + '.' + temptailCache.pop();
let addPrefixSearchArray = this.searchMethod(uri, newName, Tools.SearchMode.ExactlyEqual);// prefix search with no children
let addPrefixSearchArray = this.searchMethodforComp(uri, newName, Tools.SearchMode.ExactlyEqual);// prefix search with no children
if(addPrefixSearchArray.length > this.maxSymbolCount) addPrefixSearchArray.length = this.maxSymbolCount;
for (const element of addPrefixSearchArray) {
if(element.tagType){
Expand Down Expand Up @@ -132,7 +132,7 @@ export class TypeInfer {
tailListCache.push(searchPrefixArray.pop());
let SCHName = searchPrefixArray.join('.');
// 先搜索本文件,如果找不到再搜索调用树
let findTagRetSymbArray = this.searchMethod(uri, SCHName); // 这里的搜索范围?
let findTagRetSymbArray = this.searchMethodforComp(uri, SCHName); // 这里的搜索范围?
// 没找到,继续pop循环
if(!findTagRetSymbArray || findTagRetSymbArray.length == 0) continue;
// 找到了。遍历查tag(循环)
Expand All @@ -147,7 +147,7 @@ export class TypeInfer {
// 从 tailListCache 取出变量忘 searchPrefix上拼接,并查找结果有没有符号,符号有没有tag
let temptailCache = tailListCache.concat();
let newName = searchPrefix + '.' + temptailCache.pop();
let addPrefixSearchArray = this.searchMethod(uri, newName, Tools.SearchMode.PrefixMatch);// prefix search with no children
let addPrefixSearchArray = this.searchMethodforComp(uri, newName, Tools.SearchMode.PrefixMatch);// prefix search with no children
if(addPrefixSearchArray.length > this.maxSymbolCount) addPrefixSearchArray.length = this.maxSymbolCount;
for (const element of addPrefixSearchArray) {
if(element.tagType){
Expand All @@ -165,30 +165,48 @@ export class TypeInfer {
}
}

private static searchMethodCommon(uri, SCHName , method = Tools.SearchMode.ExactlyEqual,operation) {
if(operation === 0){
return this.searchMethodforDef(uri, SCHName, method) || [];
}else if(operation === 1){
return this.searchMethodforComp(uri, SCHName, method)|| [];
}
}

// 普通搜索, local => global, 用什么样的搜索方式ExactlyEqual ,在哪个范围(含预制)
private static searchMethodforComp(uri, SCHName , method = Tools.SearchMode.ExactlyEqual){
let findTagRetSymbArray = CodeSymbol.searchSymbolinDoc(uri , SCHName ,method);
if (findTagRetSymbArray == null || (findTagRetSymbArray &&findTagRetSymbArray.length <= 0)){
findTagRetSymbArray = CodeSymbol.searchSymbolforCompletion(uri ,SCHName, method, Tools.SearchRange.GlobalSymbols) || [];
}
return findTagRetSymbArray;
}

// 普通搜索, local => global, 用什么样的搜索方式ExactlyEqual ,在哪个范围(含预制)
private static searchMethod(uri, SCHName , method = Tools.SearchMode.ExactlyEqual){
private static searchMethodforDef(uri, SCHName , method = Tools.SearchMode.ExactlyEqual){
let findTagRetSymbArray = CodeSymbol.searchSymbolinDoc(uri , SCHName ,method);
if (findTagRetSymbArray == null || (findTagRetSymbArray &&findTagRetSymbArray.length <= 0)){
findTagRetSymbArray = CodeSymbol.searchSymbolforCompletion(uri ,SCHName, method);
findTagRetSymbArray = CodeSymbol.searchSymbolforGlobalDefinition(uri ,SCHName, method, Tools.SearchRange.GlobalSymbols) || [];
}
return findTagRetSymbArray;
}

//搜索tag
private static searchTag(element, uri){
// 搜索tag
// DEF = 0 comp = 1
private static searchTag(element, uri, operation){
let findoutSymbs;
if(element.tagType && (element.tagReason === Tools.TagReason.UserTag || element.tagReason === Tools.TagReason.Equal) ){ //USERTag
findoutSymbs = this.searchUserTag(uri, element)
findoutSymbs = this.searchUserTag(uri, element, operation);
}else if(element.tagType && element.tagReason == Tools.TagReason.MetaTable ){
findoutSymbs = this.searchMetaTable(uri, element);
findoutSymbs = this.searchMetaTable(uri, element, operation);
}else if(element.requireFile && element.requireFile.length > 0){ // 符号源于文件返回值
// let checkName = DotToBlankArr.join('.');
findoutSymbs = this.searchRequire(element);
}else if(element.funcRets){ // 符号源于函数返回值
findoutSymbs = this.searchFunctionReturn(element);
}else if(element.chunk && element.chunk.returnSymbol){
let chunkRet = element.chunk.returnSymbol;
findoutSymbs = CodeSymbol.searchSymbolforCompletion(uri ,chunkRet, Tools.SearchMode.ExactlyEqual);
findoutSymbs = this.searchMethodCommon(uri ,chunkRet, Tools.SearchMode.ExactlyEqual, operation);
}

// 为了避免 local common = common 导致搜索到自己的定义,最终无限循环。
Expand Down Expand Up @@ -245,26 +263,26 @@ export class TypeInfer {

}

private static searchUserTag(uri, element){
private static searchUserTag(uri, element, operation){
let tag_type = element.tagType;
if(tag_type){
return CodeSymbol.searchSymbolforCompletion(uri, tag_type, Tools.SearchMode.ExactlyEqual) || [];
return this.searchMethodCommon(uri, tag_type, Tools.SearchMode.ExactlyEqual, operation);
}else{
return [];
}
}

private static searchMetaTable(uri, element){
private static searchMetaTable(uri, element , operation){
let tag_type = element.tagType + ".__index";
if(tag_type){
// 得到 a.__index 的符号
let index_symbol = CodeSymbol.searchSymbolforCompletion(uri, tag_type, Tools.SearchMode.ExactlyEqual) || [];
let index_symbol = this.searchMethodCommon(uri, tag_type, Tools.SearchMode.ExactlyEqual, operation);
for (const element of index_symbol) {
if(!element.tagType){
continue;
}
let searchName = element.tagType; // a.__index 对应的tag
let tagRes = CodeSymbol.searchSymbolforCompletion(element.containerURI, searchName , Tools.SearchMode.ExactlyEqual);
let tagRes = this.searchMethodCommon(element.containerURI, searchName, Tools.SearchMode.ExactlyEqual, operation);
if(tagRes){
return tagRes;
}
Expand Down

0 comments on commit 8a184fc

Please sign in to comment.