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.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
123 this.searchcontext .set_highlight(true);
124 s.set_search_text(txt);
125 return this.searchcontext.get_occurrences_count();
131 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
139 if (file.xtype != "PlainFile") {
145 // find the text for the node..
146 if (ptype == "listener") {
147 val = node.listeners.get(key);
150 val = node.props.get(key);
153 this.key_edit.el.show();
154 this.key_edit.el.text = key;
157 this.view.load( file.toSource() );
158 this.key_edit.el.hide();
163 public class Xcls_Box2 : Object
166 private Editor _this;
172 public Xcls_Box2(Editor _owner )
175 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
179 // set gobject values
180 this.el.homogeneous = false;
181 var child_0 = new Xcls_save_button( _this );
183 this.el.pack_start ( child_0.el , false,false );
184 var child_1 = new Xcls_key_edit( _this );
186 this.el.pack_end ( child_1.el , true,true );
189 // user defined functions
191 public class Xcls_save_button : Object
193 public Gtk.Button el;
194 private Editor _this;
200 public Xcls_save_button(Editor _owner )
203 _this.save_button = this;
204 this.el = new Gtk.Button();
208 // set gobject values
209 this.el.label = "Save";
212 this.el.clicked.connect( () => {
213 _this.saveContents();
217 // user defined functions
220 public class Xcls_key_edit : Object
223 private Editor _this;
229 public Xcls_key_edit(Editor _owner )
232 _this.key_edit = this;
233 this.el = new Gtk.Entry();
237 // set gobject values
240 // user defined functions
244 public class Xcls_RightEditor : Object
246 public Gtk.ScrolledWindow el;
247 private Editor _this;
253 public Xcls_RightEditor(Editor _owner )
256 _this.RightEditor = this;
257 this.el = new Gtk.ScrolledWindow( null, null );
261 // set gobject values
262 var child_0 = new Xcls_view( _this );
264 this.el.add ( child_0.el );
268 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
271 // user defined functions
273 public class Xcls_view : Object
275 public Gtk.SourceView el;
276 private Editor _this;
282 public Xcls_view(Editor _owner )
286 this.el = new Gtk.SourceView();
290 // set gobject values
291 this.el.auto_indent = true;
292 this.el.indent_width = 4;
293 this.el.show_line_marks = true;
294 this.el.insert_spaces_instead_of_tabs = true;
295 this.el.show_line_numbers = true;
296 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
297 this.el.tab_width = 4;
298 this.el.highlight_current_line = true;
299 var child_0 = new Xcls_buffer( _this );
301 this.el.set_buffer ( child_0.el );
305 var description = Pango.FontDescription.from_string("monospace");
306 description.set_size(8000);
307 this.el.override_font(description);
310 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
311 this.el.completion.unblock_interactive();
312 this.el.completion.select_on_show = true; // select
313 this.el.completion.show_headers = false;
314 this.el.completion.remember_info_visibility = true;
317 var attrs = new Gtk.SourceMarkAttributes();
318 var pink = Gdk.RGBA();
319 pink.parse ( "pink");
320 attrs.set_background ( pink);
321 attrs.set_icon_name ( "process-stop");
322 attrs.query_tooltip_text.connect(( mark) => {
323 //print("tooltip query? %s\n", mark.name);
327 this.el.set_mark_attributes ("ERR", attrs, 1);
329 var wattrs = new Gtk.SourceMarkAttributes();
330 var blue = Gdk.RGBA();
331 blue.parse ( "#ABF4EB");
332 wattrs.set_background ( blue);
333 wattrs.set_icon_name ( "process-stop");
334 wattrs.query_tooltip_text.connect(( mark) => {
335 //print("tooltip query? %s\n", mark.name);
339 this.el.set_mark_attributes ("WARN", wattrs, 1);
343 var dattrs = new Gtk.SourceMarkAttributes();
344 var purple = Gdk.RGBA();
345 purple.parse ( "#EEA9FF");
346 dattrs.set_background ( purple);
347 dattrs.set_icon_name ( "process-stop");
348 dattrs.query_tooltip_text.connect(( mark) => {
349 //print("tooltip query? %s\n", mark.name);
353 this.el.set_mark_attributes ("DEPR", dattrs, 1);
356 this.el.key_release_event.connect( (event) => {
358 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
359 print("SAVE: ctrl-S pressed");
360 _this.saveContents();
363 // print(event.key.keyval)
370 // user defined functions
371 public void load (string str) {
373 // show the help page for the active node..
374 //this.get('/Help').show();
377 // this.get('/BottomPane').el.set_current_page(0);
378 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
379 buf.set_text(str, str.length);
380 buf.set_undo_manager(null);
382 var lm = Gtk.SourceLanguageManager.get_default();
384 if (_this.file != null) {
385 lang = _this.file.language;
387 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
388 var lg = _this.file.content_type.length > 0 ?
389 lm.guess_language(_this.file.path, _this.file.content_type) :
390 lm.get_language(lang);
393 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
395 this.el.insert_spaces_instead_of_tabs = true;
397 print("sourcelanguage = %s\n", lg.name);
398 if (lg.name == "Vala") {
399 this.el.insert_spaces_instead_of_tabs = false;
403 this.el.grab_focus();
404 _this.save_button.el.sensitive = false;
407 public class Xcls_buffer : Object
409 public Gtk.SourceBuffer el;
410 private Editor _this;
414 public bool check_queued;
415 public int error_line;
416 public bool check_running;
419 public Xcls_buffer(Editor _owner )
423 this.el = new Gtk.SourceBuffer( null );
426 this.check_queued = false;
427 this.error_line = -1;
428 this.check_running = false;
430 // set gobject values
433 this.el.changed.connect( () => {
436 _this.save_button.el.sensitive = true;
437 print("EDITOR CHANGED");
442 // this.get('/LeftPanel.model').changed( str , false);
447 // user defined functions
448 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
450 this.error_line = validate_res.size;
452 if (this.error_line < 1) {
455 var tlines = this.el.get_line_count ();
457 var valiter = validate_res.map_iterator();
458 while (valiter.next()) {
460 // print("get inter\n");
461 var eline = valiter.get_key();
462 if (eline > tlines) {
465 this.el.get_iter_at_line( out iter, eline);
466 //print("mark line\n");
467 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
471 public string toString () {
475 this.el.get_start_iter(out s);
476 this.el.get_end_iter(out e);
477 var ret = this.el.get_text(s,e,true);
478 //print("TO STRING? " + ret);
481 public bool checkSyntax () {
483 if (this.check_running) {
484 print("Check is running\n");
485 if (this.check_queued) {
486 print("Check is already queued");
489 this.check_queued = true;
490 print("Adding queued Check ");
491 GLib.Timeout.add_seconds(1, () => {
492 this.check_queued = false;
501 var str = this.toString();
504 if (this.error_line > 0) {
507 this.el.get_bounds (out start, out end);
509 this.el.remove_source_marks (start, end, null);
511 if (str.length < 1) {
512 print("checkSyntax - empty string?\n");
516 if (_this.file.xtype == "PlainFile") {
518 // assume it's gtk...
519 this.check_running = true;
521 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
525 this.check_running = false;
531 if (_this.file == null) {
534 var p = Palete.factory(_this.file.xtype);
538 this.check_running = true;
541 if (_this.file.language == "js") {
542 this.check_running = false;
543 print("calling validate javascript\n");
544 return this.highlightErrors(p.validateJavascript(
555 print("calling validate vala\n");
556 // clear the buttons.
559 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
566 this.check_running = false;
571 //print("done mark line\n");
573 return true; // at present allow saving - even if it's invalid..
575 public bool highlightErrorsJson (string type, Json.Object obj) {
578 this.el.get_bounds (out start, out end);
580 this.el.remove_source_marks (start, end, type);
583 // we should highlight other types of errors..
585 if (!obj.has_member(type)) {
586 print("Return has no errors\n");
590 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
591 _this.window.windowstate.state != WindowState.State.CODE
597 var err = obj.get_object_member(type);
600 if (_this.file == null) {
604 var valafn = _this.file.path;
606 if (_this.file.xtype != "PlainFile") {
613 var regex = new Regex("\\.bjs$");
617 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
618 } catch (GLib.RegexError e) {
625 if (!err.has_member(valafn)) {
626 print("File path has no errors\n");
630 var lines = err.get_object_member(valafn);
633 if (obj.has_member("line_offset")) {
634 offset = (int)obj.get_int_member("line_offset") + 1;
640 var tlines = this.el.get_line_count () +1;
642 lines.foreach_member((obj, line, node) => {
645 // print("get inter\n");
646 var eline = int.parse(line) - offset;
647 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
650 if (eline > tlines || eline < 0) {
653 this.el.get_iter_at_line( out iter, eline);
654 //print("mark line\n");
655 var msg = "Line: %d".printf(eline+1);
656 var ar = lines.get_array_member(line);
657 for (var i = 0 ; i < ar.get_length(); i++) {
658 msg += (msg.length > 0) ? "\n" : "";
659 msg += ar.get_string_element(i);
663 this.el.create_source_mark(msg, type, iter);