From a82cd5fce795de5e39b8ac38e378ede5a56b3233 Mon Sep 17 00:00:00 2001
From: "shane.xb.qian" <shane.qian@foxmail.com>
Date: Tue, 26 Dec 2023 14:02:56 +0800
Subject: [PATCH] none: compl item sel

Signed-off-by: shane.xb.qian <shane.qian@foxmail.com>
---
 autoload/lsp/completion.vim | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/autoload/lsp/completion.vim b/autoload/lsp/completion.vim
index 4ab65dd..26dc24a 100644
--- a/autoload/lsp/completion.vim
+++ b/autoload/lsp/completion.vim
@@ -330,23 +330,30 @@ export def CompletionReply(lspserver: dict<any>, cItems: any)
   endif
 enddef
 
-# process the completion documentation
-def ShowCompletionDocumentation(cItem: any)
-  if cItem->empty() || cItem->type() != v:t_dict
-    return
-  endif
-
-  # check if completion item is still selected
+# Check if completion item is selected
+def CheckCompletionItemSel(label: string): bool
   var cInfo = complete_info()
   if cInfo->empty() || !cInfo.pum_visible || cInfo.selected == -1
-    return
+    return false
   endif
-
   var selItem = cInfo.items->get(cInfo.selected, {})
   if selItem->empty()
       || selItem->type() != v:t_dict
       || selItem.user_data->type() != v:t_dict
-      || selItem.user_data.label != cItem.label
+      || selItem.user_data.label != label
+    return false
+  endif
+  return true
+enddef
+
+# Process the completion documentation
+def ShowCompletionDocumentation(cItem: any)
+  if cItem->empty() || cItem->type() != v:t_dict
+    return
+  endif
+
+  # check if completion item is still selected
+  if !CheckCompletionItemSel(cItem.label)
     return
   endif
 
@@ -390,13 +397,7 @@ def ShowCompletionDocumentation(cItem: any)
   endif
 
   # check if completion item is changed in meantime
-  cInfo = complete_info()
-  if cInfo->empty()
-      || !cInfo.pum_visible
-      || cInfo.selected == -1
-      || cInfo.items[cInfo.selected]->type() != v:t_dict
-      || cInfo.items[cInfo.selected].user_data->type() != v:t_dict
-      || cInfo.items[cInfo.selected].user_data.label != cItem.label
+  if !CheckCompletionItemSel(cItem.label)
     return
   endif
 
-- 
2.51.0