3 public class Editor : Object
8 public static Editor singleton()
10 if (_Editor == null) {
11 _Editor= new Editor();
15 public Xcls_save_button save_button;
16 public Xcls_key_edit key_edit;
17 public Xcls_RightEditor RightEditor;
18 public Xcls_view view;
19 public Xcls_buffer buffer;
22 public Xcls_MainWindow window;
23 public int pos_root_x;
25 public int pos_root_y;
27 public Gtk.SourceSearchContext searchcontext;
28 public int last_search_end;
29 public JsRender.NodeProp? prop;
30 public JsRender.JsRender? file;
31 public JsRender.Node node;
32 public signal void save ();
33 public string activeEditor;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
45 this.searchcontext = null;
46 this.last_search_end = 0;
50 this.activeEditor = "";
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 this.el.vexpand = true;
56 var child_0 = new Xcls_Box2( _this );
58 this.el.pack_start ( child_0.el , false,true );
59 var child_1 = new Xcls_RightEditor( _this );
61 this.el.pack_end ( child_1.el , true,true );
64 // user defined functions
65 public bool saveContents () {
68 if (_this.file == null) {
74 var str = _this.buffer.toString();
76 _this.buffer.checkSyntax();
80 // LeftPanel.model.changed( str , false);
82 _this.save_button.el.sensitive = false;
84 // find the text for the node..
85 if (_this.file.xtype != "PlainFile") {
86 // in theory these properties have to exist!?!
88 this.window.windowstate.left_props.reload();
90 _this.file.setSource( str );
99 public void forwardSearch (bool change_focus) {
101 if (this.searchcontext == null) {
105 Gtk.TextIter beg, st,en;
107 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
108 if (!this.searchcontext.forward(beg, out st, out en)) {
110 this.last_search_end = 0;
112 this.last_search_end = en.get_offset();
114 this.view.el.grab_focus();
116 this.buffer.el.place_cursor(st);
117 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
121 public void show (JsRender.JsRender file, JsRender.Node? node, JsRender.NodeProp? prop)
126 if (file.xtype != "PlainFile") {
130 // find the text for the node..
131 this.view.load( prop.val );
132 this.key_edit.el.show();
133 this.key_edit.el.text = prop.rtype + " " + prop.name;
136 this.view.load( file.toSource() );
137 this.key_edit.el.hide();
141 public int search (string txt) {
143 var s = new Gtk.SourceSearchSettings();
145 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
146 this.searchcontext .set_highlight(true);
147 s.set_search_text(txt);
148 Gtk.TextIter beg, st,en;
150 this.buffer.el.get_start_iter(out beg);
151 this.searchcontext.forward(beg, out st, out en);
152 this.last_search_end = 0;
154 return this.searchcontext.get_occurrences_count();
160 public void reset () {
165 this.searchcontext = null;
168 public void scroll_to_line (int line) {
170 GLib.Timeout.add(500, () => {
172 var buf = this.view.el.get_buffer();
174 var sbuf = (Gtk.SourceBuffer) buf;
178 sbuf.get_iter_at_line(out iter, line);
179 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
183 public class Xcls_Box2 : Object
186 private Editor _this;
192 public Xcls_Box2(Editor _owner )
195 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
199 // set gobject values
200 this.el.homogeneous = false;
201 var child_0 = new Xcls_save_button( _this );
203 this.el.pack_start ( child_0.el , false,false );
204 var child_1 = new Xcls_key_edit( _this );
206 this.el.pack_start ( child_1.el , true,true );
207 var child_2 = new Xcls_HScale5( _this );
209 this.el.pack_end ( child_2.el , true,true );
212 // user defined functions
214 public class Xcls_save_button : Object
216 public Gtk.Button el;
217 private Editor _this;
223 public Xcls_save_button(Editor _owner )
226 _this.save_button = this;
227 this.el = new Gtk.Button();
231 // set gobject values
232 this.el.label = "Save";
235 this.el.clicked.connect( () => {
236 _this.saveContents();
240 // user defined functions
243 public class Xcls_key_edit : Object
246 private Editor _this;
252 public Xcls_key_edit(Editor _owner )
255 _this.key_edit = this;
256 this.el = new Gtk.Entry();
260 // set gobject values
261 this.el.width_request = 100;
262 this.el.editable = false;
265 // user defined functions
268 public class Xcls_HScale5 : Object
270 public Gtk.HScale el;
271 private Editor _this;
277 public Xcls_HScale5(Editor _owner )
280 this.el = new Gtk.HScale.with_range (6, 30, 1);
284 // set gobject values
285 this.el.has_origin = true;
286 this.el.draw_value = true;
288 this.el.sensitive = true;
291 this.el.change_value.connect( (st, val ) => {
293 var description = Pango.FontDescription.from_string("monospace");
294 print("resize to %d", (int)val*1000);
295 description.set_size((int)val*1000);
296 _this.view.el.override_font(description);
301 // user defined functions
305 public class Xcls_RightEditor : Object
307 public Gtk.ScrolledWindow el;
308 private Editor _this;
314 public Xcls_RightEditor(Editor _owner )
317 _this.RightEditor = this;
318 this.el = new Gtk.ScrolledWindow( null, null );
322 // set gobject values
323 var child_0 = new Xcls_view( _this );
325 this.el.add ( child_0.el );
328 // user defined functions
330 public class Xcls_view : Object
332 public Gtk.SourceView el;
333 private Editor _this;
339 public Xcls_view(Editor _owner )
343 this.el = new Gtk.SourceView();
347 // set gobject values
348 this.el.auto_indent = true;
349 this.el.indent_width = 4;
350 this.el.show_line_marks = true;
351 this.el.insert_spaces_instead_of_tabs = true;
352 this.el.show_line_numbers = true;
353 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
354 this.el.tab_width = 4;
355 this.el.highlight_current_line = true;
356 var child_0 = new Xcls_buffer( _this );
358 this.el.set_buffer ( child_0.el );
361 this.el.key_release_event.connect( (event) => {
363 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
364 print("SAVE: ctrl-S pressed");
365 _this.saveContents();
368 // print(event.key.keyval)
375 // user defined functions
376 public void load (string str) {
378 // show the help page for the active node..
379 //this.get('/Help').show();
382 // this.get('/BottomPane').el.set_current_page(0);
383 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
384 buf.set_text(str, str.length);
385 buf.set_undo_manager(null);
387 var lm = Gtk.SourceLanguageManager.get_default();
389 if (_this.file != null) {
390 lang = _this.file.language;
392 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
393 var lg = _this.file.content_type.length > 0 ?
394 lm.guess_language(_this.file.path, _this.file.content_type) :
395 lm.get_language(lang);
398 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
400 this.el.insert_spaces_instead_of_tabs = true;
402 print("sourcelanguage = %s\n", lg.name);
403 if (lg.name == "Vala") {
404 this.el.insert_spaces_instead_of_tabs = false;
408 this.el.grab_focus();
409 _this.save_button.el.sensitive = false;
412 public class Xcls_buffer : Object
414 public Gtk.SourceBuffer el;
415 private Editor _this;
419 public int error_line;
420 public bool check_queued;
421 public bool check_running;
424 public Xcls_buffer(Editor _owner )
428 this.el = new Gtk.SourceBuffer( null );
431 this.error_line = -1;
432 this.check_queued = false;
433 this.check_running = false;
435 // set gobject values
438 this.el.changed.connect( () => {
441 _this.save_button.el.sensitive = true;
442 print("EDITOR CHANGED");
447 // this.get('/LeftPanel.model').changed( str , false);
452 // user defined functions
453 public bool checkSyntax () {
455 if (this.check_running) {
456 print("Check is running\n");
457 if (this.check_queued) {
458 print("Check is already queued");
461 this.check_queued = true;
462 print("Adding queued Check ");
463 GLib.Timeout.add_seconds(1, () => {
464 this.check_queued = false;
473 var str = this.toString();
476 if (this.error_line > 0) {
479 this.el.get_bounds (out start, out end);
481 this.el.remove_source_marks (start, end, null);
483 if (str.length < 1) {
484 print("checkSyntax - empty string?\n");
488 if (_this.file.xtype == "PlainFile") {
490 // assume it's gtk...
491 this.check_running = true;
493 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
497 this.check_running = false;
503 if (_this.file == null) {
506 var p = _this.file.project.palete;
510 this.check_running = true;
513 if (_this.file.language == "js") {
514 this.check_running = false;
515 print("calling validate javascript\n");
516 Gee.HashMap<int,string> errors;
517 p.javascriptHasErrors(
518 _this.window.windowstate,
521 _this.file, // no reference not node?
524 return this.highlightErrors(errors);
529 print("calling validate vala\n");
530 // clear the buttons.
533 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
539 this.check_running = false;
544 //print("done mark line\n");
546 return true; // at present allow saving - even if it's invalid..
548 public bool highlightErrorsJson (string type, Json.Object obj) {
551 this.el.get_bounds (out start, out end);
553 this.el.remove_source_marks (start, end, type);
556 // we should highlight other types of errors..
558 if (!obj.has_member(type)) {
559 print("Return has no errors\n");
563 if (_this.window.windowstate.state != WindowState.State.CODEONLY
570 var err = obj.get_object_member(type);
573 if (_this.file == null) {
577 var valafn = _this.file.path;
579 if (_this.file.xtype != "PlainFile") {
586 var regex = new Regex("\\.bjs$");
590 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
591 } catch (GLib.RegexError e) {
598 if (!err.has_member(valafn)) {
599 print("File path has no errors\n");
603 var lines = err.get_object_member(valafn);
606 if (obj.has_member("line_offset")) {
607 offset = (int)obj.get_int_member("line_offset") + 1;
613 var tlines = this.el.get_line_count () +1;
615 lines.foreach_member((obj, line, node) => {
618 // print("get inter\n");
619 var eline = int.parse(line) - offset;
620 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
623 if (eline > tlines || eline < 0) {
626 this.el.get_iter_at_line( out iter, eline);
627 //print("mark line\n");
628 var msg = "Line: %d".printf(eline+1);
629 var ar = lines.get_array_member(line);
630 for (var i = 0 ; i < ar.get_length(); i++) {
631 msg += (msg.length > 0) ? "\n" : "";
632 msg += ar.get_string_element(i);
636 this.el.create_source_mark(msg, type, iter);
645 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
647 this.error_line = validate_res.size;
649 if (this.error_line < 1) {
652 var tlines = this.el.get_line_count ();
654 var valiter = validate_res.map_iterator();
655 while (valiter.next()) {
657 // print("get inter\n");
658 var eline = valiter.get_key();
659 if (eline > tlines) {
662 this.el.get_iter_at_line( out iter, eline);
663 //print("mark line\n");
664 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
668 public string toString () {
672 this.el.get_start_iter(out s);
673 this.el.get_end_iter(out e);
674 var ret = this.el.get_text(s,e,true);
675 //print("TO STRING? " + ret);