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) {
104 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
112 if (file.xtype != "PlainFile") {
118 // find the text for the node..
119 if (ptype == "listener") {
120 val = node.listeners.get(key);
123 val = node.props.get(key);
126 this.key_edit.el.show();
127 this.key_edit.el.text = key;
130 this.view.load( file.toSource() );
131 this.key_edit.el.hide();
136 public class Xcls_Box2 : Object
139 private Editor _this;
145 public Xcls_Box2(Editor _owner )
148 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
152 // set gobject values
153 this.el.homogeneous = false;
154 var child_0 = new Xcls_save_button( _this );
156 this.el.pack_start ( child_0.el , false,false );
157 var child_1 = new Xcls_key_edit( _this );
159 this.el.pack_end ( child_1.el , true,true );
162 // user defined functions
164 public class Xcls_save_button : Object
166 public Gtk.Button el;
167 private Editor _this;
173 public Xcls_save_button(Editor _owner )
176 _this.save_button = this;
177 this.el = new Gtk.Button();
181 // set gobject values
182 this.el.label = "Save";
185 this.el.clicked.connect( () => {
186 _this.saveContents();
190 // user defined functions
193 public class Xcls_key_edit : Object
196 private Editor _this;
202 public Xcls_key_edit(Editor _owner )
205 _this.key_edit = this;
206 this.el = new Gtk.Entry();
210 // set gobject values
213 // user defined functions
217 public class Xcls_RightEditor : Object
219 public Gtk.ScrolledWindow el;
220 private Editor _this;
226 public Xcls_RightEditor(Editor _owner )
229 _this.RightEditor = this;
230 this.el = new Gtk.ScrolledWindow( null, null );
234 // set gobject values
235 var child_0 = new Xcls_view( _this );
237 this.el.add ( child_0.el );
241 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
244 // user defined functions
246 public class Xcls_view : Object
248 public Gtk.SourceView el;
249 private Editor _this;
255 public Xcls_view(Editor _owner )
259 this.el = new Gtk.SourceView();
263 // set gobject values
264 this.el.auto_indent = true;
265 this.el.indent_width = 4;
266 this.el.show_line_marks = true;
267 this.el.insert_spaces_instead_of_tabs = true;
268 this.el.show_line_numbers = true;
269 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
270 this.el.tab_width = 4;
271 this.el.highlight_current_line = true;
272 var child_0 = new Xcls_buffer( _this );
274 this.el.set_buffer ( child_0.el );
278 var description = Pango.FontDescription.from_string("monospace");
279 description.set_size(8000);
280 this.el.override_font(description);
283 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
284 this.el.completion.unblock_interactive();
285 this.el.completion.select_on_show = true; // select
286 this.el.completion.show_headers = false;
287 this.el.completion.remember_info_visibility = true;
290 var attrs = new Gtk.SourceMarkAttributes();
291 var pink = Gdk.RGBA();
292 pink.parse ( "pink");
293 attrs.set_background ( pink);
294 attrs.set_icon_name ( "process-stop");
295 attrs.query_tooltip_text.connect(( mark) => {
296 //print("tooltip query? %s\n", mark.name);
300 this.el.set_mark_attributes ("ERR", attrs, 1);
302 var wattrs = new Gtk.SourceMarkAttributes();
303 var blue = Gdk.RGBA();
304 blue.parse ( "#ABF4EB");
305 wattrs.set_background ( blue);
306 wattrs.set_icon_name ( "process-stop");
307 wattrs.query_tooltip_text.connect(( mark) => {
308 //print("tooltip query? %s\n", mark.name);
312 this.el.set_mark_attributes ("WARN", wattrs, 1);
316 var dattrs = new Gtk.SourceMarkAttributes();
317 var purple = Gdk.RGBA();
318 purple.parse ( "#EEA9FF");
319 dattrs.set_background ( purple);
320 dattrs.set_icon_name ( "process-stop");
321 dattrs.query_tooltip_text.connect(( mark) => {
322 //print("tooltip query? %s\n", mark.name);
326 this.el.set_mark_attributes ("DEPR", dattrs, 1);
329 this.el.key_release_event.connect( (event) => {
331 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
332 print("SAVE: ctrl-S pressed");
333 _this.saveContents();
336 // print(event.key.keyval)
343 // user defined functions
344 public void load (string str) {
346 // show the help page for the active node..
347 //this.get('/Help').show();
350 // this.get('/BottomPane').el.set_current_page(0);
351 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
352 buf.set_text(str, str.length);
353 buf.set_undo_manager(null);
355 var lm = Gtk.SourceLanguageManager.get_default();
357 if (_this.file != null) {
358 lang = _this.file.language;
360 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
361 var lg = _this.file.content_type.length > 0 ?
362 lm.guess_language(_this.file.path, _this.file.content_type) :
363 lm.get_language(lang);
366 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
368 this.el.insert_spaces_instead_of_tabs = true;
370 print("sourcelanguage = %s\n", lg.name);
371 if (lg.name == "Vala") {
372 this.el.insert_spaces_instead_of_tabs = false;
376 this.el.grab_focus();
377 _this.save_button.el.sensitive = false;
380 public class Xcls_buffer : Object
382 public Gtk.SourceBuffer el;
383 private Editor _this;
387 public bool check_queued;
388 public int error_line;
389 public bool check_running;
392 public Xcls_buffer(Editor _owner )
396 this.el = new Gtk.SourceBuffer( null );
399 this.check_queued = false;
400 this.error_line = -1;
401 this.check_running = false;
403 // set gobject values
406 this.el.changed.connect( () => {
409 _this.save_button.el.sensitive = true;
410 print("EDITOR CHANGED");
415 // this.get('/LeftPanel.model').changed( str , false);
420 // user defined functions
421 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
423 this.error_line = validate_res.size;
425 if (this.error_line < 1) {
428 var tlines = this.el.get_line_count ();
430 var valiter = validate_res.map_iterator();
431 while (valiter.next()) {
433 // print("get inter\n");
434 var eline = valiter.get_key();
435 if (eline > tlines) {
438 this.el.get_iter_at_line( out iter, eline);
439 //print("mark line\n");
440 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
444 public string toString () {
448 this.el.get_start_iter(out s);
449 this.el.get_end_iter(out e);
450 var ret = this.el.get_text(s,e,true);
451 //print("TO STRING? " + ret);
454 public bool checkSyntax () {
456 if (this.check_running) {
457 print("Check is running\n");
458 if (this.check_queued) {
459 print("Check is already queued");
462 this.check_queued = true;
463 print("Adding queued Check ");
464 GLib.Timeout.add_seconds(1, () => {
465 this.check_queued = false;
474 var str = this.toString();
477 if (this.error_line > 0) {
480 this.el.get_bounds (out start, out end);
482 this.el.remove_source_marks (start, end, null);
484 if (str.length < 1) {
485 print("checkSyntax - empty string?\n");
489 if (_this.file.xtype == "PlainFile") {
491 // assume it's gtk...
492 this.check_running = true;
494 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
498 this.check_running = false;
504 if (_this.file == null) {
507 var p = Palete.factory(_this.file.xtype);
511 this.check_running = true;
514 if (_this.file.language == "js") {
515 this.check_running = false;
516 print("calling validate javascript\n");
517 return this.highlightErrors(p.validateJavascript(
528 print("calling validate vala\n");
529 // clear the buttons.
532 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
539 this.check_running = false;
544 //print("done mark line\n");
546 return true; // at present allow saving - even if it's invalid..
548 public bool highlightErrorsJson (string type, Json.Object obj) {
551 this.el.get_bounds (out start, out end);
553 this.el.remove_source_marks (start, end, type);
556 // we should highlight other types of errors..
558 if (!obj.has_member(type)) {
559 print("Return has no errors\n");
563 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
564 _this.window.windowstate.state != WindowState.State.CODE
570 var err = obj.get_object_member(type);
573 if (_this.file == null) {
577 var valafn = _this.file.path;
579 if (_this.file.xtype != "PlainFile") {
586 var regex = new Regex("\\.bjs$");
590 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
591 } catch (GLib.RegexError e) {
598 if (!err.has_member(valafn)) {
599 print("File path has no errors\n");
603 var lines = err.get_object_member(valafn);
606 if (obj.has_member("line_offset")) {
607 offset = (int)obj.get_int_member("line_offset") + 1;
613 var tlines = this.el.get_line_count () +1;
615 lines.foreach_member((obj, line, node) => {
618 // print("get inter\n");
619 var eline = int.parse(line) - offset;
620 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
623 if (eline > tlines || eline < 0) {
626 this.el.get_iter_at_line( out iter, eline);
627 //print("mark line\n");
628 var msg = "Line: %d".printf(eline+1);
629 var ar = lines.get_array_member(line);
630 for (var i = 0 ; i < ar.get_length(); i++) {
631 msg += (msg.length > 0) ? "\n" : "";
632 msg += ar.get_string_element(i);
636 this.el.create_source_mark(msg, type, iter);