summaryrefslogtreecommitdiff
path: root/config/nvim/lua/plugins/fzf.lua
blob: 581e60069c6035fe3c893540c46d8565aca219a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
return {
	{
		"ibhagwan/fzf-lua",
		dependencies = { "nvim-tree/nvim-web-devicons" },
		config = function()
			local fzf = require("fzf-lua")
			fzf.setup({
				defaults = {
					file_icons = true,
				},
				grep = {
					rg_opts = table.concat({
						"--color=never",
						"--no-heading",
						"--with-filename",
						"--line-number",
						"--column",
						"--smart-case",
						"--glob=!.git/**",
						"--glob=!package-lock.json",
						"--glob=!lazy-lock.json",
						"--glob=!node_modules/**",
					}, " "),
				},
				keymap = {
					builtin = {
						["<C-h>"] = "toggle-help",
					},
					fzf = {
						["ctrl-q"] = "select-all+accept",
					},
				},
			})

			-- General pickers
			vim.keymap.set("n", "<Leader>f", fzf.resume, { desc = "Resume search" })
			vim.keymap.set("n", "<C-p>", fzf.files, { desc = "Find file" })
			vim.keymap.set("n", "<Leader>a", fzf.live_grep, { desc = "Find text" })
			vim.keymap.set("n", "\\b", fzf.buffers, { desc = "Find buffer" })
			vim.keymap.set("n", "\\h", fzf.help_tags, { desc = "Find help" })
			vim.keymap.set("n", "\\k", fzf.keymaps, { desc = "Find keymaps" })
			vim.keymap.set("n", "<Leader><Leader>t", fzf.colorschemes, { desc = "Select colorscheme" })

			-- Git
			vim.keymap.set("n", "\\g", fzf.git_status, { desc = "Find changed file" })

			-- LSP pickers
			vim.keymap.set("n", "\\r", function()
				fzf.lsp_references({ includeDeclaration = false })
			end, { desc = "Find references" })
			vim.keymap.set("n", "\\c", fzf.lsp_incoming_calls, { desc = "Find incoming calls" })
			vim.keymap.set("n", "\\o", fzf.lsp_outgoing_calls, { desc = "Find outgoing calls" })
			vim.keymap.set("n", "\\d", fzf.lsp_definitions, { desc = "Find definitions" })
			vim.keymap.set("n", "\\t", fzf.lsp_typedefs, { desc = "Find type definitions" })
			vim.keymap.set("n", "\\i", fzf.lsp_implementations, { desc = "Find implementations" })
			vim.keymap.set("n", "\\s", fzf.lsp_document_symbols, { desc = "Find document symbols" })
			vim.keymap.set("n", "\\S", fzf.lsp_live_workspace_symbols, { desc = "Find workspace symbols" })

			-- Search nvim config
			vim.keymap.set("n", "\\C", function()
				fzf.live_grep({
					cwd = vim.fn.expand("~/.config/nvim"),
					rg_opts = "--follow " .. table.concat({
						"--color=never",
						"--no-heading",
						"--with-filename",
						"--line-number",
						"--column",
						"--smart-case",
					}, " "),
				})
			end, { desc = "Search config" })

			-- Custom directory picker
			local function reset_lsp_workspace_folders()
				local clients = vim.lsp.get_clients()
				if #clients == 0 then
					vim.notify("No active LSP clients found", vim.log.levels.WARN)
					return
				end
				for _, client in ipairs(clients) do
					local current_folders = client.workspace_folders or {}
					for _, folder in ipairs(current_folders) do
						vim.lsp.buf.remove_workspace_folder(folder.name)
					end
					vim.lsp.buf.add_workspace_folder(vim.fn.getcwd())
					vim.notify(
						string.format("Reset workspace folders for %s to: %s", client.name, vim.fn.getcwd()),
						vim.log.levels.INFO
					)
				end
			end

			local function build_find_dir_cmd()
				local home = vim.fn.expand("~")
				local homeSrc = vim.fn.expand("~/src")
				local xDir = vim.fn.expand("~/x")
				local sense = "/Volumes/Sense"
				local cmd = "find"
				local search_paths = {}
				local excluded_dirs = {
					"*/cloud-copy*",
					"*/node_modules*",
					"*/.venv*",
					"*/venv*",
				}
				if vim.fn.isdirectory(sense) == 1 then
					table.insert(search_paths, sense)
					table.insert(excluded_dirs, homeSrc)
				else
					table.insert(search_paths, homeSrc)
				end
				if vim.fn.isdirectory(xDir) == 1 then
					table.insert(search_paths, xDir)
				end
				table.insert(search_paths, vim.fn.expand("~/.local/share/nvim"))

				local args = { cmd }
				for _, path in ipairs(search_paths) do
					table.insert(args, path)
				end
				table.insert(args, "-type")
				table.insert(args, "d")
				table.insert(args, "-maxdepth")
				table.insert(args, "4")
				for _, dir in ipairs(excluded_dirs) do
					table.insert(args, "-not")
					table.insert(args, "-path")
					table.insert(args, dir)
				end
				return table.concat(args, " ")
			end

			vim.keymap.set("n", "<Leader>p", function()
				fzf.fzf_exec(build_find_dir_cmd(), {
					prompt = "Change Directory> ",
					actions = {
						["default"] = function(selected)
							local dir = selected[1]
							vim.cmd("cd " .. vim.fn.fnameescape(dir))
							vim.opt.titlestring = vim.fn.fnamemodify(dir, ":t")
							vim.opt.title = true
							reset_lsp_workspace_folders()
							vim.cmd("LspRestart")
							print("Changed directory to: " .. dir)
						end,
					},
				})
			end, { desc = "Change working directory" })
		end,
	},
}