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;
29 public JsRender.JsRender file;
32 public signal void save ();
33 public JsRender.Node node;
39 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
43 this.activeEditor = "";
53 this.el.homogeneous = false;
54 var child_0 = new Xcls_Box2( _this );
56 this.el.pack_start ( child_0.el , false,true );
57 var child_1 = new Xcls_RightEditor( _this );
59 this.el.pack_end ( child_1.el , true,true );
62 // user defined functions
63 public bool saveContents () {
66 if (_this.file == null) {
75 var str = _this.buffer.toString();
77 _this.buffer.checkSyntax();
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
85 // find the text for the node..
86 if (_this.file.xtype != "PlainFile") {
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
94 _this.file.setSource( str );
103 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
111 if (file.xtype != "PlainFile") {
117 // find the text for the node..
118 if (ptype == "listener") {
119 val = node.listeners.get(key);
122 val = node.props.get(key);
128 this.view.load( file.toSource() );
131 this.key_edit.el.text = key;
134 public class Xcls_Box2 : Object
137 private Editor _this;
143 public Xcls_Box2(Editor _owner )
146 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
150 // set gobject values
151 this.el.homogeneous = false;
152 var child_0 = new Xcls_save_button( _this );
154 this.el.pack_start ( child_0.el , false,false );
155 var child_1 = new Xcls_key_edit( _this );
157 this.el.pack_end ( child_1.el , true,true );
160 // user defined functions
162 public class Xcls_save_button : Object
164 public Gtk.Button el;
165 private Editor _this;
171 public Xcls_save_button(Editor _owner )
174 _this.save_button = this;
175 this.el = new Gtk.Button();
179 // set gobject values
180 this.el.label = "Save";
183 this.el.clicked.connect( () => {
184 _this.saveContents();
188 // user defined functions
191 public class Xcls_key_edit : Object
194 private Editor _this;
200 public Xcls_key_edit(Editor _owner )
203 _this.key_edit = this;
204 this.el = new Gtk.Entry();
208 // set gobject values
211 // user defined functions
215 public class Xcls_RightEditor : Object
217 public Gtk.ScrolledWindow el;
218 private Editor _this;
224 public Xcls_RightEditor(Editor _owner )
227 _this.RightEditor = this;
228 this.el = new Gtk.ScrolledWindow( null, null );
232 // set gobject values
233 var child_0 = new Xcls_view( _this );
235 this.el.add ( child_0.el );
239 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
242 // user defined functions
244 public class Xcls_view : Object
246 public Gtk.SourceView el;
247 private Editor _this;
253 public Xcls_view(Editor _owner )
257 this.el = new Gtk.SourceView();
261 // set gobject values
262 this.el.auto_indent = true;
263 this.el.indent_width = 4;
264 this.el.show_line_marks = true;
265 this.el.insert_spaces_instead_of_tabs = true;
266 this.el.show_line_numbers = true;
267 this.el.highlight_current_line = true;
268 var child_0 = new Xcls_buffer( _this );
270 this.el.set_buffer ( child_0.el );
274 var description = Pango.FontDescription.from_string("monospace");
275 description.set_size(8000);
276 this.el.override_font(description);
280 var attrs = new Gtk.SourceMarkAttributes();
281 var pink = Gdk.RGBA();
282 pink.parse ( "pink");
283 attrs.set_background ( pink);
284 attrs.set_icon_name ( "process-stop");
285 attrs.query_tooltip_text.connect(( mark) => {
286 //print("tooltip query? %s\n", mark.name);
290 this.el.set_mark_attributes ("ERR", attrs, 1);
292 var wattrs = new Gtk.SourceMarkAttributes();
293 var blue = Gdk.RGBA();
294 blue.parse ( "#ABF4EB");
295 wattrs.set_background ( blue);
296 wattrs.set_icon_name ( "process-stop");
297 wattrs.query_tooltip_text.connect(( mark) => {
298 //print("tooltip query? %s\n", mark.name);
302 this.el.set_mark_attributes ("WARN", wattrs, 1);
306 var dattrs = new Gtk.SourceMarkAttributes();
307 var purple = Gdk.RGBA();
308 purple.parse ( "#EEA9FF");
309 dattrs.set_background ( purple);
310 dattrs.set_icon_name ( "process-stop");
311 dattrs.query_tooltip_text.connect(( mark) => {
312 //print("tooltip query? %s\n", mark.name);
316 this.el.set_mark_attributes ("DEPR", dattrs, 1);
319 this.el.key_release_event.connect( (event) => {
321 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
322 print("SAVE: ctrl-S pressed");
323 _this.saveContents();
326 // print(event.key.keyval)
333 // user defined functions
334 public void load (string str) {
336 // show the help page for the active node..
337 //this.get('/Help').show();
340 // this.get('/BottomPane').el.set_current_page(0);
341 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
342 buf.set_text(str, str.length);
343 buf.set_undo_manager(null);
345 var lm = Gtk.SourceLanguageManager.get_default();
347 if (_this.file != null) {
348 lang = _this.file.language;
350 //?? is javascript going to work as js?
352 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
356 this.el.grab_focus();
357 _this.save_button.el.sensitive = false;
360 public class Xcls_buffer : Object
362 public Gtk.SourceBuffer el;
363 private Editor _this;
367 public bool check_queued;
368 public int error_line;
369 public bool check_running;
372 public Xcls_buffer(Editor _owner )
376 this.el = new Gtk.SourceBuffer( null );
379 this.check_queued = false;
380 this.error_line = -1;
381 this.check_running = false;
383 // set gobject values
386 this.el.changed.connect( () => {
389 _this.save_button.el.sensitive = true;
390 print("EDITOR CHANGED");
395 // this.get('/LeftPanel.model').changed( str , false);
400 // user defined functions
401 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
403 this.error_line = validate_res.size;
405 if (this.error_line < 1) {
408 var tlines = this.el.get_line_count ();
410 var valiter = validate_res.map_iterator();
411 while (valiter.next()) {
413 // print("get inter\n");
414 var eline = valiter.get_key();
415 if (eline > tlines) {
418 this.el.get_iter_at_line( out iter, eline);
419 //print("mark line\n");
420 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
424 public string toString () {
428 this.el.get_start_iter(out s);
429 this.el.get_end_iter(out e);
430 var ret = this.el.get_text(s,e,true);
431 //print("TO STRING? " + ret);
434 public bool checkSyntax () {
436 if (this.check_running) {
437 print("Check is running\n");
438 if (this.check_queued) {
439 print("Check is already queued");
442 this.check_queued = true;
443 print("Adding queued Check ");
444 GLib.Timeout.add_seconds(1, () => {
445 this.check_queued = false;
454 var str = this.toString();
457 if (this.error_line > 0) {
460 this.el.get_bounds (out start, out end);
462 this.el.remove_source_marks (start, end, null);
464 if (str.length < 1) {
465 print("checkSyntax - empty string?\n");
469 if (_this.file.xtype == "PlainFile") {
471 // assume it's gtk...
472 this.check_running = true;
474 _this.window.windowstate.valasource.checkPlainFileSpawn(
481 if (_this.file == null) {
484 var p = Palete.factory(_this.file.xtype);
488 this.check_running = true;
491 if (_this.file.language == "js") {
492 this.check_running = false;
493 print("calling validate javascript\n");
494 return this.highlightErrors(p.validateJavascript(
505 print("calling validate vala\n");
506 // clear the buttons.
510 _this.window.windowstate,
522 //print("done mark line\n");
524 return true; // at present allow saving - even if it's invalid..
526 public bool highlightErrorsJson (string type, Json.Object obj) {
529 this.el.get_bounds (out start, out end);
531 this.el.remove_source_marks (start, end, type);
534 // we should highlight other types of errors..
536 if (!obj.has_member(type)) {
537 print("Return has no errors\n");
541 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
542 _this.window.windowstate.state != WindowState.State.CODE
548 var err = obj.get_object_member(type);
549 var valafn = _this.fname;
550 if (_this.file != null) {
557 var regex = new Regex("\\.bjs$");
560 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
561 } catch (GLib.RegexError e) {
568 if (!err.has_member(valafn)) {
569 print("File path has no errors\n");
573 var lines = err.get_object_member(valafn);
576 if (obj.has_member("line_offset")) {
577 offset = (int)obj.get_int_member("line_offset") + 1;
583 var tlines = this.el.get_line_count () +1;
585 lines.foreach_member((obj, line, node) => {
588 // print("get inter\n");
589 var eline = int.parse(line) - offset;
590 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
593 if (eline > tlines || eline < 0) {
596 this.el.get_iter_at_line( out iter, eline);
597 //print("mark line\n");
598 var msg = "Line: %d".printf(eline+1);
599 var ar = lines.get_array_member(line);
600 for (var i = 0 ; i < ar.get_length(); i++) {
601 msg += (msg.length > 0) ? "\n" : "";
602 msg += ar.get_string_element(i);
606 this.el.create_source_mark(msg, type, iter);