-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added smart_path option for file name getters. #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
local types = {} | ||
|
||
-- convert numbered index table into an enum | ||
-- | ||
-- example: | ||
-- enum { "foo", "bar" } | ||
-- returns: | ||
-- { foo = 1, bar = 2 } | ||
-- these are going to be option names. to enable, add <option> = true | ||
local paths = {"hide_path", "shorten_path", "tail_path", "smart_path"} | ||
local enum = function(table) | ||
for i = 1, #table do | ||
table[table[i]] = paths[i] | ||
table[i] = nil | ||
end | ||
return table | ||
end | ||
|
||
types.path_display_options = enum {"HIDE_PATH", "SHORTEN_PATH", "TAIL_PATH", "SMART_PATH"} | ||
|
||
return types |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons') | |
|
||
local pathlib = require('telescope.path') | ||
local Job = require('plenary.job') | ||
local types = require('telescope.types') | ||
|
||
local utils = {} | ||
|
||
|
@@ -147,6 +148,83 @@ utils.path_tail = (function() | |
end | ||
end)() | ||
|
||
utils.slice = function(tbl, s, e) | ||
local pos, new = 1, {} | ||
|
||
for i = s, e do | ||
new[pos] = tbl[i] | ||
pos = pos + 1 | ||
end | ||
|
||
return new | ||
end | ||
|
||
|
||
|
||
PATHS = {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be a global right? |
||
utils.path_smart = function(initial_path) | ||
local final = initial_path | ||
if (table.getn(PATHS) ~= 0) then | ||
local diff_counts = { 1, } | ||
local dirs = vim.split(initial_path, "/") | ||
for _, path in pairs(PATHS) do | ||
local _dirs = vim.split(path, "/") | ||
local l = table.getn(_dirs) | ||
local differs = 0 | ||
for i = 0, l do | ||
if (dirs[i] ~= _dirs[i]) then | ||
differs = i | ||
break | ||
end | ||
end | ||
table.insert(diff_counts, differs) | ||
end | ||
local max = math.max(unpack(diff_counts)) | ||
local length = table.getn(dirs) | ||
local final_table = utils.slice(dirs, max, length) | ||
if (max == length) then | ||
final_table = utils.slice(dirs, length - 1, length) | ||
end | ||
final = table.concat(final_table, "/") | ||
else | ||
local length = string.len(final) | ||
final = utils.slice(initial_path, length - 1, length) | ||
final = table.concat(final, "/") | ||
Comment on lines
+191
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are only using slice for create a string afterwards you should look for a different method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i figured that but i was like “i’m already using it 🤷🏻♀️“. it’s a quick hack, not production ready at all. i’ll fix these if i’m going to open the pr on the main repo. i appreciate the feedback edit: and i honestly wasn’t hopeful about the initial PR. i didn’t know we would get you involved |
||
end | ||
table.insert(PATHS, initial_path) | ||
if (final == initial_path) then | ||
return final | ||
else | ||
return "../" .. final | ||
end | ||
end | ||
|
||
utils.transform_filepath = function(opts, path) | ||
local config = require('telescope.config').values | ||
local types = types.path_display_options | ||
|
||
if path == nil or utils.get_default(opts[types.HIDE_PATH], config[types.HIDE_PATH]) then | ||
return '' | ||
end | ||
|
||
local transformed_path = path | ||
|
||
if utils.get_default(opts[types.TAIL_PATH], config[types.TAIL_PATH]) then | ||
transformed_path = utils.path_tail(transformed_path) | ||
elseif utils.get_default(opts[types.SMART_PATH], config[types.SMART_PATH]) then | ||
transformed_path = utils.path_smart(transformed_path) | ||
else | ||
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) | ||
transformed_path = pathlib.make_relative(transformed_path, cwd) | ||
|
||
if utils.get_default(opts[types.SHORTEN_PATH], config[types.SHORTEN_PATH]) then | ||
transformed_path = pathlib.shorten(transformed_path) | ||
end | ||
end | ||
|
||
return transformed_path | ||
end | ||
|
||
-- local x = utils.make_default_callable(function(opts) | ||
-- return function() | ||
-- print(opts.example, opts.another) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you actually want to copy all elements between s and e into a new table? Thats not great.
Maybe this plenary module can help https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/iterators.lua
No idea how it works tho. But i saw a range function. This is the spec file https://github.com/nvim-lua/plenary.nvim/blob/master/tests/plenary/iterators_spec.lua