peek.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. local M = {
  2. floating_buf = nil,
  3. floating_win = nil,
  4. prev_result = nil,
  5. }
  6. local function create_floating_file(location, opts)
  7. vim.validate {
  8. location = { location, "t" },
  9. opts = { opts, "t", true },
  10. }
  11. -- Set some defaults
  12. opts = opts or {}
  13. local close_events = opts.close_events or { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
  14. -- location may be LocationLink or Location
  15. local uri = location.targetUri or location.uri
  16. if uri == nil then
  17. return
  18. end
  19. local bufnr = vim.uri_to_bufnr(uri)
  20. if not vim.api.nvim_buf_is_loaded(bufnr) then
  21. vim.fn.bufload(bufnr)
  22. end
  23. local range = location.targetRange or location.range
  24. local contents = vim.api.nvim_buf_get_lines(
  25. bufnr,
  26. range.start.line,
  27. math.min(range["end"].line + 1 + (opts.context or 10), range.start.line + (opts.max_height or 15)), -- Don't let the window be more that 15 lines long(height)
  28. false
  29. )
  30. local width, height = vim.lsp.util._make_floating_popup_size(contents, opts)
  31. local if_nil = vim.F.if_nil
  32. opts = vim.lsp.util.make_floating_popup_options(if_nil(width, 30), if_nil(height, 10), opts)
  33. -- Don't make it minimal as it is meant to be fully featured
  34. opts["style"] = nil
  35. vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
  36. local winnr = vim.api.nvim_open_win(bufnr, false, opts)
  37. vim.api.nvim_win_set_option(winnr, "winblend", 0)
  38. vim.api.nvim_win_set_cursor(winnr, { range.start.line + 1, range.start.character })
  39. vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr)
  40. -- Set some autocmds to close the window
  41. vim.api.nvim_command(
  42. string.format("autocmd %s <buffer> ++once lua pcall(vim.api.nvim_win_close, %d, true)", unpack(close_events), winnr)
  43. )
  44. return bufnr, winnr
  45. end
  46. local function preview_location_callback(result)
  47. if result == nil or vim.tbl_isempty(result) then
  48. return nil
  49. end
  50. local opts = {
  51. border = "rounded",
  52. context = 10,
  53. }
  54. if vim.tbl_islist(result) then
  55. M.prev_result = result[1]
  56. M.floating_buf, M.floating_win = create_floating_file(result[1], opts)
  57. else
  58. M.prev_result = result
  59. M.floating_buf, M.floating_win = create_floating_file(result, opts)
  60. end
  61. end
  62. local function preview_location_callback_new_signature(_, result)
  63. return preview_location_callback(result)
  64. end
  65. function M.open_file()
  66. -- Get the file currently open in the floating window
  67. local filepath = vim.fn.expand "%:."
  68. if not filepath then
  69. print "peek: Unable to open the file!"
  70. return
  71. end
  72. -- Close the floating window
  73. pcall(vim.api.nvim_win_close, M.floating_win, true)
  74. -- Edit the file
  75. vim.cmd("edit " .. filepath)
  76. local winnr = vim.api.nvim_get_current_win()
  77. -- Set the cursor at the right position
  78. M.set_cursor_to_prev_pos(winnr)
  79. end
  80. function M.set_cursor_to_prev_pos(winnr)
  81. -- Get position of the thing to peek at
  82. local location = M.prev_result
  83. local range = location.targetRange or location.range
  84. local cursor_pos = { range.start.line + 1, range.start.character }
  85. -- Set the winnr to the floating window if none was passed in
  86. winnr = winnr or M.floating_win
  87. -- Set the cursor at the correct position in the floating window
  88. vim.api.nvim_win_set_cursor(winnr, cursor_pos)
  89. end
  90. function M.Peek(what)
  91. -- If a window already exists, focus it at the right position!
  92. if vim.tbl_contains(vim.api.nvim_list_wins(), M.floating_win) then
  93. local success_1, _ = pcall(vim.api.nvim_set_current_win, M.floating_win)
  94. if not success_1 then
  95. print "peek: You cannot edit the current file in a preview!"
  96. return
  97. end
  98. -- Set the cursor at the correct position in the floating window
  99. M.set_cursor_to_prev_pos()
  100. vim.api.nvim_buf_set_keymap(
  101. M.floating_buf,
  102. "n",
  103. "<CR>",
  104. ":lua require('lvim.lsp.peek').open_file()<CR>",
  105. { noremap = true, silent = true }
  106. )
  107. else
  108. -- Make a new request and then create the new window in the callback
  109. local params = vim.lsp.util.make_position_params()
  110. local preview_callback = preview_location_callback_new_signature
  111. local success, _ = pcall(vim.lsp.buf_request, 0, "textDocument/" .. what, params, preview_callback)
  112. if not success then
  113. print(
  114. 'peek: Error calling LSP method "textDocument/' .. what .. '". The current language lsp might not support it.'
  115. )
  116. end
  117. end
  118. end
  119. return M