Skip to content
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

Support order for targets #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ require('telescope-alternate').setup({
mappings = {
{ 'app/services/(.*)_services/(.*).rb', { -- alternate from services to contracts / models
{ 'app/contracts/[1]_contracts/[2].rb', 'Contract' }, -- Adding label to switch
{ 'app/models/**/*[1].rb', 'Model', true }, -- Ignore create entry (with true)
{
'app/models/**/*[1].rb',
'Model',
true, -- Ignore create entry (with true)
1 -- order is optional should be a number. Items with lower order will be shown before items with higher order
},
} },
{ 'app/contracts/(.*)_contracts/(.*).rb', { { 'app/services/[1]_services/[2].rb', 'Service' } } }, -- from contracts to services
-- Search anything on helper folder that contains pluralize version of model.
Expand Down Expand Up @@ -69,7 +74,12 @@ require('telescope').setup{
-- You also can use the verbose way to mapping:
mappings = {
{ pattern = 'app/services/(.*)_services/(.*).rb', targets = {
{ template = 'app/contracts/[1]_contracts/[2].rb', label = 'Contract', enable_new = true } -- enable_new can be a function too.
{
template = 'app/contracts/[1]_contracts/[2].rb',
label = 'Contract',
enable_new = true, -- enable_new can be a function too.
order = 1 -- order is optional should be a number. Items with lower order will be shown before items with higher order
}
}
},
{ pattern = 'app/contracts/(.*)_contracts/(.*).rb', targets = {
Expand Down
22 changes: 15 additions & 7 deletions lua/telescope-alternate/finders.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local utils = require('telescope-alternate.utils')
local defaultOrder = 9999

local functions = function()
local user_functions = vim.g.telescope_alternate_transformers or {}
Expand Down Expand Up @@ -41,6 +42,7 @@ function M.parse_available_matches(config)
local full_path = target[1]
local label = target[2]
local ignoreCreate
local order = target[4]

if type(target[3]) == 'function' then
ignoreCreate = not (target[3]())
Expand All @@ -49,13 +51,13 @@ function M.parse_available_matches(config)
end

if utils.isfile(full_path) ~= 0 then
table.insert(actions, { path = full_path, type = M.action_types[1], label = label })
table.insert(actions, { path = full_path, type = M.action_types[1], label = label, order = order })
elseif utils.isdir(full_path) ~= 0 then
local files = utils.files_in_path(full_path)

for z = 1, #files, 1 do
if utils.isfile(files[z]) ~= 0 then
table.insert(actions, { path = files[z], type = M.action_types[1], label = label })
table.insert(actions, { path = files[z], type = M.action_types[1], label = label, order = order})
end
end

Expand All @@ -82,7 +84,7 @@ function M.parse_available_matches(config)
end
end

table.insert(actions, { path = results[r], type = M.action_types[1], label = file_label })
table.insert(actions, { path = results[r], type = M.action_types[1], label = file_label, order = order })
end
end

Expand Down Expand Up @@ -151,7 +153,7 @@ function M.add_matches_to_targets(config)
target = target:gsub(without_function_regex, matches[i])
end

table.insert(parsed_targets, { target, name, ignoreCreate })
table.insert(parsed_targets, { target, name, ignoreCreate, real_target[4] or defaultOrder })
end

return parsed_targets
Expand Down Expand Up @@ -222,7 +224,6 @@ function M.go_to_selection(selection, open_command_kind)
completed = true
end
end

if answer == "y" then
if not utils.isfile(path) then
vim.fn.mkdir(vim.fn.fnamemodify(path, ":h"), "p")
Expand All @@ -248,7 +249,14 @@ function M.normalize_config(config)
else
enable_new = targets[z].enable_new
end
table.insert(items, { targets[z].template, targets[z].label, enable_new })

local order
if targets[z].order == nil then
order = defaultOrder
else
order = targets[z].order
end
table.insert(items, { targets[z].template, targets[z].label, enable_new, order })
end

table.insert(new_config, { config[i].pattern, items })
Expand Down Expand Up @@ -281,7 +289,7 @@ function M.find_alternatve_files()
local parsed = M.parse_available_matches(matched_targets)

table.sort(parsed, function(a, b)
return a.prefix == nil and b.prefix == 'NEW '
return (a.order and b.order and a.order < b.order) or (a.prefix == nil and b.prefix == 'NEW ')
end)

return parsed
Expand Down