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 string activeEditor;
24 public int pos_root_x;
25 public int pos_root_y;
27 public int last_search_end;
29 public Gtk.SourceSearchContext searchcontext;
30 public JsRender.JsRender file;
33 public signal void save ();
34 public JsRender.Node node;
40 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
44 this.activeEditor = "";
46 this.last_search_end = 0;
48 this.searchcontext = null;
55 this.el.homogeneous = false;
56 this.el.hexpand = true;
57 var child_0 = new Xcls_Box2( _this );
59 this.el.pack_start ( child_0.el , false,true );
60 var child_1 = new Xcls_RightEditor( _this );
62 this.el.pack_end ( child_1.el , true,true );
65 // user defined functions
66 public bool saveContents () {
69 if (_this.file == null) {
77 var str = _this.buffer.toString();
79 _this.buffer.checkSyntax();
83 // LeftPanel.model.changed( str , false);
85 _this.save_button.el.sensitive = false;
87 // find the text for the node..
88 if (_this.file.xtype != "PlainFile") {
89 if (ptype == "listener") {
90 this.node.listeners.set(key,str);
93 this.node.props.set(key,str);
96 _this.file.setSource( str );
105 public void scroll_to_line (int line) {
107 GLib.Timeout.add(500, () => {
109 var buf = this.view.el.get_buffer();
111 var sbuf = (Gtk.SourceBuffer) buf;
115 sbuf.get_iter_at_line(out iter, line);
116 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
120 public int search (string txt) {
122 var s = new Gtk.SourceSearchSettings();
124 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
125 this.searchcontext .set_highlight(true);
126 s.set_search_text(txt);
127 Gtk.TextIter beg, st,en;
129 this.buffer.el.get_start_iter(out beg);
130 this.searchcontext.forward(beg, out st, out en);
131 this.last_search_end = 0;
133 return this.searchcontext.get_occurrences_count();
139 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
145 this.searchcontext = null;
147 if (file.xtype != "PlainFile") {
153 // find the text for the node..
154 if (ptype == "listener") {
155 val = node.listeners.get(key);
158 val = node.props.get(key);
161 this.key_edit.el.show();
162 this.key_edit.el.text = key;
165 this.view.load( file.toSource() );
166 this.key_edit.el.hide();
171 public void forwardSearch () {
173 if (this.searchcontext == null) {
177 Gtk.TextIter beg, st,en;
179 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
180 this.searchcontext.forward(beg, out st, out en);
181 this.last_search_end = 0;
185 public class Xcls_Box2 : Object
188 private Editor _this;
194 public Xcls_Box2(Editor _owner )
197 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
201 // set gobject values
202 this.el.homogeneous = false;
203 var child_0 = new Xcls_save_button( _this );
205 this.el.pack_start ( child_0.el , false,false );
206 var child_1 = new Xcls_key_edit( _this );
208 this.el.pack_end ( child_1.el , true,true );
211 // user defined functions
213 public class Xcls_save_button : Object
215 public Gtk.Button el;
216 private Editor _this;
222 public Xcls_save_button(Editor _owner )
225 _this.save_button = this;
226 this.el = new Gtk.Button();
230 // set gobject values
231 this.el.label = "Save";
234 this.el.clicked.connect( () => {
235 _this.saveContents();
239 // user defined functions
242 public class Xcls_key_edit : Object
245 private Editor _this;
251 public Xcls_key_edit(Editor _owner )
254 _this.key_edit = this;
255 this.el = new Gtk.Entry();
259 // set gobject values
262 // user defined functions
266 public class Xcls_RightEditor : Object
268 public Gtk.ScrolledWindow el;
269 private Editor _this;
275 public Xcls_RightEditor(Editor _owner )
278 _this.RightEditor = this;
279 this.el = new Gtk.ScrolledWindow( null, null );
283 // set gobject values
284 var child_0 = new Xcls_view( _this );
286 this.el.add ( child_0.el );
290 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
293 // user defined functions
295 public class Xcls_view : Object
297 public Gtk.SourceView el;
298 private Editor _this;
304 public Xcls_view(Editor _owner )
308 this.el = new Gtk.SourceView();
312 // set gobject values
313 this.el.auto_indent = true;
314 this.el.indent_width = 4;
315 this.el.show_line_marks = true;
316 this.el.insert_spaces_instead_of_tabs = true;
317 this.el.show_line_numbers = true;
318 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
319 this.el.tab_width = 4;
320 this.el.highlight_current_line = true;
321 var child_0 = new Xcls_buffer( _this );
323 this.el.set_buffer ( child_0.el );
327 var description = Pango.FontDescription.from_string("monospace");
328 description.set_size(8000);
329 this.el.override_font(description);
332 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
333 this.el.completion.unblock_interactive();
334 this.el.completion.select_on_show = true; // select
335 this.el.completion.show_headers = false;
336 this.el.completion.remember_info_visibility = true;
339 var attrs = new Gtk.SourceMarkAttributes();
340 var pink = Gdk.RGBA();
341 pink.parse ( "pink");
342 attrs.set_background ( pink);
343 attrs.set_icon_name ( "process-stop");
344 attrs.query_tooltip_text.connect(( mark) => {
345 //print("tooltip query? %s\n", mark.name);
349 this.el.set_mark_attributes ("ERR", attrs, 1);
351 var wattrs = new Gtk.SourceMarkAttributes();
352 var blue = Gdk.RGBA();
353 blue.parse ( "#ABF4EB");
354 wattrs.set_background ( blue);
355 wattrs.set_icon_name ( "process-stop");
356 wattrs.query_tooltip_text.connect(( mark) => {
357 //print("tooltip query? %s\n", mark.name);
361 this.el.set_mark_attributes ("WARN", wattrs, 1);
365 var dattrs = new Gtk.SourceMarkAttributes();
366 var purple = Gdk.RGBA();
367 purple.parse ( "#EEA9FF");
368 dattrs.set_background ( purple);
369 dattrs.set_icon_name ( "process-stop");
370 dattrs.query_tooltip_text.connect(( mark) => {
371 //print("tooltip query? %s\n", mark.name);
375 this.el.set_mark_attributes ("DEPR", dattrs, 1);
378 this.el.key_release_event.connect( (event) => {
380 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
381 print("SAVE: ctrl-S pressed");
382 _this.saveContents();
385 // print(event.key.keyval)
392 // user defined functions
393 public void load (string str) {
395 // show the help page for the active node..
396 //this.get('/Help').show();
399 // this.get('/BottomPane').el.set_current_page(0);
400 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
401 buf.set_text(str, str.length);
402 buf.set_undo_manager(null);
404 var lm = Gtk.SourceLanguageManager.get_default();
406 if (_this.file != null) {
407 lang = _this.file.language;
409 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
410 var lg = _this.file.content_type.length > 0 ?
411 lm.guess_language(_this.file.path, _this.file.content_type) :
412 lm.get_language(lang);
415 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
417 this.el.insert_spaces_instead_of_tabs = true;
419 print("sourcelanguage = %s\n", lg.name);
420 if (lg.name == "Vala") {
421 this.el.insert_spaces_instead_of_tabs = false;
425 this.el.grab_focus();
426 _this.save_button.el.sensitive = false;
429 public class Xcls_buffer : Object
431 public Gtk.SourceBuffer el;
432 private Editor _this;
436 public bool check_queued;
437 public int error_line;
438 public bool check_running;
441 public Xcls_buffer(Editor _owner )
445 this.el = new Gtk.SourceBuffer( null );
448 this.check_queued = false;
449 this.error_line = -1;
450 this.check_running = false;
452 // set gobject values
455 this.el.changed.connect( () => {
458 _this.save_button.el.sensitive = true;
459 print("EDITOR CHANGED");
464 // this.get('/LeftPanel.model').changed( str , false);
469 // user defined functions
470 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
472 this.error_line = validate_res.size;
474 if (this.error_line < 1) {
477 var tlines = this.el.get_line_count ();
479 var valiter = validate_res.map_iterator();
480 while (valiter.next()) {
482 // print("get inter\n");
483 var eline = valiter.get_key();
484 if (eline > tlines) {
487 this.el.get_iter_at_line( out iter, eline);
488 //print("mark line\n");
489 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
493 public string toString () {
497 this.el.get_start_iter(out s);
498 this.el.get_end_iter(out e);
499 var ret = this.el.get_text(s,e,true);
500 //print("TO STRING? " + ret);
503 public bool checkSyntax () {
505 if (this.check_running) {
506 print("Check is running\n");
507 if (this.check_queued) {
508 print("Check is already queued");
511 this.check_queued = true;
512 print("Adding queued Check ");
513 GLib.Timeout.add_seconds(1, () => {
514 this.check_queued = false;
523 var str = this.toString();
526 if (this.error_line > 0) {
529 this.el.get_bounds (out start, out end);
531 this.el.remove_source_marks (start, end, null);
533 if (str.length < 1) {
534 print("checkSyntax - empty string?\n");
538 if (_this.file.xtype == "PlainFile") {
540 // assume it's gtk...
541 this.check_running = true;
543 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
547 this.check_running = false;
553 if (_this.file == null) {
556 var p = Palete.factory(_this.file.xtype);
560 this.check_running = true;
563 if (_this.file.language == "js") {
564 this.check_running = false;
565 print("calling validate javascript\n");
566 return this.highlightErrors(p.validateJavascript(
577 print("calling validate vala\n");
578 // clear the buttons.
581 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
588 this.check_running = false;
593 //print("done mark line\n");
595 return true; // at present allow saving - even if it's invalid..
597 public bool highlightErrorsJson (string type, Json.Object obj) {
600 this.el.get_bounds (out start, out end);
602 this.el.remove_source_marks (start, end, type);
605 // we should highlight other types of errors..
607 if (!obj.has_member(type)) {
608 print("Return has no errors\n");
612 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
613 _this.window.windowstate.state != WindowState.State.CODE
619 var err = obj.get_object_member(type);
622 if (_this.file == null) {
626 var valafn = _this.file.path;
628 if (_this.file.xtype != "PlainFile") {
635 var regex = new Regex("\\.bjs$");
639 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
640 } catch (GLib.RegexError e) {
647 if (!err.has_member(valafn)) {
648 print("File path has no errors\n");
652 var lines = err.get_object_member(valafn);
655 if (obj.has_member("line_offset")) {
656 offset = (int)obj.get_int_member("line_offset") + 1;
662 var tlines = this.el.get_line_count () +1;
664 lines.foreach_member((obj, line, node) => {
667 // print("get inter\n");
668 var eline = int.parse(line) - offset;
669 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
672 if (eline > tlines || eline < 0) {
675 this.el.get_iter_at_line( out iter, eline);
676 //print("mark line\n");
677 var msg = "Line: %d".printf(eline+1);
678 var ar = lines.get_array_member(line);
679 for (var i = 0 ; i < ar.get_length(); i++) {
680 msg += (msg.length > 0) ? "\n" : "";
681 msg += ar.get_string_element(i);
685 this.el.create_source_mark(msg, type, iter);