Repository: neovim/neovim
File: src/nvim/popupmenu.c (C)

Code before:
          pum_preview_set_text(curbuf, pum_array[pum_selected].pum_info, &lnum, &max_info_width);
          // Increase the height of the preview window to show the
          // text, but no more than 'previewheight' lines.
          if (repeat == 0 && !use_float) {
            lnum = MIN(lnum, (linenr_T)p_pvh);

            if (curwin->w_height < lnum) {
              win_setheight((int)lnum);
              resized = true;
            }
          }

          curbuf->b_changed = false;
          curbuf->b_p_ma = false;
          if (pum_selected != prev_selected) {
            curwin->w_topline = 1;
          } else if (curwin->w_topline > curbuf->b_ml.ml_line_count) {
            curwin->w_topline = curbuf->b_ml.ml_line_count;
          }
          curwin->w_cursor.lnum = 1;
          curwin->w_cursor.col = 0;

          if (use_float) {
            // adjust floating window by actually height and max info text width
            if (!pum_adjust_info_position(curwin, max_info_width)) {
              curwin = curwin_save;
            }
          }

          if ((curwin != curwin_save && win_valid(curwin_save))
              || (curtab != curtab_save && valid_tabpage(curtab_save))) {
            if (curtab != curtab_save && valid_tabpage(curtab_save)) {
              goto_tabpage_tp(curtab_save, false, false);
            }

            // When the first completion is done and the preview
            // window is not resized, skip the preview window's
            // status line redrawing.
            if (ins_compl_active() && !resized) {
              curwin->w_redr_status = false;
            }

            // Return cursor to where we were
            validate_cursor(curwin);
            redraw_later(curwin, UPD_SOME_VALID);

            // When the preview window was resized we need to
            // update the view on the buffer.  Only go back to
            // the window when needed, otherwise it will always be
            // redrawn.
            if (resized && win_valid(curwin_save)) {

Review comment (bug):
Setting `curwin` directly seems dubious. `curwin` after `win_close` in `pum_adjust_info_position` _should_ be `curwin_save` anyhow (makes this a no-op), but if that's somehow false then this is bugprone. (`curbuf` and other globals may remain out-of-sync)

Maybe use `win_enter`? (I'll assume `win_valid` isn't needed here as autocmds are blocked for popups... 😇)

```suggestion
              win_enter(curwin_save, false);
```

Code after:
          // Increase the height of the preview window to show the
          // text, but no more than 'previewheight' lines.
          if (repeat == 0 && !use_float) {
            lnum = MIN(lnum, (linenr_T)p_pvh);

            if (curwin->w_height < lnum) {
              win_setheight((int)lnum);
              resized = true;
            }
          }

          curbuf->b_changed = false;
          curbuf->b_p_ma = false;
          if (pum_selected != prev_selected) {
            curwin->w_topline = 1;
          } else if (curwin->w_topline > curbuf->b_ml.ml_line_count) {
            curwin->w_topline = curbuf->b_ml.ml_line_count;
          }
          curwin->w_cursor.lnum = 1;
          curwin->w_cursor.col = 0;

          if (use_float) {
            // adjust floating window by actually height and max info text width
            if (!pum_adjust_info_position(curwin, max_info_width) && win_valid(curwin_save)) {
              win_enter(curwin_save, false);
            }
          }

          if ((curwin != curwin_save && win_valid(curwin_save))
              || (curtab != curtab_save && valid_tabpage(curtab_save))) {
            if (curtab != curtab_save && valid_tabpage(curtab_save)) {
              goto_tabpage_tp(curtab_save, false, false);
            }

            // When the first completion is done and the preview
            // window is not resized, skip the preview window's
            // status line redrawing.
            if (ins_compl_active() && !resized) {
              curwin->w_redr_status = false;
            }

            // Return cursor to where we were
            validate_cursor(curwin);
            redraw_later(curwin, UPD_SOME_VALID);

            // When the preview window was resized we need to
            // update the view on the buffer.  Only go back to
            // the window when needed, otherwise it will always be
            // redrawn.
            if (resized && win_valid(curwin_save)) {
              no_u_sync++;