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 if (!this.searchcontext.forward(beg, out st, out en)) {
182 this.last_search_end = 0;
184 this.last_search_end = en.get_offset();
186 this.view.el.grab_focus();
187 this.buffer.el.place_cursor(st);
188 this.view.el.scroll_to_iter(st, 0.1f, true, 0.0f, 0.5f);
192 public class Xcls_Box2 : Object
195 private Editor _this;
201 public Xcls_Box2(Editor _owner )
204 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
208 // set gobject values
209 this.el.homogeneous = false;
210 var child_0 = new Xcls_save_button( _this );
212 this.el.pack_start ( child_0.el , false,false );
213 var child_1 = new Xcls_key_edit( _this );
215 this.el.pack_end ( child_1.el , true,true );
218 // user defined functions
220 public class Xcls_save_button : Object
222 public Gtk.Button el;
223 private Editor _this;
229 public Xcls_save_button(Editor _owner )
232 _this.save_button = this;
233 this.el = new Gtk.Button();
237 // set gobject values
238 this.el.label = "Save";
241 this.el.clicked.connect( () => {
242 _this.saveContents();
246 // user defined functions
249 public class Xcls_key_edit : Object
252 private Editor _this;
258 public Xcls_key_edit(Editor _owner )
261 _this.key_edit = this;
262 this.el = new Gtk.Entry();
266 // set gobject values
269 // user defined functions
273 public class Xcls_RightEditor : Object
275 public Gtk.ScrolledWindow el;
276 private Editor _this;
282 public Xcls_RightEditor(Editor _owner )
285 _this.RightEditor = this;
286 this.el = new Gtk.ScrolledWindow( null, null );
290 // set gobject values
291 var child_0 = new Xcls_view( _this );
293 this.el.add ( child_0.el );
297 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
300 // user defined functions
302 public class Xcls_view : Object
304 public Gtk.SourceView el;
305 private Editor _this;
311 public Xcls_view(Editor _owner )
315 this.el = new Gtk.SourceView();
319 // set gobject values
320 this.el.auto_indent = true;
321 this.el.indent_width = 4;
322 this.el.show_line_marks = true;
323 this.el.insert_spaces_instead_of_tabs = true;
324 this.el.show_line_numbers = true;
325 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
326 this.el.tab_width = 4;
327 this.el.highlight_current_line = true;
328 var child_0 = new Xcls_buffer( _this );
330 this.el.set_buffer ( child_0.el );
334 var description = Pango.FontDescription.from_string("monospace");
335 description.set_size(8000);
336 this.el.override_font(description);
339 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
340 this.el.completion.unblock_interactive();
341 this.el.completion.select_on_show = true; // select
342 this.el.completion.show_headers = false;
343 this.el.completion.remember_info_visibility = true;
346 var attrs = new Gtk.SourceMarkAttributes();
347 var pink = Gdk.RGBA();
348 pink.parse ( "pink");
349 attrs.set_background ( pink);
350 attrs.set_icon_name ( "process-stop");
351 attrs.query_tooltip_text.connect(( mark) => {
352 //print("tooltip query? %s\n", mark.name);
356 this.el.set_mark_attributes ("ERR", attrs, 1);
358 var wattrs = new Gtk.SourceMarkAttributes();
359 var blue = Gdk.RGBA();
360 blue.parse ( "#ABF4EB");
361 wattrs.set_background ( blue);
362 wattrs.set_icon_name ( "process-stop");
363 wattrs.query_tooltip_text.connect(( mark) => {
364 //print("tooltip query? %s\n", mark.name);
368 this.el.set_mark_attributes ("WARN", wattrs, 1);
372 var dattrs = new Gtk.SourceMarkAttributes();
373 var purple = Gdk.RGBA();
374 purple.parse ( "#EEA9FF");
375 dattrs.set_background ( purple);
376 dattrs.set_icon_name ( "process-stop");
377 dattrs.query_tooltip_text.connect(( mark) => {
378 //print("tooltip query? %s\n", mark.name);
382 this.el.set_mark_attributes ("DEPR", dattrs, 1);
385 this.el.key_release_event.connect( (event) => {
387 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
388 print("SAVE: ctrl-S pressed");
389 _this.saveContents();
392 // print(event.key.keyval)
399 // user defined functions
400 public void load (string str) {
402 // show the help page for the active node..
403 //this.get('/Help').show();
406 // this.get('/BottomPane').el.set_current_page(0);
407 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
408 buf.set_text(str, str.length);
409 buf.set_undo_manager(null);
411 var lm = Gtk.SourceLanguageManager.get_default();
413 if (_this.file != null) {
414 lang = _this.file.language;
416 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
417 var lg = _this.file.content_type.length > 0 ?
418 lm.guess_language(_this.file.path, _this.file.content_type) :
419 lm.get_language(lang);
422 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
424 this.el.insert_spaces_instead_of_tabs = true;
426 print("sourcelanguage = %s\n", lg.name);
427 if (lg.name == "Vala") {
428 this.el.insert_spaces_instead_of_tabs = false;
432 this.el.grab_focus();
433 _this.save_button.el.sensitive = false;
436 public class Xcls_buffer : Object
438 public Gtk.SourceBuffer el;
439 private Editor _this;
443 public bool check_queued;
444 public int error_line;
445 public bool check_running;
448 public Xcls_buffer(Editor _owner )
452 this.el = new Gtk.SourceBuffer( null );
455 this.check_queued = false;
456 this.error_line = -1;
457 this.check_running = false;
459 // set gobject values
462 this.el.changed.connect( () => {
465 _this.save_button.el.sensitive = true;
466 print("EDITOR CHANGED");
471 // this.get('/LeftPanel.model').changed( str , false);
476 // user defined functions
477 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
479 this.error_line = validate_res.size;
481 if (this.error_line < 1) {
484 var tlines = this.el.get_line_count ();
486 var valiter = validate_res.map_iterator();
487 while (valiter.next()) {
489 // print("get inter\n");
490 var eline = valiter.get_key();
491 if (eline > tlines) {
494 this.el.get_iter_at_line( out iter, eline);
495 //print("mark line\n");
496 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
500 public string toString () {
504 this.el.get_start_iter(out s);
505 this.el.get_end_iter(out e);
506 var ret = this.el.get_text(s,e,true);
507 //print("TO STRING? " + ret);
510 public bool checkSyntax () {
512 if (this.check_running) {
513 print("Check is running\n");
514 if (this.check_queued) {
515 print("Check is already queued");
518 this.check_queued = true;
519 print("Adding queued Check ");
520 GLib.Timeout.add_seconds(1, () => {
521 this.check_queued = false;
530 var str = this.toString();
533 if (this.error_line > 0) {
536 this.el.get_bounds (out start, out end);
538 this.el.remove_source_marks (start, end, null);
540 if (str.length < 1) {
541 print("checkSyntax - empty string?\n");
545 if (_this.file.xtype == "PlainFile") {
547 // assume it's gtk...
548 this.check_running = true;
550 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
554 this.check_running = false;
560 if (_this.file == null) {
563 var p = Palete.factory(_this.file.xtype);
567 this.check_running = true;
570 if (_this.file.language == "js") {
571 this.check_running = false;
572 print("calling validate javascript\n");
573 return this.highlightErrors(p.validateJavascript(
584 print("calling validate vala\n");
585 // clear the buttons.
588 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
595 this.check_running = false;
600 //print("done mark line\n");
602 return true; // at present allow saving - even if it's invalid..
604 public bool highlightErrorsJson (string type, Json.Object obj) {
607 this.el.get_bounds (out start, out end);
609 this.el.remove_source_marks (start, end, type);
612 // we should highlight other types of errors..
614 if (!obj.has_member(type)) {
615 print("Return has no errors\n");
619 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
620 _this.window.windowstate.state != WindowState.State.CODE
626 var err = obj.get_object_member(type);
629 if (_this.file == null) {
633 var valafn = _this.file.path;
635 if (_this.file.xtype != "PlainFile") {
642 var regex = new Regex("\\.bjs$");
646 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
647 } catch (GLib.RegexError e) {
654 if (!err.has_member(valafn)) {
655 print("File path has no errors\n");
659 var lines = err.get_object_member(valafn);
662 if (obj.has_member("line_offset")) {
663 offset = (int)obj.get_int_member("line_offset") + 1;
669 var tlines = this.el.get_line_count () +1;
671 lines.foreach_member((obj, line, node) => {
674 // print("get inter\n");
675 var eline = int.parse(line) - offset;
676 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
679 if (eline > tlines || eline < 0) {
682 this.el.get_iter_at_line( out iter, eline);
683 //print("mark line\n");
684 var msg = "Line: %d".printf(eline+1);
685 var ar = lines.get_array_member(line);
686 for (var i = 0 ; i < ar.get_length(); i++) {
687 msg += (msg.length > 0) ? "\n" : "";
688 msg += ar.get_string_element(i);
692 this.el.create_source_mark(msg, type, iter);