Skip to content

Commit

Permalink
fix(lsp): exclude locations that match the current line
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent cf81aac commit 8c03e13
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lua/trouble/filter.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
local M = {}

local function overlaps(pos, item)
return (pos[1] > item.pos[1] or (pos[1] == item.pos[1] and pos[2] >= item.pos[2]))
and (pos[1] < item.end_pos[1] or (pos[1] == item.end_pos[1] and pos[2] <= item.end_pos[2]))
end

local function overlaps_lines(pos, item)
return pos[1] >= item.pos[1] and pos[1] <= item.end_pos[1]
---@param opts? {lines:boolean}
function M.overlaps(pos, item, opts)
if opts and opts.lines then
return pos[1] >= item.pos[1] and pos[1] <= item.end_pos[1]
else
return (pos[1] > item.pos[1] or (pos[1] == item.pos[1] and pos[2] >= item.pos[2]))
and (pos[1] < item.end_pos[1] or (pos[1] == item.end_pos[1] and pos[2] <= item.end_pos[2]))
end
end

---@alias trouble.Filter.ctx {opts:trouble.Config, main?:trouble.Main}
Expand All @@ -32,9 +33,9 @@ M.filters = {
end
local range = item.range --[[@as trouble.Item]]
if range then
return overlaps_lines(main.cursor, range)
return M.overlaps(main.cursor, range, { lines = true })
else
return overlaps_lines(main.cursor, item)
return M.overlaps(main.cursor, item, { lines = true })
end
end,
["not"] = function(item, filter, ctx)
Expand Down
10 changes: 9 additions & 1 deletion lua/trouble/sources/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Cache = require("trouble.cache")
local Filter = require("trouble.filter")
local Item = require("trouble.item")
local Util = require("trouble.util")

Expand Down Expand Up @@ -267,8 +268,15 @@ function M.get_items(client, locations)
---@cast locations (lsp.Location|lsp.LocationLink)[]
local items = {} ---@type trouble.Item[]

local cursor = vim.api.nvim_win_get_cursor(0)
local fname = vim.api.nvim_buf_get_name(0)
fname = vim.fs.normalize(fname)

for _, loc in ipairs(locations) do
items[#items + 1] = M.location_to_item(client, loc)
local item = M.location_to_item(client, loc)
if not (item.filename == fname and Filter.overlaps(cursor, item)) then
items[#items + 1] = item
end
end

Item.add_text(items, { mode = "full" })
Expand Down

0 comments on commit 8c03e13

Please sign in to comment.