Skip to content

Commit

Permalink
Compute line number before diff application in diff_get_lineno
Browse files Browse the repository at this point in the history
  • Loading branch information
krobelus committed May 24, 2020
1 parent 7484796 commit 3aa25e8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/tig/diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void diff_restore_line(struct view *view, struct diff_state *state);
enum status_code diff_init_highlight(struct view *view, struct diff_state *state);
bool diff_done_highlight(struct diff_state *state);

unsigned int diff_get_lineno(struct view *view, struct line *line);
unsigned int diff_get_lineno(struct view *view, struct line *line, bool old);
const char *diff_get_pathname(struct view *view, struct line *line);

extern struct view diff_view;
Expand Down
16 changes: 8 additions & 8 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ diff_save_line(struct view *view, struct diff_state *state, enum open_flags flag

if (file) {
state->file = get_path(file);
state->lineno = diff_get_lineno(view, line);
state->lineno = diff_get_lineno(view, line, false);
state->pos = view->pos;
}
}
Expand Down Expand Up @@ -479,7 +479,7 @@ diff_restore_line(struct view *view, struct diff_state *state)
return;

while ((line = find_next_line_by_type(view, line, LINE_DIFF_CHUNK))) {
unsigned int lineno = diff_get_lineno(view, line);
unsigned int lineno = diff_get_lineno(view, line, false);

for (line++; view_has_line(view, line) && line->type != LINE_DIFF_CHUNK; line++) {
if (lineno == state->lineno) {
Expand Down Expand Up @@ -605,7 +605,7 @@ diff_blame_line(const char *ref, const char *file, unsigned long lineno,
}

unsigned int
diff_get_lineno(struct view *view, struct line *line)
diff_get_lineno(struct view *view, struct line *line, bool old)
{
const struct line *header, *chunk;
unsigned int lineno;
Expand All @@ -625,11 +625,11 @@ diff_get_lineno(struct view *view, struct line *line)
if (!parse_chunk_header(&chunk_header, box_text(chunk)))
return 0;

lineno = chunk_header.new.position;
lineno = old ? chunk_header.old.position : chunk_header.new.position;

for (chunk++; chunk < line; chunk++)
if (chunk->type != LINE_DIFF_DEL &&
chunk->type != LINE_DIFF_DEL2)
if (old ? chunk->type != LINE_DIFF_ADD && chunk->type != LINE_DIFF_ADD2
: chunk->type != LINE_DIFF_DEL && chunk->type != LINE_DIFF_DEL2)
lineno++;

return lineno;
Expand Down Expand Up @@ -761,7 +761,7 @@ diff_common_edit(struct view *view, enum request request, struct line *line)
lineno = view->env->lineno;
} else {
file = diff_get_pathname(view, line);
lineno = diff_get_lineno(view, line);
lineno = diff_get_lineno(view, line, false);
}

if (!file) {
Expand Down Expand Up @@ -827,7 +827,7 @@ diff_common_select(struct view *view, struct line *line, const char *changes_msg
if (changes_msg)
string_format(view->ref, "%s to '%s'", changes_msg, file);
string_format(view->env->file, "%s", file);
view->env->lineno = view->env->goto_lineno = diff_get_lineno(view, line);
view->env->lineno = view->env->goto_lineno = diff_get_lineno(view, line, false);
view->env->blob[0] = 0;
} else {
string_ncopy(view->ref, view->ops->id, strlen(view->ops->id));
Expand Down
4 changes: 2 additions & 2 deletions src/stage.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ stage_request(struct view *view, enum request request, struct line *line)
if (stage_line_type == LINE_STAT_UNTRACKED) {
open_editor(stage_status.new.name, (line - view->line) + 1);
} else {
open_editor(stage_status.new.name, diff_get_lineno(view, line));
open_editor(stage_status.new.name, diff_get_lineno(view, line, false));
}
break;

Expand All @@ -445,7 +445,7 @@ stage_request(struct view *view, enum request request, struct line *line)
}

view->env->ref[0] = 0;
view->env->goto_lineno = diff_get_lineno(view, line);
view->env->goto_lineno = diff_get_lineno(view, line, false);
if (view->env->goto_lineno > 0)
view->env->goto_lineno--;
return request;
Expand Down

0 comments on commit 3aa25e8

Please sign in to comment.