File: gitg-diff-view-file-renderer-text.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (937 lines) | stat: -rw-r--r-- 21,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
72
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
 * This file is part of gitg
 *
 * Copyright (C) 2016 - 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-file-renderer-text.ui")]
class Gitg.DiffViewFileRendererText : Gtk.SourceView, DiffSelectable, DiffViewFileRenderer, DiffViewFileRendererTextable
{
	private enum RegionType
	{
		ADDED,
		REMOVED,
		CONTEXT
	}

	public enum Style
	{
		ONE,
		OLD,
		NEW
	}

	private struct Region
	{
		public RegionType type;
		public int buffer_line_start;
		public int source_line_start;
		public int length;
	}

	public uint added { get; set; }
	public uint removed { get; set; }

	private int64 d_doffset;

	private Gee.HashMap<int, PatchSet.Patch?> d_lines;

	private DiffViewFileSelectable d_selectable;
	private DiffViewLinesRenderer d_old_lines;
	private DiffViewLinesRenderer d_new_lines;
	private DiffViewLinesRenderer d_sym_lines;

	private bool d_highlight;

	private Cancellable? d_higlight_cancellable;
	private Gtk.SourceBuffer? d_old_highlight_buffer;
	private Gtk.SourceBuffer? d_new_highlight_buffer;
	private bool d_old_highlight_ready;
	private bool d_new_highlight_ready;

	private Region[] d_regions;
	private bool d_constructed;

	private Settings? d_stylesettings;

	private FontManager d_font_manager;
	public Style d_style { get; construct set; }

	public bool new_is_workdir { get; construct set; }

	public bool wrap_lines
	{
		get { return this.wrap_mode != Gtk.WrapMode.NONE; }
		set
		{
			if (value)
			{
				this.wrap_mode = Gtk.WrapMode.WORD_CHAR;
			}
			else
			{
				this.wrap_mode = Gtk.WrapMode.NONE;
			}
		}
	}

	public new int tab_width
	{
		get { return (int)get_tab_width(); }
		set { set_tab_width((uint)value); }
	}

	public int maxlines { get; set; }

	public DiffViewFileInfo info { get; construct set; }

	public Ggit.DiffDelta? delta
	{
		get { return info.delta; }
	}

	public Repository? repository
	{
		get { return info.repository; }
	}

	public bool highlight
	{
		get { return d_highlight; }

		construct set
		{
			if (d_highlight != value)
			{
				d_highlight = value;
				update_highlight();
			}
		}
	}

	private bool d_has_selection;

	public bool has_selection
	{
		get { return d_has_selection; }
	}

	public bool can_select { get; construct set; }

	public PatchSet selection
	{
		owned get
		{
			var ret = new PatchSet();

			ret.filename = delta.get_new_file().get_path();

			var patches = new PatchSet.Patch[0];

			if (!can_select)
			{
				return ret;
			}

			var selected = d_selectable.get_selected_lines();

			for (var i = 0; i < selected.length; i++)
			{
				var line = selected[i];
				var pset = d_lines[line];

				if (i == 0)
				{
					patches += pset;
					continue;
				}

				var last = patches[patches.length - 1];

				if (last.new_offset + last.length == pset.new_offset &&
				    last.type == pset.type)
				{
					last.length += pset.length;
					patches[patches.length - 1] = last;
				}
				else
				{
					patches += pset;
				}
			}

			ret.patches = patches;
			return ret;
		}
	}

	public DiffViewFileRendererText(DiffViewFileInfo info, bool can_select, Style style)
	{
		Object(info: info, can_select: can_select, d_style: style);
	}

	construct
	{
		var gutter = this.get_gutter(Gtk.TextWindowType.LEFT);

		if (d_style == Style.ONE)
		{
			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);
		}
		else if (d_style == Style.OLD)
		{
			d_old_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.OLD);
			d_sym_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.SYMBOL_OLD);

			this.bind_property("maxlines", d_old_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);

			d_old_lines.xpad = 8;
			d_sym_lines.xpad = 6;

			gutter.insert(d_old_lines, 0);
			gutter.insert(d_sym_lines, 1);
		}
		else if (d_style == Style.NEW)
		{
			d_new_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.NEW);
			d_sym_lines = new DiffViewLinesRenderer(DiffViewLinesRenderer.Style.SYMBOL_NEW);

			this.bind_property("maxlines", d_new_lines, "maxlines", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);

			d_new_lines.xpad = 8;
			d_sym_lines.xpad = 6;

			gutter.insert(d_new_lines, 0);
			gutter.insert(d_sym_lines, 1);
		}

		this.set_border_window_size(Gtk.TextWindowType.TOP, 1);

		var settings = Gtk.Settings.get_default();
		settings.notify["gtk-application-prefer-dark-theme"].connect(update_theme);

		d_font_manager = new FontManager(this, true);

		update_theme();

		if (can_select)
		{
			d_selectable = new DiffViewFileSelectable(this);

			d_selectable.notify["has-selection"].connect(() => {
				d_has_selection = d_selectable.has_selection;
				notify_property("has-selection");
			});
		}

		d_lines = new Gee.HashMap<int, PatchSet.Patch?>();

		highlight = true;
	}

	protected override void dispose()
	{
		base.dispose();

		if (d_higlight_cancellable != null)
		{
			d_higlight_cancellable.cancel();
			d_higlight_cancellable = null;
		}
	}

	private void update_highlight()
	{
		if (!d_constructed)
		{
			return;
		}

		if (d_higlight_cancellable != null)
		{
			d_higlight_cancellable.cancel();
			d_higlight_cancellable = null;
		}

		d_old_highlight_buffer = null;
		d_new_highlight_buffer = null;

		d_old_highlight_ready = false;
		d_new_highlight_ready = false;

		if (highlight && repository != null && delta != null)
		{
			var cancellable = new Cancellable();
			d_higlight_cancellable = cancellable;

			init_highlighting_buffer_old.begin(cancellable, (obj, res) => {
				init_highlighting_buffer_old.end(res);
			});

			init_highlighting_buffer_new.begin(cancellable, (obj, res) => {
				init_highlighting_buffer_new.end(res);
			});
		}
		else
		{
			update_highlighting_ready();
		}
	}

	private async void init_highlighting_buffer_old(Cancellable cancellable)
	{
		var buffer = yield init_highlighting_buffer(delta.get_old_file(), false, cancellable);

		if (!cancellable.is_cancelled())
		{
			d_old_highlight_buffer = buffer;
			d_old_highlight_ready = true;

			update_highlighting_ready();
		}
	}

	private File? get_file_location(Ggit.DiffFile file)
	{
		var path = file.get_path();

		if (path == null)
		{
			return null;
		}

		var workdir = repository.get_workdir();

		if (workdir == null)
		{
			return null;
		}

		return workdir.get_child(path);
	}

	private async void init_highlighting_buffer_new(Cancellable cancellable)
	{
		Gtk.SourceBuffer? buffer;

		var file = delta.get_new_file();

		if (info.new_file_input_stream != null)
		{
			// Use once
			var stream = info.new_file_input_stream;
			info.new_file_input_stream = null;

			buffer = yield init_highlighting_buffer_from_stream(delta.get_new_file(),
			                                                    get_file_location(file),
			                                                    stream,
			                                                    info.new_file_content_type,
			                                                    cancellable);
		}
		else
		{
			buffer = yield init_highlighting_buffer(delta.get_new_file(), info.from_workdir, cancellable);
		}

		if (!cancellable.is_cancelled())
		{
			d_new_highlight_buffer = buffer;
			d_new_highlight_ready = true;

			update_highlighting_ready();
		}
	}

	private async Gtk.SourceBuffer? init_highlighting_buffer(Ggit.DiffFile file, bool from_workdir, Cancellable cancellable)
	{
		var id = file.get_oid();
		var location = get_file_location(file);

		if ((id.is_zero() && !from_workdir) || (location == null && from_workdir))
		{
			return null;
		}

		uint8[] content;

		if (!from_workdir)
		{
			Ggit.Blob blob;

			try
			{
				blob = repository.lookup<Ggit.Blob>(id);
			}
			catch
			{
				return null;
			}

			if (TextConv.has_textconv_command(repository, file))
				content = TextConv.get_textconv_content(repository, file);
			else
				content = blob.get_raw_content();
		}
		else
		{
			// Try to read from disk
			try
			{
				// Read it all into a buffer so we can guess the content type from
				// it. This isn't really nice, but it's simple.
				yield location.load_contents_async(cancellable, out content, null);
				if (TextConv.has_textconv_command(repository, file))
					content = TextConv.get_textconv_content_from_raw(repository, file, content);
			}
			catch
			{
				return null;
			}
		}

		bool uncertain;
		var content_type = GLib.ContentType.guess(location.get_basename(), content, out uncertain);

		var stream = new GLib.MemoryInputStream.from_bytes(new Bytes(content));

		return yield init_highlighting_buffer_from_stream(file, location, stream, content_type, cancellable);
	}

	private async Gtk.SourceBuffer? init_highlighting_buffer_from_stream(Ggit.DiffFile file, File location, InputStream stream, string content_type, Cancellable cancellable)
	{
		var manager = Gtk.SourceLanguageManager.get_default();
		var language = manager.guess_language(location != null ? location.get_basename() : null, content_type);

		var buffer = new Gtk.SourceBuffer(this.buffer.tag_table);

		if (language != null)
		{
			buffer.language = language;
		}

		var style_scheme_manager = Gtk.SourceStyleSchemeManager.get_default();

		buffer.highlight_syntax = true;

		d_stylesettings = try_settings(Gitg.Config.APPLICATION_ID + ".preferences.interface");
		if (d_stylesettings != null)
		{
			d_stylesettings.changed["style-scheme"].connect((s, k) => {
				update_style();
			});

			update_style();
		} else {
			buffer.style_scheme = style_scheme_manager.get_scheme("classic");
		}

		var sfile = new Gtk.SourceFile();
		sfile.location = location;

		var loader = new Gtk.SourceFileLoader.from_stream(buffer, sfile, stream);

		try
		{
			yield loader.load_async(GLib.Priority.LOW, cancellable, null);
			this.strip_carriage_returns(buffer);
		}
		catch (Error e)
		{
			if (!cancellable.is_cancelled())
			{
				stderr.printf(@"ERROR: failed to load $(file.get_path()) for highlighting: $(e.message)\n");
			}
		}

		return buffer;
	}

	private void update_style()
	{
		var scheme = d_stylesettings.get_string("style-scheme");
		var manager = Gtk.SourceStyleSchemeManager.get_default();
		var s = manager.get_scheme(scheme);

		if (s != null)
		{
			((Gtk.SourceBuffer) buffer).style_scheme = s;
		}
	}

	private Settings? try_settings(string schema_id)
	{
		var source = SettingsSchemaSource.get_default();

		if (source == null)
		{
			return null;
		}

		if (source.lookup(schema_id, true) != null)
		{
			return new Settings(schema_id);
		}

		return null;
	}

	private void strip_carriage_returns(Gtk.SourceBuffer buffer)
	{
		var search_settings = new Gtk.SourceSearchSettings();

		search_settings.regex_enabled = true;
		search_settings.search_text = "\\r";

		var search_context = new Gtk.SourceSearchContext(buffer, search_settings);

		try
		{
			search_context.replace_all("", 0);
		} catch (Error e) {}
	}

	private void update_highlighting_ready()
	{
		if (!d_old_highlight_ready && !d_new_highlight_ready)
		{
			// Remove highlights
			return;
		}
		else if (!d_old_highlight_ready || !d_new_highlight_ready)
		{
			// Both need to be loaded
			return;
		}

		var buffer = this.buffer;

		// Go over all the source chunks and match up to old/new buffer. Then,
		// apply the tags that are applied to the highlighted source buffers.
		foreach (var region in d_regions)
		{
			Gtk.SourceBuffer? source;

			if (region.type == RegionType.REMOVED)
			{
				source = d_old_highlight_buffer;
			}
			else
			{
				source = d_new_highlight_buffer;
			}

			if (source == null)
			{
				continue;
			}

			Gtk.TextIter buffer_iter, source_iter;

			buffer.get_iter_at_line(out buffer_iter, region.buffer_line_start);
			source.get_iter_at_line(out source_iter, region.source_line_start);

			var source_end_iter = source_iter;
			source_end_iter.forward_lines(region.length);

			source.ensure_highlight(source_iter, source_end_iter);

			var buffer_end_iter = buffer_iter;
			buffer_end_iter.forward_lines(region.length);

			var source_next_iter = source_iter;
			var tags = source_iter.get_tags();

			while (source_next_iter.forward_to_tag_toggle(null) && source_next_iter.compare(source_end_iter) < 0)
			{
				var buffer_next_iter = buffer_iter;
				buffer_next_iter.forward_chars(source_next_iter.get_offset() - source_iter.get_offset());

				foreach (var tag in tags)
				{
					buffer.apply_tag(tag, buffer_iter, buffer_next_iter);
				}

				source_iter = source_next_iter;
				buffer_iter = buffer_next_iter;

				tags = source_iter.get_tags();
			}

			foreach (var tag in tags)
			{
				buffer.apply_tag(tag, buffer_iter, buffer_end_iter);
			}
		}
	}

	protected override bool draw(Cairo.Context cr)
	{
		base.draw(cr);

		var win = this.get_window(Gtk.TextWindowType.LEFT);

		if (!Gtk.cairo_should_draw_window(cr, win))
		{
			return false;
		}

		var ctx = this.get_style_context();

		var old_lines_width = 0;
		var new_lines_width = 0;

		switch (d_style)
		{
		case Style.ONE:
			old_lines_width = d_old_lines.size + d_old_lines.xpad * 2;
			new_lines_width = d_new_lines.size + d_new_lines.xpad * 2;
			break;

		case Style.OLD:
			old_lines_width = d_old_lines.size + d_old_lines.xpad * 2;
			break;

		case Style.NEW:
			new_lines_width = d_new_lines.size + d_new_lines.xpad * 2;
			break;
		}

		var sym_lines_width = d_sym_lines.size + d_sym_lines.xpad * 2;

		if (d_style == Style.ONE)
		{
			ctx.save();
			Gtk.cairo_transform_to_window(cr, this, win);
			ctx.add_class("diff-lines-separator");
			ctx.render_frame(cr, 0, 0, old_lines_width, win.get_height());
			ctx.restore();
		}

		ctx.save();
		Gtk.cairo_transform_to_window(cr, this, win);
		ctx.add_class("diff-lines-gutter-border");
		ctx.render_frame(cr, old_lines_width + new_lines_width, 0, sym_lines_width, win.get_height());
		ctx.restore();

		return false;
	}

	private void update_theme()
	{
		var header_attributes = new Gtk.SourceMarkAttributes();
		var added_attributes = new Gtk.SourceMarkAttributes();
		var removed_attributes = new Gtk.SourceMarkAttributes();

		var dark = new Theme().is_theme_dark();

		if (dark)
		{
			header_attributes.background = Gdk.RGBA() { red = 88.0 / 255.0, green = 88.0 / 255.0, blue = 88.0 / 255.0, alpha = 1.0 };
			added_attributes.background = Gdk.RGBA() { red = 32.0 / 255.0, green = 68.0 / 255.0, blue = 21.0 / 255.0, alpha = 1.0 };
			removed_attributes.background = Gdk.RGBA() { red = 130.0 / 255.0, green = 55.0 / 255.0, blue = 53.0 / 255.0, alpha = 1.0 };
		}
		else
		{
			header_attributes.background = Gdk.RGBA() { red = 244.0 / 255.0, green = 247.0 / 255.0, blue = 251.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 };
		}

		this.set_mark_attributes("header", header_attributes, 0);
		this.set_mark_attributes("added", added_attributes, 0);
		this.set_mark_attributes("removed", removed_attributes, 0);
	}

	protected override void constructed()
	{
		base.constructed();

		d_constructed = true;
		update_highlight();
	}

	public void add_hunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
	{
		var buffer = this.buffer as Gtk.SourceBuffer;

		/* Diff hunk */
		var h = hunk.get_header();
		var pos = h.last_index_of("@@");

		if (pos >= 0)
		{
			h = h.substring(pos + 2).chug();
		}

		h = h.chomp();

		Gtk.TextIter iter;
		buffer.get_end_iter(out iter);

		if (!iter.is_start())
		{
			buffer.insert(ref iter, "\n", 1);
		}

		iter.set_line_offset(0);
		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);

		int buffer_line = iter.get_line();

		int line_hunk_start = iter.get_line();

		var region = Region() {
			type = RegionType.CONTEXT,
			buffer_line_start = 0,
			source_line_start = 0,
			length = 0
		};

		this.freeze_notify();

		var add_line_num = 0;
		var remove_line_num = 0;
		var in_change_line = false;
		for (var i = 0; i < lines.size; i++)
		{
			var line = lines[i];
			var text = line.get_text().replace("\r", "");
			var added = false;
			var removed = false;
			var origin = line.get_origin();

			var rtype = RegionType.CONTEXT;

			switch (origin)
			{
				case Ggit.DiffLineType.ADDITION:
					added = true;
					this.added++;

					rtype = RegionType.ADDED;
					break;
				case Ggit.DiffLineType.DELETION:
					removed = true;
					this.removed++;

					rtype = RegionType.REMOVED;
					break;
				case Ggit.DiffLineType.CONTEXT_EOFNL:
				case Ggit.DiffLineType.ADD_EOFNL:
				case Ggit.DiffLineType.DEL_EOFNL:
					text = text.substring(1);
					break;
			}

			if (i == 0 || rtype != region.type)
			{
				if (i != 0)
				{
					d_regions += region;
				}

				int source_line_start;

				if (rtype == RegionType.REMOVED)
				{
					source_line_start = line.get_old_lineno() - 1;
				}
				else
				{
					source_line_start = line.get_new_lineno() - 1;
				}

				region = Region() {
					type = rtype,
					buffer_line_start = buffer_line,
					source_line_start = source_line_start,
					length = 0
				};
			}

			if (d_style == Style.ONE)
				region.length++;

			if (added || removed)
			{
				var offset = (size_t)line.get_content_offset();
				var bytes = line.get_content();

				var pset = PatchSet.Patch() {
					type = added ? PatchSet.Type.ADD : PatchSet.Type.REMOVE,
					old_offset = offset,
					new_offset = offset,
					length = bytes.length
				};

				if (added)
				{
					pset.old_offset = (size_t)((int64)pset.old_offset - d_doffset);
				}
				else
				{
					pset.new_offset = (size_t)((int64)pset.new_offset + d_doffset);
				}

				d_lines[buffer_line] = pset;
				d_doffset += added ? (int64)bytes.length : -(int64)bytes.length;
			}

			if (i == lines.size - 1 && text.length > 0 && text[text.length - 1] == '\n')
			{
				text = text.slice(0, text.length - 1);
			}

			if (rtype == RegionType.CONTEXT)
			{
				if (d_style == Style.OLD || d_style == Style.NEW)
				{
					if (in_change_line == true)
					{
						bool check = d_style == Style.OLD ? add_line_num > remove_line_num : remove_line_num > add_line_num;
						if (check)
						{
							int end = d_style == Style.OLD ? add_line_num - remove_line_num : remove_line_num - add_line_num;
							for (var l = 0; l < end; l++)
							{
								Gtk.TextIter t_iter;
								buffer.get_end_iter(out t_iter);
								buffer.create_source_mark(null, "empty", t_iter);

								buffer.insert(ref iter, "\n", -1);
								buffer_line++;
								region.buffer_line_start = buffer_line;
							}
						}

						add_line_num = 0;
						remove_line_num = 0;
					}

					in_change_line = false;
				}

				buffer.insert(ref iter, text, -1);
				buffer_line++;
				if (d_style == Style.OLD || d_style == Style.NEW)
				{
					region.length++;
				}
			}

			RegionType? rtype_check = null;
			string mark = null;
			switch (d_style)
			{
			case Style.ONE:
			case Style.OLD:
				rtype_check = RegionType.REMOVED;
				mark = "removed";
				break;
			case Style.NEW:
				rtype_check = RegionType.ADDED;
				mark = "added";
				break;
			}

			if (rtype == rtype_check)
			{
				Gtk.TextIter t_iter;
				buffer.get_end_iter(out t_iter);
				buffer.create_source_mark(null, mark, t_iter);

				buffer.insert(ref iter, text, -1);
				buffer_line++;
				if (d_style == Style.OLD || d_style == Style.NEW)
				{
					region.length++;

					if (d_style == Style.OLD)
						remove_line_num++;
					else
						add_line_num++;
					in_change_line = true;
				}
			}

			switch (d_style)
			{
			case Style.ONE:
			case Style.OLD:
				rtype_check = RegionType.ADDED;
				break;
			case Style.NEW:
				rtype_check = RegionType.REMOVED;
				break;
			}
			if (rtype == rtype_check)
			{
				if (d_style == Style.OLD || d_style == Style.NEW)
				{
					if (d_style == Style.OLD)
						add_line_num++;
					else
						remove_line_num++;
					in_change_line = true;
				} else if (d_style == Style.ONE) {
					Gtk.TextIter t_iter;
					buffer.get_end_iter(out t_iter);
					buffer.create_source_mark(null, "added", t_iter);

					buffer.insert(ref iter, text, -1);
					buffer_line++;
				}
			}
		}

		if (lines.size != 0)
		{
			d_regions += region;
		}

		if (d_style == Style.ONE || d_style == Style.OLD)
		{
			d_old_lines.add_hunk(line_hunk_start, iter.get_line(), hunk, buffer);
		}
		if (d_style == Style.ONE || d_style == Style.NEW)
		{
			d_new_lines.add_hunk(line_hunk_start, iter.get_line(), hunk, buffer);
		}
		d_sym_lines.add_hunk(line_hunk_start, iter.get_line(), hunk, buffer);

		this.thaw_notify();

		sensitive = true;
	}
}

// ex:ts=4 noet