Class: Vimdb::Commands

Inherits:
Item
  • Object
show all
Defined in:
lib/vimdb/commands.rb

Instance Method Summary (collapse)

Methods inherited from Item

all, #default_field, inherited, instance, item_name, #key, #search, #tempfile

Constructor Details

- (Commands) initialize

Returns a new instance of Commands



2
3
4
# File 'lib/vimdb/commands.rb', line 2

def initialize
  @plugins_dir = Vimdb.plugins_dir
end

Instance Method Details

- (Object) create



6
7
8
9
# File 'lib/vimdb/commands.rb', line 6

def create
  cmds = parse_index_file create_index_file
  cmds + parse_command_file(create_command_file)
end

- (Object) create_command_file



37
38
39
40
41
# File 'lib/vimdb/commands.rb', line 37

def create_command_file
  tempfile(:commands_command) do |file|
    vim "redir! > #{file}", 'exe "silent! verbose command"', 'redir END'
  end
end

- (Object) create_index_file



11
12
13
14
15
# File 'lib/vimdb/commands.rb', line 11

def create_index_file
  tempfile(:commands_index) do |file|
    vim 'silent help index.txt', "silent! w! #{file}"
  end
end

- (Object) fields



63
64
65
# File 'lib/vimdb/commands.rb', line 63

def fields
  [:name, :alias, :from, :desc]
end

- (Object) info



67
68
69
# File 'lib/vimdb/commands.rb', line 67

def info
  "Created using index.txt and :command"
end

- (Object) parse_command_file(file)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vimdb/commands.rb', line 43

def parse_command_file(file)
  lines = File.read(file).strip.split("\n")
  completions = Regexp.union('dir', 'file', 'buffer', 'shellcmd', 'customlist')

  lines.slice_before {|e| e !~ /Last set/ }.map do |arr|
    cmd = {}

    cmd[:file] = arr[1].to_s[%r{Last set from (\S+)}, 1] or next
    match = cmd[:file].to_s.match(%r{/#{@plugins_dir}/(?<plugin>[^/]+)})
    cmd[:from] = match ? match[:plugin] + ' plugin' : 'user'
    cmd[:name]  = arr[0][/^(?:[!b" ]+)(\S+)/, 1]
    cmd[:desc]  = arr[0][/^(?:[!b" ]+)\S+\s*(.*)$/, 1]
    if cmd[:desc][/^(\*|\+|\?|\d)\s+(\dc?|%|\.)?\s*(#{completions})?\s*(.*)/]
      cmd[:args] = $1
      cmd[:desc] = $4
    end
    cmd
  end.compact
end

- (Object) parse_index_file(file)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vimdb/commands.rb', line 17

def parse_index_file(file)
  lines = File.read(file)[/={10,}\n5. EX commands.*/m].split("\n")

  cmds = []
  lines.each do |line|
    if line =~ /^(\S+)\t+(\S+)\t+([^\t]+)$/
      cmd = { name: $2, tag: $1, desc: $3, from: 'default' }
      cmd[:name].sub!(/^:/, '')
      if cmd[:name][/^(.*)\[([a-z]+)\]$/]
        cmd[:alias] = $1
        cmd[:name] = $1 + $2
      end
      cmds << cmd
    elsif line =~ /^\t+([^\t]+)$/
      cmds[-1][:desc] << ' ' + $1
    end
  end
  cmds
end