reduce warnings
[roobuilder] / src / Lsp.vala
index d13b916..fbc1e9a 100644 (file)
@@ -62,6 +62,12 @@ namespace Lsp {
     }
 
     public  class Position : Object, Gee.Comparable<Position> {
+        
+        public Position(uint line, uint chr)
+        {
+               this.line = line;
+               this.character = chr;
+        }
         /**
          * Line position in a document (zero-based).
          */
@@ -78,12 +84,16 @@ namespace Lsp {
         public uint character { get; set; default = -1; }
 
         public int compare_to (Position other) {
+             
             return line > other.line ? 1 :
                 (line == other.line ?
                  (character > other.character ? 1 :
                   (character == other.character ? 0 : -1)) : -1);
         }
-
+        public bool equals(Position o) {
+               return o.line == this.line && o.character == this.character;
+       }
+               
         public string to_string () {
             return @"$line:$character";
         }
@@ -98,20 +108,14 @@ namespace Lsp {
         }
 
         public Position translate (int dl = 0, int dc = 0) {
-            return new Position () {
-                line = this.line + dl,
-                character = this.character + dc
-            };
+            return new Position (this.character + dc, this.character + dc) ;
         }
     }
 
     public class Range : Object, Gee.Hashable<Range>, Gee.Comparable<Range> {
         
         public Range.simple(uint line, uint pos) {
-               var p =  new Position () {
-                line = line,
-                character = pos
-            };
+               var p =  new Position (line,pos);
                this.start = p;
                this.end = p;
                
@@ -148,7 +152,7 @@ namespace Lsp {
 
         public bool equal_to (Range other) { return this.to_string () == other.to_string (); }
                public bool equals (Range o) {
-                       return this.filename == o.filename && this.start == o.start && this.end == o.end;
+                       return this.filename == o.filename && this.start.equals(o.start) && this.end.equals(o.end);
                }
 
         public int compare_to (Range other) {
@@ -169,7 +173,10 @@ namespace Lsp {
         }
 
         public bool contains (Position pos) {
-            return start.compare_to (pos) <= 0 && pos.compare_to (end) <= 0;
+               
+            var ret =  start.compare_to (pos) <= 0 && pos.compare_to (end) <= 0;
+           // GLib.debug( "range contains %d  (%d-%d) %s", (int)pos.line, (int)start.line, (int)end.line, ret ? "Y" : "N");
+            return ret;
         }
        
     }
@@ -229,7 +236,11 @@ namespace Lsp {
                
         }
         public bool equals(Lsp.Diagnostic o) {
-               return this.range.equals(o.range) && this.severity == o.severity && this.message == o.message;
+                       var ret = this.range.equals(o.range) && this.severity == o.severity && this.message == o.message;
+               //GLib.debug("compare %s  (%s == %s)", ret ? "YES" : "NO", this.to_string(), o.to_string()); 
+               
+               
+               return ret;
         }
         public string to_string()
         {
@@ -328,25 +339,18 @@ namespace Lsp {
     }
 
     public class DocumentSymbol : Object, Json.Serializable {
-        private Vala.SourceReference? _source_reference;
-        public string name { get; set; }
-        public string? detail { get; set; }
-        public SymbolKind kind { get; set; }
-        public bool deprecated { get; set; }
-        private Range? _initial_range;
-        public Range range {
-            owned get {
-                if (_initial_range == null)
-                    _initial_range = new Range.from_sourceref (children.first ()._source_reference);
-                
-                return children.fold<Range> ((child, current_range) => current_range.union (child.range), _initial_range);
-            }
-        }
-        public Range selectionRange { get; set; }
-        public Gee.List<DocumentSymbol> children { get; private set; default = new Gee.LinkedList<DocumentSymbol> (); }
-        public string? parent_name;
 
-        private DocumentSymbol () {}
+               public string name { get; set; }
+               public string detail { get; set; default = ""; }
+               public SymbolKind kind { get; set; }
+               public bool deprecated { get; set; }
+
+               public Range range { get; set; } 
+               public Range selectionRange { get; set; }
+               public GLib.ListStore children { get;  set; default = new GLib.ListStore(typeof(DocumentSymbol)); }
+               public string? parent_name;
+
+               private DocumentSymbol () {}
 
         /**
          * @param type the data type containing this symbol, if there was one (not available for Namespaces, for example)
@@ -388,34 +392,35 @@ namespace Lsp {
         }
 
         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
-            if (property_name != "children")
+           // if (property_name != "children")
                 return default_serialize_property (property_name, value, pspec);
-            var node = new Json.Node (Json.NodeType.ARRAY);
+            /*var node = new Json.Node (Json.NodeType.ARRAY);
             node.init_array (new Json.Array ());
             var array = node.get_array ();
             foreach (var child in children)
                 array.add_element (Json.gobject_serialize (child));
             return node;
+            */
         }
 
         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) 
            {
-               
+               GLib.debug("deserialise property %s" , property_name);
                if (property_name != "children") {
                    return default_deserialize_property (property_name, out value, pspec, property_node);
                }
-            value = GLib.Value (typeof(Gee.ArrayList));
+            value = GLib.Value (typeof(GLib.ListStore));
                if (property_node.get_node_type () != Json.NodeType.ARRAY) {
-                   warning ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
+                   GLib.debug ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
                    return false;
                }
-                        
-               var arguments = new Gee.ArrayList<DocumentSymbol>();
+                       GLib.debug("got child length of %d", (int) property_node.get_array ().get_length());
+               var arguments = new GLib.ListStore(typeof(DocumentSymbol));
 
                property_node.get_array ().foreach_element ((array, index, element) => {
                    
                        var add= Json.gobject_deserialize ( typeof (DocumentSymbol),  array.get_element(index)) as DocumentSymbol;
-                               arguments.add( add);
+                               arguments.append( add);
 
                   
                });
@@ -423,6 +428,48 @@ namespace Lsp {
                value.set_object (arguments);
                return true;
           }
+          public string symbol_icon { 
+                       
+                       owned get {
+                               return this.kind.icon(); 
+                       }
+               }
+                
+               public string tooltip {
+                       owned get {
+                               GLib.debug("%s : %s", this.name, this.detail);
+                               //var detail = this.detail == "" ? (this.kind.to_string() + ": " + this.name) : this.detail;
+                                return GLib.Markup.escape_text(this.detail + "\nline: " + this.range.start.line.to_string());
+                               
+                       }
+               }
+               public string sort_key {
+                       owned get { 
+                               return this.kind.sort_key().to_string() + "=" + this.name;
+                       }
+               }
+               
+               public DocumentSymbol? containsLine(uint line, uint chr)
+               {
+                       if (!this.range.contains(new Position(line, chr))) {
+                               return null;
+                       }
+
+                       for(var i = 0; i < this.children.get_n_items();i++) {
+                               var el = (DocumentSymbol)this.children.get_item(i);
+                               var ret = el.containsLine(line,chr);
+                               if (ret != null) {
+                                       return ret;
+                               }
+                       }
+                       return this;
+                       
+               }
+               public bool equals(DocumentSymbol sym) {
+                       return this.name == sym.name && this.kind == sym.kind &&  sym.range.equals(this.range);
+               }
+          
+          
     }
 
     public class SymbolInformation : Object {
@@ -434,7 +481,7 @@ namespace Lsp {
         public SymbolInformation.from_document_symbol (DocumentSymbol dsym, string uri) {
             this.name = dsym.name;
             this.kind = dsym.kind;
-            this.location = new Location (uri, dsym.range);
+          //  this.location = new Location (uri, dsym.range);
             this.containerName = dsym.parent_name;
         }
     }
@@ -466,7 +513,63 @@ namespace Lsp {
         Struct = 23,
         Event = 24,
         Operator = 25,
-        TypeParameter = 26
+        TypeParameter = 26;
+        
+        public string icon () { 
+                               
+                       switch (this) {
+                               
+                               // case         SymbolKind.Text: return "completion-snippet-symbolic";
+                               case    SymbolKind.Method: return "lang-method-symbolic";
+                               case    SymbolKind.Function: return "lang-function-symbolic";
+                               case    SymbolKind.Constructor: return "lang-method-symbolic";
+                               case    SymbolKind.Field: return "lang-struct-field-symbolic";
+                               case    SymbolKind.Variable: return "lang-variable-symbolic";
+                               case    SymbolKind.Class: return "lang-class-symbolic";
+                               case    SymbolKind.Interface: return "lang-class-symbolic";
+                               case    SymbolKind.Module: return "lang-namespace-symbolic";
+                               case    SymbolKind.Property:return "lang-struct-field-symbolic";
+                               //case  SymbolKind.Unit: return "lang-variable-symbolic";
+                               //case  SymbolKind.Value: return "lang-variable-symbolic";
+                               case    SymbolKind.Enum: return "lang-enum-symbolic";
+                               //case  SymbolKind.Keyword: return "completion-word-symbolic";
+                               //case  SymbolKind.Snippet: return "completion-snippet-symbolic";
+
+                               //case  SymbolKind.Color: return "lang-typedef-symbolic";
+                               case    SymbolKind.File:return "lang-typedef-symbolic";
+                               //case  SymbolKind.Reference: return "lang-typedef-symbolic";
+                               //case  SymbolKind.Folder:return "lang-typedef-symbolic";
+                               case    SymbolKind.EnumMember: return "lang-typedef-symbolic";
+                               case    SymbolKind.Constant:return "lang-typedef-symbolic";
+                               case    SymbolKind.Struct: return "lang-struct-symbolic";
+                               case    SymbolKind.Event:return "lang-typedef-symbolic";
+                               case    SymbolKind.Operator:return "lang-typedef-symbolic";
+                               case    SymbolKind.TypeParameter:return "lang-typedef-symbolic";
+                       
+                               default: 
+                                return "completion-snippet-symbolic";
+                                               
+                       }
+               }
+               public int sort_key() { 
+                        
+                       switch (this) {
+                               case Enum : return 1;
+                               case Class: return 2;
+                               
+                               case Constructor : return 1;
+                               case Method : return 2;
+                               case Field : return 3;
+                               case Property : return 3;
+                               
+                               default:
+                                       return 5;
+                       }       
+               
+               
+               
+               }
+        
     }
 
        public class CompletionList : Object, Json.Serializable {
@@ -862,6 +965,12 @@ namespace Lsp {
     }
 
    public  class MarkedString : Object {
+               public MarkedString(string language, string value) 
+               {
+                       this.language = language;
+                       this.value = value;
+                       GLib.debug("new marked string %s : %s", language, value);
+               }
         public string language { get; set; }
         public string value { get; set; }
     }
@@ -899,8 +1008,30 @@ namespace Lsp {
             return node;
         }
 
-        public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
-            error ("deserialization not supported");
+        public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) 
+        {
+            if (property_name == "contents") {
+                value = GLib.Value (typeof(Gee.ArrayList));
+                       if (property_node.get_node_type () != Json.NodeType.ARRAY) {
+                           warning ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
+                           return false;
+                       }
+                               var contents = new Gee.ArrayList<MarkedString>();
+
+                       property_node.get_array ().foreach_element ((array, index, element) => {
+                               var add = new MarkedString(
+                                               array.get_object_element(index).get_string_member("language"),
+                                               array.get_object_element(index).get_string_member("value")
+                                       );
+                    
+                       contents.add ( add );
+                    
+                       });
+                value.set_object (contents);
+                       return true;
+            } 
+            
+            return default_deserialize_property (property_name, out value, pspec, property_node);
         }
     }