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) {
103 GLib.Timeout.add(500, () => {
105 var buf = this.view.el.get_buffer();
107 var sbuf = (Gtk.SourceBuffer) buf;
111 sbuf.get_iter_at_line(out iter, line);
112 this.view.el.scroll_to_iter(iter, 0.1f, true, 0.0f, 0.5f);
116 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
124 if (file.xtype != "PlainFile") {
130 // find the text for the node..
131 if (ptype == "listener") {
132 val = node.listeners.get(key);
135 val = node.props.get(key);
138 this.key_edit.el.show();
139 this.key_edit.el.text = key;
142 this.view.load( file.toSource() );
143 this.key_edit.el.hide();
148 public class Xcls_Box2 : Object
151 private Editor _this;
157 public Xcls_Box2(Editor _owner )
160 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
164 // set gobject values
165 this.el.homogeneous = false;
166 var child_0 = new Xcls_save_button( _this );
168 this.el.pack_start ( child_0.el , false,false );
169 var child_1 = new Xcls_key_edit( _this );
171 this.el.pack_end ( child_1.el , true,true );
174 // user defined functions
176 public class Xcls_save_button : Object
178 public Gtk.Button el;
179 private Editor _this;
185 public Xcls_save_button(Editor _owner )
188 _this.save_button = this;
189 this.el = new Gtk.Button();
193 // set gobject values
194 this.el.label = "Save";
197 this.el.clicked.connect( () => {
198 _this.saveContents();
202 // user defined functions
205 public class Xcls_key_edit : Object
208 private Editor _this;
214 public Xcls_key_edit(Editor _owner )
217 _this.key_edit = this;
218 this.el = new Gtk.Entry();
222 // set gobject values
225 // user defined functions
229 public class Xcls_RightEditor : Object
231 public Gtk.ScrolledWindow el;
232 private Editor _this;
238 public Xcls_RightEditor(Editor _owner )
241 _this.RightEditor = this;
242 this.el = new Gtk.ScrolledWindow( null, null );
246 // set gobject values
247 var child_0 = new Xcls_view( _this );
249 this.el.add ( child_0.el );
253 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
256 // user defined functions
258 public class Xcls_view : Object
260 public Gtk.SourceView el;
261 private Editor _this;
267 public Xcls_view(Editor _owner )
271 this.el = new Gtk.SourceView();
275 // set gobject values
276 this.el.auto_indent = true;
277 this.el.indent_width = 4;
278 this.el.show_line_marks = true;
279 this.el.insert_spaces_instead_of_tabs = true;
280 this.el.show_line_numbers = true;
281 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
282 this.el.tab_width = 4;
283 this.el.highlight_current_line = true;
284 var child_0 = new Xcls_buffer( _this );
286 this.el.set_buffer ( child_0.el );
290 var description = Pango.FontDescription.from_string("monospace");
291 description.set_size(8000);
292 this.el.override_font(description);
295 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
296 this.el.completion.unblock_interactive();
297 this.el.completion.select_on_show = true; // select
298 this.el.completion.show_headers = false;
299 this.el.completion.remember_info_visibility = true;
302 var attrs = new Gtk.SourceMarkAttributes();
303 var pink = Gdk.RGBA();
304 pink.parse ( "pink");
305 attrs.set_background ( pink);
306 attrs.set_icon_name ( "process-stop");
307 attrs.query_tooltip_text.connect(( mark) => {
308 //print("tooltip query? %s\n", mark.name);
312 this.el.set_mark_attributes ("ERR", attrs, 1);
314 var wattrs = new Gtk.SourceMarkAttributes();
315 var blue = Gdk.RGBA();
316 blue.parse ( "#ABF4EB");
317 wattrs.set_background ( blue);
318 wattrs.set_icon_name ( "process-stop");
319 wattrs.query_tooltip_text.connect(( mark) => {
320 //print("tooltip query? %s\n", mark.name);
324 this.el.set_mark_attributes ("WARN", wattrs, 1);
328 var dattrs = new Gtk.SourceMarkAttributes();
329 var purple = Gdk.RGBA();
330 purple.parse ( "#EEA9FF");
331 dattrs.set_background ( purple);
332 dattrs.set_icon_name ( "process-stop");
333 dattrs.query_tooltip_text.connect(( mark) => {
334 //print("tooltip query? %s\n", mark.name);
338 this.el.set_mark_attributes ("DEPR", dattrs, 1);
341 this.el.key_release_event.connect( (event) => {
343 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
344 print("SAVE: ctrl-S pressed");
345 _this.saveContents();
348 // print(event.key.keyval)
355 // user defined functions
356 public void load (string str) {
358 // show the help page for the active node..
359 //this.get('/Help').show();
362 // this.get('/BottomPane').el.set_current_page(0);
363 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
364 buf.set_text(str, str.length);
365 buf.set_undo_manager(null);
367 var lm = Gtk.SourceLanguageManager.get_default();
369 if (_this.file != null) {
370 lang = _this.file.language;
372 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
373 var lg = _this.file.content_type.length > 0 ?
374 lm.guess_language(_this.file.path, _this.file.content_type) :
375 lm.get_language(lang);
378 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
380 this.el.insert_spaces_instead_of_tabs = true;
382 print("sourcelanguage = %s\n", lg.name);
383 if (lg.name == "Vala") {
384 this.el.insert_spaces_instead_of_tabs = false;
388 this.el.grab_focus();
389 _this.save_button.el.sensitive = false;
392 public class Xcls_buffer : Object
394 public Gtk.SourceBuffer el;
395 private Editor _this;
399 public bool check_queued;
400 public int error_line;
401 public bool check_running;
404 public Xcls_buffer(Editor _owner )
408 this.el = new Gtk.SourceBuffer( null );
411 this.check_queued = false;
412 this.error_line = -1;
413 this.check_running = false;
415 // set gobject values
418 this.el.changed.connect( () => {
421 _this.save_button.el.sensitive = true;
422 print("EDITOR CHANGED");
427 // this.get('/LeftPanel.model').changed( str , false);
432 // user defined functions
433 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
435 this.error_line = validate_res.size;
437 if (this.error_line < 1) {
440 var tlines = this.el.get_line_count ();
442 var valiter = validate_res.map_iterator();
443 while (valiter.next()) {
445 // print("get inter\n");
446 var eline = valiter.get_key();
447 if (eline > tlines) {
450 this.el.get_iter_at_line( out iter, eline);
451 //print("mark line\n");
452 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
456 public string toString () {
460 this.el.get_start_iter(out s);
461 this.el.get_end_iter(out e);
462 var ret = this.el.get_text(s,e,true);
463 //print("TO STRING? " + ret);
466 public bool checkSyntax () {
468 if (this.check_running) {
469 print("Check is running\n");
470 if (this.check_queued) {
471 print("Check is already queued");
474 this.check_queued = true;
475 print("Adding queued Check ");
476 GLib.Timeout.add_seconds(1, () => {
477 this.check_queued = false;
486 var str = this.toString();
489 if (this.error_line > 0) {
492 this.el.get_bounds (out start, out end);
494 this.el.remove_source_marks (start, end, null);
496 if (str.length < 1) {
497 print("checkSyntax - empty string?\n");
501 if (_this.file.xtype == "PlainFile") {
503 // assume it's gtk...
504 this.check_running = true;
506 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
510 this.check_running = false;
516 if (_this.file == null) {
519 var p = Palete.factory(_this.file.xtype);
523 this.check_running = true;
526 if (_this.file.language == "js") {
527 this.check_running = false;
528 print("calling validate javascript\n");
529 return this.highlightErrors(p.validateJavascript(
540 print("calling validate vala\n");
541 // clear the buttons.
544 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
551 this.check_running = false;
556 //print("done mark line\n");
558 return true; // at present allow saving - even if it's invalid..
560 public bool highlightErrorsJson (string type, Json.Object obj) {
563 this.el.get_bounds (out start, out end);
565 this.el.remove_source_marks (start, end, type);
568 // we should highlight other types of errors..
570 if (!obj.has_member(type)) {
571 print("Return has no errors\n");
575 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
576 _this.window.windowstate.state != WindowState.State.CODE
582 var err = obj.get_object_member(type);
585 if (_this.file == null) {
589 var valafn = _this.file.path;
591 if (_this.file.xtype != "PlainFile") {
598 var regex = new Regex("\\.bjs$");
602 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
603 } catch (GLib.RegexError e) {
610 if (!err.has_member(valafn)) {
611 print("File path has no errors\n");
615 var lines = err.get_object_member(valafn);
618 if (obj.has_member("line_offset")) {
619 offset = (int)obj.get_int_member("line_offset") + 1;
625 var tlines = this.el.get_line_count () +1;
627 lines.foreach_member((obj, line, node) => {
630 // print("get inter\n");
631 var eline = int.parse(line) - offset;
632 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
635 if (eline > tlines || eline < 0) {
638 this.el.get_iter_at_line( out iter, eline);
639 //print("mark line\n");
640 var msg = "Line: %d".printf(eline+1);
641 var ar = lines.get_array_member(line);
642 for (var i = 0 ; i < ar.get_length(); i++) {
643 msg += (msg.length > 0) ? "\n" : "";
644 msg += ar.get_string_element(i);
648 this.el.create_source_mark(msg, type, iter);