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 bool saveContents () {
65 if (_this.file == null) {
73 var str = _this.buffer.toString();
75 _this.buffer.checkSyntax();
79 // LeftPanel.model.changed( str , false);
81 _this.save_button.el.sensitive = false;
83 // find the text for the node..
84 if (_this.file.xtype != "PlainFile") {
85 if (ptype == "listener") {
86 this.node.listeners.set(key,str);
89 this.node.props.set(key,str);
92 _this.file.setSource( str );
101 public void scroll_to_line (int line) {
102 var buf = this.view.el.get_buffer();
104 var sbuf = (Gtk.SourceBuffer) buf;
108 sbuf.get_iter_at_line(out iter, line);
109 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.0f);
112 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
120 if (file.xtype != "PlainFile") {
126 // find the text for the node..
127 if (ptype == "listener") {
128 val = node.listeners.get(key);
131 val = node.props.get(key);
134 this.key_edit.el.show();
135 this.key_edit.el.text = key;
138 this.view.load( file.toSource() );
139 this.key_edit.el.hide();
144 public class Xcls_Box2 : Object
147 private Editor _this;
153 public Xcls_Box2(Editor _owner )
156 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
160 // set gobject values
161 this.el.homogeneous = false;
162 var child_0 = new Xcls_save_button( _this );
164 this.el.pack_start ( child_0.el , false,false );
165 var child_1 = new Xcls_key_edit( _this );
167 this.el.pack_end ( child_1.el , true,true );
170 // user defined functions
172 public class Xcls_save_button : Object
174 public Gtk.Button el;
175 private Editor _this;
181 public Xcls_save_button(Editor _owner )
184 _this.save_button = this;
185 this.el = new Gtk.Button();
189 // set gobject values
190 this.el.label = "Save";
193 this.el.clicked.connect( () => {
194 _this.saveContents();
198 // user defined functions
201 public class Xcls_key_edit : Object
204 private Editor _this;
210 public Xcls_key_edit(Editor _owner )
213 _this.key_edit = this;
214 this.el = new Gtk.Entry();
218 // set gobject values
221 // user defined functions
225 public class Xcls_RightEditor : Object
227 public Gtk.ScrolledWindow el;
228 private Editor _this;
234 public Xcls_RightEditor(Editor _owner )
237 _this.RightEditor = this;
238 this.el = new Gtk.ScrolledWindow( null, null );
242 // set gobject values
243 var child_0 = new Xcls_view( _this );
245 this.el.add ( child_0.el );
249 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
252 // user defined functions
254 public class Xcls_view : Object
256 public Gtk.SourceView el;
257 private Editor _this;
263 public Xcls_view(Editor _owner )
267 this.el = new Gtk.SourceView();
271 // set gobject values
272 this.el.auto_indent = true;
273 this.el.indent_width = 4;
274 this.el.show_line_marks = true;
275 this.el.insert_spaces_instead_of_tabs = true;
276 this.el.show_line_numbers = true;
277 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
278 this.el.tab_width = 4;
279 this.el.highlight_current_line = true;
280 var child_0 = new Xcls_buffer( _this );
282 this.el.set_buffer ( child_0.el );
286 var description = Pango.FontDescription.from_string("monospace");
287 description.set_size(8000);
288 this.el.override_font(description);
291 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
292 this.el.completion.unblock_interactive();
293 this.el.completion.select_on_show = true; // select
294 this.el.completion.show_headers = false;
295 this.el.completion.remember_info_visibility = true;
298 var attrs = new Gtk.SourceMarkAttributes();
299 var pink = Gdk.RGBA();
300 pink.parse ( "pink");
301 attrs.set_background ( pink);
302 attrs.set_icon_name ( "process-stop");
303 attrs.query_tooltip_text.connect(( mark) => {
304 //print("tooltip query? %s\n", mark.name);
308 this.el.set_mark_attributes ("ERR", attrs, 1);
310 var wattrs = new Gtk.SourceMarkAttributes();
311 var blue = Gdk.RGBA();
312 blue.parse ( "#ABF4EB");
313 wattrs.set_background ( blue);
314 wattrs.set_icon_name ( "process-stop");
315 wattrs.query_tooltip_text.connect(( mark) => {
316 //print("tooltip query? %s\n", mark.name);
320 this.el.set_mark_attributes ("WARN", wattrs, 1);
324 var dattrs = new Gtk.SourceMarkAttributes();
325 var purple = Gdk.RGBA();
326 purple.parse ( "#EEA9FF");
327 dattrs.set_background ( purple);
328 dattrs.set_icon_name ( "process-stop");
329 dattrs.query_tooltip_text.connect(( mark) => {
330 //print("tooltip query? %s\n", mark.name);
334 this.el.set_mark_attributes ("DEPR", dattrs, 1);
337 this.el.key_release_event.connect( (event) => {
339 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
340 print("SAVE: ctrl-S pressed");
341 _this.saveContents();
344 // print(event.key.keyval)
351 // user defined functions
352 public void load (string str) {
354 // show the help page for the active node..
355 //this.get('/Help').show();
358 // this.get('/BottomPane').el.set_current_page(0);
359 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
360 buf.set_text(str, str.length);
361 buf.set_undo_manager(null);
363 var lm = Gtk.SourceLanguageManager.get_default();
365 if (_this.file != null) {
366 lang = _this.file.language;
368 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
369 var lg = _this.file.content_type.length > 0 ?
370 lm.guess_language(_this.file.path, _this.file.content_type) :
371 lm.get_language(lang);
374 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
376 this.el.insert_spaces_instead_of_tabs = true;
378 print("sourcelanguage = %s\n", lg.name);
379 if (lg.name == "Vala") {
380 this.el.insert_spaces_instead_of_tabs = false;
384 this.el.grab_focus();
385 _this.save_button.el.sensitive = false;
388 public class Xcls_buffer : Object
390 public Gtk.SourceBuffer el;
391 private Editor _this;
395 public bool check_queued;
396 public int error_line;
397 public bool check_running;
400 public Xcls_buffer(Editor _owner )
404 this.el = new Gtk.SourceBuffer( null );
407 this.check_queued = false;
408 this.error_line = -1;
409 this.check_running = false;
411 // set gobject values
414 this.el.changed.connect( () => {
417 _this.save_button.el.sensitive = true;
418 print("EDITOR CHANGED");
423 // this.get('/LeftPanel.model').changed( str , false);
428 // user defined functions
429 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
431 this.error_line = validate_res.size;
433 if (this.error_line < 1) {
436 var tlines = this.el.get_line_count ();
438 var valiter = validate_res.map_iterator();
439 while (valiter.next()) {
441 // print("get inter\n");
442 var eline = valiter.get_key();
443 if (eline > tlines) {
446 this.el.get_iter_at_line( out iter, eline);
447 //print("mark line\n");
448 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
452 public string toString () {
456 this.el.get_start_iter(out s);
457 this.el.get_end_iter(out e);
458 var ret = this.el.get_text(s,e,true);
459 //print("TO STRING? " + ret);
462 public bool checkSyntax () {
464 if (this.check_running) {
465 print("Check is running\n");
466 if (this.check_queued) {
467 print("Check is already queued");
470 this.check_queued = true;
471 print("Adding queued Check ");
472 GLib.Timeout.add_seconds(1, () => {
473 this.check_queued = false;
482 var str = this.toString();
485 if (this.error_line > 0) {
488 this.el.get_bounds (out start, out end);
490 this.el.remove_source_marks (start, end, null);
492 if (str.length < 1) {
493 print("checkSyntax - empty string?\n");
497 if (_this.file.xtype == "PlainFile") {
499 // assume it's gtk...
500 this.check_running = true;
502 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
506 this.check_running = false;
512 if (_this.file == null) {
515 var p = Palete.factory(_this.file.xtype);
519 this.check_running = true;
522 if (_this.file.language == "js") {
523 this.check_running = false;
524 print("calling validate javascript\n");
525 return this.highlightErrors(p.validateJavascript(
536 print("calling validate vala\n");
537 // clear the buttons.
540 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
547 this.check_running = false;
552 //print("done mark line\n");
554 return true; // at present allow saving - even if it's invalid..
556 public bool highlightErrorsJson (string type, Json.Object obj) {
559 this.el.get_bounds (out start, out end);
561 this.el.remove_source_marks (start, end, type);
564 // we should highlight other types of errors..
566 if (!obj.has_member(type)) {
567 print("Return has no errors\n");
571 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
572 _this.window.windowstate.state != WindowState.State.CODE
578 var err = obj.get_object_member(type);
581 if (_this.file == null) {
585 var valafn = _this.file.path;
587 if (_this.file.xtype != "PlainFile") {
594 var regex = new Regex("\\.bjs$");
598 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
599 } catch (GLib.RegexError e) {
606 if (!err.has_member(valafn)) {
607 print("File path has no errors\n");
611 var lines = err.get_object_member(valafn);
614 if (obj.has_member("line_offset")) {
615 offset = (int)obj.get_int_member("line_offset") + 1;
621 var tlines = this.el.get_line_count () +1;
623 lines.foreach_member((obj, line, node) => {
626 // print("get inter\n");
627 var eline = int.parse(line) - offset;
628 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
631 if (eline > tlines || eline < 0) {
634 this.el.get_iter_at_line( out iter, eline);
635 //print("mark line\n");
636 var msg = "Line: %d".printf(eline+1);
637 var ar = lines.get_array_member(line);
638 for (var i = 0 ; i < ar.get_length(); i++) {
639 msg += (msg.length > 0) ? "\n" : "";
640 msg += ar.get_string_element(i);
644 this.el.create_source_mark(msg, type, iter);