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 () {
65 public bool saveContents () {
68 if (_this.file == null) {
76 var str = _this.buffer.toString();
78 _this.buffer.checkSyntax();
82 // LeftPanel.model.changed( str , false);
84 _this.save_button.el.sensitive = false;
86 // find the text for the node..
87 if (_this.file.xtype != "PlainFile") {
88 if (ptype == "listener") {
89 this.node.listeners.set(key,str);
92 this.node.props.set(key,str);
95 _this.file.setSource( str );
104 public void scroll_to_line (int line) {
106 GLib.Timeout.add(500, () => {
108 var buf = this.view.el.get_buffer();
110 var sbuf = (Gtk.SourceBuffer) buf;
114 sbuf.get_iter_at_line(out iter, line);
115 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
119 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
127 if (file.xtype != "PlainFile") {
133 // find the text for the node..
134 if (ptype == "listener") {
135 val = node.listeners.get(key);
138 val = node.props.get(key);
141 this.key_edit.el.show();
142 this.key_edit.el.text = key;
145 this.view.load( file.toSource() );
146 this.key_edit.el.hide();
151 public class Xcls_Box2 : Object
154 private Editor _this;
160 public Xcls_Box2(Editor _owner )
163 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
167 // set gobject values
168 this.el.homogeneous = false;
169 var child_0 = new Xcls_save_button( _this );
171 this.el.pack_start ( child_0.el , false,false );
172 var child_1 = new Xcls_key_edit( _this );
174 this.el.pack_end ( child_1.el , true,true );
177 // user defined functions
179 public class Xcls_save_button : Object
181 public Gtk.Button el;
182 private Editor _this;
188 public Xcls_save_button(Editor _owner )
191 _this.save_button = this;
192 this.el = new Gtk.Button();
196 // set gobject values
197 this.el.label = "Save";
200 this.el.clicked.connect( () => {
201 _this.saveContents();
205 // user defined functions
208 public class Xcls_key_edit : Object
211 private Editor _this;
217 public Xcls_key_edit(Editor _owner )
220 _this.key_edit = this;
221 this.el = new Gtk.Entry();
225 // set gobject values
228 // user defined functions
232 public class Xcls_RightEditor : Object
234 public Gtk.ScrolledWindow el;
235 private Editor _this;
241 public Xcls_RightEditor(Editor _owner )
244 _this.RightEditor = this;
245 this.el = new Gtk.ScrolledWindow( null, null );
249 // set gobject values
250 var child_0 = new Xcls_view( _this );
252 this.el.add ( child_0.el );
256 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
259 // user defined functions
261 public class Xcls_view : Object
263 public Gtk.SourceView el;
264 private Editor _this;
270 public Xcls_view(Editor _owner )
274 this.el = new Gtk.SourceView();
278 // set gobject values
279 this.el.auto_indent = true;
280 this.el.indent_width = 4;
281 this.el.show_line_marks = true;
282 this.el.insert_spaces_instead_of_tabs = true;
283 this.el.show_line_numbers = true;
284 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
285 this.el.tab_width = 4;
286 this.el.highlight_current_line = true;
287 var child_0 = new Xcls_buffer( _this );
289 this.el.set_buffer ( child_0.el );
293 var description = Pango.FontDescription.from_string("monospace");
294 description.set_size(8000);
295 this.el.override_font(description);
298 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
299 this.el.completion.unblock_interactive();
300 this.el.completion.select_on_show = true; // select
301 this.el.completion.show_headers = false;
302 this.el.completion.remember_info_visibility = true;
305 var attrs = new Gtk.SourceMarkAttributes();
306 var pink = Gdk.RGBA();
307 pink.parse ( "pink");
308 attrs.set_background ( pink);
309 attrs.set_icon_name ( "process-stop");
310 attrs.query_tooltip_text.connect(( mark) => {
311 //print("tooltip query? %s\n", mark.name);
315 this.el.set_mark_attributes ("ERR", attrs, 1);
317 var wattrs = new Gtk.SourceMarkAttributes();
318 var blue = Gdk.RGBA();
319 blue.parse ( "#ABF4EB");
320 wattrs.set_background ( blue);
321 wattrs.set_icon_name ( "process-stop");
322 wattrs.query_tooltip_text.connect(( mark) => {
323 //print("tooltip query? %s\n", mark.name);
327 this.el.set_mark_attributes ("WARN", wattrs, 1);
331 var dattrs = new Gtk.SourceMarkAttributes();
332 var purple = Gdk.RGBA();
333 purple.parse ( "#EEA9FF");
334 dattrs.set_background ( purple);
335 dattrs.set_icon_name ( "process-stop");
336 dattrs.query_tooltip_text.connect(( mark) => {
337 //print("tooltip query? %s\n", mark.name);
341 this.el.set_mark_attributes ("DEPR", dattrs, 1);
344 this.el.key_release_event.connect( (event) => {
346 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
347 print("SAVE: ctrl-S pressed");
348 _this.saveContents();
351 // print(event.key.keyval)
358 // user defined functions
359 public void load (string str) {
361 // show the help page for the active node..
362 //this.get('/Help').show();
365 // this.get('/BottomPane').el.set_current_page(0);
366 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
367 buf.set_text(str, str.length);
368 buf.set_undo_manager(null);
370 var lm = Gtk.SourceLanguageManager.get_default();
372 if (_this.file != null) {
373 lang = _this.file.language;
375 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
376 var lg = _this.file.content_type.length > 0 ?
377 lm.guess_language(_this.file.path, _this.file.content_type) :
378 lm.get_language(lang);
381 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
383 this.el.insert_spaces_instead_of_tabs = true;
385 print("sourcelanguage = %s\n", lg.name);
386 if (lg.name == "Vala") {
387 this.el.insert_spaces_instead_of_tabs = false;
391 this.el.grab_focus();
392 _this.save_button.el.sensitive = false;
395 public class Xcls_buffer : Object
397 public Gtk.SourceBuffer el;
398 private Editor _this;
402 public bool check_queued;
403 public int error_line;
404 public bool check_running;
407 public Xcls_buffer(Editor _owner )
411 this.el = new Gtk.SourceBuffer( null );
414 this.check_queued = false;
415 this.error_line = -1;
416 this.check_running = false;
418 // set gobject values
421 this.el.changed.connect( () => {
424 _this.save_button.el.sensitive = true;
425 print("EDITOR CHANGED");
430 // this.get('/LeftPanel.model').changed( str , false);
435 // user defined functions
436 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
438 this.error_line = validate_res.size;
440 if (this.error_line < 1) {
443 var tlines = this.el.get_line_count ();
445 var valiter = validate_res.map_iterator();
446 while (valiter.next()) {
448 // print("get inter\n");
449 var eline = valiter.get_key();
450 if (eline > tlines) {
453 this.el.get_iter_at_line( out iter, eline);
454 //print("mark line\n");
455 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
459 public string toString () {
463 this.el.get_start_iter(out s);
464 this.el.get_end_iter(out e);
465 var ret = this.el.get_text(s,e,true);
466 //print("TO STRING? " + ret);
469 public bool checkSyntax () {
471 if (this.check_running) {
472 print("Check is running\n");
473 if (this.check_queued) {
474 print("Check is already queued");
477 this.check_queued = true;
478 print("Adding queued Check ");
479 GLib.Timeout.add_seconds(1, () => {
480 this.check_queued = false;
489 var str = this.toString();
492 if (this.error_line > 0) {
495 this.el.get_bounds (out start, out end);
497 this.el.remove_source_marks (start, end, null);
499 if (str.length < 1) {
500 print("checkSyntax - empty string?\n");
504 if (_this.file.xtype == "PlainFile") {
506 // assume it's gtk...
507 this.check_running = true;
509 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
513 this.check_running = false;
519 if (_this.file == null) {
522 var p = Palete.factory(_this.file.xtype);
526 this.check_running = true;
529 if (_this.file.language == "js") {
530 this.check_running = false;
531 print("calling validate javascript\n");
532 return this.highlightErrors(p.validateJavascript(
543 print("calling validate vala\n");
544 // clear the buttons.
547 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
554 this.check_running = false;
559 //print("done mark line\n");
561 return true; // at present allow saving - even if it's invalid..
563 public bool highlightErrorsJson (string type, Json.Object obj) {
566 this.el.get_bounds (out start, out end);
568 this.el.remove_source_marks (start, end, type);
571 // we should highlight other types of errors..
573 if (!obj.has_member(type)) {
574 print("Return has no errors\n");
578 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
579 _this.window.windowstate.state != WindowState.State.CODE
585 var err = obj.get_object_member(type);
588 if (_this.file == null) {
592 var valafn = _this.file.path;
594 if (_this.file.xtype != "PlainFile") {
601 var regex = new Regex("\\.bjs$");
605 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
606 } catch (GLib.RegexError e) {
613 if (!err.has_member(valafn)) {
614 print("File path has no errors\n");
618 var lines = err.get_object_member(valafn);
621 if (obj.has_member("line_offset")) {
622 offset = (int)obj.get_int_member("line_offset") + 1;
628 var tlines = this.el.get_line_count () +1;
630 lines.foreach_member((obj, line, node) => {
633 // print("get inter\n");
634 var eline = int.parse(line) - offset;
635 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
638 if (eline > tlines || eline < 0) {
641 this.el.get_iter_at_line( out iter, eline);
642 //print("mark line\n");
643 var msg = "Line: %d".printf(eline+1);
644 var ar = lines.get_array_member(line);
645 for (var i = 0 ; i < ar.get_length(); i++) {
646 msg += (msg.length > 0) ? "\n" : "";
647 msg += ar.get_string_element(i);
651 this.el.create_source_mark(msg, type, iter);