Skip to content

Commit

Permalink
fix:修复了代码补全时会修改到符号的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwang committed Dec 30, 2019
1 parent 55d32c7 commit 7a7b285
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 63 deletions.
53 changes: 19 additions & 34 deletions src/code/server/codeCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,25 @@ export class CodeCompletion {
userInputString = userInputString.replace(/:/g,"."); //因为chunk,符号列表中的: 都被转换为 . 这里也要转换,否则查不到
// 先对[用户的完整输入]做一次[直接搜索]
let searchResArray = this.commonCompletionSearch(uri, userInputString) || []; // 这里搜索的范围应该是用户代码, 所有预制文件
// 如果用户输入字段中含有分隔符[.:], 准备分段处理,检索tag
// 保存Completion信息的数组
let retCompletionArray;
// 如果用户输入字段中含有分隔符[.:], 准备分段处理, 检索tag
let userInputSplitArr = this.splitStringwithTrigger(userInputString); //userInputSplitArr 用户输入的字符串
if(userInputSplitArr && userInputSplitArr.length > 1){
if(searchResArray.length === 0){
// 使用类型推导
let lastPrefixSearchRet = TypeInfer.SymbolTagForCompletionEntry(uri, userInputString) || [];
if(lastPrefixSearchRet.length > 0){
lastPrefixSearchRet = this.keepSuffix(lastPrefixSearchRet);
}else{
// 类型推导也没有搜索到的处理方式 -STUART TODO- 使用最后一个字符直接搜索是否有必要?
// let lastPrefix = Tools.splitToArrayByDot(userInputString).pop();
// if(lastPrefix != ''){
// lastPrefixSearchRet = this.commonCompletionSearch(uri, lastPrefix) || [];
// }
}
searchResArray = searchResArray.concat(lastPrefixSearchRet);
}else{
// 把带有分隔符,的直搜结果去除前缀,仅保留要补全的后缀
searchResArray = searchResArray.concat(this.keepSuffix(searchResArray));
}
// 用户输入中含有分隔符[.:]
let lastPrefixSearchRet = TypeInfer.SymbolTagForCompletionEntry(uri, userInputString) || [];
searchResArray = searchResArray.concat(lastPrefixSearchRet);
// 把带有分隔符,的直搜结果去除前缀,仅保留要补全的后缀
retCompletionArray = this.symbolToCompletionArray(searchResArray, true);
}else{
// 处理直接搜索到的, 不含分隔符[.:]
retCompletionArray = this.symbolToCompletionArray(searchResArray);
}

// 处理搜索到的符号
let retCompletionArray = this.symbolToCompletionArray(searchResArray);
let retCompleteItem = this.completeItemDuplicateRemoval(retCompletionArray);
return retCompleteItem;
let retCompletionItem = this.completeItemDuplicateRemoval(retCompletionArray);
return retCompletionItem;
}

// 把用户输入的前缀去除,仅保留要补全的后缀
private static keepSuffix(symbolsArray) {
for (const key in symbolsArray) {
const element = symbolsArray[key];
let userInputSplitArr = this.splitStringwithTrigger(element.searchName);
element.searchName = userInputSplitArr.pop();
}
return symbolsArray;
}


private static fmtParamToSnippet(paramArray: string[]): string {
let snippet = '(' + paramArray.map((param, i) => `\${${i + 1}:${param}}`).join(', ') + ')';
return snippet;
Expand Down Expand Up @@ -130,7 +110,8 @@ export class CodeCompletion {
}

// 把符号数组转换为VSCode能够识别的补全数组
private static symbolToCompletionArray(retSymb){
// @onlyKeepPostfix 仅保留后缀
private static symbolToCompletionArray(retSymb, onlyKeepPostfix = false){
if (!isArray(retSymb)) {
return [];
}
Expand All @@ -139,6 +120,10 @@ export class CodeCompletion {
for (let idx = 0; idx < retSymb.length; idx++) {

let finalInsertText = retSymb[idx].searchName;
if(onlyKeepPostfix){
let userInputSplitArr = this.splitStringwithTrigger(finalInsertText);
finalInsertText = userInputSplitArr.pop();
}
let completeKind : CompletionItemKind
let labelTxt = finalInsertText;
switch(retSymb[idx].kind){
Expand Down
8 changes: 1 addition & 7 deletions src/code/server/docSymbolProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,13 @@ export class DocSymbolProcessor {
private createSymbolInfo(name: string, searchName:string, originalName:string,
kind:SymbolKind, location:Location, isLocal:boolean,
containerName?:string, containerList?:Array<Tools.chunkClass>, funcParamArray?:Array<string>, tagType? :string, reason?: Tools.TagReason): Tools.SymbolInformation{

let showName = name;
if(isLocal){
showName = "local " + showName;
}

//searchName中的全部:被替换为 . , 目的是提高查找效率
if(searchName.match(':')){
searchName = searchName.replace(/:/g,".");
}

return{
name: showName,
name: name,
searchName: searchName,
originalName: originalName,
kind: kind,
Expand Down
54 changes: 32 additions & 22 deletions src/code/server/typeInfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//包含 前缀的去除 , 查找, 补回查找
import * as Tools from './codeTools';
import { CodeSymbol } from "./codeSymbol";
import { CodeSettings } from './codeSettings';

//类型推断文件
export class TypeInfer {
Expand All @@ -17,7 +18,7 @@ export class TypeInfer {
private static retArray = []; //用来记录 Completion 和 Definition 返回数组
private static startMS;

private static maxSymbolCount = 20;
private static maxSymbolCount = 1;
// 定义查找接口
// @symbolInfo 是符号名
public static SymbolTagForDefinitionEntry(symbolInfo, uri){
Expand Down Expand Up @@ -62,7 +63,7 @@ export class TypeInfer {
let uri = value.containerURI;
//[尝试]向上补全, 所以补全这边的 tailListCache 要拷贝
this.recursiveProcessSymbolTagForCompletion(uri, value.searchName, tailListCache, false);
if(this.retArray.length === 0){
if(value.tagType){
this.recursiveSearchTagForCompletion(value, uri, searchPrefix, tailListCache, isStripping);
}
}
Expand All @@ -78,7 +79,9 @@ export class TypeInfer {
// @isStripping 剥离模式/合并模式
public static recursiveProcessSymbolTagForDefinition(uri, searchPrefix, tailListCache, isStripping = true){
if(isStripping){
if(this.startMS + 2000 < Date.now()) return; //超时返回
if(CodeSettings.isOpenDebugCode === false){
if(this.startMS + 2000 < Date.now()) return; //超时返回 ReleaseCode
}
let searchPrefixArray = Tools.splitToArrayByDot(searchPrefix);
for (let index = searchPrefixArray.length - 1; index >= 0; index--) {
// 切断用户输入 a.b.c => [a,b,c], 拼接, 开始循环(每次pop一个成员){
Expand Down Expand Up @@ -124,8 +127,10 @@ export class TypeInfer {
// @isStripping 剥离模式/合并模式
public static recursiveProcessSymbolTagForCompletion(uri, searchPrefix, tailListCache, isStripping = true){
// 防止循环遍历,记录[文件名] -- [符号名]
if(isStripping){
if(this.startMS + 2000 < Date.now()) return; //超时返回
if(isStripping){
if(CodeSettings.isOpenDebugCode === false){
if(this.startMS + 2000 < Date.now()) return; //超时返回 ReleaseCode
}
let searchPrefixArray = Tools.splitToArrayByDot(searchPrefix);
for (let index = searchPrefixArray.length - 1; index > 0; index--) {
// 切断用户输入 a.b.c => [a,b,c], 拼接, 开始循环(每次pop一个成员){
Expand Down Expand Up @@ -242,26 +247,31 @@ export class TypeInfer {
}
//
let retrunSymbol = new Array();
if(returnFuncList && returnFuncList.length == 0){
//等式右边的符号无法被直接搜索到,使用递归搜索
let tempRetArray = this.retArray;
this.retArray = [];
this.recursiveProcessSymbolTagForDefinition(uri, searchName, []);
if(this.retArray.length>0){
returnFuncList = this.retArray;
this.retArray = tempRetArray;
}
}
if(returnFuncList && returnFuncList.length > 0){
// for (let index = 0; index < returnFuncList.length; index++) {
// 遍历所有的函数,并在函数中查找返回类型
const retFuncSymbol = returnFuncList[0];
let chunks =CodeSymbol.getCretainDocChunkDic(retFuncSymbol.containerURI);
if( chunks[retFuncSymbol.searchName] ){
// 找到函数符号的
let chunkRetSymbolName = chunks[retFuncSymbol.searchName].returnSymbol;
//然后再chunk 所在文件,查找chunkRetSymbolName
retrunSymbol = CodeSymbol.searchSymbolinDoc(uri , chunkRetSymbolName , Tools.SearchMode.ExactlyEqual);
if (retrunSymbol == null || ( retrunSymbol && retrunSymbol.length <= 0) ){
retrunSymbol = CodeSymbol.searchSymbolforCompletion(uri ,chunkRetSymbolName ,Tools.SearchMode.ExactlyEqual);
}
return retrunSymbol;
// 遍历所有的函数,并在函数中查找返回类型
const retFuncSymbol = returnFuncList[0];
let chunks =CodeSymbol.getCretainDocChunkDic(retFuncSymbol.containerURI);
if( chunks[retFuncSymbol.searchName] ){
// 找到函数符号的
let chunkRetSymbolName = chunks[retFuncSymbol.searchName].returnSymbol;
// 然后再chunk 所在文件,查找chunkRetSymbolName
retrunSymbol = CodeSymbol.searchSymbolinDoc(uri , chunkRetSymbolName , Tools.SearchMode.ExactlyEqual);
if (retrunSymbol == null || ( retrunSymbol && retrunSymbol.length <= 0) ){
retrunSymbol = CodeSymbol.searchSymbolforCompletion(uri ,chunkRetSymbolName ,Tools.SearchMode.ExactlyEqual);
}
// }
}else{
//等式右边的符号无法被直接搜索到
return retrunSymbol;
}
}

}

private static searchUserTag(uri, element, operation){
Expand Down

0 comments on commit 7a7b285

Please sign in to comment.