summaryrefslogtreecommitdiff
path: root/lua/telescope/make_entry.lua
blob: 6b096daa12cb6690f00b4a80564377a6aaaed9b4 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
---@tag telescope.make_entry

---@brief [[
---
--- Each picker has a finder made up of two parts, the results which are the
--- data to be displayed, and the entry_maker. These entry_makers are functions
--- returned from make_entry functions. These will be referred to as
--- entry_makers in the following documentation.
---
--- Every entry maker returns a function that accepts the data to be used for
--- an entry. This function will return an entry table (or nil, meaning skip
--- this entry) which contains the following important keys:
--- - value any: value key can be anything but still required
--- - valid bool: is an optional key because it defaults to true but if the key
---   is set to false it will not be displayed by the picker (optional)
--- - ordinal string: is the text that is used for filtering (required)
--- - display string|function: is either a string of the text that is being
---   displayed or a function receiving the entry at a later stage, when the entry
---   is actually being displayed. A function can be useful here if a complex
---   calculation has to be done. `make_entry` can also return a second value
---   a highlight array which will then apply to the line. Highlight entry in
---   this array has the following signature `{ { start_col, end_col }, hl_group }`
---   (required)
--- - filename string: will be interpreted by the default `<cr>` action as
---   open this file (optional)
--- - bufnr number: will be interpreted by the default `<cr>` action as open
---   this buffer (optional)
--- - lnum number: lnum value which will be interpreted by the default `<cr>`
---   action as a jump to this line (optional)
--- - col number: col value which will be interpreted by the default `<cr>`
---   action as a jump to this column (optional)
---
--- For more information on easier displaying, see |telescope.pickers.entry_display|
---
--- TODO: Document something we call `entry_index`
---@brief ]]

local entry_display = require "telescope.pickers.entry_display"
local utils = require "telescope.utils"
local strings = require "plenary.strings"
local Path = require "plenary.path"

local treesitter_type_highlight = {
  ["associated"] = "TSConstant",
  ["constant"] = "TSConstant",
  ["field"] = "TSField",
  ["function"] = "TSFunction",
  ["method"] = "TSMethod",
  ["parameter"] = "TSParameter",
  ["property"] = "TSProperty",
  ["struct"] = "Struct",
  ["var"] = "TSVariableBuiltin",
}

local lsp_type_highlight = {
  ["Class"] = "TelescopeResultsClass",
  ["Constant"] = "TelescopeResultsConstant",
  ["Field"] = "TelescopeResultsField",
  ["Function"] = "TelescopeResultsFunction",
  ["Method"] = "TelescopeResultsMethod",
  ["Property"] = "TelescopeResultsOperator",
  ["Struct"] = "TelescopeResultsStruct",
  ["Variable"] = "TelescopeResultsVariable",
}

local get_filename_fn = function()
  local bufnr_name_cache = {}
  return function(bufnr)
    bufnr = vim.F.if_nil(bufnr, 0)
    local c = bufnr_name_cache[bufnr]
    if c then
      return c
    end

    local n = vim.api.nvim_buf_get_name(bufnr)
    bufnr_name_cache[bufnr] = n
    return n
  end
end

local handle_entry_index = function(opts, t, k)
  local override = ((opts or {}).entry_index or {})[k]
  if not override then
    return
  end

  local val, save = override(t, opts)
  if save then
    rawset(t, k, val)
  end
  return val
end

local make_entry = {}

make_entry.set_default_entry_mt = function(tbl, opts)
  return setmetatable({}, {
    __index = function(t, k)
      local override = handle_entry_index(opts, t, k)
      if override then
        return override
      end

      -- Only hit tbl once
      local val = tbl[k]
      if val then
        rawset(t, k, val)
      end

      return val
    end,
  })
end

do
  local lookup_keys = {
    display = 1,
    ordinal = 1,
    value = 1,
  }

  function make_entry.gen_from_string(opts)
    local mt_string_entry = {
      __index = function(t, k)
        local override = handle_entry_index(opts, t, k)
        if override then
          return override
        end

        return rawget(t, rawget(lookup_keys, k))
      end,
    }

    return function(line)
      return setmetatable({
        line,
      }, mt_string_entry)
    end
  end
end

do
  local lookup_keys = {
    ordinal = 1,
    value = 1,
    filename = 1,
    cwd = 2,
  }

  function make_entry.gen_from_file(opts)
    opts = opts or {}

    local cwd = vim.fn.expand(opts.cwd or vim.loop.cwd())

    local disable_devicons = opts.disable_devicons

    local mt_file_entry = {}

    mt_file_entry.cwd = cwd
    mt_file_entry.display = function(entry)
      local hl_group
      local display = utils.transform_path(opts, entry.value)

      display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons)

      if hl_group then
        return display, { { { 1, 3 }, hl_group } }
      else
        return display
      end
    end

    mt_file_entry.__index = function(t, k)
      local override = handle_entry_index(opts, t, k)
      if override then
        return override
      end

      local raw = rawget(mt_file_entry, k)
      if raw then
        return raw
      end

      if k == "path" then
        local retpath = Path:new({ t.cwd, t.value }):absolute()
        if not vim.loop.fs_access(retpath, "R", nil) then
          retpath = t.value
        end
        return retpath
      end

      return rawget(t, rawget(lookup_keys, k))
    end

    if opts.file_entry_encoding then
      return function(line)
        line = vim.iconv(line, opts.file_entry_encoding, "utf8")
        return setmetatable({ line }, mt_file_entry)
      end
    else
      return function(line)
        return setmetatable({ line }, mt_file_entry)
      end
    end
  end
end

do
  local lookup_keys = {
    value = 1,
    ordinal = 1,
  }

  -- Gets called only once to parse everything out for the vimgrep, after that looks up directly.
  local parse_with_col = function(t)
    local _, _, filename, lnum, col, text = string.find(t.value, [[(..-):(%d+):(%d+):(.*)]])

    local ok
    ok, lnum = pcall(tonumber, lnum)
    if not ok then
      lnum = nil
    end

    ok, col = pcall(tonumber, col)
    if not ok then
      col = nil
    end

    t.filename = filename
    t.lnum = lnum
    t.col = col
    t.text = text

    return { filename, lnum, col, text }
  end

  local parse_without_col = function(t)
    local _, _, filename, lnum, text = string.find(t.value, [[(..-):(%d+):(.*)]])

    local ok
    ok, lnum = pcall(tonumber, lnum)
    if not ok then
      lnum = nil
    end

    t.filename = filename
    t.lnum = lnum
    t.col = nil
    t.text = text

    return { filename, lnum, nil, text }
  end

  local parse_only_filename = function(t)
    t.filename = t.value
    t.lnum = nil
    t.col = nil
    t.text = ""

    return { t.filename, nil, nil, "" }
  end

  function make_entry.gen_from_vimgrep(opts)
    opts = opts or {}

    local mt_vimgrep_entry
    local parse = parse_with_col
    if opts.__matches == true then
      parse = parse_only_filename
    elseif opts.__inverted == true then
      parse = parse_without_col
    end

    local disable_devicons = opts.disable_devicons
    local disable_coordinates = opts.disable_coordinates
    local only_sort_text = opts.only_sort_text

    local execute_keys = {
      path = function(t)
        if Path:new(t.filename):is_absolute() then
          return t.filename, false
        else
          return Path:new({ t.cwd, t.filename }):absolute(), false
        end
      end,

      filename = function(t)
        return parse(t)[1], true
      end,

      lnum = function(t)
        return parse(t)[2], true
      end,

      col = function(t)
        return parse(t)[3], true
      end,

      text = function(t)
        return parse(t)[4], true
      end,
    }

    -- For text search only, the ordinal value is actually the text.
    if only_sort_text then
      execute_keys.ordinal = function(t)
        return t.text
      end
    end

    local display_string = "%s%s%s"

    mt_vimgrep_entry = {
      cwd = vim.fn.expand(opts.cwd or vim.loop.cwd()),

      display = function(entry)
        local display_filename = utils.transform_path(opts, entry.filename)

        local coordinates = ":"
        if not disable_coordinates then
          if entry.lnum then
            if entry.col then
              coordinates = string.format(":%s:%s:", entry.lnum, entry.col)
            else
              coordinates = string.format(":%s:", entry.lnum)
            end
          end
        end

        local display, hl_group = utils.transform_devicons(
          entry.filename,
          string.format(display_string, display_filename, coordinates, entry.text),
          disable_devicons
        )

        if hl_group then
          return display, { { { 1, 3 }, hl_group } }
        else
          return display
        end
      end,

      __index = function(t, k)
        local override = handle_entry_index(opts, t, k)
        if override then
          return override
        end

        local raw = rawget(mt_vimgrep_entry, k)
        if raw then
          return raw
        end

        local executor = rawget(execute_keys, k)
        if executor then
          local val, save = executor(t)
          if save then
            rawset(t, k, val)
          end
          return val
        end

        return rawget(t, rawget(lookup_keys, k))
      end,
    }

    return function(line)
      return setmetatable({ line }, mt_vimgrep_entry)
    end
  end
end

function make_entry.gen_from_git_stash(opts)
  local displayer = entry_display.create {
    separator = " ",
    items = {
      { width = 10 },
      opts.show_branch and { width = 15 } or "",
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.value, "TelescopeResultsLineNr" },
      opts.show_branch and { entry.branch_name, "TelescopeResultsIdentifier" } or "",
      entry.commit_info,
    }
  end

  return function(entry)
    if entry == "" then
      return nil
    end

    local splitted = utils.max_split(entry, ": ", 2)
    local stash_idx = splitted[1]
    local _, branch_name = string.match(splitted[2], "^([WIP on|On]+) (.+)")
    local commit_info = splitted[3]

    return make_entry.set_default_entry_mt({
      value = stash_idx,
      ordinal = commit_info,
      branch_name = branch_name,
      commit_info = commit_info,
      display = make_display,
    }, opts)
  end
end

function make_entry.gen_from_git_commits(opts)
  opts = opts or {}

  local displayer = entry_display.create {
    separator = " ",
    items = {
      { width = 8 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.value, "TelescopeResultsIdentifier" },
      entry.msg,
    }
  end

  return function(entry)
    if entry == "" then
      return nil
    end

    local marker, sha, msg = string.match(entry, "([*\\/| ]+) +([0-9a-f]*) +(.*)")

    if not sha then
      marker = entry
      sha = ""
      msg = ""
    end

    if not msg then
      msg = "<empty commit message>"
    end

    marker, _ = string.gsub(marker, "\\", "+")
    marker, _ = string.gsub(marker, "/", "-")
    marker, _ = string.gsub(marker, "+", "/")
    marker, _ = string.gsub(marker, "-", "\\")

    return make_entry.set_default_entry_mt({
      value = sha,
      ordinal = marker .. " " .. sha .. " " .. msg,
      msg = marker .. " " .. msg,
      display = make_display,
      current_file = opts.current_file,
    }, opts)
  end
end

function make_entry.gen_from_quickfix(opts)
  opts = opts or {}
  local show_line = vim.F.if_nil(opts.show_line, true)

  local hidden = utils.is_path_hidden(opts)
  local items = {
    { width = vim.F.if_nil(opts.fname_width, 30) },
    { remaining = true },
  }
  if hidden then
    items[1] = { width = 8 }
  end
  if not show_line then
    table.remove(items, 1)
  end

  local displayer = entry_display.create { separator = "▏", items = items }

  local make_display = function(entry)
    local input = {}
    if not hidden then
      table.insert(input, string.format("%s:%d:%d", utils.transform_path(opts, entry.filename), entry.lnum, entry.col))
    else
      table.insert(input, string.format("%4d:%2d", entry.lnum, entry.col))
    end

    if show_line then
      local text = entry.text
      if opts.trim_text then
        text = text:gsub("^%s*(.-)%s*$", "%1")
      end
      text = text:gsub(".* | ", "")
      table.insert(input, text)
    end

    return displayer(input)
  end

  local get_filename = get_filename_fn()
  return function(entry)
    local filename = vim.F.if_nil(entry.filename, get_filename(entry.bufnr))

    return make_entry.set_default_entry_mt({
      value = entry,
      ordinal = (not hidden and filename or "") .. " " .. entry.text,
      display = make_display,

      bufnr = entry.bufnr,
      filename = filename,
      lnum = entry.lnum,
      col = entry.col,
      text = entry.text,
      start = entry.start,
      finish = entry.finish,
    }, opts)
  end
end

function make_entry.gen_from_lsp_symbols(opts)
  opts = opts or {}

  local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()

  -- Default we have two columns, symbol and type(unbound)
  -- If path is not hidden then its, filepath, symbol and type(still unbound)
  -- If show_line is also set, type is bound to len 8
  local display_items = {
    { width = opts.symbol_width or 25 },
    { remaining = true },
  }

  local hidden = utils.is_path_hidden(opts)
  if not hidden then
    table.insert(display_items, 1, { width = vim.F.if_nil(opts.fname_width, 30) })
  end

  if opts.show_line then
    -- bound type to len 8 or custom
    table.insert(display_items, #display_items, { width = opts.symbol_type_width or 8 })
  end

  local displayer = entry_display.create {
    separator = " ",
    hl_chars = { ["["] = "TelescopeBorder", ["]"] = "TelescopeBorder" },
    items = display_items,
  }
  local type_highlight = vim.F.if_nil(opts.symbol_highlights or lsp_type_highlight)

  local make_display = function(entry)
    local msg

    if opts.show_line then
      msg = vim.trim(vim.F.if_nil(vim.api.nvim_buf_get_lines(bufnr, entry.lnum - 1, entry.lnum, false)[1], ""))
    end

    if hidden then
      return displayer {
        entry.symbol_name,
        { entry.symbol_type:lower(), type_highlight[entry.symbol_type] },
        msg,
      }
    else
      return displayer {
        utils.transform_path(opts, entry.filename),
        entry.symbol_name,
        { entry.symbol_type:lower(), type_highlight[entry.symbol_type] },
        msg,
      }
    end
  end

  local get_filename = get_filename_fn()
  return function(entry)
    local filename = vim.F.if_nil(entry.filename, get_filename(entry.bufnr))
    local symbol_msg = entry.text
    local symbol_type, symbol_name = symbol_msg:match "%[(.+)%]%s+(.*)"
    local ordinal = ""
    if not hidden and filename then
      ordinal = filename .. " "
    end
    ordinal = ordinal .. symbol_name .. " " .. (symbol_type or "unknown")
    return make_entry.set_default_entry_mt({
      value = entry,
      ordinal = ordinal,
      display = make_display,

      filename = filename,
      lnum = entry.lnum,
      col = entry.col,
      symbol_name = symbol_name,
      symbol_type = symbol_type,
      start = entry.start,
      finish = entry.finish,
    }, opts)
  end
end

function make_entry.gen_from_buffer(opts)
  opts = opts or {}

  local disable_devicons = opts.disable_devicons

  local icon_width = 0
  if not disable_devicons then
    local icon, _ = utils.get_devicons("fname", disable_devicons)
    icon_width = strings.strdisplaywidth(icon)
  end

  local displayer = entry_display.create {
    separator = " ",
    items = {
      { width = opts.bufnr_width },
      { width = 4 },
      { width = icon_width },
      { remaining = true },
    },
  }

  local cwd = vim.fn.expand(opts.cwd or vim.loop.cwd())

  local make_display = function(entry)
    -- bufnr_width + modes + icon + 3 spaces + : + lnum
    opts.__prefix = opts.bufnr_width + 4 + icon_width + 3 + 1 + #tostring(entry.lnum)
    local display_bufname = utils.transform_path(opts, entry.filename)
    local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons)

    return displayer {
      { entry.bufnr, "TelescopeResultsNumber" },
      { entry.indicator, "TelescopeResultsComment" },
      { icon, hl_group },
      display_bufname .. ":" .. entry.lnum,
    }
  end

  return function(entry)
    local bufname = entry.info.name ~= "" and entry.info.name or "[No Name]"
    -- if bufname is inside the cwd, trim that part of the string
    bufname = Path:new(bufname):normalize(cwd)

    local hidden = entry.info.hidden == 1 and "h" or "a"
    local readonly = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and "=" or " "
    local changed = entry.info.changed == 1 and "+" or " "
    local indicator = entry.flag .. hidden .. readonly .. changed
    local line_count = vim.api.nvim_buf_line_count(entry.bufnr)

    return make_entry.set_default_entry_mt({
      value = bufname,
      ordinal = entry.bufnr .. " : " .. bufname,
      display = make_display,

      bufnr = entry.bufnr,
      filename = bufname,
      -- account for potentially stale lnum as getbufinfo might not be updated or from resuming buffers picker
      lnum = entry.info.lnum ~= 0 and math.max(math.min(entry.info.lnum, line_count), 1) or 1,
      indicator = indicator,
    }, opts)
  end
end

function make_entry.gen_from_treesitter(opts)
  opts = opts or {}

  local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()

  local display_items = {
    { width = 25 },
    { width = 10 },
    { remaining = true },
  }

  if opts.show_line then
    table.insert(display_items, 2, { width = 6 })
  end

  local displayer = entry_display.create {
    separator = " ",
    items = display_items,
  }

  local type_highlight = opts.symbol_highlights or treesitter_type_highlight

  local make_display = function(entry)
    local msg = vim.api.nvim_buf_get_lines(bufnr, entry.lnum, entry.lnum, false)[1] or ""
    msg = vim.trim(msg)

    local display_columns = {
      entry.text,
      { entry.kind, type_highlight[entry.kind], type_highlight[entry.kind] },
      msg,
    }
    if opts.show_line then
      table.insert(display_columns, 2, { entry.lnum .. ":" .. entry.col, "TelescopeResultsLineNr" })
    end

    return displayer(display_columns)
  end

  local get_filename = get_filename_fn()
  return function(entry)
    local ts_utils = require "nvim-treesitter.ts_utils"
    local start_row, start_col, end_row, _ = ts_utils.get_node_range(entry.node)
    local node_text = vim.treesitter.get_node_text(entry.node, bufnr)
    return make_entry.set_default_entry_mt({
      value = entry.node,
      kind = entry.kind,
      ordinal = node_text .. " " .. (entry.kind or "unknown"),
      display = make_display,

      node_text = node_text,

      filename = get_filename(bufnr),
      -- need to add one since the previewer substacts one
      lnum = start_row + 1,
      col = start_col,
      text = node_text,
      start = start_row,
      finish = end_row,
    }, opts)
  end
end

function make_entry.gen_from_packages(opts)
  opts = opts or {}

  local make_display = function(module_name)
    local p_path = package.searchpath(module_name, package.path) or ""
    local display = string.format("%-" .. opts.column_len .. "s : %s", module_name, vim.fn.fnamemodify(p_path, ":~:."))

    return display
  end

  return function(module_name)
    return make_entry.set_default_entry_mt({
      valid = module_name ~= "",
      value = module_name,
      ordinal = module_name,
      display = make_display(module_name),
    }, opts)
  end
end

function make_entry.gen_from_apropos(opts)
  local sections = {}
  if #opts.sections == 1 and opts.sections[1] == "ALL" then
    setmetatable(sections, {
      __index = function()
        return true
      end,
    })
  else
    for _, section in ipairs(opts.sections) do
      sections[section] = true
    end
  end

  local displayer = entry_display.create {
    separator = " ",
    items = {
      { width = 30 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.keyword, "TelescopeResultsFunction" },
      entry.description,
    }
  end

  return function(line)
    local keyword, cmd, section, desc = line:match "^((.-)%s*%(([^)]+)%).-)%s+%-%s+(.*)$"
    -- apropos might return alternatives for the cmd which are split on `,` and breaks everything else
    -- for example on void linux it will return `alacritty, Alacritty` which will later result in
    -- `man 1 alacritty, Alacritty`. So we just take the first one.
    -- doing this outside of regex because of obvious reasons
    cmd = vim.split(cmd, ",")[1]
    return keyword
        and sections[section]
        and make_entry.set_default_entry_mt({
          value = cmd,
          description = desc,
          ordinal = cmd,
          display = make_display,
          section = section,
          keyword = keyword,
        }, opts)
      or nil
  end
end

function make_entry.gen_from_marks(opts)
  return function(item)
    return make_entry.set_default_entry_mt({
      value = item.line,
      ordinal = item.line,
      display = item.line,
      lnum = item.lnum,
      col = item.col,
      start = item.lnum,
      filename = item.filename,
    }, opts)
  end
end

function make_entry.gen_from_registers(opts)
  local displayer = entry_display.create {
    separator = " ",
    hl_chars = { ["["] = "TelescopeBorder", ["]"] = "TelescopeBorder" },
    items = {
      { width = 3 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    local content = entry.content
    return displayer {
      { "[" .. entry.value .. "]", "TelescopeResultsNumber" },
      type(content) == "string" and content:gsub("\n", "\\n") or content,
    }
  end

  return function(entry)
    local contents = vim.fn.getreg(entry)
    return make_entry.set_default_entry_mt({
      value = entry,
      ordinal = string.format("%s %s", entry, contents),
      content = contents,
      display = make_display,
    }, opts)
  end
end

function make_entry.gen_from_keymaps(opts)
  local function get_desc(entry)
    if entry.callback and not entry.desc then
      return require("telescope.actions.utils")._get_anon_function_name(entry.callback)
    end
    return vim.F.if_nil(entry.desc, entry.rhs)
  end

  local function get_lhs(entry)
    return utils.display_termcodes(entry.lhs)
  end

  local function get_attr(entry)
    local ret = ""
    if entry.value.noremap ~= 0 then
      ret = ret .. "*"
    end
    if entry.value.buffer ~= 0 then
      ret = ret .. "@"
    end
    return ret
  end

  local displayer = require("telescope.pickers.entry_display").create {
    separator = "▏",
    items = {
      { width = 3 },
      { width = opts.width_lhs },
      { width = 2 },
      { remaining = true },
    },
  }
  local make_display = function(entry)
    return displayer {
      entry.mode,
      get_lhs(entry),
      get_attr(entry),
      get_desc(entry),
    }
  end

  return function(entry)
    return make_entry.set_default_entry_mt({
      mode = entry.mode,
      lhs = get_lhs(entry),
      desc = get_desc(entry),
      --
      valid = entry ~= "",
      value = entry,
      ordinal = entry.mode .. " " .. get_lhs(entry) .. " " .. get_desc(entry),
      display = make_display,
    }, opts)
  end
end

function make_entry.gen_from_highlights(opts)
  local make_display = function(entry)
    local display = entry.value
    return display, { { { 0, #display }, display } }
  end

  return function(entry)
    return make_entry.set_default_entry_mt({
      value = entry,
      display = make_display,
      ordinal = entry,
    }, opts)
  end
end

function make_entry.gen_from_picker(opts)
  local displayer = entry_display.create {
    separator = " │ ",
    items = {
      { width = 0.5 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      entry.value.prompt_title,
      entry.value.default_text,
    }
  end

  return function(entry)
    return make_entry.set_default_entry_mt({
      value = entry,
      text = entry.prompt_title,
      ordinal = string.format("%s %s", entry.prompt_title, vim.F.if_nil(entry.default_text, "")),
      display = make_display,
    }, opts)
  end
end

function make_entry.gen_from_buffer_lines(opts)
  local displayer = entry_display.create {
    separator = " │ ",
    items = {
      { width = 5 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.lnum, opts.lnum_highlight_group or "TelescopeResultsSpecialComment" },
      {
        entry.text,
        function()
          if not opts.line_highlights then
            return {}
          end

          local line_hl = opts.line_highlights[entry.lnum] or {}
          -- TODO: We could probably squash these together if the are the same...
          --        But I don't think that it's worth it at the moment.
          local result = {}

          for col, hl in pairs(line_hl) do
            table.insert(result, { { col, col + 1 }, hl })
          end

          return result
        end,
      },
    }
  end

  return function(entry)
    if opts.skip_empty_lines and string.match(entry.text, "^$") then
      return
    end

    return make_entry.set_default_entry_mt({
      ordinal = entry.text,
      display = make_display,
      filename = entry.filename,
      lnum = entry.lnum,
      text = entry.text,
    }, opts)
  end
end

function make_entry.gen_from_vimoptions(opts)
  local displayer = entry_display.create {
    separator = "",
    hl_chars = { ["["] = "TelescopeBorder", ["]"] = "TelescopeBorder" },
    items = {
      { width = 25 },
      { width = 12 },
      { width = 11 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.value.name, "Keyword" },
      { "[" .. entry.value.type .. "]", "Type" },
      { "[" .. entry.value.scope .. "]", "Identifier" },
      utils.display_termcodes(tostring(entry.value.value)),
    }
  end

  return function(o)
    local entry = {
      display = make_display,
      value = {
        name = o.name,
        value = o.default,
        type = o.type,
        scope = o.scope,
      },
      ordinal = string.format("%s %s %s", o.name, o.type, o.scope),
    }

    local ok, value = pcall(vim.api.nvim_get_option, o.name)
    if ok then
      entry.value.value = value
      entry.ordinal = entry.ordinal .. " " .. utils.display_termcodes(tostring(value))
    else
      entry.ordinal = entry.ordinal .. " " .. utils.display_termcodes(tostring(o.default))
    end

    return make_entry.set_default_entry_mt(entry, opts)
  end
end

function make_entry.gen_from_ctags(opts)
  opts = opts or {}

  local cwd = vim.fn.expand(opts.cwd or vim.loop.cwd())
  local current_file = Path:new(vim.api.nvim_buf_get_name(opts.bufnr)):normalize(cwd)

  local display_items = {
    { remaining = true },
  }

  local idx = 1
  local hidden = utils.is_path_hidden(opts)
  if not hidden then
    table.insert(display_items, idx, { width = vim.F.if_nil(opts.fname_width, 30) })
    idx = idx + 1
  end

  if opts.show_line then
    table.insert(display_items, idx, { width = 30 })
  end

  local displayer = entry_display.create {
    separator = " │ ",
    items = display_items,
  }

  local make_display = function(entry)
    local filename = utils.transform_path(opts, entry.filename)

    local scode
    if opts.show_line then
      scode = entry.scode
    end

    if hidden then
      return displayer {
        entry.tag,
        scode,
      }
    else
      return displayer {
        filename,
        entry.tag,
        scode,
      }
    end
  end

  local mt = {}
  mt.__index = function(t, k)
    local override = handle_entry_index(opts, t, k)
    if override then
      return override
    end

    if k == "path" then
      local retpath = Path:new({ t.filename }):absolute()
      if not vim.loop.fs_access(retpath, "R", nil) then
        retpath = t.filename
      end
      return retpath
    end
  end

  local current_file_cache = {}
  return function(line)
    if line == "" or line:sub(1, 1) == "!" then
      return nil
    end

    local tag, file, scode, lnum
    -- ctags gives us: 'tags\tfile\tsource'
    tag, file, scode = string.match(line, '([^\t]+)\t([^\t]+)\t/^?\t?(.*)/;"\t+.*')
    if not tag then
      -- hasktags gives us: 'tags\tfile\tlnum'
      tag, file, lnum = string.match(line, "([^\t]+)\t([^\t]+)\t(%d+).*")
    end

    if Path.path.sep == "\\" then
      file = string.gsub(file, "/", "\\")
    end

    if opts.only_current_file then
      if current_file_cache[file] == nil then
        current_file_cache[file] = Path:new(file):normalize(cwd) == current_file
      end

      if current_file_cache[file] == false then
        return nil
      end
    end

    local tag_entry = {}
    if opts.only_sort_tags then
      tag_entry.ordinal = tag
    else
      tag_entry.ordinal = file .. ": " .. tag
    end

    tag_entry.display = make_display
    tag_entry.scode = scode
    tag_entry.tag = tag
    tag_entry.filename = file
    tag_entry.col = 1
    tag_entry.lnum = lnum and tonumber(lnum) or 1

    return setmetatable(tag_entry, mt)
  end
end

function make_entry.gen_from_diagnostics(opts)
  opts = opts or {}

  local signs = (function()
    if opts.no_sign then
      return
    end
    local signs = {}
    local type_diagnostic = vim.diagnostic.severity
    for _, severity in ipairs(type_diagnostic) do
      local status, sign = pcall(function()
        -- only the first char is upper all others are lowercalse
        return vim.trim(vim.fn.sign_getdefined("DiagnosticSign" .. severity:lower():gsub("^%l", string.upper))[1].text)
      end)
      if not status then
        sign = severity:sub(1, 1)
      end
      signs[severity] = sign
    end
    return signs
  end)()

  local display_items = {
    { width = signs ~= nil and 10 or 8 },
    { remaining = true },
  }
  local line_width = vim.F.if_nil(opts.line_width, 0.5)
  local hidden = utils.is_path_hidden(opts)
  if not hidden then
    table.insert(display_items, 2, { width = line_width })
  end
  local displayer = entry_display.create {
    separator = "▏",
    items = display_items,
  }

  local make_display = function(entry)
    local filename = utils.transform_path(opts, entry.filename)

    -- add styling of entries
    local pos = string.format("%4d:%2d", entry.lnum, entry.col)
    local line_info = {
      (signs and signs[entry.type] .. " " or "") .. pos,
      "DiagnosticSign" .. entry.type,
    }

    return displayer {
      line_info,
      entry.text,
      filename,
    }
  end

  return function(entry)
    return make_entry.set_default_entry_mt({
      value = entry,
      ordinal = ("%s %s"):format(not hidden and entry.filename or "", entry.text),
      display = make_display,
      filename = entry.filename,
      type = entry.type,
      lnum = entry.lnum,
      col = entry.col,
      text = entry.text,
    }, opts)
  end
end

function make_entry.gen_from_autocommands(opts)
  local displayer = entry_display.create {
    separator = "▏",
    items = {
      { width = 14 },
      { width = 18 },
      { width = 16 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    return displayer {
      { entry.value.event, "vimAutoEvent" },
      { entry.value.group_name, "vimAugroup" },
      { entry.value.pattern, "vimAutoCmdSfxList" },
      entry.value.command,
    }
  end

  return function(entry)
    local group_name = vim.F.if_nil(entry.group_name, "<anonymous>")
    local command = entry.command
    if entry.desc and (entry.callback or vim.startswith(command, "<lua: ")) then
      command = entry.desc
    end
    if command == nil or command == "" then
      command = "<lua function>"
    end
    return make_entry.set_default_entry_mt({
      value = {
        event = entry.event,
        group_name = group_name,
        pattern = entry.pattern,
        command = command,
      },
      --
      ordinal = entry.event .. " " .. group_name .. " " .. entry.pattern .. " " .. entry.command,
      display = make_display,
    }, opts)
  end
end

function make_entry.gen_from_commands(opts)
  local displayer = entry_display.create {
    separator = "▏",
    items = {
      { width = 0.2 },
      { width = 4 },
      { width = 4 },
      { width = 11 },
      { remaining = true },
    },
  }

  local make_display = function(entry)
    local attrs = ""
    if entry.bang then
      attrs = attrs .. "!"
    end
    if entry.bar then
      attrs = attrs .. "|"
    end
    if entry.register then
      attrs = attrs .. '"'
    end
    return displayer {
      { entry.name, "TelescopeResultsIdentifier" },
      attrs,
      entry.nargs,
      entry.complete or "",
      entry.definition:gsub("\n", " "),
    }
  end

  return function(entry)
    return make_entry.set_default_entry_mt({
      name = entry.name,
      bang = entry.bang,
      nargs = entry.nargs,
      complete = entry.complete,
      definition = entry.definition,
      --
      value = entry,
      ordinal = entry.name,
      display = make_display,
    }, opts)
  end
end

local git_icon_defaults = {
  added = "+",
  changed = "~",
  copied = ">",
  deleted = "-",
  renamed = "➡",
  unmerged = "‡",
  untracked = "?",
}

function make_entry.gen_from_git_status(opts)
  opts = opts or {}

  local col_width = ((opts.git_icons and opts.git_icons.added) and opts.git_icons.added:len() + 2) or 2
  local displayer = entry_display.create {
    separator = "",
    items = {
      { width = col_width },
      { width = col_width },
      { remaining = true },
    },
  }

  local icons = vim.tbl_extend("keep", opts.git_icons or {}, git_icon_defaults)

  local git_abbrev = {
    ["A"] = { icon = icons.added, hl = "TelescopeResultsDiffAdd" },
    ["U"] = { icon = icons.unmerged, hl = "TelescopeResultsDiffAdd" },
    ["M"] = { icon = icons.changed, hl = "TelescopeResultsDiffChange" },
    ["C"] = { icon = icons.copied, hl = "TelescopeResultsDiffChange" },
    ["R"] = { icon = icons.renamed, hl = "TelescopeResultsDiffChange" },
    ["D"] = { icon = icons.deleted, hl = "TelescopeResultsDiffDelete" },
    ["?"] = { icon = icons.untracked, hl = "TelescopeResultsDiffUntracked" },
  }

  local make_display = function(entry)
    local x = string.sub(entry.status, 1, 1)
    local y = string.sub(entry.status, -1)
    local status_x = git_abbrev[x] or {}
    local status_y = git_abbrev[y] or {}

    local empty_space = " "
    return displayer {
      { status_x.icon or empty_space, status_x.hl },
      { status_y.icon or empty_space, status_y.hl },
      entry.value,
    }
  end

  return function(entry)
    if entry == "" then
      return nil
    end
    local mod, file = string.match(entry, "(..).*%s[->%s]?(.+)")

    return setmetatable({
      value = file,
      status = mod,
      ordinal = entry,
      display = make_display,
      path = Path:new({ opts.cwd, file }):absolute(),
    }, opts)
  end
end

return make_entry