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;
28 public Gtk.SourceSearchContext searchcontext;
29 public JsRender.JsRender file;
32 public signal void save ();
33 public JsRender.Node node;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
43 this.activeEditor = "";
46 this.searchcontext = null;
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 var child_0 = new Xcls_Box2( _this );
57 this.el.pack_start ( child_0.el , false,true );
58 var child_1 = new Xcls_RightEditor( _this );
60 this.el.pack_end ( child_1.el , true,true );
63 // user defined functions
64 public bool saveContents () {
67 if (_this.file == null) {
75 var str = _this.buffer.toString();
77 _this.buffer.checkSyntax();
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
85 // find the text for the node..
86 if (_this.file.xtype != "PlainFile") {
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
94 _this.file.setSource( str );
103 public void scroll_to_line (int line) {
105 GLib.Timeout.add(500, () => {
107 var buf = this.view.el.get_buffer();
109 var sbuf = (Gtk.SourceBuffer) buf;
113 sbuf.get_iter_at_line(out iter, line);
114 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
118 public int search (string txt) {
120 var s = new Gtk.SourceSearchSettings();
122 this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 this.searchcontext .set_highlight(true);
124 s.set_search_text(txt);
125 Gtk.TextIter beg, st,en;
127 this.buffer.el.get_start_iter(out beg);
128 this.searchcontext.forward(beg, out st, out en);
131 return this.searchcontext.get_occurrences_count();
137 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
143 this.searchcontext = null;
145 if (file.xtype != "PlainFile") {
151 // find the text for the node..
152 if (ptype == "listener") {
153 val = node.listeners.get(key);
156 val = node.props.get(key);
159 this.key_edit.el.show();
160 this.key_edit.el.text = key;
163 this.view.load( file.toSource() );
164 this.key_edit.el.hide();
169 public void forwardSearch () {
171 if (this.searchcontext == null) {
177 public class Xcls_Box2 : Object
180 private Editor _this;
186 public Xcls_Box2(Editor _owner )
189 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
193 // set gobject values
194 this.el.homogeneous = false;
195 var child_0 = new Xcls_save_button( _this );
197 this.el.pack_start ( child_0.el , false,false );
198 var child_1 = new Xcls_key_edit( _this );
200 this.el.pack_end ( child_1.el , true,true );
203 // user defined functions
205 public class Xcls_save_button : Object
207 public Gtk.Button el;
208 private Editor _this;
214 public Xcls_save_button(Editor _owner )
217 _this.save_button = this;
218 this.el = new Gtk.Button();
222 // set gobject values
223 this.el.label = "Save";
226 this.el.clicked.connect( () => {
227 _this.saveContents();
231 // user defined functions
234 public class Xcls_key_edit : Object
237 private Editor _this;
243 public Xcls_key_edit(Editor _owner )
246 _this.key_edit = this;
247 this.el = new Gtk.Entry();
251 // set gobject values
254 // user defined functions
258 public class Xcls_RightEditor : Object
260 public Gtk.ScrolledWindow el;
261 private Editor _this;
267 public Xcls_RightEditor(Editor _owner )
270 _this.RightEditor = this;
271 this.el = new Gtk.ScrolledWindow( null, null );
275 // set gobject values
276 var child_0 = new Xcls_view( _this );
278 this.el.add ( child_0.el );
282 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
285 // user defined functions
287 public class Xcls_view : Object
289 public Gtk.SourceView el;
290 private Editor _this;
296 public Xcls_view(Editor _owner )
300 this.el = new Gtk.SourceView();
304 // set gobject values
305 this.el.auto_indent = true;
306 this.el.indent_width = 4;
307 this.el.show_line_marks = true;
308 this.el.insert_spaces_instead_of_tabs = true;
309 this.el.show_line_numbers = true;
310 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
311 this.el.tab_width = 4;
312 this.el.highlight_current_line = true;
313 var child_0 = new Xcls_buffer( _this );
315 this.el.set_buffer ( child_0.el );
319 var description = Pango.FontDescription.from_string("monospace");
320 description.set_size(8000);
321 this.el.override_font(description);
324 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
325 this.el.completion.unblock_interactive();
326 this.el.completion.select_on_show = true; // select
327 this.el.completion.show_headers = false;
328 this.el.completion.remember_info_visibility = true;
331 var attrs = new Gtk.SourceMarkAttributes();
332 var pink = Gdk.RGBA();
333 pink.parse ( "pink");
334 attrs.set_background ( pink);
335 attrs.set_icon_name ( "process-stop");
336 attrs.query_tooltip_text.connect(( mark) => {
337 //print("tooltip query? %s\n", mark.name);
341 this.el.set_mark_attributes ("ERR", attrs, 1);
343 var wattrs = new Gtk.SourceMarkAttributes();
344 var blue = Gdk.RGBA();
345 blue.parse ( "#ABF4EB");
346 wattrs.set_background ( blue);
347 wattrs.set_icon_name ( "process-stop");
348 wattrs.query_tooltip_text.connect(( mark) => {
349 //print("tooltip query? %s\n", mark.name);
353 this.el.set_mark_attributes ("WARN", wattrs, 1);
357 var dattrs = new Gtk.SourceMarkAttributes();
358 var purple = Gdk.RGBA();
359 purple.parse ( "#EEA9FF");
360 dattrs.set_background ( purple);
361 dattrs.set_icon_name ( "process-stop");
362 dattrs.query_tooltip_text.connect(( mark) => {
363 //print("tooltip query? %s\n", mark.name);
367 this.el.set_mark_attributes ("DEPR", dattrs, 1);
370 this.el.key_release_event.connect( (event) => {
372 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
373 print("SAVE: ctrl-S pressed");
374 _this.saveContents();
377 // print(event.key.keyval)
384 // user defined functions
385 public void load (string str) {
387 // show the help page for the active node..
388 //this.get('/Help').show();
391 // this.get('/BottomPane').el.set_current_page(0);
392 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
393 buf.set_text(str, str.length);
394 buf.set_undo_manager(null);
396 var lm = Gtk.SourceLanguageManager.get_default();
398 if (_this.file != null) {
399 lang = _this.file.language;
401 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
402 var lg = _this.file.content_type.length > 0 ?
403 lm.guess_language(_this.file.path, _this.file.content_type) :
404 lm.get_language(lang);
407 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
409 this.el.insert_spaces_instead_of_tabs = true;
411 print("sourcelanguage = %s\n", lg.name);
412 if (lg.name == "Vala") {
413 this.el.insert_spaces_instead_of_tabs = false;
417 this.el.grab_focus();
418 _this.save_button.el.sensitive = false;
421 public class Xcls_buffer : Object
423 public Gtk.SourceBuffer el;
424 private Editor _this;
428 public bool check_queued;
429 public int error_line;
430 public bool check_running;
433 public Xcls_buffer(Editor _owner )
437 this.el = new Gtk.SourceBuffer( null );
440 this.check_queued = false;
441 this.error_line = -1;
442 this.check_running = false;
444 // set gobject values
447 this.el.changed.connect( () => {
450 _this.save_button.el.sensitive = true;
451 print("EDITOR CHANGED");
456 // this.get('/LeftPanel.model').changed( str , false);
461 // user defined functions
462 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
464 this.error_line = validate_res.size;
466 if (this.error_line < 1) {
469 var tlines = this.el.get_line_count ();
471 var valiter = validate_res.map_iterator();
472 while (valiter.next()) {
474 // print("get inter\n");
475 var eline = valiter.get_key();
476 if (eline > tlines) {
479 this.el.get_iter_at_line( out iter, eline);
480 //print("mark line\n");
481 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
485 public string toString () {
489 this.el.get_start_iter(out s);
490 this.el.get_end_iter(out e);
491 var ret = this.el.get_text(s,e,true);
492 //print("TO STRING? " + ret);
495 public bool checkSyntax () {
497 if (this.check_running) {
498 print("Check is running\n");
499 if (this.check_queued) {
500 print("Check is already queued");
503 this.check_queued = true;
504 print("Adding queued Check ");
505 GLib.Timeout.add_seconds(1, () => {
506 this.check_queued = false;
515 var str = this.toString();
518 if (this.error_line > 0) {
521 this.el.get_bounds (out start, out end);
523 this.el.remove_source_marks (start, end, null);
525 if (str.length < 1) {
526 print("checkSyntax - empty string?\n");
530 if (_this.file.xtype == "PlainFile") {
532 // assume it's gtk...
533 this.check_running = true;
535 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
539 this.check_running = false;
545 if (_this.file == null) {
548 var p = Palete.factory(_this.file.xtype);
552 this.check_running = true;
555 if (_this.file.language == "js") {
556 this.check_running = false;
557 print("calling validate javascript\n");
558 return this.highlightErrors(p.validateJavascript(
569 print("calling validate vala\n");
570 // clear the buttons.
573 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
580 this.check_running = false;
585 //print("done mark line\n");
587 return true; // at present allow saving - even if it's invalid..
589 public bool highlightErrorsJson (string type, Json.Object obj) {
592 this.el.get_bounds (out start, out end);
594 this.el.remove_source_marks (start, end, type);
597 // we should highlight other types of errors..
599 if (!obj.has_member(type)) {
600 print("Return has no errors\n");
604 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
605 _this.window.windowstate.state != WindowState.State.CODE
611 var err = obj.get_object_member(type);
614 if (_this.file == null) {
618 var valafn = _this.file.path;
620 if (_this.file.xtype != "PlainFile") {
627 var regex = new Regex("\\.bjs$");
631 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
632 } catch (GLib.RegexError e) {
639 if (!err.has_member(valafn)) {
640 print("File path has no errors\n");
644 var lines = err.get_object_member(valafn);
647 if (obj.has_member("line_offset")) {
648 offset = (int)obj.get_int_member("line_offset") + 1;
654 var tlines = this.el.get_line_count () +1;
656 lines.foreach_member((obj, line, node) => {
659 // print("get inter\n");
660 var eline = int.parse(line) - offset;
661 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
664 if (eline > tlines || eline < 0) {
667 this.el.get_iter_at_line( out iter, eline);
668 //print("mark line\n");
669 var msg = "Line: %d".printf(eline+1);
670 var ar = lines.get_array_member(line);
671 for (var i = 0 ; i < ar.get_length(); i++) {
672 msg += (msg.length > 0) ? "\n" : "";
673 msg += ar.get_string_element(i);
677 this.el.create_source_mark(msg, type, iter);