Module: RuVim::Git::Diff::HandlerMethods

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

Overview

Command handler methods

Instance Method Summary collapse

Instance Method Details

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



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

def git_diff(ctx, argv: [], **)
  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 = Diff.run(file_path, args: argv)
  unless lines
    ctx.editor.echo_error("git diff: #{err}")
    return
  end

  if lines.empty?
    ctx.editor.echo("No diff output (working tree clean)")
    return
  end

  buf = ctx.editor.add_virtual_buffer(
    kind: :git_diff,
    name: "[Git Diff]",
    lines: lines,
    filetype: "diff",
    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 Diff]")
end

#git_diff_open_file(ctx) ⇒ Object



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

def git_diff_open_file(ctx, **)
  buf = ctx.buffer
  unless buf.kind == :git_diff || buf.kind == :git_log
    ctx.editor.echo_error("Not a git diff buffer")
    return
  end

  filename, line_num = Diff.parse_location(buf.lines, ctx.window.cursor_y)
  unless filename
    ctx.editor.echo_error("No file on this line")
    return
  end

  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