Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline snippet ghost text with dynamic window position flip #1955

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

xzbdmw
Copy link

@xzbdmw xzbdmw commented Jun 8, 2024

follow up of #1886
fix #1886 (comment)
close #495

should behave the same as vscode now. Custom snippet not supported yet, only lsp snippet and copilot-similar source will trigger multi-line ghost-text, but should be easy to add.

The functionality is all implemented,
@hrsh7th do let me know what do you think, Thanks!

iShot_2024-06-09_07.56.42.mp4

@xzbdmw xzbdmw marked this pull request as draft June 8, 2024 21:29
@xzbdmw xzbdmw marked this pull request as ready for review June 8, 2024 23:58
@michaelbrusegard
Copy link

This is so awesome! I will test it shortly! Can you share how you have set up CMP @xzbdmw . I want to try and avoid making a mistake in the setup on my part. I was using Copilot.lua with the cmp extension for my ghost text

@xzbdmw
Copy link
Author

xzbdmw commented Jun 9, 2024

Hey I think you don’t need to change anything. should work out of box, and there is no extra config option in cmp . this is my keymap, and near cursor(vedio effect, optional)

@cybergaz
Copy link

cybergaz commented Jun 9, 2024

please add an option to toggle between multiline and singleline ghost text, not everyone like to have multiline previews.

@michaelbrusegard
Copy link

@xzbdmw It works really well and swaps every time there is a multiline ghost text! My completion menu does not seem to follow the mouse as much as yours and I am not completely sure about the hiding of the menu when there is a space (but keeping the ghost text). If you want to take a look at my cmp config: https://github.com/michaelbrusegard/dotfiles/blob/25f81267d8f3d9253153b54bf92dd6e757ac089a/macos/.config/nvim/init.lua#L551

Maybe you have some feedback or suggestions, I am very new to neovim btw
Thanks for the great work

@xzbdmw
Copy link
Author

xzbdmw commented Jun 10, 2024

@cybergaz That shoud be easy(I’m waiting for maintainer’s reply)
@michaelbrusegard could you use a gif to show that

@michaelbrusegard
Copy link

Screen.Recording.2024-06-10.at.23.01.39.mov

This is how it looks, but the menu doesnt hide on space (while keeping the ghost text) and it doesnt move with the cursor. It may be my config is wrong for all I know

@xzbdmw
Copy link
Author

xzbdmw commented Jun 11, 2024

but the menu doesnt hide on space

That’s because copilot suggestion contains space, so it just matches

it doesnt move with the cursor

you can try
require("cmp").setup({
view = {
entries = {
follow_cursor = true
}
}
})

@michaelbrusegard
Copy link

@xzbdmw Thank you, that worked with the cursor. Are there any convenient way to make the keybinds. I feel like when I have the completion complete with return or tab it can be annoying if I actually want to use any of those

@michaelbrusegard
Copy link

@xzbdmw follow_cursor=true only works when the completion menu shows up below the line. When it shows up above it is placed at the start.
Screenshot 2024-08-27 at 17 44 10

@xzbdmw
Copy link
Author

xzbdmw commented Sep 1, 2024

Have you updated to the latest commit? works as expect to me.

iShot_2024-09-02_02.13.09.mp4

@michaelbrusegard
Copy link

You are right... I had removed the follow cursor option from my config. Probably by accident once

@mikecsmith
Copy link

mikecsmith commented Sep 12, 2024

Hey - this is great functionality - just combined it with a snippet I created to prevent ghost_text from firing in the middle of words.

One edge case I've encountered when using it: I've had issues with the completion menu appearing when at the top of the file. The multiline ghost_text displays correctly but I assume the menu is rendering outside the visible bounds?

I'm currently applying this PR via a custom build step that fetches and applies it as a patch - don't think that's the cause but including for visibility:

Utils:

local M = {}

-- Function to apply a patch to a plugin from a URL
function M.apply_patch(plugin_name, patch_url, patch_name)
  local plugin_dir = vim.fn.stdpath('data') .. '/lazy/' .. plugin_name
  local patch_path = plugin_dir .. '/' .. patch_name .. '.patch'

  vim.fn.system({'curl', '-L', patch_url, '-o', patch_path})

  local result = vim.fn.system({'git', '-C', plugin_dir, 'apply', patch_path})

  if vim.v.shell_error ~= 0 then
    vim.notify('Failed to apply patch: ' .. result, vim.log.levels.ERROR)
  else
    vim.notify('Patch applied successfully to ' .. plugin_name, vim.log.levels.INFO)
  end
end

return M

Completions:

local utils = require("mike.utils")
return {
  -- Other plugin setup here...
  {
    'hrsh7th/nvim-cmp',
    dependencies = {
      'hrsh7th/cmp-nvim-lsp',
      'hrsh7th/cmp-buffer',
      'hrsh7th/cmp-path',
      'hrsh7th/cmp-cmdline',
      'hrsh7th/cmp-nvim-lsp-signature-help',
      {
        'windwp/nvim-autopairs',
        event = 'InsertEnter',
        opts = {
          disable_filetype = { 'TelescopePrompt', 'vim' },
        },
      },
    },
    build = function ()
      utils.apply_patch('nvim-cmp', 'https://github.com/hrsh7th/nvim-cmp/pull/1955.patch', '1955')
    end,
    config = function ()
    -- Setup stuff here ...
   
   -- Conditional ghost_text
   -- Consolidated list of characters that should toggle ghost_text
      local toggle_chars = {
        '"', "'", '`', '<', '>', '{', '}', '[', ']', '(', ')', ' ', '' 
      }

      local cmp_config = require('cmp.config')

      local function toggle_ghost_text()
        if vim.api.nvim_get_mode().mode ~= "i" then
          return
        end

        -- Get the current cursor column and line content
        local cursor_col = vim.fn.col('.')  -- Get cursor column
        local line = vim.fn.getline('.')    -- Get current line content

        -- Get the character after the cursor
        local char_after = line:sub(cursor_col, cursor_col)
        
        -- Check if the character after the cursor is in the toggle list (pair characters, spaces, or end of line)
        local should_enable_ghost_text = vim.tbl_contains(toggle_chars, char_after)

        -- Enable or disable ghost_text based on the conditions
        cmp_config.set_onetime({
          experimental = {
            ghost_text = should_enable_ghost_text,
          },
        })
      end

      vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMovedI" }, {
        callback = toggle_ghost_text,
      })

       -- Final setup here...
     end,
   }
 }

@xzbdmw
Copy link
Author

xzbdmw commented Sep 12, 2024

@mikecsmith If the completion menu can't be fit in area that above the cursor, the menu still stays below, do you have some way to reproduce that issue?

iShot_2024-09-13_05.46.43.mp4

@mikecsmith
Copy link

@mikecsmith If the completion menu can't be fit in area that above the cursor, the menu still stays below, do you have some way to reproduce that issue?

iShot_2024-09-13_05.46.43.mp4

I can't reproduce it this morning - it's working perfectly. Apologies - suspect it may have been something in my configs that caused it!

@barnacker
Copy link

Currently running off this branch with high satisfaction, combined with copilot

xzb and others added 2 commits October 22, 2024 09:07
add support for all selection order

if top space is not enough, do not flip window

take border into consider

set every possible winconfig

see if it is multiline  for 'noselect'
@xzbdmw
Copy link
Author

xzbdmw commented Oct 22, 2024

I've rebased with master that has perf improvement.

@vex9z7
Copy link

vex9z7 commented Nov 28, 2024

waiting for merge, we need this feature

@xzbdmw
Copy link
Author

xzbdmw commented Dec 31, 2024

I've integrate with mini-snippets (try it out! It is really good) with a source, so that snippets defined by mini.snippets has the same look as lsp snippets in this pr.

There is a video in echasnovski/mini.nvim#1428 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow to position the float window above the current line
6 participants