0
0

init.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. local pickers = require("telescope.pickers")
  2. local actions = require("telescope.actions")
  3. local action_state = require("telescope.actions.state")
  4. local finders = require("telescope.finders")
  5. local conf = require("telescope.config").values
  6. local entry_display = require("telescope.pickers.entry_display")
  7. -- Utility function: navigate diagnostics
  8. local diagnostic = function(next, severity)
  9. local opts = { float = false }
  10. if severity then opts.severity = { min = severity } end
  11. if next then
  12. opts.count = 1
  13. return vim.diagnostic.jump(opts)
  14. else
  15. opts.count = -1
  16. return vim.diagnostic.jump(opts)
  17. end
  18. end
  19. -- Utility function: navigate git hunks (if gitsigns is installed)
  20. local githunk
  21. local gitsigns
  22. if pcall(require, "gitsigns") then
  23. gitsigns = require("gitsigns")
  24. githunk = function(next)
  25. if (next) then
  26. gitsigns.next_hunk()
  27. else
  28. gitsigns.prev_hunk()
  29. end
  30. vim.defer_fn(function()
  31. vim.schedule(function() gitsigns.preview_hunk_inline() end)
  32. end, 100)
  33. end
  34. else
  35. githunk = function(_) end
  36. end
  37. local targets = {
  38. {
  39. "Error", "Any error diagnostic in the current buffer",
  40. function() diagnostic(true, vim.diagnostic.severity.ERROR) end,
  41. function() diagnostic(false, vim.diagnostic.severity.ERROR) end
  42. }, {
  43. "Warn", "Any warn or error diagnostic in the current buffer",
  44. function() diagnostic(true, vim.diagnostic.severity.WARN) end,
  45. function() diagnostic(false, vim.diagnostic.severity.WARN) end
  46. }, {
  47. "Info", "Any info, warn, or error diagnostic in the current buffer",
  48. function() diagnostic(true, vim.diagnostic.severity.INFO) end,
  49. function() diagnostic(false, vim.diagnostic.severity.INFO) end
  50. }, {
  51. "Diagnostic",
  52. "Any diagnostic in the current buffer, regardless of severity",
  53. function() diagnostic(true) end, function() diagnostic(false) end
  54. }, {
  55. "Search", "The last search made with /",
  56. function()
  57. vim.fn.search(vim.fn.getreginfo("/")["regcontents"][1])
  58. end,
  59. function()
  60. vim.fn.search(vim.fn.getreginfo("/")["regcontents"][1], "b")
  61. end
  62. }, {
  63. "Git", "Next diff hunk in current buffer", function()
  64. githunk(true)
  65. end, function() githunk(false) end
  66. }
  67. }
  68. local target = targets[4]
  69. local set_target = function(index) target = targets[index] end
  70. local open_picker
  71. open_picker = function(opts)
  72. pickers.new(opts, {
  73. prompt_title = "Bit-Browse Target",
  74. finder = finders.new_table({
  75. results = targets,
  76. entry_maker = function(entry)
  77. local displayer = entry_display.create({
  78. separator = " ▏",
  79. items = { { width = 32 }, { remaining = true } }
  80. })
  81. return {
  82. value = entry,
  83. display = function()
  84. return displayer({ { entry[1] }, { entry[2] } })
  85. end,
  86. ordinal = string.format("%s %s", entry[1], entry[2])
  87. }
  88. end
  89. }),
  90. sorter = conf.generic_sorter(),
  91. attach_mappings = function(prompt_bufnr, _)
  92. actions.select_default:replace(function()
  93. actions.close(prompt_bufnr)
  94. target = action_state.get_selected_entry().value
  95. end)
  96. return true
  97. end
  98. }):find()
  99. end
  100. local function add_item() end
  101. local function next_item() target[3]() end
  102. local function prev_item() target[4]() end
  103. local function get_target() return target[1] end
  104. return {
  105. open = open_picker,
  106. add = add_item,
  107. next = next_item,
  108. prev = prev_item,
  109. set_target = set_target,
  110. get_target = get_target
  111. }