Skip to content

Commit

Permalink
annotation 以及其他符号总是先排序再处理,隐藏函数内的 local variable,修复 -- MARK: 排序
Browse files Browse the repository at this point in the history
  • Loading branch information
lalawue committed Jul 29, 2022
1 parent f2662e6 commit 0ec5acb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
19 changes: 19 additions & 0 deletions luahelper-lsp/langserver/check/check_lsp_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a *AllProject) FindFileAllSymbol(strFile string) (symbolVec []common.FileS
// 2) 获取文件的注解类型信息, 用于显示当前文档的符号
annotateSymbolVec := a.GetAnnotateFileSymbolStruct(strFile)

// 3) 先排序
sort.Sort(commonSymbolSlice(symbolVec))
sort.Sort(commonSymbolSlice(annotateSymbolVec))

if strings.HasSuffix(strFile, ".mooc") {
symbolVec = appendFileSymbolStruct(symbolVec, annotateSymbolVec)
} else {
Expand Down Expand Up @@ -876,3 +880,18 @@ func recvFindSymbol(results *resultSorter, recvData symbolsChan) {
}
results.results = append(results.results, recvData.returnResult...)
}

// for sort lsp.DocumentSymbol
type commonSymbolSlice []common.FileSymbolStruct

func (a commonSymbolSlice) Len() int {
return len(a)
}

func (a commonSymbolSlice) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

func (a commonSymbolSlice) Less(i, j int) bool {
return a[i].Loc.StartLine < a[j].Loc.StartLine
}
27 changes: 8 additions & 19 deletions luahelper-lsp/langserver/textdocument_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"luahelper-lsp/langserver/lspcommon"
"luahelper-lsp/langserver/pathpre"
lsp "luahelper-lsp/langserver/protocol"
"sort"
"strings"
)

Expand All @@ -32,10 +31,9 @@ func (l *LspServer) TextDocumentSymbol(ctx context.Context, vs lsp.DocumentSymbo
func transferSymbolVec(strFile string, level int, fileSymbolVec []common.FileSymbolStruct) (items []lsp.DocumentSymbol) {
vecLen := len(fileSymbolVec)
items = make([]lsp.DocumentSymbol, 0, vecLen)
itemFuncs := make([]lsp.DocumentSymbol, 0, vecLen/2+1)
itemLVars := make([]lsp.DocumentSymbol, 0, vecLen/2+1)

isMooc := strings.HasSuffix(strFile, ".mooc")
lastFuncSymbol := lsp.DocumentSymbol{}

for _, oneSymbol := range fileSymbolVec {
ra := lspcommon.LocToRange(&oneSymbol.Loc)
Expand Down Expand Up @@ -93,6 +91,7 @@ func transferSymbolVec(strFile string, level int, fileSymbolVec []common.FileSym
} else if oneSymbol.Kind == common.IKFunction {
symbol.Kind = lsp.Function
symbol.Detail = "function"
lastFuncSymbol = symbol
} else if oneSymbol.Kind == common.IKAnnotateMark {
symbol.Kind = lsp.Field
symbol.Detail = oneSymbol.Name
Expand All @@ -103,26 +102,16 @@ func transferSymbolVec(strFile string, level int, fileSymbolVec []common.FileSym
symbol.Detail = "variable"
}

if symbol.Detail == "function" {
itemFuncs = append(itemFuncs, symbol)
} else if symbol.Detail == "variable" && oneSymbol.ContainerName == "local" {
itemLVars = append(itemLVars, symbol)
if symbol.Detail == "variable" &&
oneSymbol.ContainerName == "local" &&
lastFuncSymbol.Detail == "function" &&
symbol.Range.Start.Line > lastFuncSymbol.Range.Start.Line &&
symbol.Range.End.Line < lastFuncSymbol.Range.End.Line {
// igonre local variable inside function
} else {
items = append(items, symbol)
}
}

// ignore local variable inside functions
sort.Sort(lspSymbolSlice(itemFuncs))
sort.Sort(lspSymbolSlice(itemLVars))
index := 0
for _, it := range itemLVars {
if isSymbolRangeIn(itemFuncs, &index, it.Range.Start.Line) {
continue
}
items = append(items, it)
}
items = append(items, itemFuncs...)
return
}

Expand Down

0 comments on commit 0ec5acb

Please sign in to comment.