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) {
74 var str = _this.buffer.toString();
76 _this.buffer.checkSyntax();
80 // LeftPanel.model.changed( str , false);
82 _this.save_button.el.sensitive = false;
84 // find the text for the node..
85 if (_this.file.xtype != "PlainFile") {
86 if (ptype == "listener") {
87 this.node.listeners.set(key,str);
90 this.node.props.set(key,str);
93 _this.file.setSource( str );
102 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
110 if (file.xtype != "PlainFile") {
116 // find the text for the node..
117 if (ptype == "listener") {
118 val = node.listeners.get(key);
121 val = node.props.get(key);
124 this.key_edit.el.show();
125 this.key_edit.el.text = key;
128 this.view.load( file.toSource() );
129 this.key_edit.el.hide();
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.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
268 this.el.tab_width = 4;
269 this.el.highlight_current_line = true;
270 var child_0 = new Xcls_buffer( _this );
272 this.el.set_buffer ( child_0.el );
277 var description = Pango.FontDescription.from_string("monospace");
278 description.set_size(8000);
279 this.el.override_font(description);
280 } catch (GLib.Error e) {
284 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
285 this.el.completion.unblock_interactive();
286 this.el.completion.select_on_show = true; // select
287 this.el.completion.show_headers = false;
288 this.el.completion.remember_info_visibility = true;
291 var attrs = new Gtk.SourceMarkAttributes();
292 var pink = Gdk.RGBA();
293 pink.parse ( "pink");
294 attrs.set_background ( pink);
295 attrs.set_icon_name ( "process-stop");
296 attrs.query_tooltip_text.connect(( mark) => {
297 //print("tooltip query? %s\n", mark.name);
301 this.el.set_mark_attributes ("ERR", attrs, 1);
303 var wattrs = new Gtk.SourceMarkAttributes();
304 var blue = Gdk.RGBA();
305 blue.parse ( "#ABF4EB");
306 wattrs.set_background ( blue);
307 wattrs.set_icon_name ( "process-stop");
308 wattrs.query_tooltip_text.connect(( mark) => {
309 //print("tooltip query? %s\n", mark.name);
313 this.el.set_mark_attributes ("WARN", wattrs, 1);
317 var dattrs = new Gtk.SourceMarkAttributes();
318 var purple = Gdk.RGBA();
319 purple.parse ( "#EEA9FF");
320 dattrs.set_background ( purple);
321 dattrs.set_icon_name ( "process-stop");
322 dattrs.query_tooltip_text.connect(( mark) => {
323 //print("tooltip query? %s\n", mark.name);
327 this.el.set_mark_attributes ("DEPR", dattrs, 1);
330 this.el.key_release_event.connect( (event) => {
332 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
333 print("SAVE: ctrl-S pressed");
334 _this.saveContents();
337 // print(event.key.keyval)
344 // user defined functions
345 public void load (string str) {
347 // show the help page for the active node..
348 //this.get('/Help').show();
351 // this.get('/BottomPane').el.set_current_page(0);
352 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
353 buf.set_text(str, str.length);
354 buf.set_undo_manager(null);
356 var lm = Gtk.SourceLanguageManager.get_default();
358 if (_this.file != null) {
359 lang = _this.file.language;
361 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
362 var lg = _this.file.content_type.length > 0 ?
363 lm.guess_language(_this.file.path, _this.file.content_type) :
364 lm.get_language(lang);
367 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
369 this.el.insert_spaces_instead_of_tabs = true;
371 print("sourcelanguage = %s\n", lg.name);
372 if (lg.name == "Vala") {
373 this.el.insert_spaces_instead_of_tabs = false;
377 this.el.grab_focus();
378 _this.save_button.el.sensitive = false;
381 public class Xcls_buffer : Object
383 public Gtk.SourceBuffer el;
384 private Editor _this;
388 public bool check_queued;
389 public int error_line;
390 public bool check_running;
393 public Xcls_buffer(Editor _owner )
397 this.el = new Gtk.SourceBuffer( null );
400 this.check_queued = false;
401 this.error_line = -1;
402 this.check_running = false;
404 // set gobject values
407 this.el.changed.connect( () => {
410 _this.save_button.el.sensitive = true;
411 print("EDITOR CHANGED");
416 // this.get('/LeftPanel.model').changed( str , false);
421 // user defined functions
422 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
424 this.error_line = validate_res.size;
426 if (this.error_line < 1) {
429 var tlines = this.el.get_line_count ();
431 var valiter = validate_res.map_iterator();
432 while (valiter.next()) {
434 // print("get inter\n");
435 var eline = valiter.get_key();
436 if (eline > tlines) {
439 this.el.get_iter_at_line( out iter, eline);
440 //print("mark line\n");
441 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
445 public string toString () {
449 this.el.get_start_iter(out s);
450 this.el.get_end_iter(out e);
451 var ret = this.el.get_text(s,e,true);
452 //print("TO STRING? " + ret);
455 public bool checkSyntax () {
457 if (this.check_running) {
458 print("Check is running\n");
459 if (this.check_queued) {
460 print("Check is already queued");
463 this.check_queued = true;
464 print("Adding queued Check ");
465 GLib.Timeout.add_seconds(1, () => {
466 this.check_queued = false;
475 var str = this.toString();
478 if (this.error_line > 0) {
481 this.el.get_bounds (out start, out end);
483 this.el.remove_source_marks (start, end, null);
485 if (str.length < 1) {
486 print("checkSyntax - empty string?\n");
490 if (_this.file.xtype == "PlainFile") {
492 // assume it's gtk...
493 this.check_running = true;
495 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
499 this.check_running = false;
505 if (_this.file == null) {
508 var p = Palete.factory(_this.file.xtype);
512 this.check_running = true;
515 if (_this.file.language == "js") {
516 this.check_running = false;
517 print("calling validate javascript\n");
518 return this.highlightErrors(p.validateJavascript(
529 print("calling validate vala\n");
530 // clear the buttons.
533 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
540 this.check_running = false;
545 //print("done mark line\n");
547 return true; // at present allow saving - even if it's invalid..
549 public bool highlightErrorsJson (string type, Json.Object obj) {
552 this.el.get_bounds (out start, out end);
554 this.el.remove_source_marks (start, end, type);
557 // we should highlight other types of errors..
559 if (!obj.has_member(type)) {
560 print("Return has no errors\n");
564 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
565 _this.window.windowstate.state != WindowState.State.CODE
571 var err = obj.get_object_member(type);
574 if (_this.file == null) {
578 var valafn = _this.file.path;
580 if (_this.file.xtype != "PlainFile") {
587 var regex = new Regex("\\.bjs$");
591 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
592 } catch (GLib.RegexError e) {
599 if (!err.has_member(valafn)) {
600 print("File path has no errors\n");
604 var lines = err.get_object_member(valafn);
607 if (obj.has_member("line_offset")) {
608 offset = (int)obj.get_int_member("line_offset") + 1;
614 var tlines = this.el.get_line_count () +1;
616 lines.foreach_member((obj, line, node) => {
619 // print("get inter\n");
620 var eline = int.parse(line) - offset;
621 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
624 if (eline > tlines || eline < 0) {
627 this.el.get_iter_at_line( out iter, eline);
628 //print("mark line\n");
629 var msg = "Line: %d".printf(eline+1);
630 var ar = lines.get_array_member(line);
631 for (var i = 0 ; i < ar.get_length(); i++) {
632 msg += (msg.length > 0) ? "\n" : "";
633 msg += ar.get_string_element(i);
637 this.el.create_source_mark(msg, type, iter);