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 JsRender.JsRender file;
31 public signal void save ();
32 public JsRender.Node node;
38 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
42 this.activeEditor = "";
51 this.el.homogeneous = false;
52 this.el.hexpand = true;
53 var child_0 = new Xcls_Box2( _this );
55 this.el.pack_start ( child_0.el , false,true );
56 var child_1 = new Xcls_RightEditor( _this );
58 this.el.pack_end ( child_1.el , true,true );
61 // user defined functions
62 public Gee.ArrayList<int> search (string txt) {
63 var ret = new Gee.ArrayList<int>();
65 var str = this.buffer.toString();
67 var lines = str.split("\n");
68 for (var i = 0 ; i < lines.length; i++) {
69 if (lines[i].contains(txt)) {
76 public bool saveContents () {
79 if (_this.file == null) {
87 var str = _this.buffer.toString();
89 _this.buffer.checkSyntax();
93 // LeftPanel.model.changed( str , false);
95 _this.save_button.el.sensitive = false;
97 // find the text for the node..
98 if (_this.file.xtype != "PlainFile") {
99 if (ptype == "listener") {
100 this.node.listeners.set(key,str);
103 this.node.props.set(key,str);
106 _this.file.setSource( str );
115 public void scroll_to_line (int line) {
117 GLib.Timeout.add(500, () => {
119 var buf = this.view.el.get_buffer();
121 var sbuf = (Gtk.SourceBuffer) buf;
125 sbuf.get_iter_at_line(out iter, line);
126 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
130 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
138 if (file.xtype != "PlainFile") {
144 // find the text for the node..
145 if (ptype == "listener") {
146 val = node.listeners.get(key);
149 val = node.props.get(key);
152 this.key_edit.el.show();
153 this.key_edit.el.text = key;
156 this.view.load( file.toSource() );
157 this.key_edit.el.hide();
162 public class Xcls_Box2 : Object
165 private Editor _this;
171 public Xcls_Box2(Editor _owner )
174 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
178 // set gobject values
179 this.el.homogeneous = false;
180 var child_0 = new Xcls_save_button( _this );
182 this.el.pack_start ( child_0.el , false,false );
183 var child_1 = new Xcls_key_edit( _this );
185 this.el.pack_end ( child_1.el , true,true );
188 // user defined functions
190 public class Xcls_save_button : Object
192 public Gtk.Button el;
193 private Editor _this;
199 public Xcls_save_button(Editor _owner )
202 _this.save_button = this;
203 this.el = new Gtk.Button();
207 // set gobject values
208 this.el.label = "Save";
211 this.el.clicked.connect( () => {
212 _this.saveContents();
216 // user defined functions
219 public class Xcls_key_edit : Object
222 private Editor _this;
228 public Xcls_key_edit(Editor _owner )
231 _this.key_edit = this;
232 this.el = new Gtk.Entry();
236 // set gobject values
239 // user defined functions
243 public class Xcls_RightEditor : Object
245 public Gtk.ScrolledWindow el;
246 private Editor _this;
252 public Xcls_RightEditor(Editor _owner )
255 _this.RightEditor = this;
256 this.el = new Gtk.ScrolledWindow( null, null );
260 // set gobject values
261 var child_0 = new Xcls_view( _this );
263 this.el.add ( child_0.el );
267 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
270 // user defined functions
272 public class Xcls_view : Object
274 public Gtk.SourceView el;
275 private Editor _this;
281 public Xcls_view(Editor _owner )
285 this.el = new Gtk.SourceView();
289 // set gobject values
290 this.el.auto_indent = true;
291 this.el.indent_width = 4;
292 this.el.show_line_marks = true;
293 this.el.insert_spaces_instead_of_tabs = true;
294 this.el.show_line_numbers = true;
295 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
296 this.el.tab_width = 4;
297 this.el.highlight_current_line = true;
298 var child_0 = new Xcls_buffer( _this );
300 this.el.set_buffer ( child_0.el );
304 var description = Pango.FontDescription.from_string("monospace");
305 description.set_size(8000);
306 this.el.override_font(description);
309 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
310 this.el.completion.unblock_interactive();
311 this.el.completion.select_on_show = true; // select
312 this.el.completion.show_headers = false;
313 this.el.completion.remember_info_visibility = true;
316 var attrs = new Gtk.SourceMarkAttributes();
317 var pink = Gdk.RGBA();
318 pink.parse ( "pink");
319 attrs.set_background ( pink);
320 attrs.set_icon_name ( "process-stop");
321 attrs.query_tooltip_text.connect(( mark) => {
322 //print("tooltip query? %s\n", mark.name);
326 this.el.set_mark_attributes ("ERR", attrs, 1);
328 var wattrs = new Gtk.SourceMarkAttributes();
329 var blue = Gdk.RGBA();
330 blue.parse ( "#ABF4EB");
331 wattrs.set_background ( blue);
332 wattrs.set_icon_name ( "process-stop");
333 wattrs.query_tooltip_text.connect(( mark) => {
334 //print("tooltip query? %s\n", mark.name);
338 this.el.set_mark_attributes ("WARN", wattrs, 1);
342 var dattrs = new Gtk.SourceMarkAttributes();
343 var purple = Gdk.RGBA();
344 purple.parse ( "#EEA9FF");
345 dattrs.set_background ( purple);
346 dattrs.set_icon_name ( "process-stop");
347 dattrs.query_tooltip_text.connect(( mark) => {
348 //print("tooltip query? %s\n", mark.name);
352 this.el.set_mark_attributes ("DEPR", dattrs, 1);
355 this.el.key_release_event.connect( (event) => {
357 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
358 print("SAVE: ctrl-S pressed");
359 _this.saveContents();
362 // print(event.key.keyval)
369 // user defined functions
370 public void load (string str) {
372 // show the help page for the active node..
373 //this.get('/Help').show();
376 // this.get('/BottomPane').el.set_current_page(0);
377 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
378 buf.set_text(str, str.length);
379 buf.set_undo_manager(null);
381 var lm = Gtk.SourceLanguageManager.get_default();
383 if (_this.file != null) {
384 lang = _this.file.language;
386 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
387 var lg = _this.file.content_type.length > 0 ?
388 lm.guess_language(_this.file.path, _this.file.content_type) :
389 lm.get_language(lang);
392 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
394 this.el.insert_spaces_instead_of_tabs = true;
396 print("sourcelanguage = %s\n", lg.name);
397 if (lg.name == "Vala") {
398 this.el.insert_spaces_instead_of_tabs = false;
402 this.el.grab_focus();
403 _this.save_button.el.sensitive = false;
406 public class Xcls_buffer : Object
408 public Gtk.SourceBuffer el;
409 private Editor _this;
413 public bool check_queued;
414 public int error_line;
415 public bool check_running;
418 public Xcls_buffer(Editor _owner )
422 this.el = new Gtk.SourceBuffer( null );
425 this.check_queued = false;
426 this.error_line = -1;
427 this.check_running = false;
429 // set gobject values
432 this.el.changed.connect( () => {
435 _this.save_button.el.sensitive = true;
436 print("EDITOR CHANGED");
441 // this.get('/LeftPanel.model').changed( str , false);
446 // user defined functions
447 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
449 this.error_line = validate_res.size;
451 if (this.error_line < 1) {
454 var tlines = this.el.get_line_count ();
456 var valiter = validate_res.map_iterator();
457 while (valiter.next()) {
459 // print("get inter\n");
460 var eline = valiter.get_key();
461 if (eline > tlines) {
464 this.el.get_iter_at_line( out iter, eline);
465 //print("mark line\n");
466 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
470 public string toString () {
474 this.el.get_start_iter(out s);
475 this.el.get_end_iter(out e);
476 var ret = this.el.get_text(s,e,true);
477 //print("TO STRING? " + ret);
480 public bool checkSyntax () {
482 if (this.check_running) {
483 print("Check is running\n");
484 if (this.check_queued) {
485 print("Check is already queued");
488 this.check_queued = true;
489 print("Adding queued Check ");
490 GLib.Timeout.add_seconds(1, () => {
491 this.check_queued = false;
500 var str = this.toString();
503 if (this.error_line > 0) {
506 this.el.get_bounds (out start, out end);
508 this.el.remove_source_marks (start, end, null);
510 if (str.length < 1) {
511 print("checkSyntax - empty string?\n");
515 if (_this.file.xtype == "PlainFile") {
517 // assume it's gtk...
518 this.check_running = true;
520 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
524 this.check_running = false;
530 if (_this.file == null) {
533 var p = Palete.factory(_this.file.xtype);
537 this.check_running = true;
540 if (_this.file.language == "js") {
541 this.check_running = false;
542 print("calling validate javascript\n");
543 return this.highlightErrors(p.validateJavascript(
554 print("calling validate vala\n");
555 // clear the buttons.
558 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
565 this.check_running = false;
570 //print("done mark line\n");
572 return true; // at present allow saving - even if it's invalid..
574 public bool highlightErrorsJson (string type, Json.Object obj) {
577 this.el.get_bounds (out start, out end);
579 this.el.remove_source_marks (start, end, type);
582 // we should highlight other types of errors..
584 if (!obj.has_member(type)) {
585 print("Return has no errors\n");
589 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
590 _this.window.windowstate.state != WindowState.State.CODE
596 var err = obj.get_object_member(type);
599 if (_this.file == null) {
603 var valafn = _this.file.path;
605 if (_this.file.xtype != "PlainFile") {
612 var regex = new Regex("\\.bjs$");
616 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
617 } catch (GLib.RegexError e) {
624 if (!err.has_member(valafn)) {
625 print("File path has no errors\n");
629 var lines = err.get_object_member(valafn);
632 if (obj.has_member("line_offset")) {
633 offset = (int)obj.get_int_member("line_offset") + 1;
639 var tlines = this.el.get_line_count () +1;
641 lines.foreach_member((obj, line, node) => {
644 // print("get inter\n");
645 var eline = int.parse(line) - offset;
646 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
649 if (eline > tlines || eline < 0) {
652 this.el.get_iter_at_line( out iter, eline);
653 //print("mark line\n");
654 var msg = "Line: %d".printf(eline+1);
655 var ar = lines.get_array_member(line);
656 for (var i = 0 ; i < ar.get_length(); i++) {
657 msg += (msg.length > 0) ? "\n" : "";
658 msg += ar.get_string_element(i);
662 this.el.create_source_mark(msg, type, iter);