summaryrefslogtreecommitdiff
path: root/lua/telescope/command.lua
blob: 978d5be3ae6b9dbe536697436bdc5a029bb1de3f (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
local themes = require('telescope.themes')
local builtin = require('telescope.builtin')
local extensions = require('telescope._extensions').manager
local config = require('telescope.config')
local command = {}

local arg_value = {
  ['nil'] = nil,['""'] = '',['"'] = ''
}

local bool_type = {
  ['false'] = false,['true'] = true
}

-- convert command line string arguments to
-- lua number boolean type and nil value
local function convert_user_opts(user_opts)
  local default_opts = config.values

  local _switch = {
    ['boolean'] = function(key,val)
      if val == 'false' then
        user_opts[key] = false
        return
      end
      user_opts[key] = true
    end,
    ['number'] = function(key,val)
      user_opts[key] = tonumber(val)
    end,
    ['string'] = function(key,val)
      if arg_value[val] ~= nil then
        user_opts[key] = arg_value[val]
        return
      end

      if bool_type[val] ~= nil then
        user_opts[key] = bool_type[val]
      end
    end
  }

  local _switch_metatable = {
    __index = function(_,k)
      print(string.format('Type of %s does not match',k))
    end
  }

  setmetatable(_switch,_switch_metatable)

  for key,val in pairs(user_opts) do
    if default_opts[key] ~= nil then
      _switch[type(default_opts[key])](key,val)
    else
      _switch['string'](key,val)
    end
  end
end

-- receive the viml command args
-- it should be a table value like
-- {
--   cmd = 'find_files',
--   theme = 'dropdown',
--   extension_type  = 'command'
--   opts = {
--      cwd = '***',
-- }
local function run_command(args)
  local user_opts = args or {}
  if next(user_opts) == nil and not user_opts.cmd then
    print('[Telescope] your command miss args')
    return
  end

  local cmd = user_opts.cmd
  local opts = user_opts.opts or {}
  local extension_type = user_opts.extension_type or ''
  local theme = user_opts.theme or ''

  if next(opts) ~= nil then
    convert_user_opts(opts)
  end

  if string.len(theme) > 0 then
    opts = themes[theme](opts)
  end

  if string.len(extension_type) > 0 and extension_type ~= '"' then
    extensions[cmd][extension_type](opts)
    return
  end

  if builtin[cmd] then
    builtin[cmd](opts)
    return
  end

  if rawget(extensions,cmd) then
    extensions[cmd][cmd](opts)
  end
end

-- @Summary get extensions sub command
-- register extensions dap gh etc.
-- input in command line `Telescope gh <TAB>`
-- Returns a list for each extension.
function command.get_extensions_subcommand()
  local exts = require('telescope._extensions').manager
  local complete_ext_table = {}
  for cmd,value in pairs(exts) do
    if type(value) == "table" then
      local subcmds = {}
      for key,_ in pairs(value) do
        table.insert(subcmds, key)
      end
      complete_ext_table[cmd] = subcmds
    end
  end
  return complete_ext_table
end

local split_keywords = {
  ['find_command'] = true,
  ['vimgrep_arguments'] = true,
  ['sections'] = true
}

function command.register_keyword(keyword)
  split_keywords[keyword] = true
end

function command.load_command(cmd,...)
  local args = {...}
  if cmd == nil then
    run_command({cmd = 'builtin'})
    return
  end

  local user_opts = {}
  user_opts['cmd'] = cmd
  user_opts.opts = {}

  for _,arg in ipairs(args) do
    if arg:find('=',1) == nil then
      user_opts['extension_type'] = arg
    else
      local param = vim.split(arg,'=')
      if param[1] == 'theme' then
        user_opts['theme'] = param[2]
      elseif split_keywords[param[1]] then
        user_opts.opts[param[1]] = vim.split(param[2],',')
      else
        user_opts.opts[param[1]] = param[2]
      end
    end
  end

  run_command(user_opts)
end

return command