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 = en.get_offset();
183 this.view.el.grab_focus();
184 this.buffer.el.place_cursor(st);
189 public class Xcls_Box2 : Object
192 private Editor _this;
198 public Xcls_Box2(Editor _owner )
201 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
205 // set gobject values
206 this.el.homogeneous = false;
207 var child_0 = new Xcls_save_button( _this );
209 this.el.pack_start ( child_0.el , false,false );
210 var child_1 = new Xcls_key_edit( _this );
212 this.el.pack_end ( child_1.el , true,true );
215 // user defined functions
217 public class Xcls_save_button : Object
219 public Gtk.Button el;
220 private Editor _this;
226 public Xcls_save_button(Editor _owner )
229 _this.save_button = this;
230 this.el = new Gtk.Button();
234 // set gobject values
235 this.el.label = "Save";
238 this.el.clicked.connect( () => {
239 _this.saveContents();
243 // user defined functions
246 public class Xcls_key_edit : Object
249 private Editor _this;
255 public Xcls_key_edit(Editor _owner )
258 _this.key_edit = this;
259 this.el = new Gtk.Entry();
263 // set gobject values
266 // user defined functions
270 public class Xcls_RightEditor : Object
272 public Gtk.ScrolledWindow el;
273 private Editor _this;
279 public Xcls_RightEditor(Editor _owner )
282 _this.RightEditor = this;
283 this.el = new Gtk.ScrolledWindow( null, null );
287 // set gobject values
288 var child_0 = new Xcls_view( _this );
290 this.el.add ( child_0.el );
294 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
297 // user defined functions
299 public class Xcls_view : Object
301 public Gtk.SourceView el;
302 private Editor _this;
308 public Xcls_view(Editor _owner )
312 this.el = new Gtk.SourceView();
316 // set gobject values
317 this.el.auto_indent = true;
318 this.el.indent_width = 4;
319 this.el.show_line_marks = true;
320 this.el.insert_spaces_instead_of_tabs = true;
321 this.el.show_line_numbers = true;
322 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
323 this.el.tab_width = 4;
324 this.el.highlight_current_line = true;
325 var child_0 = new Xcls_buffer( _this );
327 this.el.set_buffer ( child_0.el );
331 var description = Pango.FontDescription.from_string("monospace");
332 description.set_size(8000);
333 this.el.override_font(description);
336 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
337 this.el.completion.unblock_interactive();
338 this.el.completion.select_on_show = true; // select
339 this.el.completion.show_headers = false;
340 this.el.completion.remember_info_visibility = true;
343 var attrs = new Gtk.SourceMarkAttributes();
344 var pink = Gdk.RGBA();
345 pink.parse ( "pink");
346 attrs.set_background ( pink);
347 attrs.set_icon_name ( "process-stop");
348 attrs.query_tooltip_text.connect(( mark) => {
349 //print("tooltip query? %s\n", mark.name);
353 this.el.set_mark_attributes ("ERR", attrs, 1);
355 var wattrs = new Gtk.SourceMarkAttributes();
356 var blue = Gdk.RGBA();
357 blue.parse ( "#ABF4EB");
358 wattrs.set_background ( blue);
359 wattrs.set_icon_name ( "process-stop");
360 wattrs.query_tooltip_text.connect(( mark) => {
361 //print("tooltip query? %s\n", mark.name);
365 this.el.set_mark_attributes ("WARN", wattrs, 1);
369 var dattrs = new Gtk.SourceMarkAttributes();
370 var purple = Gdk.RGBA();
371 purple.parse ( "#EEA9FF");
372 dattrs.set_background ( purple);
373 dattrs.set_icon_name ( "process-stop");
374 dattrs.query_tooltip_text.connect(( mark) => {
375 //print("tooltip query? %s\n", mark.name);
379 this.el.set_mark_attributes ("DEPR", dattrs, 1);
382 this.el.key_release_event.connect( (event) => {
384 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
385 print("SAVE: ctrl-S pressed");
386 _this.saveContents();
389 // print(event.key.keyval)
396 // user defined functions
397 public void load (string str) {
399 // show the help page for the active node..
400 //this.get('/Help').show();
403 // this.get('/BottomPane').el.set_current_page(0);
404 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
405 buf.set_text(str, str.length);
406 buf.set_undo_manager(null);
408 var lm = Gtk.SourceLanguageManager.get_default();
410 if (_this.file != null) {
411 lang = _this.file.language;
413 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
414 var lg = _this.file.content_type.length > 0 ?
415 lm.guess_language(_this.file.path, _this.file.content_type) :
416 lm.get_language(lang);
419 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
421 this.el.insert_spaces_instead_of_tabs = true;
423 print("sourcelanguage = %s\n", lg.name);
424 if (lg.name == "Vala") {
425 this.el.insert_spaces_instead_of_tabs = false;
429 this.el.grab_focus();
430 _this.save_button.el.sensitive = false;
433 public class Xcls_buffer : Object
435 public Gtk.SourceBuffer el;
436 private Editor _this;
440 public bool check_queued;
441 public int error_line;
442 public bool check_running;
445 public Xcls_buffer(Editor _owner )
449 this.el = new Gtk.SourceBuffer( null );
452 this.check_queued = false;
453 this.error_line = -1;
454 this.check_running = false;
456 // set gobject values
459 this.el.changed.connect( () => {
462 _this.save_button.el.sensitive = true;
463 print("EDITOR CHANGED");
468 // this.get('/LeftPanel.model').changed( str , false);
473 // user defined functions
474 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
476 this.error_line = validate_res.size;
478 if (this.error_line < 1) {
481 var tlines = this.el.get_line_count ();
483 var valiter = validate_res.map_iterator();
484 while (valiter.next()) {
486 // print("get inter\n");
487 var eline = valiter.get_key();
488 if (eline > tlines) {
491 this.el.get_iter_at_line( out iter, eline);
492 //print("mark line\n");
493 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
497 public string toString () {
501 this.el.get_start_iter(out s);
502 this.el.get_end_iter(out e);
503 var ret = this.el.get_text(s,e,true);
504 //print("TO STRING? " + ret);
507 public bool checkSyntax () {
509 if (this.check_running) {
510 print("Check is running\n");
511 if (this.check_queued) {
512 print("Check is already queued");
515 this.check_queued = true;
516 print("Adding queued Check ");
517 GLib.Timeout.add_seconds(1, () => {
518 this.check_queued = false;
527 var str = this.toString();
530 if (this.error_line > 0) {
533 this.el.get_bounds (out start, out end);
535 this.el.remove_source_marks (start, end, null);
537 if (str.length < 1) {
538 print("checkSyntax - empty string?\n");
542 if (_this.file.xtype == "PlainFile") {
544 // assume it's gtk...
545 this.check_running = true;
547 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
551 this.check_running = false;
557 if (_this.file == null) {
560 var p = Palete.factory(_this.file.xtype);
564 this.check_running = true;
567 if (_this.file.language == "js") {
568 this.check_running = false;
569 print("calling validate javascript\n");
570 return this.highlightErrors(p.validateJavascript(
581 print("calling validate vala\n");
582 // clear the buttons.
585 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
592 this.check_running = false;
597 //print("done mark line\n");
599 return true; // at present allow saving - even if it's invalid..
601 public bool highlightErrorsJson (string type, Json.Object obj) {
604 this.el.get_bounds (out start, out end);
606 this.el.remove_source_marks (start, end, type);
609 // we should highlight other types of errors..
611 if (!obj.has_member(type)) {
612 print("Return has no errors\n");
616 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
617 _this.window.windowstate.state != WindowState.State.CODE
623 var err = obj.get_object_member(type);
626 if (_this.file == null) {
630 var valafn = _this.file.path;
632 if (_this.file.xtype != "PlainFile") {
639 var regex = new Regex("\\.bjs$");
643 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
644 } catch (GLib.RegexError e) {
651 if (!err.has_member(valafn)) {
652 print("File path has no errors\n");
656 var lines = err.get_object_member(valafn);
659 if (obj.has_member("line_offset")) {
660 offset = (int)obj.get_int_member("line_offset") + 1;
666 var tlines = this.el.get_line_count () +1;
668 lines.foreach_member((obj, line, node) => {
671 // print("get inter\n");
672 var eline = int.parse(line) - offset;
673 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
676 if (eline > tlines || eline < 0) {
679 this.el.get_iter_at_line( out iter, eline);
680 //print("mark line\n");
681 var msg = "Line: %d".printf(eline+1);
682 var ar = lines.get_array_member(line);
683 for (var i = 0 ; i < ar.get_length(); i++) {
684 msg += (msg.length > 0) ? "\n" : "";
685 msg += ar.get_string_element(i);
689 this.el.create_source_mark(msg, type, iter);