Module: RuVim::Git::Grep::HandlerMethods

Included in:
Handler
Defined in:
lib/ruvim/git/grep.rb

Instance Method Summary collapse

Instance Method Details

#git_grep(ctx, argv: []) ⇒ Object



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
# File 'lib/ruvim/git/grep.rb', line 37

def git_grep(ctx, argv: [], **)
  if argv.empty?
    ctx.editor.echo_error("Usage: :git grep <pattern> [<args>...]")
    return
  end

  file_path = git_resolve_path(ctx)
  unless file_path
    ctx.editor.echo_error("No file or directory to resolve git repo")
    return
  end

  lines, root, err = Grep.run(file_path, args: argv)
  unless lines
    ctx.editor.echo_error("git grep: #{err}")
    return
  end

  if lines.empty?
    ctx.editor.echo("No matches found")
    return
  end

  buf = ctx.editor.add_virtual_buffer(
    kind: :git_grep,
    name: "[Git Grep]",
    lines: lines,
    readonly: true,
    modifiable: false
  )
  buf.options["git_repo_root"] = root
  ctx.editor.switch_to_buffer(buf.id)
  bind_git_buffer_keys(ctx.editor, buf.id)
  ctx.editor.echo("[Git Grep] #{lines.length} match(es)")
end

#git_grep_open_file(ctx) ⇒ Object



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
# File 'lib/ruvim/git/grep.rb', line 73

def git_grep_open_file(ctx, **)
  buf = ctx.buffer
  unless buf.kind == :git_grep
    ctx.editor.echo_error("Not a git grep buffer")
    return
  end

  line = buf.lines[ctx.window.cursor_y]
  location = Grep.parse_location(line)
  unless location
    ctx.editor.echo_error("No file on this line")
    return
  end

  filename, line_num = location
  root = buf.options["git_repo_root"]
  full_path = File.join(root, filename)
  unless File.exist?(full_path)
    ctx.editor.echo_error("File not found: #{filename}")
    return
  end

  existing = ctx.editor.buffers.values.find { |b| b.path == full_path }
  if existing
    ctx.editor.switch_to_buffer(existing.id)
  else
    new_buf = ctx.editor.add_buffer_from_file(full_path)
    ctx.editor.switch_to_buffer(new_buf.id)
  end
  ctx.editor.current_window.cursor_y = [line_num - 1, 0].max
end