Have one single source view for all the hunks

This commit is contained in:
Ignacio Casal Quinteiro 2015-12-18 22:38:11 +01:00
parent 2f636af5a2
commit 7cfd5a21e8
7 changed files with 537 additions and 691 deletions

View file

@ -63,7 +63,6 @@ libgitg_libgitg_1_0_la_VALASOURCES = \
libgitg/gitg-diff-stat.vala \
libgitg/gitg-diff-view.vala \
libgitg/gitg-diff-view-file.vala \
libgitg/gitg-diff-view-hunk.vala \
libgitg/gitg-diff-view-lines-renderer.vala \
libgitg/gitg-diff-view-commit-details.vala \
libgitg/gitg-diff-view-options.vala \

View file

@ -26,15 +26,17 @@ class Gitg.DiffViewFile : Gtk.Grid
[GtkChild( name = "label_file_header" )]
private Gtk.Label d_label_file_header;
[GtkChild( name = "grid_hunks" )]
private Gtk.Grid d_grid_hunks;
[GtkChild( name = "diff_stat_file" )]
private DiffStat d_diff_stat_file;
[GtkChild( name = "revealer_hunks" )]
private Gtk.Revealer d_revealer_hunks;
[GtkChild( name = "sourceview_hunks" )]
private Gtk.SourceView d_sourceview_hunks;
private string d_selection_category = "selection";
private bool d_expanded;
public bool expanded
@ -67,12 +69,30 @@ class Gitg.DiffViewFile : Gtk.Grid
public bool wrap
{
get; set;
get { return d_sourceview_hunks.wrap_mode != Gtk.WrapMode.NONE; }
set
{
if (value)
{
d_sourceview_hunks.wrap_mode = Gtk.WrapMode.WORD_CHAR;
}
else
{
d_sourceview_hunks.wrap_mode = Gtk.WrapMode.NONE;
}
}
}
public int tab_width
{
get; set;
get { return (int)d_sourceview_hunks.tab_width; }
set
{
if (value > 0)
{
d_sourceview_hunks.tab_width = (uint)value;
}
}
}
public int maxlines
@ -104,6 +124,321 @@ class Gitg.DiffViewFile : Gtk.Grid
Object(delta: delta, handle_selection: handle_selection);
}
private DiffViewLinesRenderer d_old_lines;
private DiffViewLinesRenderer d_new_lines;
private DiffViewLinesRenderer d_sym_lines;
construct
{
var gutter = d_sourceview_hunks.get_gutter(Gtk.TextWindowType.LEFT);
d_old_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.OLD);
d_new_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.NEW);
d_sym_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.SYMBOL);
this.bind_property("maxlines", d_old_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
this.bind_property("maxlines", d_new_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
d_old_lines.xpad = 8;
d_new_lines.xpad = 8;
d_sym_lines.xpad = 6;
gutter.insert(d_old_lines, 0);
gutter.insert(d_new_lines, 1);
gutter.insert(d_sym_lines, 2);
if (handle_selection)
{
d_sourceview_hunks.button_press_event.connect(button_press_event_on_view);
d_sourceview_hunks.motion_notify_event.connect(motion_notify_event_on_view);
d_sourceview_hunks.button_release_event.connect(button_release_event_on_view);
d_sourceview_hunks.get_style_context().add_class("handle-selection");
d_sourceview_hunks.realize.connect(update_cursor);
d_sourceview_hunks.notify["state-flags"].connect(update_cursor);
}
d_sourceview_hunks.set_border_window_size(Gtk.TextWindowType.TOP, 1);
var settings = Gtk.Settings.get_default();
settings.notify["gtk-application-prefer-dark-theme"].connect(update_theme);
update_theme();
}
private void update_cursor()
{
var window = d_sourceview_hunks.get_window(Gtk.TextWindowType.TEXT);
if (window == null)
{
return;
}
var cursor = new Gdk.Cursor.for_display(d_sourceview_hunks.get_display(), Gdk.CursorType.LEFT_PTR);
window.set_cursor(cursor);
}
private void update_theme()
{
var selection_attributes = new Gtk.SourceMarkAttributes();
var header_attributes = new Gtk.SourceMarkAttributes();
var added_attributes = new Gtk.SourceMarkAttributes();
var removed_attributes = new Gtk.SourceMarkAttributes();
var settings = Gtk.Settings.get_default();
if (settings.gtk_application_prefer_dark_theme)
{
selection_attributes.background = Gdk.RGBA() { red = 52.0 / 255.0, green = 101.0 / 255.0, blue = 164.0 / 255.0, alpha = 1.0 };
header_attributes.background = Gdk.RGBA() { red = 224.0 / 255.0, green = 239.0 / 255.0, blue = 1.0, alpha = 1.0 };
added_attributes.background = Gdk.RGBA() { red = 164.0 / 255.0, green = 0.0, blue = 0.0, alpha = 1.0 };
removed_attributes.background = Gdk.RGBA() { red = 78.0 / 255.0, green = 154.0 / 255.0, blue = 6.0 / 255.0, alpha = 1.0 };
}
else
{
selection_attributes.background = Gdk.RGBA() { red = 168.0 / 255.0, green = 207.0 / 255.0, blue = 214.0 / 255.0, alpha = 1.0 };
header_attributes.background = Gdk.RGBA() { red = 224.0 / 255.0, green = 239.0 / 255.0, blue = 1.0, alpha = 1.0 };
added_attributes.background = Gdk.RGBA() { red = 220.0 / 255.0, green = 1.0, blue = 220.0 / 255.0, alpha = 1.0 };
removed_attributes.background = Gdk.RGBA() { red = 1.0, green = 220.0 / 255.0, blue = 220.0 / 255.0, alpha = 1.0 };
}
d_sourceview_hunks.set_mark_attributes(d_selection_category, selection_attributes, 0);
d_sourceview_hunks.set_mark_attributes("header", header_attributes, 0);
d_sourceview_hunks.set_mark_attributes("added", added_attributes, 0);
d_sourceview_hunks.set_mark_attributes("removed", removed_attributes, 0);
}
private bool get_line_selected(Gtk.TextIter iter)
{
var text_view = d_sourceview_hunks as Gtk.TextView;
Gtk.TextIter start = iter;
start.set_line_offset(0);
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
return buffer.get_source_marks_at_iter(start, d_selection_category) != null;
}
private bool get_line_is_diff(Gtk.TextIter iter)
{
var text_view = d_sourceview_hunks as Gtk.TextView;
Gtk.TextIter start = iter;
start.set_line_offset(0);
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
return (buffer.get_source_marks_at_iter(start, "added") != null) ||
(buffer.get_source_marks_at_iter(start, "removed") != null);
}
private bool get_iter_from_pointer_position(out Gtk.TextIter iter)
{
var text_view = d_sourceview_hunks as Gtk.TextView;
var win = text_view.get_window(Gtk.TextWindowType.TEXT);
int x, y, width, height;
width = win.get_width();
height = win.get_height();
var pointer = Gdk.Display.get_default().get_device_manager().get_client_pointer();
win.get_device_position(pointer, out x, out y, null);
if (x < 0 || y < 0 || x > width || y > height)
{
return false;
}
int win_x, win_y;
text_view.window_to_buffer_coords(Gtk.TextWindowType.TEXT, x, y, out win_x, out win_y);
text_view.get_iter_at_location(out iter, win_x, win_y);
return true;
}
private void select_range(Gtk.TextIter start, Gtk.TextIter end)
{
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
start.order(end);
while (start.get_line() <= end.get_line())
{
start.set_line_offset(0);
if (get_line_is_diff(start))
{
buffer.create_source_mark(null, d_selection_category, start);
}
if (!start.forward_line())
{
break;
}
}
}
private void deselect_range(Gtk.TextIter start, Gtk.TextIter end)
{
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
Gtk.TextIter real_start, real_end;
real_start = start;
real_start.set_line_offset(0);
real_end = end;
real_end.forward_to_line_end();
buffer.remove_source_marks(real_start, real_end, d_selection_category);
}
private bool d_is_selecting;
private bool d_is_deselecting;
private Gtk.TextMark d_start_selection_mark;
private Gtk.TextMark d_end_selection_mark;
private bool button_press_event_on_view(Gdk.EventButton event)
{
if (event.button != 1)
{
return false;
}
Gtk.TextIter iter;
if (!get_iter_from_pointer_position(out iter))
{
return false;
}
if (get_line_selected(iter))
{
d_is_deselecting = true;
deselect_range(iter, iter);
}
else
{
d_is_selecting = true;
select_range(iter, iter);
}
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer();
d_start_selection_mark = buffer.create_mark(null, iter, false);
d_end_selection_mark = buffer.create_mark(null, iter, false);
return false;
}
private bool motion_notify_event_on_view(Gdk.EventMotion event)
{
if (!d_is_selecting && !d_is_deselecting)
{
return false;
}
Gtk.TextIter iter;
if (!get_iter_from_pointer_position(out iter))
{
return false;
}
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer();
Gtk.TextIter start, end, current;
current = iter;
buffer.get_iter_at_mark(out start, d_start_selection_mark);
start.order(current);
if (d_is_selecting)
{
select_range(start, current);
}
else
{
deselect_range(start, current);
}
buffer.get_iter_at_mark(out end, d_end_selection_mark);
if (!end.in_range(start, current))
{
start = end;
current = iter;
start.order(current);
if (d_is_selecting)
{
deselect_range(start, current);
}
else
{
select_range(start, current);
}
}
buffer.move_mark(d_end_selection_mark, iter);
return false;
}
private void update_has_selection()
{
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer();
Gtk.TextIter iter;
buffer.get_start_iter(out iter);
bool something_selected = false;
if (get_line_selected(iter))
{
something_selected = true;
}
else
{
something_selected = (buffer as Gtk.SourceBuffer).forward_iter_to_source_mark(iter, d_selection_category);
}
if (something_selected != d_has_selection)
{
d_has_selection = something_selected;
notify_property("has-selection");
}
}
private bool button_release_event_on_view(Gdk.EventButton event)
{
if (event.button != 1)
{
return false;
}
d_is_selecting = false;
d_is_deselecting = false;
var text_view = d_sourceview_hunks as Gtk.TextView;
var buffer = text_view.get_buffer();
buffer.delete_mark(d_start_selection_mark);
d_start_selection_mark = null;
buffer.delete_mark(d_end_selection_mark);
d_end_selection_mark = null;
update_has_selection();
return false;
}
protected override void constructed()
{
base.constructed();
@ -130,41 +465,91 @@ class Gitg.DiffViewFile : Gtk.Grid
d_expander.bind_property("expanded", this, "expanded", BindingFlags.BIDIRECTIONAL);
}
private void on_selection_changed()
{
bool something_selected = false;
foreach (var child in d_grid_hunks.get_children())
{
if ((child as Gitg.DiffViewHunk).has_selection)
{
something_selected = true;
break;
}
}
if (d_has_selection != something_selected)
{
d_has_selection = something_selected;
notify_property("has-selection");
}
}
public void add_hunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
{
var widget = new Gitg.DiffViewHunk(hunk, lines, handle_selection);
widget.show();
var buffer = d_sourceview_hunks.buffer as Gtk.SourceBuffer;
d_diff_stat_file.added += widget.added;
d_diff_stat_file.removed += widget.removed;
/* Diff hunk */
var h = hunk.get_header();
var pos = h.last_index_of("@@");
d_grid_hunks.add(widget);
if (pos >= 0)
{
h = h.substring(pos + 2).chug();
}
this.bind_property("maxlines", widget, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
this.bind_property("wrap", widget, "wrap", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
this.bind_property("tab-width", widget, "tab-width", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
h = h.chomp();
widget.notify["has-selection"].connect(on_selection_changed);
Gtk.TextIter iter;
buffer.get_end_iter(out iter);
if (!iter.is_start())
{
buffer.insert(ref iter, "\n", 1);
}
iter.set_line_offset(0);
int line_header = iter.get_line();
buffer.create_source_mark(null, "header", iter);
var header = @"@@ -$(hunk.get_old_start()),$(hunk.get_old_lines()) +$(hunk.get_new_start()),$(hunk.get_new_lines()) @@ $h\n";
buffer.insert(ref iter, header, -1);
/* Diff Content */
var content = new StringBuilder();
for (var i = 0; i < lines.size; i++)
{
var line = lines[i];
var text = line.get_text();
switch (line.get_origin())
{
case Ggit.DiffLineType.ADDITION:
break;
case Ggit.DiffLineType.DELETION:
break;
case Ggit.DiffLineType.CONTEXT_EOFNL:
case Ggit.DiffLineType.ADD_EOFNL:
case Ggit.DiffLineType.DEL_EOFNL:
text = text.substring(1);
break;
}
if (i == lines.size - 1 && text.length > 0 && text[text.length - 1] == '\n')
{
text = text.slice(0, text.length - 1);
}
content.append(text);
}
buffer.insert(ref iter, (string)content.data, -1);
d_old_lines.add_hunk(line_header, iter.get_line(), hunk, lines);
d_new_lines.add_hunk(line_header, iter.get_line(), hunk, lines);
d_sym_lines.add_hunk(line_header, iter.get_line(), hunk, lines);
for (var i = 0; i < lines.size; i++)
{
var line = lines[i];
string? category = null;
switch (line.get_origin())
{
case Ggit.DiffLineType.ADDITION:
category = "added";
break;
case Ggit.DiffLineType.DELETION:
category = "removed";
break;
}
if (category != null)
{
buffer.get_iter_at_line(out iter, line_header + i);
buffer.create_source_mark(null, category, iter);
}
}
sensitive = true;
}

View file

@ -1,542 +0,0 @@
/*
* This file is part of gitg
*
* Copyright (C) 2015 - Jesse van den Kieboom
*
* gitg is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* gitg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gitg. If not, see <http://www.gnu.org/licenses/>.
*/
[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-diff-view-hunk.ui")]
class Gitg.DiffViewHunk : Gtk.Grid
{
private Gtk.Label d_label_hunk;
[GtkChild( name = "sourceview_hunk" )]
private Gtk.SourceView d_sourceview_hunk;
private string d_selection_category = "selection";
public Ggit.DiffHunk hunk
{
get;
construct set;
}
public Gee.ArrayList<Ggit.DiffLine> lines
{
get;
construct set;
}
public bool handle_selection
{
get;
construct set;
}
public DiffViewHunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines, bool handle_selection)
{
Object(hunk: hunk, lines: lines, handle_selection: handle_selection);
}
private uint d_added;
public uint added
{
get { return d_added; }
}
private uint d_removed;
public uint removed
{
get { return d_removed; }
}
public int maxlines
{
get; set;
}
public bool wrap
{
get { return d_sourceview_hunk.wrap_mode != Gtk.WrapMode.NONE; }
set
{
if (value)
{
d_sourceview_hunk.wrap_mode = Gtk.WrapMode.WORD_CHAR;
}
else
{
d_sourceview_hunk.wrap_mode = Gtk.WrapMode.NONE;
}
}
}
public int tab_width
{
get { return (int)d_sourceview_hunk.tab_width; }
set
{
if (value > 0)
{
d_sourceview_hunk.tab_width = (uint)value;
}
}
}
private bool d_has_selection;
public bool has_selection
{
get { return d_has_selection; }
}
private DiffViewLinesRenderer d_old_lines;
private DiffViewLinesRenderer d_new_lines;
private DiffViewLinesRenderer d_sym_lines;
construct
{
var gutter = d_sourceview_hunk.get_gutter(Gtk.TextWindowType.LEFT);
d_old_lines = new DiffViewLinesRenderer(hunk, lines, DiffViewLinesRenderer.Style.OLD);
d_new_lines = new DiffViewLinesRenderer(hunk, lines, DiffViewLinesRenderer.Style.NEW);
d_sym_lines = new DiffViewLinesRenderer(hunk, lines, DiffViewLinesRenderer.Style.SYMBOL);
this.bind_property("maxlines", d_old_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
this.bind_property("maxlines", d_new_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
d_old_lines.xpad = 8;
d_new_lines.xpad = 8;
d_sym_lines.xpad = 6;
gutter.insert(d_old_lines, 0);
gutter.insert(d_new_lines, 1);
gutter.insert(d_sym_lines, 2);
d_old_lines.notify["size"].connect(update_top_window_size);
d_new_lines.notify["size"].connect(update_top_window_size);
d_sym_lines.notify["size"].connect(update_top_window_size);
if (handle_selection)
{
d_sourceview_hunk.button_press_event.connect(button_press_event_on_view);
d_sourceview_hunk.motion_notify_event.connect(motion_notify_event_on_view);
d_sourceview_hunk.button_release_event.connect(button_release_event_on_view);
d_sourceview_hunk.get_style_context().add_class("handle-selection");
d_sourceview_hunk.realize.connect(update_cursor);
d_sourceview_hunk.notify["state-flags"].connect(update_cursor);
}
update_hunk_label();
update_lines();
d_sourceview_hunk.set_border_window_size(Gtk.TextWindowType.TOP, 1);
d_sourceview_hunk.add_child_in_window(d_label_hunk, Gtk.TextWindowType.TOP, 0, 0);
d_label_hunk.style_updated.connect(update_top_window_size);
update_top_window_size();
var settings = Gtk.Settings.get_default();
settings.notify["gtk-application-prefer-dark-theme"].connect(update_theme);
update_theme();
}
private void update_cursor()
{
var window = d_sourceview_hunk.get_window(Gtk.TextWindowType.TEXT);
if (window == null)
{
return;
}
var cursor = new Gdk.Cursor.for_display(d_sourceview_hunk.get_display(), Gdk.CursorType.LEFT_PTR);
window.set_cursor(cursor);
}
private void update_theme()
{
var selection_attributes = new Gtk.SourceMarkAttributes();
var added_attributes = new Gtk.SourceMarkAttributes();
var removed_attributes = new Gtk.SourceMarkAttributes();
var settings = Gtk.Settings.get_default();
if (settings.gtk_application_prefer_dark_theme)
{
selection_attributes.background = Gdk.RGBA() { red = 52.0 / 255.0, green = 101.0 / 255.0, blue = 164.0 / 255.0, alpha = 1.0 };
added_attributes.background = Gdk.RGBA() { red = 164.0 / 255.0, green = 0.0, blue = 0.0, alpha = 1.0 };
removed_attributes.background = Gdk.RGBA() { red = 78.0 / 255.0, green = 154.0 / 255.0, blue = 6.0 / 255.0, alpha = 1.0 };
}
else
{
selection_attributes.background = Gdk.RGBA() { red = 168.0 / 255.0, green = 207.0 / 255.0, blue = 214.0 / 255.0, alpha = 1.0 };
added_attributes.background = Gdk.RGBA() { red = 220.0 / 255.0, green = 1.0, blue = 220.0 / 255.0, alpha = 1.0 };
removed_attributes.background = Gdk.RGBA() { red = 1.0, green = 220.0 / 255.0, blue = 220.0 / 255.0, alpha = 1.0 };
}
d_sourceview_hunk.set_mark_attributes(d_selection_category, selection_attributes, 0);
d_sourceview_hunk.set_mark_attributes("added", added_attributes, 0);
d_sourceview_hunk.set_mark_attributes("removed", removed_attributes, 0);
}
private bool get_line_selected(Gtk.TextIter iter)
{
var text_view = d_sourceview_hunk as Gtk.TextView;
Gtk.TextIter start = iter;
start.set_line_offset(0);
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
return buffer.get_source_marks_at_iter(start, d_selection_category) != null;
}
private bool get_line_is_diff(Gtk.TextIter iter)
{
var text_view = d_sourceview_hunk as Gtk.TextView;
Gtk.TextIter start = iter;
start.set_line_offset(0);
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
return (buffer.get_source_marks_at_iter(start, "added") != null) ||
(buffer.get_source_marks_at_iter(start, "removed") != null);
}
private bool get_iter_from_pointer_position(out Gtk.TextIter iter)
{
var text_view = d_sourceview_hunk as Gtk.TextView;
var win = text_view.get_window(Gtk.TextWindowType.TEXT);
int x, y, width, height;
width = win.get_width();
height = win.get_height();
var pointer = Gdk.Display.get_default().get_device_manager().get_client_pointer();
win.get_device_position(pointer, out x, out y, null);
if (x < 0 || y < 0 || x > width || y > height)
{
return false;
}
int win_x, win_y;
text_view.window_to_buffer_coords(Gtk.TextWindowType.TEXT, x, y, out win_x, out win_y);
text_view.get_iter_at_location(out iter, win_x, win_y);
return true;
}
private void select_range(Gtk.TextIter start, Gtk.TextIter end)
{
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
start.order(end);
while (start.get_line() <= end.get_line())
{
start.set_line_offset(0);
if (get_line_is_diff(start))
{
buffer.create_source_mark(null, d_selection_category, start);
}
if (!start.forward_line())
{
break;
}
}
}
private void deselect_range(Gtk.TextIter start, Gtk.TextIter end)
{
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer() as Gtk.SourceBuffer;
Gtk.TextIter real_start, real_end;
real_start = start;
real_start.set_line_offset(0);
real_end = end;
real_end.forward_to_line_end();
buffer.remove_source_marks(real_start, real_end, d_selection_category);
}
private bool d_is_selecting;
private bool d_is_deselecting;
private Gtk.TextMark d_start_selection_mark;
private Gtk.TextMark d_end_selection_mark;
private bool button_press_event_on_view(Gdk.EventButton event)
{
if (event.button != 1)
{
return false;
}
Gtk.TextIter iter;
if (!get_iter_from_pointer_position(out iter))
{
return false;
}
if (get_line_selected(iter))
{
d_is_deselecting = true;
deselect_range(iter, iter);
}
else
{
d_is_selecting = true;
select_range(iter, iter);
}
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer();
d_start_selection_mark = buffer.create_mark(null, iter, false);
d_end_selection_mark = buffer.create_mark(null, iter, false);
return false;
}
private bool motion_notify_event_on_view(Gdk.EventMotion event)
{
if (!d_is_selecting && !d_is_deselecting)
{
return false;
}
Gtk.TextIter iter;
if (!get_iter_from_pointer_position(out iter))
{
return false;
}
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer();
Gtk.TextIter start, end, current;
current = iter;
buffer.get_iter_at_mark(out start, d_start_selection_mark);
start.order(current);
if (d_is_selecting)
{
select_range(start, current);
}
else
{
deselect_range(start, current);
}
buffer.get_iter_at_mark(out end, d_end_selection_mark);
if (!end.in_range(start, current))
{
start = end;
current = iter;
start.order(current);
if (d_is_selecting)
{
deselect_range(start, current);
}
else
{
select_range(start, current);
}
}
buffer.move_mark(d_end_selection_mark, iter);
return false;
}
private void update_has_selection()
{
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer();
Gtk.TextIter iter;
buffer.get_start_iter(out iter);
bool something_selected = false;
if (get_line_selected(iter))
{
something_selected = true;
}
else
{
something_selected = (buffer as Gtk.SourceBuffer).forward_iter_to_source_mark(iter, d_selection_category);
}
if (something_selected != d_has_selection)
{
d_has_selection = something_selected;
notify_property("has-selection");
}
}
private bool button_release_event_on_view(Gdk.EventButton event)
{
if (event.button != 1)
{
return false;
}
d_is_selecting = false;
d_is_deselecting = false;
var text_view = d_sourceview_hunk as Gtk.TextView;
var buffer = text_view.get_buffer();
buffer.delete_mark(d_start_selection_mark);
d_start_selection_mark = null;
buffer.delete_mark(d_end_selection_mark);
d_end_selection_mark = null;
update_has_selection();
return false;
}
private void update_top_window_size()
{
int minheight, natheight;
d_label_hunk.get_preferred_height(out minheight, out natheight);
if (natheight > 0)
{
d_sourceview_hunk.set_border_window_size(Gtk.TextWindowType.TOP, natheight);
}
var wx = d_new_lines.size +
d_new_lines.xpad * 2 +
d_old_lines.size +
d_old_lines.xpad * 2 +
d_sym_lines.size +
d_sym_lines.xpad * 2;
d_sourceview_hunk.move_child(d_label_hunk, -wx + 6, 0);
}
protected override bool map_event(Gdk.EventAny event)
{
var ret = base.map_event(event);
update_top_window_size();
return ret;
}
private void update_hunk_label()
{
var h = hunk.get_header();
var pos = h.last_index_of("@@");
if (pos >= 0)
{
h = h.substring(pos + 2).chug();
}
h = h.chomp();
d_label_hunk = new Gtk.Label(@"@@ -$(hunk.get_old_start()),$(hunk.get_old_lines()) +$(hunk.get_new_start()),$(hunk.get_new_lines()) @@ $h");
d_label_hunk.halign = Gtk.Align.START;
d_label_hunk.xalign = 0;
d_label_hunk.selectable = false;
d_label_hunk.can_focus = false;
d_label_hunk.margin_top = 6;
d_label_hunk.margin_bottom = 6;
d_label_hunk.show();
}
private void update_lines()
{
var content = new StringBuilder();
for (var i = 0; i < lines.size; i++)
{
var line = lines[i];
var text = line.get_text();
switch (line.get_origin())
{
case Ggit.DiffLineType.ADDITION:
++d_added;
break;
case Ggit.DiffLineType.DELETION:
++d_removed;
break;
case Ggit.DiffLineType.CONTEXT_EOFNL:
case Ggit.DiffLineType.ADD_EOFNL:
case Ggit.DiffLineType.DEL_EOFNL:
text = text.substring(1);
break;
}
if (i == lines.size - 1 && text.length > 0 && text[text.length - 1] == '\n')
{
text = text.slice(0, text.length - 1);
}
content.append(text);
}
var buffer = d_sourceview_hunk.buffer as Gtk.SourceBuffer;
buffer.set_text((string)content.data);
for (var i = 0; i < lines.size; i++)
{
var line = lines[i];
string? category = null;
switch (line.get_origin())
{
case Ggit.DiffLineType.ADDITION:
category = "added";
break;
case Ggit.DiffLineType.DELETION:
category = "removed";
break;
}
if (category != null)
{
Gtk.TextIter iter;
buffer.get_iter_at_line(out iter, i);
buffer.create_source_mark(null, category, iter);
}
}
notify_property("added");
notify_property("removed");
}
}
// ex:ts=4 noet

View file

@ -32,17 +32,14 @@ class Gitg.DiffViewLinesRenderer : Gtk.SourceGutterRendererText
private ulong d_view_style_updated_id;
private string[] d_line_infos;
public Ggit.DiffHunk hunk
private struct HunkInfo
{
get; construct set;
int start;
int end;
string[] line_infos;
}
public Gee.ArrayList<Ggit.DiffLine> lines
{
get; construct set;
}
private Gee.HashMap<Ggit.DiffHunk, HunkInfo?> d_hunks_map;
public Style style
{
@ -66,64 +63,17 @@ class Gitg.DiffViewLinesRenderer : Gtk.SourceGutterRendererText
}
}
public DiffViewLinesRenderer(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines, Style style)
public DiffViewLinesRenderer(Style style)
{
Object(hunk: hunk, lines: lines, style: style);
Object(style: style);
}
construct
{
d_hunks_map = new Gee.HashMap<Ggit.DiffHunk, HunkInfo?>();
set_alignment(1.0f, 0.5f);
}
protected override void constructed()
{
calculate_num_digits();
precalculate_line_strings();
}
private void precalculate_line_strings()
{
var oldn = hunk.get_old_start();
var newn = hunk.get_new_start();
var lns = lines;
d_line_infos = new string[lns.size];
for (var i = 0; i < lns.size; i++)
{
var line = lns[i];
var origin = line.get_origin();
string ltext = "";
switch (style)
{
case Style.NEW:
if (origin == Ggit.DiffLineType.CONTEXT || origin == Ggit.DiffLineType.ADDITION)
{
ltext = d_num_digits_fmts.printf(newn);
newn++;
}
break;
case Style.OLD:
if (origin == Ggit.DiffLineType.CONTEXT || origin == Ggit.DiffLineType.DELETION)
{
ltext = d_num_digits_fmts.printf(oldn);
oldn++;
}
break;
case Style.SYMBOL:
if (origin == Ggit.DiffLineType.ADDITION)
{
ltext = "+";
}
else if (origin == Ggit.DiffLineType.DELETION)
{
ltext = "-";
}
break;
}
d_line_infos[i] = ltext;
}
}
protected Gtk.TextBuffer buffer
@ -134,14 +84,24 @@ class Gitg.DiffViewLinesRenderer : Gtk.SourceGutterRendererText
protected override void query_data(Gtk.TextIter start, Gtk.TextIter end, Gtk.SourceGutterRendererState state)
{
var line = start.get_line();
HunkInfo? info = null;
if (line >= d_line_infos.length)
foreach (var i in d_hunks_map.values)
{
if (line >= i.start && line <= i.end)
{
info = i;
break;
}
}
if (info == null || line >= info.line_infos.length)
{
set_text("", -1);
}
else
{
set_text(d_line_infos[start.get_line()], -1);
set_text(info.line_infos[start.get_line() - info.start], -1);
}
}
@ -184,18 +144,21 @@ class Gitg.DiffViewLinesRenderer : Gtk.SourceGutterRendererText
if (style == Style.OLD || style == Style.NEW)
{
var oldn = hunk.get_old_start() + hunk.get_old_lines();
var newn = hunk.get_new_start() + hunk.get_new_lines();
var num = int.max(int.max(oldn, newn), d_maxlines);
while (num > 0)
foreach (var hunk in d_hunks_map.keys)
{
++num_digits;
num /= 10;
}
var oldn = hunk.get_old_start() + hunk.get_old_lines();
var newn = hunk.get_new_start() + hunk.get_new_lines();
d_num_digits = int.max(2, num_digits);
var num = int.max(int.max(oldn, newn), d_maxlines);
while (num > 0)
{
++num_digits;
num /= 10;
}
d_num_digits = int.max(2, num_digits);
}
}
else
{
@ -235,6 +198,71 @@ class Gitg.DiffViewLinesRenderer : Gtk.SourceGutterRendererText
ctx.restore();
}
}
private string[] precalculate_line_strings(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
{
var oldn = hunk.get_old_start();
var newn = hunk.get_new_start();
var lns = lines;
var line_infos = new string[lns.size];
for (var i = 0; i < lns.size; i++)
{
var line = lns[i];
var origin = line.get_origin();
string ltext = "";
switch (style)
{
case Style.NEW:
if (origin == Ggit.DiffLineType.CONTEXT || origin == Ggit.DiffLineType.ADDITION)
{
ltext = d_num_digits_fmts.printf(newn);
newn++;
}
break;
case Style.OLD:
if (origin == Ggit.DiffLineType.CONTEXT || origin == Ggit.DiffLineType.DELETION)
{
ltext = d_num_digits_fmts.printf(oldn);
oldn++;
}
break;
case Style.SYMBOL:
if (origin == Ggit.DiffLineType.ADDITION)
{
ltext = "+";
}
else if (origin == Ggit.DiffLineType.DELETION)
{
ltext = "-";
}
break;
}
line_infos[i] = ltext;
}
return line_infos;
}
public void add_hunk(int buffer_line_start, int buffer_line_end, Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
{
HunkInfo info = HunkInfo();
calculate_num_digits();
info.start = buffer_line_start;
info.end = buffer_line_end;
info.line_infos = precalculate_line_strings(hunk, lines);
d_hunks_map[hunk] = info;
recalculate_size();
}
}
// ex:ts=4 noet

View file

@ -5,7 +5,6 @@
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-authentication-dialog.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-diff-view.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-diff-view-file.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-diff-view-hunk.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-diff-view-options.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-diff-view-commit-details.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/gitg-sidebar.ui</file>

View file

@ -56,13 +56,26 @@
<property name="can_focus">False</property>
<property name="transition_type">slide-down</property>
<child>
<object class="GtkGrid" id="grid_hunks">
<object class="GtkSourceView" id="sourceview_hunks">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="show_line_numbers">False</property>
<property name="editable">False</property>
<property name="cursor_visible">False</property>
<property name="monospace">True</property>
<property name="hexpand">True</property>
<property name="pixels_above_lines">1</property>
<property name="pixels_below_lines">1</property>
<property name="left_margin">6</property>
<property name="right_margin">6</property>
<property name="buffer">buffer</property>
</object>
</child>
</object>
</child>
</template>
<object class="GtkSourceBuffer" id="buffer">
<property name="highlight_matching_brackets">False</property>
<property name="implicit_trailing_newline">False</property>
</object>
</interface>

View file

@ -1,36 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.16"/>
<template class="GitgDiffViewHunk" parent="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hexpand">True</property>
<style>
<class name="gitg-hunk-frame"/>
</style>
<child>
<object class="GtkSourceView" id="sourceview_hunk">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="show_line_numbers">False</property>
<property name="editable">False</property>
<property name="cursor_visible">False</property>
<property name="monospace">True</property>
<property name="hexpand">True</property>
<property name="pixels_above_lines">1</property>
<property name="pixels_below_lines">1</property>
<property name="left_margin">6</property>
<property name="right_margin">6</property>
<property name="buffer">buffer</property>
<style>
<class name="gitg-hunk-source"/>
</style>
</object>
</child>
</template>
<object class="GtkSourceBuffer" id="buffer">
<property name="highlight_matching_brackets">False</property>
<property name="implicit_trailing_newline">False</property>
</object>
</interface>