Class: NvimControl::DataExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/nvim_control/data_extractor.rb

Class Method Summary collapse

Class Method Details

.context(client:) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/nvim_control/data_extractor.rb', line 5

def self.context(client:)
  {
    cursor: cursor(client: client),
    file: file(client: client),
    selection: visual_selection(client: client),
    diagnostics: diagnostics(client: client)
  }
end

.cursor(client:) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/nvim_control/data_extractor.rb', line 14

def self.cursor(client:)
  cursor = client.current.window.cursor
  { line: cursor[0], col: cursor[1] }
rescue StandardError => e
  raise OperationError,
        "Failed to get cursor info: #{e.message}",
        e.backtrace
end

.diagnostics(client:) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nvim_control/data_extractor.rb', line 41

def self.diagnostics(client:)
  client.eval("vim.diagnostic.get(0)").map do |diagnostic|
    {
      line: diagnostic["lnum"] + 1,
      col: diagnostic["col"] + 1,
      message: diagnostic["message"],
      severity: diagnostic["severity"]
    }
  end
rescue StandardError
  []
end

.file(client:) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/nvim_control/data_extractor.rb', line 23

def self.file(client:)
  client.current.buffer.name
rescue StandardError => e
  raise OperationError,
        "Failed to get file info: #{e.message}",
        e.backtrace
end

.visual_selection(client:) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/nvim_control/data_extractor.rb', line 31

def self.visual_selection(client:)
  return nil unless visual_mode?(client)

  marks = visual_marks(client)
  text = selected_text(client, marks)
  build_selection_info(marks, text)
rescue StandardError
  nil
end