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 Gtk.SourceSearchContext searchcontext;
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 = "";
46 this.searchcontext = null;
53 this.el.homogeneous = false;
54 this.el.hexpand = true;
55 var child_0 = new Xcls_Box2( _this );
57 this.el.pack_start ( child_0.el , false,true );
58 var child_1 = new Xcls_RightEditor( _this );
60 this.el.pack_end ( child_1.el , true,true );
63 // user defined functions
64 public bool saveContents () {
67 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 scroll_to_line (int line) {
105 GLib.Timeout.add(500, () => {
107 var buf = this.view.el.get_buffer();
109 var sbuf = (Gtk.SourceBuffer) buf;
113 sbuf.get_iter_at_line(out iter, line);
114 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
118 public int search (string txt) {
120 var s = new Gtk.SourceSearchSettings();
122 this.sourcecontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 sctx.set_highlight(true);
124 s.set_search_text(txt);
125 return this.sourcecontext.get_occurences_count();
128 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
136 if (file.xtype != "PlainFile") {
142 // find the text for the node..
143 if (ptype == "listener") {
144 val = node.listeners.get(key);
147 val = node.props.get(key);
150 this.key_edit.el.show();
151 this.key_edit.el.text = key;
154 this.view.load( file.toSource() );
155 this.key_edit.el.hide();
160 public class Xcls_Box2 : Object
163 private Editor _this;
169 public Xcls_Box2(Editor _owner )
172 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
176 // set gobject values
177 this.el.homogeneous = false;
178 var child_0 = new Xcls_save_button( _this );
180 this.el.pack_start ( child_0.el , false,false );
181 var child_1 = new Xcls_key_edit( _this );
183 this.el.pack_end ( child_1.el , true,true );
186 // user defined functions
188 public class Xcls_save_button : Object
190 public Gtk.Button el;
191 private Editor _this;
197 public Xcls_save_button(Editor _owner )
200 _this.save_button = this;
201 this.el = new Gtk.Button();
205 // set gobject values
206 this.el.label = "Save";
209 this.el.clicked.connect( () => {
210 _this.saveContents();
214 // user defined functions
217 public class Xcls_key_edit : Object
220 private Editor _this;
226 public Xcls_key_edit(Editor _owner )
229 _this.key_edit = this;
230 this.el = new Gtk.Entry();
234 // set gobject values
237 // user defined functions
241 public class Xcls_RightEditor : Object
243 public Gtk.ScrolledWindow el;
244 private Editor _this;
250 public Xcls_RightEditor(Editor _owner )
253 _this.RightEditor = this;
254 this.el = new Gtk.ScrolledWindow( null, null );
258 // set gobject values
259 var child_0 = new Xcls_view( _this );
261 this.el.add ( child_0.el );
265 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
268 // user defined functions
270 public class Xcls_view : Object
272 public Gtk.SourceView el;
273 private Editor _this;
279 public Xcls_view(Editor _owner )
283 this.el = new Gtk.SourceView();
287 // set gobject values
288 this.el.auto_indent = true;
289 this.el.indent_width = 4;
290 this.el.show_line_marks = true;
291 this.el.insert_spaces_instead_of_tabs = true;
292 this.el.show_line_numbers = true;
293 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
294 this.el.tab_width = 4;
295 this.el.highlight_current_line = true;
296 var child_0 = new Xcls_buffer( _this );
298 this.el.set_buffer ( child_0.el );
302 var description = Pango.FontDescription.from_string("monospace");
303 description.set_size(8000);
304 this.el.override_font(description);
307 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
308 this.el.completion.unblock_interactive();
309 this.el.completion.select_on_show = true; // select
310 this.el.completion.show_headers = false;
311 this.el.completion.remember_info_visibility = true;
314 var attrs = new Gtk.SourceMarkAttributes();
315 var pink = Gdk.RGBA();
316 pink.parse ( "pink");
317 attrs.set_background ( pink);
318 attrs.set_icon_name ( "process-stop");
319 attrs.query_tooltip_text.connect(( mark) => {
320 //print("tooltip query? %s\n", mark.name);
324 this.el.set_mark_attributes ("ERR", attrs, 1);
326 var wattrs = new Gtk.SourceMarkAttributes();
327 var blue = Gdk.RGBA();
328 blue.parse ( "#ABF4EB");
329 wattrs.set_background ( blue);
330 wattrs.set_icon_name ( "process-stop");
331 wattrs.query_tooltip_text.connect(( mark) => {
332 //print("tooltip query? %s\n", mark.name);
336 this.el.set_mark_attributes ("WARN", wattrs, 1);
340 var dattrs = new Gtk.SourceMarkAttributes();
341 var purple = Gdk.RGBA();
342 purple.parse ( "#EEA9FF");
343 dattrs.set_background ( purple);
344 dattrs.set_icon_name ( "process-stop");
345 dattrs.query_tooltip_text.connect(( mark) => {
346 //print("tooltip query? %s\n", mark.name);
350 this.el.set_mark_attributes ("DEPR", dattrs, 1);
353 this.el.key_release_event.connect( (event) => {
355 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
356 print("SAVE: ctrl-S pressed");
357 _this.saveContents();
360 // print(event.key.keyval)
367 // user defined functions
368 public void load (string str) {
370 // show the help page for the active node..
371 //this.get('/Help').show();
374 // this.get('/BottomPane').el.set_current_page(0);
375 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
376 buf.set_text(str, str.length);
377 buf.set_undo_manager(null);
379 var lm = Gtk.SourceLanguageManager.get_default();
381 if (_this.file != null) {
382 lang = _this.file.language;
384 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
385 var lg = _this.file.content_type.length > 0 ?
386 lm.guess_language(_this.file.path, _this.file.content_type) :
387 lm.get_language(lang);
390 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
392 this.el.insert_spaces_instead_of_tabs = true;
394 print("sourcelanguage = %s\n", lg.name);
395 if (lg.name == "Vala") {
396 this.el.insert_spaces_instead_of_tabs = false;
400 this.el.grab_focus();
401 _this.save_button.el.sensitive = false;
404 public class Xcls_buffer : Object
406 public Gtk.SourceBuffer el;
407 private Editor _this;
411 public bool check_queued;
412 public int error_line;
413 public bool check_running;
416 public Xcls_buffer(Editor _owner )
420 this.el = new Gtk.SourceBuffer( null );
423 this.check_queued = false;
424 this.error_line = -1;
425 this.check_running = false;
427 // set gobject values
430 this.el.changed.connect( () => {
433 _this.save_button.el.sensitive = true;
434 print("EDITOR CHANGED");
439 // this.get('/LeftPanel.model').changed( str , false);
444 // user defined functions
445 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
447 this.error_line = validate_res.size;
449 if (this.error_line < 1) {
452 var tlines = this.el.get_line_count ();
454 var valiter = validate_res.map_iterator();
455 while (valiter.next()) {
457 // print("get inter\n");
458 var eline = valiter.get_key();
459 if (eline > tlines) {
462 this.el.get_iter_at_line( out iter, eline);
463 //print("mark line\n");
464 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
468 public string toString () {
472 this.el.get_start_iter(out s);
473 this.el.get_end_iter(out e);
474 var ret = this.el.get_text(s,e,true);
475 //print("TO STRING? " + ret);
478 public bool checkSyntax () {
480 if (this.check_running) {
481 print("Check is running\n");
482 if (this.check_queued) {
483 print("Check is already queued");
486 this.check_queued = true;
487 print("Adding queued Check ");
488 GLib.Timeout.add_seconds(1, () => {
489 this.check_queued = false;
498 var str = this.toString();
501 if (this.error_line > 0) {
504 this.el.get_bounds (out start, out end);
506 this.el.remove_source_marks (start, end, null);
508 if (str.length < 1) {
509 print("checkSyntax - empty string?\n");
513 if (_this.file.xtype == "PlainFile") {
515 // assume it's gtk...
516 this.check_running = true;
518 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
522 this.check_running = false;
528 if (_this.file == null) {
531 var p = Palete.factory(_this.file.xtype);
535 this.check_running = true;
538 if (_this.file.language == "js") {
539 this.check_running = false;
540 print("calling validate javascript\n");
541 return this.highlightErrors(p.validateJavascript(
552 print("calling validate vala\n");
553 // clear the buttons.
556 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
563 this.check_running = false;
568 //print("done mark line\n");
570 return true; // at present allow saving - even if it's invalid..
572 public bool highlightErrorsJson (string type, Json.Object obj) {
575 this.el.get_bounds (out start, out end);
577 this.el.remove_source_marks (start, end, type);
580 // we should highlight other types of errors..
582 if (!obj.has_member(type)) {
583 print("Return has no errors\n");
587 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
588 _this.window.windowstate.state != WindowState.State.CODE
594 var err = obj.get_object_member(type);
597 if (_this.file == null) {
601 var valafn = _this.file.path;
603 if (_this.file.xtype != "PlainFile") {
610 var regex = new Regex("\\.bjs$");
614 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
615 } catch (GLib.RegexError e) {
622 if (!err.has_member(valafn)) {
623 print("File path has no errors\n");
627 var lines = err.get_object_member(valafn);
630 if (obj.has_member("line_offset")) {
631 offset = (int)obj.get_int_member("line_offset") + 1;
637 var tlines = this.el.get_line_count () +1;
639 lines.foreach_member((obj, line, node) => {
642 // print("get inter\n");
643 var eline = int.parse(line) - offset;
644 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
647 if (eline > tlines || eline < 0) {
650 this.el.get_iter_at_line( out iter, eline);
651 //print("mark line\n");
652 var msg = "Line: %d".printf(eline+1);
653 var ar = lines.get_array_member(line);
654 for (var i = 0 ; i < ar.get_length(); i++) {
655 msg += (msg.length > 0) ? "\n" : "";
656 msg += ar.get_string_element(i);
660 this.el.create_source_mark(msg, type, iter);