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(1000, () => {
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.0f);
115 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
123 if (file.xtype != "PlainFile") {
129 // find the text for the node..
130 if (ptype == "listener") {
131 val = node.listeners.get(key);
134 val = node.props.get(key);
137 this.key_edit.el.show();
138 this.key_edit.el.text = key;
141 this.view.load( file.toSource() );
142 this.key_edit.el.hide();
147 public class Xcls_Box2 : Object
150 private Editor _this;
156 public Xcls_Box2(Editor _owner )
159 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
163 // set gobject values
164 this.el.homogeneous = false;
165 var child_0 = new Xcls_save_button( _this );
167 this.el.pack_start ( child_0.el , false,false );
168 var child_1 = new Xcls_key_edit( _this );
170 this.el.pack_end ( child_1.el , true,true );
173 // user defined functions
175 public class Xcls_save_button : Object
177 public Gtk.Button el;
178 private Editor _this;
184 public Xcls_save_button(Editor _owner )
187 _this.save_button = this;
188 this.el = new Gtk.Button();
192 // set gobject values
193 this.el.label = "Save";
196 this.el.clicked.connect( () => {
197 _this.saveContents();
201 // user defined functions
204 public class Xcls_key_edit : Object
207 private Editor _this;
213 public Xcls_key_edit(Editor _owner )
216 _this.key_edit = this;
217 this.el = new Gtk.Entry();
221 // set gobject values
224 // user defined functions
228 public class Xcls_RightEditor : Object
230 public Gtk.ScrolledWindow el;
231 private Editor _this;
237 public Xcls_RightEditor(Editor _owner )
240 _this.RightEditor = this;
241 this.el = new Gtk.ScrolledWindow( null, null );
245 // set gobject values
246 var child_0 = new Xcls_view( _this );
248 this.el.add ( child_0.el );
252 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
255 // user defined functions
257 public class Xcls_view : Object
259 public Gtk.SourceView el;
260 private Editor _this;
266 public Xcls_view(Editor _owner )
270 this.el = new Gtk.SourceView();
274 // set gobject values
275 this.el.auto_indent = true;
276 this.el.indent_width = 4;
277 this.el.show_line_marks = true;
278 this.el.insert_spaces_instead_of_tabs = true;
279 this.el.show_line_numbers = true;
280 this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
281 this.el.tab_width = 4;
282 this.el.highlight_current_line = true;
283 var child_0 = new Xcls_buffer( _this );
285 this.el.set_buffer ( child_0.el );
289 var description = Pango.FontDescription.from_string("monospace");
290 description.set_size(8000);
291 this.el.override_font(description);
294 this.el.completion.add_provider(new Palete.CompletionProvider(_this));
295 this.el.completion.unblock_interactive();
296 this.el.completion.select_on_show = true; // select
297 this.el.completion.show_headers = false;
298 this.el.completion.remember_info_visibility = true;
301 var attrs = new Gtk.SourceMarkAttributes();
302 var pink = Gdk.RGBA();
303 pink.parse ( "pink");
304 attrs.set_background ( pink);
305 attrs.set_icon_name ( "process-stop");
306 attrs.query_tooltip_text.connect(( mark) => {
307 //print("tooltip query? %s\n", mark.name);
311 this.el.set_mark_attributes ("ERR", attrs, 1);
313 var wattrs = new Gtk.SourceMarkAttributes();
314 var blue = Gdk.RGBA();
315 blue.parse ( "#ABF4EB");
316 wattrs.set_background ( blue);
317 wattrs.set_icon_name ( "process-stop");
318 wattrs.query_tooltip_text.connect(( mark) => {
319 //print("tooltip query? %s\n", mark.name);
323 this.el.set_mark_attributes ("WARN", wattrs, 1);
327 var dattrs = new Gtk.SourceMarkAttributes();
328 var purple = Gdk.RGBA();
329 purple.parse ( "#EEA9FF");
330 dattrs.set_background ( purple);
331 dattrs.set_icon_name ( "process-stop");
332 dattrs.query_tooltip_text.connect(( mark) => {
333 //print("tooltip query? %s\n", mark.name);
337 this.el.set_mark_attributes ("DEPR", dattrs, 1);
340 this.el.key_release_event.connect( (event) => {
342 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
343 print("SAVE: ctrl-S pressed");
344 _this.saveContents();
347 // print(event.key.keyval)
354 // user defined functions
355 public void load (string str) {
357 // show the help page for the active node..
358 //this.get('/Help').show();
361 // this.get('/BottomPane').el.set_current_page(0);
362 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
363 buf.set_text(str, str.length);
364 buf.set_undo_manager(null);
366 var lm = Gtk.SourceLanguageManager.get_default();
368 if (_this.file != null) {
369 lang = _this.file.language;
371 print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
372 var lg = _this.file.content_type.length > 0 ?
373 lm.guess_language(_this.file.path, _this.file.content_type) :
374 lm.get_language(lang);
377 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg);
379 this.el.insert_spaces_instead_of_tabs = true;
381 print("sourcelanguage = %s\n", lg.name);
382 if (lg.name == "Vala") {
383 this.el.insert_spaces_instead_of_tabs = false;
387 this.el.grab_focus();
388 _this.save_button.el.sensitive = false;
391 public class Xcls_buffer : Object
393 public Gtk.SourceBuffer el;
394 private Editor _this;
398 public bool check_queued;
399 public int error_line;
400 public bool check_running;
403 public Xcls_buffer(Editor _owner )
407 this.el = new Gtk.SourceBuffer( null );
410 this.check_queued = false;
411 this.error_line = -1;
412 this.check_running = false;
414 // set gobject values
417 this.el.changed.connect( () => {
420 _this.save_button.el.sensitive = true;
421 print("EDITOR CHANGED");
426 // this.get('/LeftPanel.model').changed( str , false);
431 // user defined functions
432 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
434 this.error_line = validate_res.size;
436 if (this.error_line < 1) {
439 var tlines = this.el.get_line_count ();
441 var valiter = validate_res.map_iterator();
442 while (valiter.next()) {
444 // print("get inter\n");
445 var eline = valiter.get_key();
446 if (eline > tlines) {
449 this.el.get_iter_at_line( out iter, eline);
450 //print("mark line\n");
451 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
455 public string toString () {
459 this.el.get_start_iter(out s);
460 this.el.get_end_iter(out e);
461 var ret = this.el.get_text(s,e,true);
462 //print("TO STRING? " + ret);
465 public bool checkSyntax () {
467 if (this.check_running) {
468 print("Check is running\n");
469 if (this.check_queued) {
470 print("Check is already queued");
473 this.check_queued = true;
474 print("Adding queued Check ");
475 GLib.Timeout.add_seconds(1, () => {
476 this.check_queued = false;
485 var str = this.toString();
488 if (this.error_line > 0) {
491 this.el.get_bounds (out start, out end);
493 this.el.remove_source_marks (start, end, null);
495 if (str.length < 1) {
496 print("checkSyntax - empty string?\n");
500 if (_this.file.xtype == "PlainFile") {
502 // assume it's gtk...
503 this.check_running = true;
505 if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
509 this.check_running = false;
515 if (_this.file == null) {
518 var p = Palete.factory(_this.file.xtype);
522 this.check_running = true;
525 if (_this.file.language == "js") {
526 this.check_running = false;
527 print("calling validate javascript\n");
528 return this.highlightErrors(p.validateJavascript(
539 print("calling validate vala\n");
540 // clear the buttons.
543 if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
550 this.check_running = false;
555 //print("done mark line\n");
557 return true; // at present allow saving - even if it's invalid..
559 public bool highlightErrorsJson (string type, Json.Object obj) {
562 this.el.get_bounds (out start, out end);
564 this.el.remove_source_marks (start, end, type);
567 // we should highlight other types of errors..
569 if (!obj.has_member(type)) {
570 print("Return has no errors\n");
574 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
575 _this.window.windowstate.state != WindowState.State.CODE
581 var err = obj.get_object_member(type);
584 if (_this.file == null) {
588 var valafn = _this.file.path;
590 if (_this.file.xtype != "PlainFile") {
597 var regex = new Regex("\\.bjs$");
601 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
602 } catch (GLib.RegexError e) {
609 if (!err.has_member(valafn)) {
610 print("File path has no errors\n");
614 var lines = err.get_object_member(valafn);
617 if (obj.has_member("line_offset")) {
618 offset = (int)obj.get_int_member("line_offset") + 1;
624 var tlines = this.el.get_line_count () +1;
626 lines.foreach_member((obj, line, node) => {
629 // print("get inter\n");
630 var eline = int.parse(line) - offset;
631 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
634 if (eline > tlines || eline < 0) {
637 this.el.get_iter_at_line( out iter, eline);
638 //print("mark line\n");
639 var msg = "Line: %d".printf(eline+1);
640 var ar = lines.get_array_member(line);
641 for (var i = 0 ; i < ar.get_length(); i++) {
642 msg += (msg.length > 0) ? "\n" : "";
643 msg += ar.get_string_element(i);
647 this.el.create_source_mark(msg, type, iter);