forked from jamiepg1/valencia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
valencia.vala
1605 lines (1295 loc) · 56.5 KB
/
valencia.vala
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2009-2015 Yorba Foundation
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
using Gee;
using Vte;
using Valencia;
public abstract class Destination : Object {
public abstract void get_range(Gtk.TextBuffer buffer,
out Gtk.TextIter start, out Gtk.TextIter end);
}
class LineNumber : Destination {
int line; // starting from 0
public LineNumber(int line) { this.line = line; }
public override void get_range(Gtk.TextBuffer buffer,
out Gtk.TextIter start, out Gtk.TextIter end) {
Gtk.TextIter iter;
buffer.get_iter_at_line(out iter, line);
get_line_start_end(iter, out start, out end);
}
}
class LineCharRange : Destination {
int start_line; // starting from 0
int start_char;
int end_line;
int end_char;
public LineCharRange(int start_line, int start_char, int end_line, int end_char) {
this.start_line = start_line;
this.start_char = start_char;
this.end_line = end_line;
this.end_char = end_char;
}
public override void get_range(Gtk.TextBuffer buffer,
out Gtk.TextIter start, out Gtk.TextIter end) {
start = iter_at_line_offset(buffer, start_line, start_char);
end = iter_at_line_offset(buffer, end_line, end_char);
}
}
class CharRange : Destination {
int start_char;
int end_char;
public CharRange(int start_char, int end_char) {
this.start_char = start_char;
this.end_char = end_char;
}
public override void get_range(Gtk.TextBuffer buffer,
out Gtk.TextIter start, out Gtk.TextIter end) {
buffer.get_iter_at_offset(out start, start_char);
buffer.get_iter_at_offset(out end, end_char);
}
}
class ScanInfo : Object {
public ParseInfo parse_info;
public Method? method;
public int cursor_pos;
public ScanInfo(Method? method, ParseInfo parse_info, int cursor_position) {
this.method = method;
this.parse_info = parse_info;
cursor_pos = cursor_position;
}
public ScanInfo.empty() { }
public Expression inner() { return parse_info.inner; }
public Expression outer() { return parse_info.outer; }
}
public class Instance : Peas.ExtensionBase, Gedit.WindowActivatable {
static Gee.ArrayList<Instance> instances = new Gee.ArrayList<Instance>();
Gedit.Window _window;
public Gedit.Window window {
construct { _window = value; }
owned get { return _window; }
}
Program last_program_to_build;
Gtk.ActionGroup action_group;
Gtk.MenuItem go_to_definition_menu_item;
Gtk.MenuItem find_symbol_menu_item;
Gtk.MenuItem go_to_outer_scope_menu_item;
Gtk.MenuItem go_back_menu_item;
Gtk.MenuItem go_forward_menu_item;
Gtk.MenuItem next_error_menu_item;
Gtk.MenuItem prev_error_menu_item;
Gtk.MenuItem display_tooltip_menu_item;
Gtk.MenuItem build_menu_item;
Gtk.MenuItem clean_menu_item;
Gtk.MenuItem run_menu_item;
Gtk.MenuItem settings_menu_item;
uint ui_id;
int saving;
bool child_process_running;
// Output pane
Gtk.TextTag error_tag;
Gtk.TextTag italic_tag;
Gtk.TextTag bold_tag;
Gtk.TextTag highlight_tag;
Gtk.TextBuffer output_buffer;
Gtk.TextView output_view;
Gtk.ScrolledWindow output_pane;
delegate bool ProcessFinished();
unowned ProcessFinished on_process_finshed;
// Settings dialog
ProjectSettingsDialog settings_dialog;
// Parsing dialog
ProgressBarDialog parsing_dialog;
// Run command
Gtk.ScrolledWindow run_pane;
Vte.Terminal run_terminal;
// Error pane
Regex error_regex;
string target_filename;
Destination destination;
// Symbol pane
SymbolBrowser symbol_browser;
ulong symbol_browser_connect_id;
// Jump to definition history
static ArrayList<Gtk.TextMark> history;
const int MAX_HISTORY = 10;
int history_index;
bool browsing_history;
// Tooltips
Tooltip tip;
AutocompleteDialog autocomplete;
// Signal handlers
SignalConnection instance_connections;
ArrayList<SignalConnection> tab_connections;
// Display enclosing class in statusbar
int old_cursor_offset;
// Keeps track of all open documents' previous modified state
static HashMap<weak Gedit.Document, bool> documents_modified_state =
new HashMap<weak Gedit.Document, bool>();
Gedit.View view_to_scroll;
// Menu item entries
const Gtk.ActionEntry[] entries = {
{ "SearchGoToDefinition", null, "Go to _Definition", "F12",
"Jump to a symbol's definition", on_go_to_definition },
{ "SearchFindSymbol", Gtk.Stock.FIND, "Find _Symbol...", "<ctrl><alt>s",
"Search for a symbol by name", on_find_symbol },
{ "SearchGoToEnclosingMethod", null, "Go to _Outer Scope", "<ctrl>F12",
"Jump to the enclosing method or class", on_go_to_outer_scope },
{ "SearchGoBack", Gtk.Stock.GO_BACK, "Go _Back", "<alt>Left",
"Go back after jumping to a definition", on_go_back },
{ "SearchGoForward", Gtk.Stock.GO_FORWARD, "Go F_orward", "<alt>Right",
"Go forward to a definition after jumping backwards", on_go_forward },
{ "SearchNextError", null, "_Next Error", "<ctrl><alt>e",
"Go to the next compiler error in the ouput and view panes", on_next_error },
{ "SearchPrevError", null, "_Previous Error", "<ctrl><alt>p",
"Go to the previous compiler error in the ouput and view panes", on_prev_error },
{ "SearchAutocomplete", null, "_AutoComplete", "<ctrl>space",
"Display method or symbol information", on_display_tooltip_or_autocomplete },
{ "Project", null, "_Project" }, // top-level menu
{ "ProjectBuild", Gtk.Stock.CONVERT, "_Build", "<ctrl><alt>b",
"Build the project", on_build },
{ "ProjectClean", Gtk.Stock.CLEAR, "_Clean", "<ctrl><alt>c",
"Clean build output", on_clean },
{ "ProjectRun", Gtk.Stock.EXECUTE, "_Run", "<ctrl><alt>r",
"Run the program", on_run },
{ "ProjectSettings", Gtk.Stock.PROPERTIES, "_Settings", "<ctrl><alt>t",
"Customize the build and clean commands", on_project_settings },
{ "ProjectWipeValencia", null, "Wipe _Valencia Symbols", null,
"Wipe Valencia's discovered symbols and rebuild", on_wipe_valencia }
};
const string ui = """
<ui>
<menubar name="MenuBar">
<menu name="SearchMenu" action="Search">
<placeholder name="SearchOps_8">
<menuitem name="SearchGoToDefinitionMenu" action="SearchGoToDefinition"/>
<menuitem name="SearchFindSymbolMenu" action="SearchFindSymbol"/>
<menuitem name="SearchGoToEnclosingMethodMenu" action="SearchGoToEnclosingMethod"/>
<menuitem name="SearchGoBackMenu" action="SearchGoBack"/>
<menuitem name="SearchGoForwardMenu" action="SearchGoForward"/>
<separator/>
<menuitem name="SearchNextErrorMenu" action="SearchNextError"/>
<menuitem name="SearchPrevErrorMenu" action="SearchPrevError"/>
<separator/>
<menuitem name="SearchAutocompleteMenu" action="SearchAutocomplete"/>
</placeholder>
</menu>
<placeholder name="ExtraMenu_1">
<menu name="ProjectMenu" action="Project">
<menuitem name="ProjectBuildMenu" action="ProjectBuild"/>
<menuitem name="ProjectCleanMenu" action="ProjectClean"/>
<menuitem name="ProjectRunMenu" action="ProjectRun"/>
<menuitem name="ProjectSettingsMenu" action="ProjectSettings"/>
<separator/>
<menuitem name="ProjectWipeValenciaMenu" action="ProjectWipeValencia"/>
</menu>
</placeholder>
</menubar>
</ui>
""";
public Instance() {
Object();
}
public void activate() {
instances.add(this);
if (history == null)
history = new ArrayList<Gtk.TextMark>();
// Settings dialog
settings_dialog = new ProjectSettingsDialog(window);
settings_dialog.settings_changed.connect(on_settings_changed);
// Tooltips
tip = new Tooltip(window);
autocomplete = new AutocompleteDialog(window);
// Output pane
output_buffer = new Gtk.TextBuffer(null);
error_tag = output_buffer.create_tag("error", "foreground", "#c00");
italic_tag = output_buffer.create_tag("italic", "style", Pango.Style.OBLIQUE);
bold_tag = output_buffer.create_tag("bold", "weight", Pango.Weight.BOLD);
highlight_tag = output_buffer.create_tag("highlight", "foreground", "black", "background",
"#abd");
output_view = new Gtk.TextView.with_buffer(output_buffer);
output_view.set_editable(false);
output_view.set_cursor_visible(false);
Pango.FontDescription font = Pango.FontDescription.from_string("Monospace");
output_view.override_font(font);
output_view.button_press_event.connect(on_button_press);
output_pane = new Gtk.ScrolledWindow(null, null);
output_pane.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
output_pane.add(output_view);
output_pane.show_all();
Gedit.Panel panel = window.get_bottom_panel();
panel.add_item_with_stock_icon(output_pane, "build", "Build", Gtk.Stock.CONVERT);
// Run pane
run_terminal = new Vte.Terminal();
run_terminal.child_exited.connect(on_run_child_exit);
child_process_running = false;
run_pane = new Gtk.ScrolledWindow(null, null);
run_pane.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
run_pane.add(run_terminal);
run_pane.show_all();
panel.add_item_with_stock_icon(run_pane, "run", "Run", Gtk.Stock.EXECUTE);
// Symbol pane
symbol_browser = new SymbolBrowser(this);
symbol_browser_connect_id = Signal.connect(window, "active-tab-changed",
(Callback) SymbolBrowser.on_active_tab_changed, symbol_browser);
symbol_browser.activate();
// Enclosing class in statusbar
old_cursor_offset = 0;
// Signal connections
instance_connections = new SignalConnection(this);
tab_connections = new ArrayList<SignalConnection>();
// Toolbar menu
Gtk.UIManager manager = window.get_ui_manager();
action_group = new Gtk.ActionGroup("valencia");
action_group.add_actions(entries, this);
manager.insert_action_group(action_group, 0);
try {
ui_id = manager.add_ui_from_string(ui, -1);
} catch (Error e) {
error("error in add_ui_from_string: %s", e.message);
}
initialize_menu_items(manager);
init_error_regex();
instance_connections.add_signal(window, "tab-added", (Callback) tab_added_callback, this);
instance_connections.add_signal(window, "tab-removed", (Callback) tab_removed_callback, this);
foreach (Gedit.Document document in window.get_documents()) {
tab_added_callback(window, Gedit.Tab.get_from_document(document), this);
}
}
public static Instance? find(Gedit.Window window) {
foreach (Instance i in instances)
if (i.window == window)
return i;
return null;
}
void initialize_menu_items(Gtk.UIManager manager) {
Gtk.MenuItem search_menu = get_menu_item(manager, "/MenuBar/SearchMenu");
search_menu.activate.connect(on_search_menu_activated);
Gtk.MenuItem project_menu = get_menu_item(manager, "/MenuBar/ExtraMenu_1/ProjectMenu");
project_menu.activate.connect(on_project_menu_activated);
go_to_definition_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchGoToDefinitionMenu");
find_symbol_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchFindSymbolMenu");
go_to_outer_scope_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchGoToEnclosingMethodMenu");
go_back_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchGoBackMenu");
go_forward_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchGoForwardMenu");
next_error_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchNextErrorMenu");
prev_error_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchPrevErrorMenu");
display_tooltip_menu_item = get_menu_item(manager,
"/MenuBar/SearchMenu/SearchOps_8/SearchAutocompleteMenu");
build_menu_item = get_menu_item(manager,
"/MenuBar/ExtraMenu_1/ProjectMenu/ProjectBuildMenu");
clean_menu_item = get_menu_item(manager,
"/MenuBar/ExtraMenu_1/ProjectMenu/ProjectCleanMenu");
run_menu_item = get_menu_item(manager,
"/MenuBar/ExtraMenu_1/ProjectMenu/ProjectRunMenu");
settings_menu_item = get_menu_item(manager,
"/MenuBar/ExtraMenu_1/ProjectMenu/ProjectSettingsMenu");
}
static void tab_added_callback(Gedit.Window window, Gedit.Tab tab, Instance instance) {
Gedit.Document document = tab.get_document();
documents_modified_state.set(document, false);
SignalConnection connection = new SignalConnection(tab);
instance.tab_connections.add(connection);
// Hook up this particular tab's view with tooltips
Gedit.View tab_view = tab.get_view();
connection.add_signal(tab_view, "key-press-event", (Callback) key_press_callback, instance);
connection.add_signal(tab_view, "show-completion",
(Callback) show_completion_callback, instance);
Gtk.Widget widget = tab_view.get_parent();
Gtk.ScrolledWindow scrolled_window = widget as Gtk.ScrolledWindow;
assert(scrolled_window != null);
Gtk.Adjustment vert_adjust = scrolled_window.get_vadjustment();
connection.add_signal(vert_adjust, "value-changed", (Callback) scrolled_callback, instance);
connection.add_signal(document, "saved", (Callback) all_save_callback, instance);
connection.add_signal(document, "insert-text", (Callback) text_inserted_callback, instance, true);
connection.add_signal(document, "delete-range", (Callback) text_deleted_callback, instance, true);
connection.add_signal(document, "cursor-moved", (Callback) cursor_moved_callback, instance, true);
connection.add_signal(tab_view, "focus-out-event", (Callback) focus_off_view_callback, instance);
connection.add_signal(tab_view, "button-press-event", (Callback) button_press_callback, instance);
}
static void tab_removed_callback(Gedit.Window window, Gedit.Tab tab, Instance instance) {
weak Gedit.Document removed_document = tab.get_document();
bool document_exists_in_map = documents_modified_state.unset(removed_document);
assert(document_exists_in_map);
foreach (SignalConnection connection in instance.tab_connections) {
if (connection.base_instance == tab) {
instance.tab_connections.remove(connection);
break;
}
}
Gedit.Document document = tab.get_document();
if (document.get_modified()) {
// We're closing a document without saving changes. Reparse the symbol tree
// from the source file on disk (if the file exists on disk).
string path = document_filename(document);
if (path != null && FileUtils.test(path, FileTest.EXISTS))
Program.update_any(path, null);
}
}
static void scrolled_callback(Gtk.Adjustment adjust, Instance instance) {
instance.tip.hide();
instance.autocomplete.hide();
}
static void show_completion_callback(Gedit.View view, Instance instance) {
instance.on_display_tooltip_or_autocomplete();
}
static bool key_press_callback(Gedit.View view, Gdk.EventKey key, Instance instance) {
bool handled = false;
// These will always catch, even with alt and ctrl modifiers
switch(Gdk.keyval_name(key.keyval)) {
case "Escape":
if (instance.autocomplete.is_visible())
instance.autocomplete.hide();
else
instance.tip.hide();
handled = true;
break;
case "Up":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.select_previous();
handled = true;
}
break;
case "Down":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.select_next();
handled = true;
}
break;
// We handle Alt+Left and Alt+Right explicitly to override GtkSourceView, which
// normally uses these as shortcuts for moving the selected word left or right.
case "Left":
if (key.state == Gdk.ModifierType.MOD1_MASK) {
instance.on_go_back();
handled = true;
}
break;
case "Right":
if (key.state == Gdk.ModifierType.MOD1_MASK) {
instance.on_go_forward();
handled = true;
}
break;
case "Home":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.select_first_cell();
handled = true;
}
break;
case "End":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.select_last_cell();
handled = true;
}
break;
case "Page_Up":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.page_up();
handled = true;
}
break;
case "Page_Down":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.page_down();
handled = true;
}
break;
case "Return":
if (instance.autocomplete.is_visible()) {
instance.autocomplete.select_item();
handled = true;
}
break;
default:
break;
}
return handled;
}
static bool focus_off_view_callback(Gedit.View view, Gdk.EventFocus focus, Instance instance) {
instance.tip.hide();
instance.autocomplete.hide();
// Make sure to display the new enclosing class when switching tabs
instance.old_cursor_offset = 0;
instance.update_status_bar();
// Let other handlers catch this event as well
return false;
}
static void text_inserted_callback(Gedit.Document doc, Gtk.TextIter iter, string text,
int length, Instance instance) {
if (instance.autocomplete.is_visible()) {
if (text.get_char().isspace())
instance.autocomplete.hide();
else
instance.display_autocomplete(instance.get_scan_info(true));
}
if (instance.tip.is_visible()) {
if (text == ")" || text == "(") {
instance.tip.hide();
instance.autocomplete.hide();
instance.display_tooltip(instance.get_scan_info(true));
}
}
}
static void text_deleted_callback(Gedit.Document doc, Gtk.TextIter start, Gtk.TextIter end,
Instance instance) {
if (instance.tip.is_visible()) {
string line = instance.tip.get_method_line();
if (!line.contains(instance.tip.get_method_name() + "("))
instance.tip.hide();
}
if (instance.autocomplete.is_visible()) {
instance.autocomplete.hide();
instance.on_display_tooltip_or_autocomplete();
}
}
static void cursor_moved_callback(Gedit.Document doc, Instance instance) {
instance.update_status_bar();
}
static bool button_press_callback(Gedit.View view, Gdk.EventButton event, Instance instance) {
instance.tip.hide();
instance.autocomplete.hide();
// Let other handlers catch this event as well
return false;
}
// TODO: Merge this method with saved_callback, below.
static void all_save_callback(Gedit.Document document, void *arg1, Instance instance) {
string path = document_filename(document);
Program.update_any(path, buffer_contents(document));
instance.symbol_browser.on_document_saved();
}
bool scroll_to_end() {
Gtk.TextIter end;
output_buffer.get_end_iter(out end);
output_view.scroll_to_iter(end, 0.25, false, 0.0, 0.0);
return false;
}
bool on_build_output(IOChannel source, bool error) {
bool ret = true;
bool appended = false;
while (true) {
string line;
size_t length;
size_t terminator_pos;
IOStatus status;
try {
status = source.read_line(out line, out length, out terminator_pos);
} catch (ConvertError e) {
return false; // TODO: report error
} catch (IOChannelError e) {
return false; // TODO: report error
}
if (status == IOStatus.EOF) {
if (error) {
appended = on_process_finshed();
}
ret = false;
break;
}
if (status != IOStatus.NORMAL)
break;
append_with_tag(output_buffer, line, error ? error_tag : null);
appended = true;
}
if (appended)
Idle.add(scroll_to_end);
return ret;
}
bool on_build_stdout(IOChannel source, IOCondition condition) {
return on_build_output(source, false);
}
bool on_build_stderr(IOChannel source, IOCondition condition) {
return on_build_output(source, true);
}
bool on_build_finished() {
append_with_tag(output_buffer, "\nBuild complete", italic_tag);
// Always regenerate the list *after* a new build
generate_error_history(last_program_to_build);
return true;
}
void hide_old_build_output() {
foreach (Instance instance in instances) {
if (instance != this && last_program_to_build == instance.last_program_to_build) {
instance.output_pane.hide();
instance.last_program_to_build = null;
}
}
}
string get_active_document_filename() {
Gedit.Document document = window.get_active_document();
return document_filename(document);
}
void show_output_pane() {
output_pane.show();
Gedit.Panel panel = window.get_bottom_panel();
panel.activate_item(output_pane);
panel.show();
}
void spawn_process(string command, string working_directory, ProcessFinished callback) {
string[] argv;
try {
if (!Shell.parse_argv(command, out argv)) {
warning("can't parse command arguments");
return;
}
} catch (ShellError e) {
warning("error parsing command arguments: %s", e.message);
return;
}
on_process_finshed = callback;
output_buffer.set_text("", 0);
output_pane.show();
Gedit.Panel panel = window.get_bottom_panel();
panel.activate_item(output_pane);
panel.show();
Pid child_pid;
int input_fd;
int output_fd;
int error_fd;
try {
Process.spawn_async_with_pipes(
working_directory, // working directory
argv,
null, // environment
SpawnFlags.SEARCH_PATH,
null, // child_setup
out child_pid,
out input_fd,
out output_fd,
out error_fd);
} catch (SpawnError e) {
append_with_tag(output_buffer, "Could not execute ", italic_tag);
append_with_tag(output_buffer, argv[0], bold_tag);
append_with_tag(output_buffer, " in ", italic_tag);
append_with_tag(output_buffer, working_directory, bold_tag);
return;
}
try {
make_pipe(output_fd, on_build_stdout);
make_pipe(error_fd, on_build_stderr);
} catch (IOChannelError e) {
append_with_tag(output_buffer, "There was an I/O error trying to run ", italic_tag);
append_with_tag(output_buffer, argv[0], bold_tag);
append_with_tag(output_buffer, " in ", italic_tag);
append_with_tag(output_buffer, working_directory, bold_tag);
return;
}
append_with_tag(output_buffer, "Running ", italic_tag);
append_with_tag(output_buffer, command, bold_tag);
append_with_tag(output_buffer, " in ", italic_tag);
append_with_tag(output_buffer, working_directory, bold_tag);
append(output_buffer, "\n\n");
}
void build() {
string filename = get_active_document_filename();
if (filename == null)
return;
Program.rescan_build_root(filename);
// Record the last program to build in this window so that we don't accidentally hide
// output that isn't part of a program that gets built later
last_program_to_build = Program.find_containing(filename);
hide_old_build_output();
string command = last_program_to_build.config_file.get_build_command();
spawn_process(command, last_program_to_build.get_top_directory(), on_build_finished);
}
void on_saved() {
if (--saving == 0)
build();
}
static void saved_callback(Gedit.Document document, void *arg1, Instance instance) {
SignalHandler.disconnect_by_func(document, (void *) saved_callback, instance);
instance.on_saved();
}
void on_build() {
foreach (Gedit.Document d in ((Gedit.App) Application.get_default()).get_documents())
if (!d.is_untitled() && d.get_modified()) {
++saving;
Signal.connect(d, "saved", (Callback) saved_callback, this);
d.do_save(0);
}
if (saving == 0)
build();
}
bool scroll_view_to_cursor() {
if (view_to_scroll != null) {
view_to_scroll.scroll_to_cursor();
view_to_scroll = null;
}
return false;
}
void go(Gedit.Tab tab, Destination dest) {
Gedit.Document document = tab.get_document();
Gtk.TextIter start;
Gtk.TextIter end;
dest.get_range(document, out start, out end);
document.select_range(start, end);
// We need to scroll to the selection in an idle handler. If we don't, then when
// a new document is being loaded sometimes the scrolling won't happen at all,
// especially if the document is long. This was http://trac.yorba.org/ticket/1000 .
view_to_scroll = tab.get_view();
Idle.add(scroll_view_to_cursor);
}
void on_document_loaded(Gedit.Document document) {
if (document_filename(document) == target_filename) {
Gedit.Tab tab = Gedit.Tab.get_from_document(document);
go(tab, destination);
target_filename = null;
destination = null;
}
}
static void document_loaded_callback(Gedit.Document document, void *arg1, Instance instance) {
instance.on_document_loaded(document);
}
public void jump(string filename, Destination dest) {
Gedit.Window w;
Gedit.Tab tab = find_tab(filename, out w);
if (tab != null) {
w.set_active_tab(tab);
w.present();
go(tab, dest);
return;
}
Gedit.Encoding encoding = null;
tab = window.create_tab_from_location(File.new_for_path(filename), encoding, 0, 0, false, true);
target_filename = filename;
destination = dest;
Signal.connect(tab.get_document(), "loaded", (Callback) document_loaded_callback, this);
}
// We look for two kinds of error lines:
// foo.vala:297.15-297.19: ... (valac errors)
// foo.c:268: ... (GCC errors, containing a line number only)
void init_error_regex() {
try {
error_regex = new Regex("""^(.*):(\d+)(?:\.(\d+)-(\d+)\.(\d+))?:""");
} catch (RegexError e) {
stderr.puts("A RegexError occured when creating a new regular expression.\n");
return; // TODO: report error
}
}
string get_line(Gtk.TextIter iter) {
Gtk.TextIter start;
Gtk.TextIter end;
weak Gtk.TextBuffer buffer = iter.get_buffer();
get_line_start_end(iter, out start, out end);
return buffer.get_text(start, end, true);
}
// Look for error position information in the line containing the given iterator.
ErrorInfo? error_info(Gtk.TextIter iter) {
string line = get_line(iter);
MatchInfo info;
if (error_regex.match(line, 0, out info)) {
ErrorInfo e = new ErrorInfo();
e.filename = info.fetch(1);
e.start_line = info.fetch(2);
e.start_char = info.fetch(3);
e.end_line = info.fetch(4);
e.end_char = info.fetch(5);
return e;
}
else return null;
}
// Return true if s is composed of ^^^ characters pointing to an error snippet above.
bool is_snippet_marker(string s) {
weak string p = s;
while (p != "") {
unichar c = p.get_char();
if (!c.isspace() && c != '^')
return false;
p = p.next_char();
}
return true;
}
void tag_text_buffer_line(Gtk.TextBuffer buffer, Gtk.TextTag tag, Gtk.TextIter iter) {
Gtk.TextIter start;
Gtk.TextIter end;
buffer.get_bounds(out start, out end);
buffer.remove_tag(tag, start, end);
get_line_start_end(iter, out start, out end);
buffer.apply_tag(tag, start, end);
}
void jump_to_document_error(Gtk.TextIter iter, ErrorInfo info, Program program) {
int line_number = int.parse(info.start_line);
Destination dest;
if (info.start_char == null)
dest = new LineNumber(line_number - 1);
else
dest = new LineCharRange(line_number - 1, int.parse(info.start_char) - 1,
int.parse(info.end_line) - 1, int.parse(info.end_char));
if (Path.is_absolute(info.filename)) {
jump(info.filename, dest);
} else {
string filename = program.get_path_for_filename(info.filename);
if (filename == null)
return;
jump(filename, dest);
}
}
////////////////////////////////////////////////////////////
// Jump to Definition //
////////////////////////////////////////////////////////////
void add_mark_at_insert_to_history() {
Gedit.Document doc = window.get_active_document();
Gtk.TextIter insert = get_insert_iter(doc);
// Don't add a mark to history if the most recent mark is on the same line
if (history.size > 0) {
Gtk.TextMark old_mark = history.get(history.size - 1);
Gedit.Document old_doc = (Gedit.Document) old_mark.get_buffer();
if (old_doc == doc) {
Gtk.TextIter old_iter;
old_doc.get_iter_at_mark(out old_iter, old_mark);
if (old_iter.get_line() == insert.get_line())
return;
}
}
Gtk.TextMark mark = doc.create_mark(null, insert, false);
history.add(mark);
if (history.size > MAX_HISTORY)
history.remove_at(0);
history_index = history.size; // always set the current index to be at the top
}
void add_insert_cursor_to_history() {
// Make sure the current index is the last element
while (history.size > 0 && history.size > history_index)
history.remove_at(history.size - 1);
add_mark_at_insert_to_history();
browsing_history = false;
}
public void reparse_modified_documents(string filename) {
Program program = Program.find_containing(filename, true);
foreach (Gedit.Document document in ((Gedit.App) Application.get_default()).get_documents()) {
assert(documents_modified_state.has_key(document));
bool previously_modified = documents_modified_state.get(document);
bool currently_modified = document.get_modified();
documents_modified_state.set(document, currently_modified);
if (!currently_modified && !previously_modified)
continue;
string path = document_filename(document);
if (path != null)
program.update(path, buffer_contents(document));
}
}
void get_buffer_contents_and_position(string filename, out string source, out int pos) {
reparse_modified_documents(filename);
Gedit.Document document = window.get_active_document();
source = buffer_contents(document);
Gtk.TextIter insert = get_insert_iter(document);
pos = insert.get_offset();
}
void on_go_to_definition() {
string? filename = active_filename();
if (filename == null || !Program.is_vala(filename))
return;
Program program = Program.find_containing(filename, true);
if (program.is_parsing()) {
program.parsed_file.connect(update_parse_dialog);
program.system_parse_complete.connect(jump_to_symbol_definition);
} else jump_to_symbol_definition();
}
void jump_to_symbol_definition() {
string? filename = active_filename();
if (filename == null)
return;
string source;
int pos;
get_buffer_contents_and_position(filename, out source, out pos);
ScanInfo info = get_scan_info(false);
if (info == null || info.inner() == null)
return;
Program program = Program.find_containing(filename);
SourceFile sf = program.find_source(filename);
Symbol? sym = sf.resolve(info.inner(), pos, false);
if (sym == null)
return;
add_insert_cursor_to_history();
SourceFile dest = sym.source;
jump(dest.filename, new CharRange(sym.start, sym.start + sym.name_length()));
}
void on_go_to_outer_scope() {
string? filename = active_filename();
if (filename == null || !Program.is_vala(filename))
return;
string source;
int pos;
get_buffer_contents_and_position(filename, out source, out pos);
ScanScope? scan_scope = new Parser().find_enclosing_scope(source, pos, false);
if (scan_scope == null)
return;
add_insert_cursor_to_history();