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>();
67 public bool saveContents () {
70 if (_this.file == null) {
78 var str = _this.buffer.toString();
80 _this.buffer.checkSyntax();
84 // LeftPanel.model.changed( str , false);
86 _this.save_button.el.sensitive = false;
88 // find the text for the node..
89 if (_this.file.xtype != "PlainFile") {
90 if (ptype == "listener") {
91 this.node.listeners.set(key,str);
94 this.node.props.set(key,str);
97 _this.file.setSource( str );
106 public void scroll_to_line (int line) {
108 GLib.Timeout.add(500, () => {
110 var buf = this.view.el.get_buffer();
112 var sbuf = (Gtk.SourceBuffer) buf;
116 sbuf.get_iter_at_line(out iter, line);
117 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
121 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
129 if (file.xtype != "PlainFile") {
135 // find the text for the node..
136 if (ptype == "listener") {
137 val = node.listeners.get(key);
140 val = node.props.get(key);
143 this.key_edit.el.show();
144 this.key_edit.el.text = key;
147 this.view.load( file.toSource() );
148 this.key_edit.el.hide();
153 public class Xcls_Box2 : Object
156 private Editor _this;
162 public Xcls_Box2(Editor _owner )
165 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
169 // set gobject values
170 this.el.homogeneous = false;
171 var child_0 = new Xcls_save_button( _this );
173 this.el.pack_start ( child_0.el , false,false );
174 var child_1 = new Xcls_key_edit( _this );
176 this.el.pack_end ( child_1.el , true,true );
179 // user defined functions
181 public class Xcls_save_button : Object
183 public Gtk.Button el;
184 private Editor _this;
190 public Xcls_save_button(Editor _owner )
193 _this.save_button = this;
194 this.el = new Gtk.Button();
198 // set gobject values
199 this.el.label = "Save";
202 this.el.clicked.connect( () => {
203 _this.saveContents();
207 // user defined functions
210 public class Xcls_key_edit : Object
213 private Editor _this;
219 public Xcls_key_edit(Editor _owner )
222 _this.key_edit = this;
223 this.el = new Gtk.Entry();
227 // set gobject values
230 // user defined functions
234 public class Xcls_RightEditor : Object
236 public Gtk.ScrolledWindow el;
237 private Editor _this;
243 public Xcls_RightEditor(Editor _owner )
246 _this.RightEditor = this;
247 this.el = new Gtk.ScrolledWindow( null, null );
251 // set gobject values
252 var child_0 = new Xcls_view( _this );
254 this.el.add ( child_0.el );
258 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
261 // user defined functions
263 public class Xcls_view : Object
265 public Gtk.SourceView el;
266 private Editor _this;
272 public Xcls_view(Editor _owner )
276 this.el = new Gtk.SourceView();
280 // set gobject values
281 this.el.auto_indent = true;
282 this.el.indent_width = 4;
283 this.el.show_line_marks = true;
284 this.el.insert_spaces_instead_of_tabs = true;
285 this.el.show_line_numbers = true;
286 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
287 this.el.tab_width = 4;
288 this.el.highlight_current_line = true;
289 var child_0 = new Xcls_buffer( _this );
291 this.el.set_buffer ( child_0.el );
295 var description = Pango.FontDescription.from_string("monospace");
296 description.set_size(8000);
297 this.el.override_font(description);
300 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
301 this.el.completion.unblock_interactive();
302 this.el.completion.select_on_show = true; // select
303 this.el.completion.show_headers = false;
304 this.el.completion.remember_info_visibility = true;
307 var attrs = new Gtk.SourceMarkAttributes();
308 var pink = Gdk.RGBA();
309 pink.parse ( "pink");
310 attrs.set_background ( pink);
311 attrs.set_icon_name ( "process-stop");
312 attrs.query_tooltip_text.connect(( mark) => {
313 //print("tooltip query? %s\n", mark.name);
317 this.el.set_mark_attributes ("ERR", attrs, 1);
319 var wattrs = new Gtk.SourceMarkAttributes();
320 var blue = Gdk.RGBA();
321 blue.parse ( "#ABF4EB");
322 wattrs.set_background ( blue);
323 wattrs.set_icon_name ( "process-stop");
324 wattrs.query_tooltip_text.connect(( mark) => {
325 //print("tooltip query? %s\n", mark.name);
329 this.el.set_mark_attributes ("WARN", wattrs, 1);
333 var dattrs = new Gtk.SourceMarkAttributes();
334 var purple = Gdk.RGBA();
335 purple.parse ( "#EEA9FF");
336 dattrs.set_background ( purple);
337 dattrs.set_icon_name ( "process-stop");
338 dattrs.query_tooltip_text.connect(( mark) => {
339 //print("tooltip query? %s\n", mark.name);
343 this.el.set_mark_attributes ("DEPR", dattrs, 1);
346 this.el.key_release_event.connect( (event) => {
348 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
349 print("SAVE: ctrl-S pressed");
350 _this.saveContents();
353 // print(event.key.keyval)
360 // user defined functions
361 public void load (string str) {
363 // show the help page for the active node..
364 //this.get('/Help').show();
367 // this.get('/BottomPane').el.set_current_page(0);
368 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
369 buf.set_text(str, str.length);
370 buf.set_undo_manager(null);
372 var lm = Gtk.SourceLanguageManager.get_default();
374 if (_this.file != null) {
375 lang = _this.file.language;
377 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
378 var lg = _this.file.content_type.length > 0 ?
379 lm.guess_language(_this.file.path, _this.file.content_type) :
380 lm.get_language(lang);
383 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
385 this.el.insert_spaces_instead_of_tabs = true;
387 print("sourcelanguage = %s\n", lg.name);
388 if (lg.name == "Vala") {
389 this.el.insert_spaces_instead_of_tabs = false;
393 this.el.grab_focus();
394 _this.save_button.el.sensitive = false;
397 public class Xcls_buffer : Object
399 public Gtk.SourceBuffer el;
400 private Editor _this;
404 public bool check_queued;
405 public int error_line;
406 public bool check_running;
409 public Xcls_buffer(Editor _owner )
413 this.el = new Gtk.SourceBuffer( null );
416 this.check_queued = false;
417 this.error_line = -1;
418 this.check_running = false;
420 // set gobject values
423 this.el.changed.connect( () => {
426 _this.save_button.el.sensitive = true;
427 print("EDITOR CHANGED");
432 // this.get('/LeftPanel.model').changed( str , false);
437 // user defined functions
438 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
440 this.error_line = validate_res.size;
442 if (this.error_line < 1) {
445 var tlines = this.el.get_line_count ();
447 var valiter = validate_res.map_iterator();
448 while (valiter.next()) {
450 // print("get inter\n");
451 var eline = valiter.get_key();
452 if (eline > tlines) {
455 this.el.get_iter_at_line( out iter, eline);
456 //print("mark line\n");
457 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
461 public string toString () {
465 this.el.get_start_iter(out s);
466 this.el.get_end_iter(out e);
467 var ret = this.el.get_text(s,e,true);
468 //print("TO STRING? " + ret);
471 public bool checkSyntax () {
473 if (this.check_running) {
474 print("Check is running\n");
475 if (this.check_queued) {
476 print("Check is already queued");
479 this.check_queued = true;
480 print("Adding queued Check ");
481 GLib.Timeout.add_seconds(1, () => {
482 this.check_queued = false;
491 var str = this.toString();
494 if (this.error_line > 0) {
497 this.el.get_bounds (out start, out end);
499 this.el.remove_source_marks (start, end, null);
501 if (str.length < 1) {
502 print("checkSyntax - empty string?\n");
506 if (_this.file.xtype == "PlainFile") {
508 // assume it's gtk...
509 this.check_running = true;
511 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
515 this.check_running = false;
521 if (_this.file == null) {
524 var p = Palete.factory(_this.file.xtype);
528 this.check_running = true;
531 if (_this.file.language == "js") {
532 this.check_running = false;
533 print("calling validate javascript\n");
534 return this.highlightErrors(p.validateJavascript(
545 print("calling validate vala\n");
546 // clear the buttons.
549 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
556 this.check_running = false;
561 //print("done mark line\n");
563 return true; // at present allow saving - even if it's invalid..
565 public bool highlightErrorsJson (string type, Json.Object obj) {
568 this.el.get_bounds (out start, out end);
570 this.el.remove_source_marks (start, end, type);
573 // we should highlight other types of errors..
575 if (!obj.has_member(type)) {
576 print("Return has no errors\n");
580 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
581 _this.window.windowstate.state != WindowState.State.CODE
587 var err = obj.get_object_member(type);
590 if (_this.file == null) {
594 var valafn = _this.file.path;
596 if (_this.file.xtype != "PlainFile") {
603 var regex = new Regex("\\.bjs$");
607 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
608 } catch (GLib.RegexError e) {
615 if (!err.has_member(valafn)) {
616 print("File path has no errors\n");
620 var lines = err.get_object_member(valafn);
623 if (obj.has_member("line_offset")) {
624 offset = (int)obj.get_int_member("line_offset") + 1;
630 var tlines = this.el.get_line_count () +1;
632 lines.foreach_member((obj, line, node) => {
635 // print("get inter\n");
636 var eline = int.parse(line) - offset;
637 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
640 if (eline > tlines || eline < 0) {
643 this.el.get_iter_at_line( out iter, eline);
644 //print("mark line\n");
645 var msg = "Line: %d".printf(eline+1);
646 var ar = lines.get_array_member(line);
647 for (var i = 0 ; i < ar.get_length(); i++) {
648 msg += (msg.length > 0) ? "\n" : "";
649 msg += ar.get_string_element(i);
653 this.el.create_source_mark(msg, type, iter);