Class: Torkify::Vim::Quickfix::API

Inherits:
Object
  • Object
show all
Defined in:
lib/torkify/vim/quickfix.rb

Instance Method Summary collapse

Constructor Details

#initialize(vim) ⇒ API

Returns a new instance of API.



5
6
7
8
# File 'lib/torkify/vim/quickfix.rb', line 5

def initialize(vim)
  @stringifier = Stringifier.new
  @vim = vim
end

Instance Method Details

#buffer_from_file(file) ⇒ Object



25
26
27
# File 'lib/torkify/vim/quickfix.rb', line 25

def buffer_from_file(file)
  @vim.expr("bufnr(\"#{file}\")").to_i
end

#clearObject



41
42
43
44
45
# File 'lib/torkify/vim/quickfix.rb', line 41

def clear
  @vim.expr("setqflist([])")
  @vim.send ':cclose<CR>'
  self
end

#getObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/torkify/vim/quickfix.rb', line 10

def get
  existing = "[" + @vim.expr('getqflist()').gsub(/(?<=})\n(?={)/, ",") + "]"
  qflist = existing.gsub(/(?<=[^\\])"/, '\"')
                   .gsub("'", '"')
                   .gsub("\n", '\n')
  if qflist.length > 0
    JSON.parse(qflist)
  else
    []
  end
rescue => e
  Torkify.logger.fatal { "Couldn't retrieve vim quickfix list: {#{e.class.name}} #{e.message}" }
  []
end

#openObject



47
48
49
50
51
# File 'lib/torkify/vim/quickfix.rb', line 47

def open
  if @vim.expr("mode()") == "n"
    @vim.send ':cw<CR>'
  end
end

#set(errors) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/torkify/vim/quickfix.rb', line 29

def set(errors)
  error_strings = errors.map { |e| @stringifier.convert e }
  if error_strings.any?
    error_strings.each_slice(100).each_with_index do |strings, i|
      action = i == 0 ? '' : ', "a"'
      @vim.expr("setqflist([#{strings.join(",")}]#{action})")
    end
  else
    clear
  end
end