summaryrefslogtreecommitdiff
path: root/lua/telescope/config.lua
blob: 00d4101f41de9d110b062fc2e2ff9b10ebd4244b (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
local strings = require "plenary.strings"
local deprecated = require "telescope.deprecated"
local sorters = require "telescope.sorters"
local if_nil = vim.F.if_nil
local os_sep = require("plenary.path").path.sep

-- Keep the values around between reloads
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}
_TelescopeConfigurationPickers = _TelescopeConfigurationPickers or {}

local function first_non_null(...)
  local n = select("#", ...)
  for i = 1, n do
    local value = select(i, ...)

    if value ~= nil then
      return value
    end
  end
end

-- A function that creates an amended copy of the `base` table,
-- by replacing keys at "level 2" that match keys in "level 1" in `priority`,
-- and then performs a deep_extend.
-- May give unexpected results if used with tables of "depth"
-- greater than 2.
local smarter_depth_2_extend = function(priority, base)
  local result = {}
  for key, val in pairs(base) do
    if type(val) ~= "table" then
      result[key] = first_non_null(priority[key], val)
    else
      result[key] = {}
      for k, v in pairs(val) do
        result[key][k] = first_non_null(priority[k], v)
      end
    end
  end
  for key, val in pairs(priority) do
    if type(val) ~= "table" then
      result[key] = first_non_null(val,result[key])
    else
      result[key] = vim.tbl_extend("keep",val,result[key] or {})
    end
  end
  return result
end

-- TODO: Add other major configuration points here.
-- selection_strategy

local config = {}
config.smarter_depth_2_extend = smarter_depth_2_extend

config.values = _TelescopeConfigurationValues
config.descriptions = {}
config.pickers = _TelescopeConfigurationPickers

function config.set_pickers(pickers)
  pickers = if_nil(pickers, {})

  for k, v in pairs(pickers) do
    config.pickers[k] = v
  end
end

local layout_config_defaults = {
  width = 0.8,
  height = 0.9,

  horizontal = {
    prompt_position = "bottom",
    preview_cutoff = 120,
  },

  vertical = {
    preview_cutoff = 40,
  },

  center = {
    preview_cutoff = 40,
  },
}

local layout_config_description = string.format([[
    Determines the default configuration values for layout strategies.
    See |telescope.layout| for details of the configurations options for
    each strategy.

    Allows setting defaults for all strategies as top level options and
    for overriding for specific options.
    For example, the default values below set the default width to 80%% of
    the screen width for all strategies except 'center', which has width
    of 50%% of the screen width.

    Default: %s
]], vim.inspect(
  layout_config_defaults,
  { newline = "\n    ", indent = "  " }
))

-- A table of all the usual defaults for telescope.
-- Keys will be the name of the default,
-- values will be a list where:
-- - first entry is the value
-- - second entry is the description of the option

local telescope_defaults = {

  sorting_strategy = {
    "descending",
    [[
    Determines the direction "better" results are sorted towards.

    Available options are:
    - "descending" (default)
    - "ascending"]],
  },

  selection_strategy = {
    "reset",
    [[
    Determines how the cursor acts after each sort iteration.

    Available options are:
    - "reset" (default)
    - "follow"
    - "row"
    - "closest"]],
  },

  scroll_strategy = {
    "cycle",
    [[
    Determines what happens you try to scroll past view of the picker.

    Available options are:
    - "cycle" (default)
    - "limit"]],
  },

  layout_strategy = {
    "horizontal",
    [[
    Determines the default layout of Telescope pickers.
    See |telescope.layout| for details of the available strategies.

    Default: 'horizontal']],
  },

  layout_config = { layout_config_defaults, layout_config_description },

  winblend = { 0 },

  prompt_prefix = { "> ", [[
    Will be shown in front of the prompt.

    Default: '> ']]
  },

  selection_caret = { "> ", [[
    Will be shown in front of the selection.

    Default: '> ']]
  },

  entry_prefix = { "  ", [[
    Prefix in front of each result entry. Current selection not included.

    Default: '  ']]
  },

  initial_mode = { "insert" },

  border = { true, [[
    Boolean defining if borders are added to Telescope windows.

    Default: true]]
  },

  path_display = { {}, [[
    Determines how file paths are displayed

    path_display can be set to an array with a combination of:
    - "hidden"    hide file names
    - "tail"      only display the file name, and not the path
    - "absolute"  display absolute paths
    - "shorten"   only display the first character of each directory in
                  the path

    path_display can also be set to 'hidden' string to hide file names

    Default: {}]]
  },

  borderchars = { { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } },

  get_status_text = {
    function(self)
      local xx = (self.stats.processed or 0) - (self.stats.filtered or 0)
      local yy = self.stats.processed or 0
      if xx == 0 and yy == 0 then
        return ""
      end

      return string.format("%s / %s", xx, yy)
    end,
  },

  dynamic_preview_title = { false, [[
    Will change the title of the preview window dynamically, where it
    is supported. Means the preview window will for example show the
    full filename.

    Default: false]],
  },

  history = { {
    path = vim.fn.stdpath("data") .. os_sep .. "telescope_history",
    limit = 100,
    handler = function(...) return require('telescope.actions.history').get_simple_history(...) end,
  }, [[
    This field handles the configuration for prompt history.
    By default it is a table, with default values (more below).
    To disable history, set it to either false or nil.

    Currently mappings still need to be added, Example:
      mappings = {
        i = {
          ["<C-Down>"] = require('telescope.actions').cycle_history_next,
          ["<C-Up>"] = require('telescope.actions').cycle_history_prev,
        },
      },

    Fields:
      - path:    The path to the telescope history as string.
                 default: stdpath("data")/telescope_history
      - limit:   The amount of entries that will be written in the
                 history.
                 Warning: If limit is set to nil it will grown unbound.
                 default: 100
      - handler: A lua function that implements the history.
                 This is meant as a developer setting for extensions to
                 override the history handling, e.g.,
                 https://github.com/nvim-telescope/telescope-smart-history.nvim,
                 which allows context sensitive (cwd + picker) history.

                 Default:
                 require('telescope.actions.history').get_simple_history
  ]],

  },

  -- Builtin configuration

  -- List that will be executed.
  --    Last argument will be the search term (passed in during execution)
  vimgrep_arguments = {
    { "rg", "--color=never", "--no-heading", "--with-filename", "--line-number", "--column", "--smart-case" },
  },

  use_less = { true },

  color_devicons = { true },

  set_env = { nil },

  mappings = {
    {}, [[
    Your mappings to override telescope's default mappings.

    Format is:
    {
      mode = { ..keys }
    }

    where {mode} is the one character letter for a mode
    ('i' for insert, 'n' for normal).

    For example:

    mappings = {
      i = {
        ["<esc>"] = require('telescope.actions').close,
      },
    }


    To disable a keymap, put [map] = false
      So, to not map "<C-n>", just put

        ...,
        ["<C-n>"] = false,
        ...,

      Into your config.


    otherwise, just set the mapping to the function that you want it to
    be.

        ...,
        ["<C-i>"] = require('telescope.actions').select_default,
        ...,

    If the function you want is part of `telescope.actions`, then you can
    simply give a string.
      For example, the previous option is equivalent to:

        ...,
        ["<C-i>"] = "select_default",
        ...,

    You can also add other mappings using tables with `type = "command"`.
      For example:

        ...,
        ["jj"] = { "<esc>", type = "command" },
        ["kk"] = { "<cmd>echo \"Hello, World!\"<cr>", type = "command" },)
        ...,
    ]],
  },

  default_mappings = {
    nil,
    [[
    Not recommended to use except for advanced users.

    Will allow you to completely remove all of telescope's default maps
    and use your own.
    ]],
  },

  generic_sorter = { sorters.get_generic_fuzzy_sorter },
  prefilter_sorter = { sorters.prefilter },
  file_sorter = { sorters.get_fuzzy_file },

  file_ignore_patterns = { nil },

  file_previewer = {
    function(...)
      return require("telescope.previewers").vim_buffer_cat.new(...)
    end,
  },
  grep_previewer = {
    function(...)
      return require("telescope.previewers").vim_buffer_vimgrep.new(...)
    end,
  },
  qflist_previewer = {
    function(...)
      return require("telescope.previewers").vim_buffer_qflist.new(...)
    end,
  },
  buffer_previewer_maker = {
    function(...)
      return require("telescope.previewers").buffer_previewer_maker(...)
    end,
  },
}

-- @param user_defaults table: a table where keys are the names of options,
--    and values are the ones the user wants
-- @param tele_defaults table: (optional) a table containing all of the defaults
--    for telescope [defaults to `telescope_defaults`]
function config.set_defaults(user_defaults, tele_defaults)
  user_defaults = if_nil(user_defaults, {})
  tele_defaults = if_nil(tele_defaults, telescope_defaults)

  -- Check if using layout keywords outside of `layout_config`
  deprecated.picker_window_options(user_defaults)

  -- Check if using `layout_defaults` instead of `layout_config`
  user_defaults = deprecated.layout_configuration(user_defaults)

  local function get(name, default_val)
    if name == "layout_config" then
      return smarter_depth_2_extend(
        if_nil(user_defaults[name], {}),
        vim.tbl_deep_extend("keep", if_nil(config.values[name], {}), if_nil(default_val, {}))
      )
    end
    if name == "history" then
      if not user_defaults[name] or not config.values[name] then
        return false
      end

      return smarter_depth_2_extend(
        if_nil(user_defaults[name], {}),
        vim.tbl_deep_extend("keep", if_nil(config.values[name], {}), if_nil(default_val, {}))
      )
    end
    return first_non_null(user_defaults[name], config.values[name], default_val)
  end

  local function set(name, default_val, description)
    -- TODO(doc): Once we have descriptions for all of these, then we can add this back in.
    -- assert(description, "Config values must always have a description")

    config.values[name] = get(name, default_val)
    if description then
      config.descriptions[name] = strings.dedent(description)
    end
  end

  for key, info in pairs(tele_defaults) do
    set(key, info[1], info[2])
  end

  local M = {}
  M.get = get
  return M
end

function config.clear_defaults()
  for k, _ in pairs(config.values) do
    config.values[k] = nil
  end
end

config.set_defaults()

return config