From bb9fe789cdcb3df9799b7d633d22020b7bf066d1 Mon Sep 17 00:00:00 2001 From: yinfei8 Date: Sat, 13 Jul 2024 13:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A1=A5=E5=85=A8=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E5=90=8C=E6=97=B6=E8=A1=A5=E5=85=A8=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E7=B1=BB=E5=9E=8B=E4=B8=8E=E6=B3=A8=E8=A7=A3=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../check/check_lsp_annotate_complete.go | 4 +- .../langserver/textdocument_complete_test.go | 132 ++++++++++++++++++ luahelper-lsp/testdata/complete/test10.lua | 9 ++ 3 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 luahelper-lsp/testdata/complete/test10.lua diff --git a/luahelper-lsp/langserver/check/check_lsp_annotate_complete.go b/luahelper-lsp/langserver/check/check_lsp_annotate_complete.go index 4079653..0ab1abe 100644 --- a/luahelper-lsp/langserver/check/check_lsp_annotate_complete.go +++ b/luahelper-lsp/langserver/check/check_lsp_annotate_complete.go @@ -355,7 +355,9 @@ func (a *AllProject) getVarInfoCompleteExt(symbol *common.Symbol, colonFlag bool for _, oneClass := range classList { a.convertClassInfoToCompleteVecs(oneClass, colonFlag) } - return + + // 注释掉,同时补全注解类型与变量的类型 + // return } // 2) 没有注解的模式 diff --git a/luahelper-lsp/langserver/textdocument_complete_test.go b/luahelper-lsp/langserver/textdocument_complete_test.go index 5a1c239..39414bf 100644 --- a/luahelper-lsp/langserver/textdocument_complete_test.go +++ b/luahelper-lsp/langserver/textdocument_complete_test.go @@ -3083,3 +3083,135 @@ func TestCompleteIntelligent3(t *testing.T) { } } } + +// 变量同时有类型也有注解 +func TestCompleteVarAndAnnotate(t *testing.T) { + _, filename, _, _ := runtime.Caller(0) + paths, _ := filepath.Split(filename) + + strRootPath := paths + "../testdata/complete" + strRootPath, _ = filepath.Abs(strRootPath) + + strRootURI := "file://" + strRootPath + lspServer := createLspTest(strRootPath, strRootURI) + context := context.Background() + + fileName := strRootPath + "/" + "test10.lua" + data, err := ioutil.ReadFile(fileName) + + if err != nil { + t.Fatalf("read file:%s err=%s", fileName, err.Error()) + } + + var testCompleteList []TestCompleteInfo = []TestCompleteInfo{} + var oneComplete TestCompleteInfo + + // 0) + { + oneComplete.changeRange = lsp.Range{ + Start: lsp.Position{ + Line: 7, + Character: 0, + }, + } + oneComplete.changeRange.End = oneComplete.changeRange.Start + oneComplete.changText = "dd." + oneComplete.compLoc = lsp.Position{ + Line: oneComplete.changeRange.Start.Line, + Character: oneComplete.changeRange.Start.Character + (uint32)(len(oneComplete.changText)), + } + oneComplete.resultList = []string{"a", "b", "c"} + testCompleteList = append(testCompleteList, oneComplete) + } + + for index, oneComplete := range testCompleteList { + openParams := lsp.DidOpenTextDocumentParams{ + TextDocument: lsp.TextDocumentItem{ + URI: lsp.DocumentURI(fileName), + Text: string(data), + }, + } + err1 := lspServer.TextDocumentDidOpen(context, openParams) + if err1 != nil { + t.Fatalf("didopen file:%s err=%s", fileName, err1.Error()) + } + + changParams := lsp.DidChangeTextDocumentParams{ + TextDocument: lsp.VersionedTextDocumentIdentifier{ + TextDocumentIdentifier: lsp.TextDocumentIdentifier{ + URI: lsp.DocumentURI(fileName), + }, + }, + ContentChanges: []lsp.TextDocumentContentChangeEvent{ + { + Range: &oneComplete.changeRange, + RangeLength: 0, + Text: oneComplete.changText, + }, + }, + } + + lspServer.TextDocumentDidChange(context, changParams) + + completionParams := lsp.CompletionParams{ + TextDocumentPositionParams: lsp.TextDocumentPositionParams{ + TextDocument: lsp.TextDocumentIdentifier{ + URI: lsp.DocumentURI(fileName), + }, + Position: oneComplete.compLoc, + }, + Context: lsp.CompletionContext{ + TriggerKind: lsp.CompletionTriggerKind(1), + }, + } + + completionReturn, err2 := lspServer.TextDocumentComplete(context, completionParams) + if err2 != nil { + t.Fatalf("complete file:%s err=%s", fileName, err2.Error()) + } + + completionListTmp, _ := completionReturn.(CompletionListTmp) + var oneItem CompletionItemTmp + for _, resultStr := range oneComplete.resultList { + findFlag := false + for _, oneCompReturn := range completionListTmp.Items { + if resultStr == oneCompReturn.Label { + findFlag = true + oneItem = oneCompReturn + } + } + + if !findFlag { + t.Fatalf("not find complete index=%d, str=%s", index, resultStr) + } + } + + var inputItem lsp.CompletionItem = lsp.CompletionItem{ + Label: oneItem.Label, + Kind: oneItem.Kind, + Data: oneItem.Data, + } + + resultItem, err3 := lspServer.TextDocumentCompleteResolve(context, inputItem) + if err3 != nil { + t.Fatalf("TextDocumentCompleteResolve err, index=%d", index) + } + + if len(oneComplete.resolveList) == 0 { + continue + } + + resolveFlag := false + for _, oneStr := range oneComplete.resolveList { + if strings.Contains(resultItem.Documentation.Value, oneStr) { + resolveFlag = true + } + } + if !resolveFlag { + t.Fatalf("TextDocumentCompleteResolve file=%s, index=%d", fileName, index) + } + if strings.Contains(resultItem.InsertText, "\"") || strings.Contains(resultItem.InsertText, "'") { + t.Fatalf("TextDocumentCompleteResolve file=%s, index=%d contains err", fileName, index) + } + } +} diff --git a/luahelper-lsp/testdata/complete/test10.lua b/luahelper-lsp/testdata/complete/test10.lua new file mode 100644 index 0000000..5751700 --- /dev/null +++ b/luahelper-lsp/testdata/complete/test10.lua @@ -0,0 +1,9 @@ +---@type table +local dd = { + a = 1, + b = 2, + c = 3 +} + + +