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 var child_0 = new Xcls_Box2( _this );
54 this.el.pack_start ( child_0.el , false,true );
55 var child_1 = new Xcls_RightEditor( _this );
57 this.el.pack_end ( child_1.el , true,true );
60 // user defined functions
61 public bool saveContents () {
64 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 show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
109 if (file.xtype != "PlainFile") {
115 // find the text for the node..
116 if (ptype == "listener") {
117 val = node.listeners.get(key);
120 val = node.props.get(key);
123 this.key_edit.el.show();
124 this.key_edit.el.text = key;
127 this.view.load( file.toSource() );
128 this.key_edit.el.hide();
133 public class Xcls_Box2 : Object
136 private Editor _this;
142 public Xcls_Box2(Editor _owner )
145 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
149 // set gobject values
150 this.el.homogeneous = false;
151 var child_0 = new Xcls_save_button( _this );
153 this.el.pack_start ( child_0.el , false,false );
154 var child_1 = new Xcls_key_edit( _this );
156 this.el.pack_end ( child_1.el , true,true );
159 // user defined functions
161 public class Xcls_save_button : Object
163 public Gtk.Button el;
164 private Editor _this;
170 public Xcls_save_button(Editor _owner )
173 _this.save_button = this;
174 this.el = new Gtk.Button();
178 // set gobject values
179 this.el.label = "Save";
182 this.el.clicked.connect( () => {
183 _this.saveContents();
187 // user defined functions
190 public class Xcls_key_edit : Object
193 private Editor _this;
199 public Xcls_key_edit(Editor _owner )
202 _this.key_edit = this;
203 this.el = new Gtk.Entry();
207 // set gobject values
210 // user defined functions
214 public class Xcls_RightEditor : Object
216 public Gtk.ScrolledWindow el;
217 private Editor _this;
223 public Xcls_RightEditor(Editor _owner )
226 _this.RightEditor = this;
227 this.el = new Gtk.ScrolledWindow( null, null );
231 // set gobject values
232 var child_0 = new Xcls_view( _this );
234 this.el.add ( child_0.el );
238 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
241 // user defined functions
243 public class Xcls_view : Object
245 public Gtk.SourceView el;
246 private Editor _this;
252 public Xcls_view(Editor _owner )
256 this.el = new Gtk.SourceView();
260 // set gobject values
261 this.el.auto_indent = true;
262 this.el.indent_width = 4;
263 this.el.show_line_marks = true;
264 this.el.insert_spaces_instead_of_tabs = true;
265 this.el.show_line_numbers = true;
266 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.ALL;
267 this.el.tab_width = 4;
268 this.el.highlight_current_line = true;
269 var child_0 = new Xcls_buffer( _this );
271 this.el.set_buffer ( child_0.el );
275 var description = Pango.FontDescription.from_string("monospace");
276 description.set_size(8000);
277 this.el.override_font(description);
281 var attrs = new Gtk.SourceMarkAttributes();
282 var pink = Gdk.RGBA();
283 pink.parse ( "pink");
284 attrs.set_background ( pink);
285 attrs.set_icon_name ( "process-stop");
286 attrs.query_tooltip_text.connect(( mark) => {
287 //print("tooltip query? %s\n", mark.name);
291 this.el.set_mark_attributes ("ERR", attrs, 1);
293 var wattrs = new Gtk.SourceMarkAttributes();
294 var blue = Gdk.RGBA();
295 blue.parse ( "#ABF4EB");
296 wattrs.set_background ( blue);
297 wattrs.set_icon_name ( "process-stop");
298 wattrs.query_tooltip_text.connect(( mark) => {
299 //print("tooltip query? %s\n", mark.name);
303 this.el.set_mark_attributes ("WARN", wattrs, 1);
307 var dattrs = new Gtk.SourceMarkAttributes();
308 var purple = Gdk.RGBA();
309 purple.parse ( "#EEA9FF");
310 dattrs.set_background ( purple);
311 dattrs.set_icon_name ( "process-stop");
312 dattrs.query_tooltip_text.connect(( mark) => {
313 //print("tooltip query? %s\n", mark.name);
317 this.el.set_mark_attributes ("DEPR", dattrs, 1);
320 this.el.key_release_event.connect( (event) => {
322 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
323 print("SAVE: ctrl-S pressed");
324 _this.saveContents();
327 // print(event.key.keyval)
334 // user defined functions
335 public void load (string str) {
337 // show the help page for the active node..
338 //this.get('/Help').show();
341 // this.get('/BottomPane').el.set_current_page(0);
342 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
343 buf.set_text(str, str.length);
344 buf.set_undo_manager(null);
346 var lm = Gtk.SourceLanguageManager.get_default();
348 if (_this.file != null) {
349 lang = _this.file.language;
351 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
352 var lg = _this.file.content_type.length > 0 ?
353 lm.guess_language(_this.file.path, _this.file.content_type) :
354 lm.get_language(lang);
358 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
360 this.el.insert_spaces_instead_of_tabs = true;
362 print("sourcelanguage = %s\n", lg.name);
363 if (lg.name == "Vala") {
364 this.el.insert_spaces_instead_of_tabs = false;
368 this.el.grab_focus();
369 _this.save_button.el.sensitive = false;
372 public class Xcls_buffer : Object
374 public Gtk.SourceBuffer el;
375 private Editor _this;
379 public bool check_queued;
380 public int error_line;
381 public bool check_running;
384 public Xcls_buffer(Editor _owner )
388 this.el = new Gtk.SourceBuffer( null );
391 this.check_queued = false;
392 this.error_line = -1;
393 this.check_running = false;
395 // set gobject values
398 this.el.changed.connect( () => {
401 _this.save_button.el.sensitive = true;
402 print("EDITOR CHANGED");
407 // this.get('/LeftPanel.model').changed( str , false);
412 // user defined functions
413 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
415 this.error_line = validate_res.size;
417 if (this.error_line < 1) {
420 var tlines = this.el.get_line_count ();
422 var valiter = validate_res.map_iterator();
423 while (valiter.next()) {
425 // print("get inter\n");
426 var eline = valiter.get_key();
427 if (eline > tlines) {
430 this.el.get_iter_at_line( out iter, eline);
431 //print("mark line\n");
432 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
436 public string toString () {
440 this.el.get_start_iter(out s);
441 this.el.get_end_iter(out e);
442 var ret = this.el.get_text(s,e,true);
443 //print("TO STRING? " + ret);
446 public bool checkSyntax () {
448 if (this.check_running) {
449 print("Check is running\n");
450 if (this.check_queued) {
451 print("Check is already queued");
454 this.check_queued = true;
455 print("Adding queued Check ");
456 GLib.Timeout.add_seconds(1, () => {
457 this.check_queued = false;
466 var str = this.toString();
469 if (this.error_line > 0) {
472 this.el.get_bounds (out start, out end);
474 this.el.remove_source_marks (start, end, null);
476 if (str.length < 1) {
477 print("checkSyntax - empty string?\n");
481 if (_this.file.xtype == "PlainFile") {
483 // assume it's gtk...
484 this.check_running = true;
486 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
490 this.check_running = false;
496 if (_this.file == null) {
499 var p = Palete.factory(_this.file.xtype);
503 this.check_running = true;
506 if (_this.file.language == "js") {
507 this.check_running = false;
508 print("calling validate javascript\n");
509 return this.highlightErrors(p.validateJavascript(
520 print("calling validate vala\n");
521 // clear the buttons.
524 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
531 this.check_running = false;
536 //print("done mark line\n");
538 return true; // at present allow saving - even if it's invalid..
540 public bool highlightErrorsJson (string type, Json.Object obj) {
543 this.el.get_bounds (out start, out end);
545 this.el.remove_source_marks (start, end, type);
548 // we should highlight other types of errors..
550 if (!obj.has_member(type)) {
551 print("Return has no errors\n");
555 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
556 _this.window.windowstate.state != WindowState.State.CODE
562 var err = obj.get_object_member(type);
565 if (_this.file == null) {
569 var valafn = _this.file.path;
571 if (_this.file.xtype != "PlainFile") {
578 var regex = new Regex("\\.bjs$");
582 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
583 } catch (GLib.RegexError e) {
590 if (!err.has_member(valafn)) {
591 print("File path has no errors\n");
595 var lines = err.get_object_member(valafn);
598 if (obj.has_member("line_offset")) {
599 offset = (int)obj.get_int_member("line_offset") + 1;
605 var tlines = this.el.get_line_count () +1;
607 lines.foreach_member((obj, line, node) => {
610 // print("get inter\n");
611 var eline = int.parse(line) - offset;
612 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
615 if (eline > tlines || eline < 0) {
618 this.el.get_iter_at_line( out iter, eline);
619 //print("mark line\n");
620 var msg = "Line: %d".printf(eline+1);
621 var ar = lines.get_array_member(line);
622 for (var i = 0 ; i < ar.get_length(); i++) {
623 msg += (msg.length > 0) ? "\n" : "";
624 msg += ar.get_string_element(i);
628 this.el.create_source_mark(msg, type, iter);