patch 9.1.0956: completion may crash, completion highlight wrong with preview window

Problem:  completion may crash, completion highlight wrong with preview
          window (after v9.1.0954)
Solution: correctly calculate scroll offset, check for preview window
          when adding extra highlighting
          (glepnir)

when there have a preview window prepare_tagpreview
will change curwin to preview window and this may cause
ComplMatchIns check condition not correct. check wp is curwin
and also the type of wp is not a preview or poup info

fixes: #16284
closes: #16283

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/popupmenu.c b/src/popupmenu.c
index 6298b5c..ea2edca 100644
--- a/src/popupmenu.c
+++ b/src/popupmenu.c
@@ -917,7 +917,7 @@
 {
     int	    resized = FALSE;
     int	    context = pum_height / 2;
-    int	    scroll_offset = pum_selected - pum_height;
+    int	    scroll_offset;
 #ifdef FEAT_QUICKFIX
     int	    prev_selected = pum_selected;
     unsigned	cur_cot_flags = get_cot_flags();
@@ -927,6 +927,7 @@
 #endif
 
     pum_selected = n;
+    scroll_offset = pum_selected - pum_height;
 
     if (pum_selected >= 0 && pum_selected < pum_size)
     {
@@ -950,7 +951,7 @@
 	    // scroll up; when we did a jump it's probably a PageDown then
 	    // scroll a whole page
 	    if (pum_first < scroll_offset + 3)
-		pum_first = MAX(pum_first, scroll_offset + 1);
+		pum_first = MAX(pum_first + pum_height - 2, scroll_offset + 1);
 	    else
 		pum_first = scroll_offset + 1;
 	}