Explorar o código

fix(telescope-shroud): improve telescope shroud

Joe hai 5 meses
pai
achega
3115408ff6

+ 7 - 2
.config/nvim/custom/proj-conf/lua/proj-conf/telescope-shroud.lua

@@ -3,8 +3,13 @@ local shroud = require("telescope-shroud")
 return {
     default = {},
     shroud_tests = function()
-        shroud.set_default({
-            "!**/tests/**", "!tests/*"
+        shroud.add_default({
+            "!**/tests/**", "!tests/*",
+        })
+    end,
+    shroud_migrations = function()
+        shroud.add_default({
+            "!**/migrations/**", "!migrations/*",
         })
     end
 }

+ 73 - 39
.config/nvim/custom/telescope-shroud/lua/telescope-shroud/init.lua

@@ -30,12 +30,12 @@ local get_filters = function()
     end)
     async.run(function()
         local result = vim.fn.system(
-            "fd -t d | xargs -n 1 -I{} printf '{}*\n!{}*\n'")
+            "fd -t d | xargs -n 1 -I{} printf '{}**\n!{}**\n'")
         sender.send(result)
     end)
     async.run(function()
         local result = vim.fn.system(
-            "fd -t d | rev | cut -d/ -f2 -s | rev | sort | uniq | xargs -n 1 -I{} printf '**/{}/*\n!**/{}/*\n'")
+            "fd -t d | rev | cut -d/ -f2 -s | rev | sort | uniq | xargs -n 1 -I{} printf '**/{}/**\n!**/{}/**\n'")
         sender.send(result)
     end)
     for _ = 1, 3 do
@@ -57,8 +57,8 @@ local title_from_filters = function(filters)
     if vim.tbl_count(filters) == 0 then
         return ""
     end
-    if vim.tbl_count(filters) > 3 then
-        return " (3+ Filters)"
+    if vim.tbl_count(filters) > 6 then
+        return " (6+ Filters)"
     end
     return " (" .. table.concat(filters, ", ") .. ")"
 end
@@ -72,17 +72,43 @@ local open_default = function()
     })
 end
 
-local open_narrow = function()
-    local results
-    if all_filters[1] ~= nil then
-        results = all_filters
-    else
-        results = populate()
+
+local build_default_results_table = function()
+    local unique_filters = {}
+    for _, v in pairs(get_selected()) do
+        unique_filters[v] = true
+    end
+    if all_filters[1] == nil then
+        all_filters = populate()
+    end
+    for _, v in pairs(all_filters) do
+        unique_filters[v] = true
     end
-    local default_option = "**/*"
+    local results = {}
+
+    -- Lazy implementation to put selected filters at the top
+    for k, _ in pairs(unique_filters) do
+        if selected_filters[k] then
+            table.insert(results, k)
+        end
+    end
+    for k, _ in pairs(unique_filters) do
+        if selected_filters[k] ~= true and k ~= "" then
+            table.insert(results, k)
+        end
+    end
+
+    return results
+end
+
+local open_narrow = function()
+    local results = build_default_results_table()
+    local default_option = "**/**"
     if results[1] ~= default_option then
         table.insert(results, 1, default_option)
     end
+
+    local startup_complete = false
     pickers.new(themes.get_dropdown({}),
         {
             prompt_title = "Grep Filters",
@@ -90,16 +116,35 @@ local open_narrow = function()
                 results = results
             }),
             sorter = conf.generic_sorter(),
+            on_complete = { function(picker)
+                if startup_complete == false then
+                    local i = 1
+                    for entry in picker.manager:iter() do
+                        if selected_filters[entry[1]] then
+                            picker:add_selection(picker:get_row(i))
+                        end
+                        i = i + 1
+                    end
+                    startup_complete = true
+                end
+            end },
             attach_mappings = function(prompt_bufnr, _)
-                actions.toggle_selection:replace(function() end)
                 actions.select_default:replace(function()
-                    actions.close(prompt_bufnr)
                     local selection = action_state
-                        .get_selected_entry()
+                        .get_selected_entry().value
+                    local selected = {selection}
+                    if selection ~= default_option then
+                        for k, v in pairs(action_state.get_current_picker(prompt_bufnr)._multi._entries) do
+                            if v and k[1] ~= selection then
+                                table.insert(selected, k[1])
+                            end
+                        end
+                    end
+                    actions.close(prompt_bufnr)
                     builtin.live_grep({
                         prompt_title = "Grep" ..
-                            title_from_filters(selection),
-                        glob_pattern = selection
+                            title_from_filters(selected),
+                        glob_pattern = selected
                     })
                 end)
                 return true
@@ -109,29 +154,7 @@ end
 
 
 local create_selected_filter_picker = function()
-    local unique_filters = {}
-    for _, v in pairs(get_selected()) do
-        unique_filters[v] = true
-    end
-    if all_filters[1] == nil then
-        all_filters = populate()
-    end
-    for _, v in pairs(all_filters) do
-        unique_filters[v] = true
-    end
-    local results = {}
-
-    -- Lazy implementation to put selected filters at the top
-    for k, _ in pairs(unique_filters) do
-        if selected_filters[k] then
-            table.insert(results, k)
-        end
-    end
-    for k, _ in pairs(unique_filters) do
-        if selected_filters[k] ~= true and k ~= "" then
-            table.insert(results, k)
-        end
-    end
+    local results = build_default_results_table()
 
     local startup_complete = false
     local picker = pickers.new(themes.get_dropdown({}), {
@@ -182,9 +205,20 @@ local set_default = function(items)
     create_selected_filter_picker():find()
 end
 
+local add_default = function (items)
+    if items ~= nil then
+        for _, v in pairs(items) do
+            selected_filters[v] = true
+        end
+        return
+    end
+    create_selected_filter_picker():find()
+end
+
 return {
     populate = populate,
     set_default = set_default,
+    add_default = add_default,
     open_narrow = open_narrow,
     open_default = open_default,
     get_selected = get_selected,