Skip to content

Commit

Permalink
Merge pull request #43 from desdic/20240308relpath
Browse files Browse the repository at this point in the history
fix: Bug with relative paths
  • Loading branch information
desdic committed Mar 8, 2024
2 parents 772737d + 71c6a09 commit eb804ab
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 33 deletions.
19 changes: 15 additions & 4 deletions lua/agrolens/core.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
local M = {}

local ppath = require("plenary.path")
local utils = require("telescope._extensions.utils")
local ppath = require("plenary.path")

M.create_entry = function(filename, matches, iter_query, bufnr, capture_name)
M.create_entry = function(
filename,
relfilename,
matches,
iter_query,
bufnr,
capture_name
)
local entry = {}
entry.filename = filename
entry.relfilename = relfilename
entry.bufnr = bufnr

for i, _ in pairs(matches) do
Expand Down Expand Up @@ -60,6 +68,7 @@ M.add_entries = function(
capture_names,
bufnr,
filename,
relfilename,
filetype
)
local ts = vim.treesitter
Expand All @@ -76,6 +85,7 @@ M.add_entries = function(
for _, matches, _ in iter_query:iter_matches(root, bufnr) do
local entry = M.create_entry(
filename,
relfilename,
matches,
iter_query,
bufnr,
Expand Down Expand Up @@ -105,7 +115,7 @@ M.get_captures = function(opts)

for _, bufnr in ipairs(opts.bufids) do
local buffilename = vim.api.nvim_buf_get_name(bufnr)
local relpath = ppath:new(buffilename):make_relative(opts.cwd)
local relfilename = ppath:new(buffilename):make_relative(opts.cwd)
local filetype = vim.filetype.match({ buf = bufnr })

if filetype and filetype ~= "" then
Expand All @@ -114,7 +124,8 @@ M.get_captures = function(opts)
entries,
opts.queries,
bufnr,
relpath,
buffilename,
relfilename,
filetype
)
end
Expand Down
6 changes: 4 additions & 2 deletions lua/telescope/_extensions/agrolenslib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@ agrolens.entry_maker = function(entry)
icon_width = 2
end

local fname = entry.filename
local fname = entry.relfilename
if
agrolens.cur_opts.buffers ~= "all"
and agrolens.telescope_opts.force_long_filepath ~= true
then
fname = vim.fs.basename(fname)
fname = vim.fs.basename(entry.filename)
-- else
-- fname = ppath:new(fname):make_relative(agrolens.cur_opts.cwd)
end

local line = fname
Expand Down
6 changes: 3 additions & 3 deletions tests/c_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("c", function()

eq(#entries, 5)

eq(entries[1].filename, "tests/c/test.c")
eq(entries[1].relfilename, "tests/c/test.c")
eq(entries[1].lnum, 16)
eq(entries[1].col, 0)
eq(entries[1].line, "int days_since_birth(struct Person *p) {")
Expand All @@ -39,7 +39,7 @@ describe("c", function()
core.get_captures({ queries = { "callings" }, bufids = buffers })
eq(#entries, 10)

eq(entries[1].filename, "tests/c/test.c")
eq(entries[1].relfilename, "tests/c/test.c")
eq(entries[1].lnum, 23)
eq(entries[1].col, 2)

Expand All @@ -65,7 +65,7 @@ describe("c", function()
core.get_captures({ queries = { "comments" }, bufids = buffers })
eq(#entries, 4)

eq(entries[1].filename, "tests/c/test.c")
eq(entries[1].relfilename, "tests/c/test.c")
eq(entries[1].lnum, 52)
eq(entries[1].col, 0)

Expand Down
6 changes: 3 additions & 3 deletions tests/cpp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("cpp", function()
core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/cpp/main.cc")
eq(entries[1].relfilename, "tests/cpp/main.cc")
eq(entries[1].lnum, 18)
eq(entries[1].col, 0)

Expand All @@ -42,7 +42,7 @@ describe("cpp", function()
core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 5)
eq(entries[1].filename, "tests/cpp/main.cc")
eq(entries[1].relfilename, "tests/cpp/main.cc")
eq(entries[1].lnum, 36)
eq(entries[1].col, 4)

Expand All @@ -58,7 +58,7 @@ describe("cpp", function()
core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 3)
eq(entries[1].filename, "tests/cpp/main.cc")
eq(entries[1].relfilename, "tests/cpp/main.cc")
eq(entries[1].lnum, 23)
eq(entries[1].col, 0)
eq(entries[1].line, '/* basic seconds to days')
Expand Down
6 changes: 3 additions & 3 deletions tests/glsl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("glsl", function()

-- functions
eq(#entries, 4)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].relfilename, "tests/glsl/default.frag")
eq(entries[1].lnum, 26)
eq(entries[1].col, 0)
eq(entries[1].line, "vec4 pointLight()")
Expand All @@ -35,15 +35,15 @@ describe("glsl", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 1)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].relfilename, "tests/glsl/default.frag")
eq(entries[1].lnum, 107)
eq(entries[1].col, 2)
eq(entries[1].line, " FragColor = spotLight();")
end)
it("comments", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })
eq(#entries, 23)
eq(entries[1].filename, "tests/glsl/default.frag")
eq(entries[1].relfilename, "tests/glsl/default.frag")
eq(entries[1].lnum, 3)
eq(entries[1].col, 0)
eq(entries[1].line, "// Outputs colors in RGBA")
Expand Down
6 changes: 3 additions & 3 deletions tests/go_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("go", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/go/main.go")
eq(entries[1].relfilename, "tests/go/main.go")
eq(entries[1].lnum, 15)
eq(entries[1].col, 0)

Expand All @@ -34,7 +34,7 @@ describe("go", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 10)
eq(entries[1].filename, "tests/go/main.go")
eq(entries[1].relfilename, "tests/go/main.go")
eq(entries[1].lnum, 20)
eq(entries[1].col, 2)

Expand All @@ -49,7 +49,7 @@ describe("go", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 2)
eq(entries[1].filename, "tests/go/main.go")
eq(entries[1].relfilename, "tests/go/main.go")
eq(entries[1].lnum, 26)
eq(entries[1].col, 1)

Expand Down
3 changes: 3 additions & 0 deletions tests/lua_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("lua", function()
core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 6)
eq(entries[1].relfilename, "tests/lua/test.lua")
eq(entries[1].lnum, 1)
eq(entries[1].col, 0)
eq(entries[1].line, "local hello1 = function()")
Expand All @@ -36,6 +37,7 @@ describe("lua", function()
core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 12)
eq(entries[1].relfilename, "tests/lua/test.lua")
eq(entries[1].lnum, 2)
eq(entries[1].col, 4)
eq(entries[1].line, ' print("hello1")')
Expand All @@ -48,6 +50,7 @@ describe("lua", function()
core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 2)
eq(entries[1].relfilename, "tests/lua/test.lua")
eq(entries[1].lnum, 13)
eq(entries[1].col, 0)
eq(entries[1].line, "-- Local object")
Expand Down
2 changes: 1 addition & 1 deletion tests/make_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("make", function()
local entries = core.get_captures({ queries = { "labels" }, bufids = buffers })

eq(#entries, 5)
eq(entries[1].filename, "tests/make/Makefile")
eq(entries[1].relfilename, "tests/make/Makefile")
eq(entries[1].lnum, 1)
eq(entries[1].col, 0)
eq(entries[1].line, ".PHONY: all")
Expand Down
2 changes: 1 addition & 1 deletion tests/markdown_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("markdown", function()
local entries = core.get_captures({ queries = { "labels" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/markdown/note.md")
eq(entries[1].relfilename, "tests/markdown/note.md")
eq(entries[1].lnum, 1)
eq(entries[1].col, 0)
eq(entries[1].line, "# Heading#1")
Expand Down
6 changes: 3 additions & 3 deletions tests/php_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("php", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 3)
eq(entries[1].filename, "tests/php/test.php")
eq(entries[1].relfilename, "tests/php/test.php")
eq(entries[1].lnum, 6)
eq(entries[1].col, 2)

Expand All @@ -33,7 +33,7 @@ describe("php", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 5)
eq(entries[1].filename, "tests/php/test.php")
eq(entries[1].relfilename, "tests/php/test.php")
eq(entries[1].lnum, 21)
eq(entries[1].col, 0)

Expand All @@ -45,7 +45,7 @@ describe("php", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 2)
eq(entries[1].filename, "tests/php/test.php")
eq(entries[1].relfilename, "tests/php/test.php")
eq(entries[1].lnum, 3)
eq(entries[1].col, 0)

Expand Down
2 changes: 1 addition & 1 deletion tests/python_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("python", function()
core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/python/test.py")
eq(entries[1].relfilename, "tests/python/test.py")
eq(entries[1].lnum, 11)
eq(entries[1].col, 4)

Expand Down
6 changes: 3 additions & 3 deletions tests/ruby_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("ruby", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 6)
eq(entries[1].filename, "tests/ruby/test.rb")
eq(entries[1].relfilename, "tests/ruby/test.rb")
eq(entries[1].lnum, 7)
eq(entries[1].col, 2)

Expand All @@ -36,7 +36,7 @@ describe("ruby", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 14)
eq(entries[1].filename, "tests/ruby/test.rb")
eq(entries[1].relfilename, "tests/ruby/test.rb")
eq(entries[1].lnum, 3)
eq(entries[1].col, 0)

Expand All @@ -49,7 +49,7 @@ describe("ruby", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 3)
eq(entries[1].filename, "tests/ruby/test.rb")
eq(entries[1].relfilename, "tests/ruby/test.rb")
eq(entries[1].lnum, 1)
eq(entries[1].col, 0)

Expand Down
6 changes: 3 additions & 3 deletions tests/rust_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("rust", function()
local entries = core.get_captures({ queries = { "functions" }, bufids = buffers })

eq(#entries, 4)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].relfilename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 9)
eq(entries[1].col, 4)

Expand All @@ -34,7 +34,7 @@ describe("rust", function()
local entries = core.get_captures({ queries = { "callings" }, bufids = buffers })

eq(#entries, 7)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].relfilename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 10)
eq(entries[1].col, 8)

Expand All @@ -51,7 +51,7 @@ describe("rust", function()
local entries = core.get_captures({ queries = { "comments" }, bufids = buffers })

eq(#entries, 2)
eq(entries[1].filename, "tests/rust/src/main.rs")
eq(entries[1].relfilename, "tests/rust/src/main.rs")
eq(entries[1].lnum, 27)
eq(entries[1].col, 0)
eq(entries[1].line, "/*")
Expand Down
2 changes: 1 addition & 1 deletion tests/toml_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("toml", function()
local entries = core.get_captures({ queries = { "labels" }, bufids = buffers })

eq(#entries, 5)
eq(entries[1].filename, "tests/toml/test.toml")
eq(entries[1].relfilename, "tests/toml/test.toml")
eq(entries[1].lnum, 5)
eq(entries[1].col, 0)

Expand Down
4 changes: 2 additions & 2 deletions tests/yaml_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("yaml", function()
local entries = core.get_captures({ queries = { "docker-compose" }, bufids = buffers })

eq(#entries, 3)
eq(entries[1].filename, "tests/yaml/docker-compose.yml")
eq(entries[1].relfilename, "tests/yaml/docker-compose.yml")
eq(entries[1].lnum, 4)
eq(entries[1].col, 2)

Expand All @@ -34,7 +34,7 @@ describe("yaml2", function()
local entries = core.get_captures({ queries = { "github-workflow-steps" }, bufids = buffers })

eq(#entries, 12)
eq(entries[1].filename, "tests/yaml/github-ci.yml")
eq(entries[1].relfilename, "tests/yaml/github-ci.yml")
eq(entries[1].lnum, 29)
eq(entries[1].col, 20)

Expand Down

0 comments on commit eb804ab

Please sign in to comment.