Fix #8104 - update nav tree by comparing changes
[roobuilder] / src / Builder4 / Editor.vala
index 94dd0ca..d54bc2a 100644 (file)
@@ -29,6 +29,7 @@ public class Editor : Object
        public Xcls_navigationwindow navigationwindow;
        public Xcls_navigation navigation;
        public Xcls_navigationselmodel navigationselmodel;
+       public Xcls_navigationsort navigationsort;
        public Xcls_navliststore navliststore;
 
                // my vars (def)
@@ -137,8 +138,15 @@ public class Editor : Object
        public void show (JsRender.JsRender file, JsRender.Node? node, JsRender.NodeProp? prop)
        {
            this.reset();
+           if (this.file != null) {
+               this.file.navigation_tree_updated.disconnect(
+                       _this.navigation.show
+               );
+           }
            this.file = file;    
-           
+           this.file.navigation_tree_updated.connect(
+                       _this.navigation.show
+               );
            if (file.xtype != "PlainFile") {
                this.prop = prop;
                this.node = node;
@@ -153,12 +161,13 @@ public class Editor : Object
            
            } else {
                this.view.load(        file.toSource() );
-                this.updateErrorMarks();
+               this.updateErrorMarks();
                this.close_btn.el.hide();
                var ls = file.getLanguageServer();
-               ls.documentSymbols.begin(file, (a,o) => {
-                       _this.navigation.show(ls.documentSymbols.end(o)); 
-               });
+               ls.queueDocumentSymbols(file);
+               ////ls.documentSymbols.begin(file, (a,o) => {
+               //      _this.navigation.show(ls.documentSymbols.end(o)); 
+              //});
                //documentSymbols
                
            }
@@ -926,6 +935,19 @@ public class Editor : Object
                        buf.create_tag ("DEPR", "weight", Pango.Weight.BOLD, "background", "#EEA9FF");
 
                        //listeners
+                       this.el.cursor_moved.connect( ( ) => {
+                       
+                               Gtk.TextIter iter;
+                               this.el.get_iter_at_offset (
+                                               out iter, this.el.cursor_position);
+                       
+                               _this.navigation.updateSelectedLine(
+                                               (uint)iter.get_line(),
+                                               (uint)iter.get_line_offset()
+                                       );
+                       
+                       
+                       });
                        this.el.changed.connect( () => {
                            // check syntax??
                            // ??needed..??
@@ -1078,7 +1100,7 @@ public class Editor : Object
                        _this.file.setSource(str);
                            BuilderApplication.showSpinner("appointment soon","document change pending");
                        _this.file.getLanguageServer().document_change(_this.file);
-               
+                               _this.file.getLanguageServer().queueDocumentSymbols(_this.file);
                        _this.file.setSource(oldcode);
                        
                                 
@@ -1206,9 +1228,7 @@ public class Editor : Object
                                } 
                            //_this.view.el.show_completion();
                           // print(event.key.keyval)
-                          
-                          
-                          
+                           
                            
                            return;
                         
@@ -1760,6 +1780,8 @@ public class Editor : Object
 
 
                        // my vars (def)
+               public int last_selected_line;
+               public Gtk.Widget? selected_row;
 
                // ctor
                public Xcls_navigation(Editor _owner )
@@ -1770,25 +1792,87 @@ public class Editor : Object
                        this.el = new Gtk.ColumnView( _this.navigationselmodel.el );
 
                        // my vars (dec)
+                       this.last_selected_line = -1;
+                       this.selected_row = null;
 
                        // set gobject values
                        this.el.name = "editor-navigation";
                        var child_2 = new Xcls_ColumnViewColumn29( _this );
                        child_2.ref();
                        this.el.append_column( child_2.el );
-                       var child_3 = new Xcls_GestureClick34( _this );
+                       var child_3 = new Xcls_GestureClick38( _this );
                        child_3.ref();
                        this.el.add_controller(  child_3.el );
                }
 
                // user defined functions
+               public Gtk.Widget? getRowWidgetAt (double x,  double  y, out string pos) {
+               
+                       pos = "";
+                       var w = this.el.pick(x, y, Gtk.PickFlags.DEFAULT);
+                       //GLib.debug("got widget %s", w == null ? "nothing" : w.get_type().name());
+                       if (w == null) {
+                               return null;
+                       }
+                       
+                       var row= w.get_ancestor(GLib.Type.from_name("GtkColumnViewRowWidget"));
+                       if (row == null) {
+                               return null;
+                       }
+                       
+                       //GLib.debug("got colview %s", row == null ? "nothing" : row.get_type().name());
+                        
+                
+                       
+                       //GLib.debug("row number is %d", rn);
+                       //GLib.debug("click %d, %d", (int)x, (int)y);
+                       // above or belw
+                       Graphene.Rect  bounds;
+                       row.compute_bounds(this.el, out bounds);
+                       //GLib.debug("click x=%d, y=%d, w=%d, h=%d", 
+                       //      (int)bounds.get_x(), (int)bounds.get_y(),
+                       //      (int)bounds.get_width(), (int)bounds.get_height()
+                       //      );
+                       var ypos = y - bounds.get_y();
+                       //GLib.debug("rel ypos = %d", (int)ypos);       
+                       var rpos = 100.0 * (ypos / bounds.get_height());
+                       //GLib.debug("rel pos = %d %%", (int)rpos);
+                       pos = "over";
+                       
+                       if (rpos > 80) {
+                               pos = "below";
+                       } else if (rpos < 20) {
+                               pos = "above";
+                       } 
+                       return row;
+                }
                public void show (Gee.ArrayList<Lsp.DocumentSymbol> syms) {
                        _this.navigationwindow.el.show();
-                       _this.navliststore.el.remove_all();
+                       //_this.navliststore.el.remove_all();
+                       
+                       
+                       var ls  = new GLib.ListStore(typeof(Lsp.DocumentSymbol));
+                       
                        foreach(var sym in syms) {
-                               _this.navliststore.el.append(sym);
+                               ls.append(sym);
                        }
-                       
+                       Lsp.DocumentSymbol.copyList(ls, _this.navliststore.el);
+                       //_this.navliststore.el.append(sym);
+                       this.last_selected_line = -1;
+                       GLib.Idle.add(() => {
+               
+                               Gtk.TextIter iter;
+                               _this.buffer.el.get_iter_at_offset (
+                                               out iter, _this.buffer.el.cursor_position);
+                               
+                               GLib.debug("idle update scroll %d, %d", iter.get_line(),
+                                               iter.get_line_offset());
+                               this.updateSelectedLine(
+                                               (uint)iter.get_line(),
+                                               (uint)iter.get_line_offset()
+                               );
+                               return false;
+                       });
                
                }
                public int getRowAt (double x,  double  y, out string pos) {
@@ -1837,6 +1921,46 @@ public class Editor : Object
                        } 
                        return rn;
                 }
+               public void updateSelectedLine (uint line, uint chr) {
+                       if (line == this.last_selected_line) {
+                               return;
+                       }
+                       GLib.debug("select line %d", (int)line);
+                       this.last_selected_line = (int)line;
+                       
+                       
+                       var new_row = -1;
+                       var sym = _this.navliststore.symbolAtLine(line, chr);
+                       if (sym != null) {
+                               new_row = _this.navigationsort.getRowFromSymbol(sym);
+                               GLib.debug("select line %d - row found %d", (int)line, new_row);
+                       } else {
+                               GLib.debug(" no symbol found at line %d", (int)line);
+                       }
+                       
+                       if (this.selected_row != null) { 
+                               GLib.debug(" remove selected row");
+                               this.selected_row.remove_css_class("selected-row");
+                       }
+                       this.selected_row  = null;
+                       if (new_row > -1) {
+                               this.el.scroll_to(new_row,null,Gtk.ListScrollFlags.NONE, null);
+                               var row = sym.get_data<Gtk.Widget>("widget");
+                               if (row != null) {
+                                       GLib.debug(" Add selected row");
+                                       
+                                       row.add_css_class("selected-row");
+                                       this.selected_row = row;
+               
+                                       
+                               } else {
+                                       GLib.debug("could not find widget on row %d", new_row);
+                               }
+               
+                       }
+               
+               
+               }
        }
        public class Xcls_ColumnViewColumn29 : Object
        {
@@ -1921,7 +2045,12 @@ public class Editor : Object
                                var lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();
                                var sym = (Lsp.DocumentSymbol) lr.get_item();
                                
-                               GLib.debug("got %d children for %s" , (int)sym.children.get_n_items(), sym.name);
+                               sym.set_data<Gtk.Widget>("widget", expand.get_parent());
+                               expand.get_parent().get_parent().set_data<Lsp.DocumentSymbol>("symbol", sym);
+                               
+                               //GLib.debug("save sym on %s", expand.get_parent().get_parent().get_type().name());
+                               
+                               //GLib.debug("got %d children for %s" , (int)sym.children.get_n_items(), sym.name);
                            
                            expand.set_hide_expander( sym.children.get_n_items()  < 1);
                                expand.set_list_row(lr);
@@ -1961,9 +2090,8 @@ public class Editor : Object
                {
                        _this = _owner;
                        _this.navigationselmodel = this;
-                       var child_1 = new Xcls_SortListModel60( _this );
-                       child_1.ref();
-                       this.el = new Gtk.NoSelection( child_1.el );
+                       new Xcls_navigationsort( _this );
+                       this.el = new Gtk.NoSelection( _this.navigationsort.el );
 
                        // my vars (dec)
 
@@ -1971,19 +2099,8 @@ public class Editor : Object
                }
 
                // user defined functions
-               public Lsp.DocumentSymbol? getSymoblAt (uint row) {
-               
-                  var tr = (Gtk.TreeListRow)this.el.get_item(row);
-                  
-                  var a = tr.get_item();;   
-                  GLib.debug("get_item (2) = %s", a.get_type().name());
-                       
-                  
-                  return (Lsp.DocumentSymbol)tr.get_item();
-                        
-               }
        }
-       public class Xcls_SortListModel60 : Object
+       public class Xcls_navigationsort : Object
        {
                public Gtk.SortListModel el;
                private Editor  _this;
@@ -1992,12 +2109,13 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_SortListModel60(Editor _owner )
+               public Xcls_navigationsort(Editor _owner )
                {
                        _this = _owner;
-                       var child_1 = new Xcls_TreeListModel171( _this );
+                       _this.navigationsort = this;
+                       var child_1 = new Xcls_TreeListModel33( _this );
                        child_1.ref();
-                       var child_2 = new Xcls_TreeListRowSorter209( _this );
+                       var child_2 = new Xcls_TreeListRowSorter35( _this );
                        child_2.ref();
                        this.el = new Gtk.SortListModel( child_1.el, child_2.el );
 
@@ -2007,8 +2125,30 @@ public class Editor : Object
                }
 
                // user defined functions
+               public int getRowFromSymbol (Lsp.DocumentSymbol sym) {
+               
+                       for (var i=0;i < this.el.get_n_items(); i++) {
+                               var tr = (Gtk.TreeListRow)this.el.get_item(i);
+                          
+                               if (sym.equals( (Lsp.DocumentSymbol)tr.get_item())) {
+                                       return i;
+                               }
+                       }
+                       return -1;
+               }
+               public Lsp.DocumentSymbol? getSymbolAt (uint row) {
+               
+                  var tr = (Gtk.TreeListRow)this.el.get_item(row);
+                  
+                  var a = tr.get_item();;   
+                  GLib.debug("get_item (2) = %s", a.get_type().name());
+                       
+                  
+                  return (Lsp.DocumentSymbol)tr.get_item();
+                        
+               }
        }
-       public class Xcls_TreeListModel171 : Object
+       public class Xcls_TreeListModel33 : Object
        {
                public Gtk.TreeListModel el;
                private Editor  _this;
@@ -2017,7 +2157,7 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_TreeListModel171(Editor _owner )
+               public Xcls_TreeListModel33(Editor _owner )
                {
                        _this = _owner;
                        new Xcls_navliststore( _this );
@@ -2055,10 +2195,28 @@ public class Editor : Object
                }
 
                // user defined functions
+               public Lsp.DocumentSymbol? symbolAtLine (uint line, uint chr) {
+                
+                       
+                       for(var i = 0; i < this.el.get_n_items();i++) {
+                               var el = (Lsp.DocumentSymbol)this.el.get_item(i);
+                               //GLib.debug("Check sym %s : %d-%d",
+                               //      el.name , (int)el.range.start.line,
+                               //      (int)el.range.end.line
+                               //);
+                               var ret = el.containsLine(line,chr);
+                               if (ret != null) {
+                                       return ret;
+                               }
+                               
+                       }
+                       
+                       return null;
+               }
        }
 
 
-       public class Xcls_TreeListRowSorter209 : Object
+       public class Xcls_TreeListRowSorter35 : Object
        {
                public Gtk.TreeListRowSorter el;
                private Editor  _this;
@@ -2067,10 +2225,10 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_TreeListRowSorter209(Editor _owner )
+               public Xcls_TreeListRowSorter35(Editor _owner )
                {
                        _this = _owner;
-                       var child_1 = new Xcls_StringSorter217( _this );
+                       var child_1 = new Xcls_StringSorter36( _this );
                        child_1.ref();
                        this.el = new Gtk.TreeListRowSorter( child_1.el );
 
@@ -2081,7 +2239,7 @@ public class Editor : Object
 
                // user defined functions
        }
-       public class Xcls_StringSorter217 : Object
+       public class Xcls_StringSorter36 : Object
        {
                public Gtk.StringSorter el;
                private Editor  _this;
@@ -2090,10 +2248,10 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_StringSorter217(Editor _owner )
+               public Xcls_StringSorter36(Editor _owner )
                {
                        _this = _owner;
-                       var child_1 = new Xcls_PropertyExpression224( _this );
+                       var child_1 = new Xcls_PropertyExpression37( _this );
                        child_1.ref();
                        this.el = new Gtk.StringSorter( child_1.el );
 
@@ -2104,7 +2262,7 @@ public class Editor : Object
 
                // user defined functions
        }
-       public class Xcls_PropertyExpression224 : Object
+       public class Xcls_PropertyExpression37 : Object
        {
                public Gtk.PropertyExpression el;
                private Editor  _this;
@@ -2113,7 +2271,7 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_PropertyExpression224(Editor _owner )
+               public Xcls_PropertyExpression37(Editor _owner )
                {
                        _this = _owner;
                        this.el = new Gtk.PropertyExpression( typeof(Lsp.DocumentSymbol), null, "sort_key" );
@@ -2130,7 +2288,7 @@ public class Editor : Object
 
 
 
-       public class Xcls_GestureClick34 : Object
+       public class Xcls_GestureClick38 : Object
        {
                public Gtk.GestureClick el;
                private Editor  _this;
@@ -2139,7 +2297,7 @@ public class Editor : Object
                        // my vars (def)
 
                // ctor
-               public Xcls_GestureClick34(Editor _owner )
+               public Xcls_GestureClick38(Editor _owner )
                {
                        _this = _owner;
                        this.el = new Gtk.GestureClick();
@@ -2151,13 +2309,15 @@ public class Editor : Object
                        //listeners
                        this.el.pressed.connect( (n_press, x, y) => {
                                string pos;
-                               var row = _this.navigation.getRowAt(x,y, out pos );
-                           if (row < 0) {
+                               var row = _this.navigation.getRowWidgetAt(x,y, out pos );
+                       
+                           if (row == null) {
                                    GLib.debug("no row selected items");
                                    return;
                            }
+                               GLib.debug("got click on %s", row.get_type().name());    
                            //Lsp.DocumentSymbol
-                           var sym =   _this.navigationselmodel.getSymoblAt(row);
+                           var sym =  row.get_data<Lsp.DocumentSymbol>("symbol");
                            if (sym == null) {
                                return;
                                }
@@ -2172,8 +2332,15 @@ public class Editor : Object
                                        "character" : 39
                                      }
                                    },
-                                   */
+                               */
+                            GLib.debug("goto line %d",   (int)sym.range.start.line); 
                            _this.scroll_to_line((int)sym.range.start.line);
+                           Gtk.TextIter iter;
+                           _this.buffer.el.get_iter_at_line_offset(out iter, 
+                               (int)sym.range.start.line,
+                               (int)sym.range.start.character
+                               );
+                           _this.buffer.el.place_cursor(iter);
                                
                        });
                }