fix line numbering issues with vala generator - hopefully fixes completion in node...
[roobuilder] / src / Lsp.vala
1 /* protocol.vala
2  *
3  * Copyright 2017-2019 Ben Iofel <ben@iofel.me>
4  * Copyright 2017-2020 Princeton Ferro <princetonferro@gmail.com>
5  * Copyright 2020 Sergii Fesenko <s.fesenko@outlook.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation, either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 namespace Lsp {
22     /**
23      * Defines how the host (editor) should sync document changes to the language server.
24      */
25     [CCode (default_value = "LSP_TEXT_DOCUMENT_SYNC_KIND_Unset")]
26     public enum TextDocumentSyncKind {
27         Unset = -1,
28         /**
29          * Documents should not be synced at all.
30          */
31         None = 0,
32         /**
33          * Documents are synced by always sending the full content of the document.
34          */
35         Full = 1,
36         /**
37          * Documents are synced by sending the full content on open. After that only incremental
38          * updates to the document are sent.
39          */
40         Incremental = 2
41     }
42
43     public  enum DiagnosticSeverity {
44         Unset = 0,
45         /**
46          * Reports an error.
47          */
48         Error = 1,
49         /**
50          * Reports a warning.
51          */
52         Warning = 2,
53         /**
54          * Reports an information.
55          */
56         Information = 3,
57         /**
58          * Reports a hint.
59          */
60         Hint = 4
61         
62     }
63
64     public  class Position : Object, Gee.Comparable<Position> {
65         /**
66          * Line position in a document (zero-based).
67          */
68         public uint line { get; set; default = -1; }
69
70         /**
71          * Character offset on a line in a document (zero-based). Assuming that the line is
72          * represented as a string, the `character` value represents the gap between the
73          * `character` and `character + 1`.
74          *
75          * If the character value is greater than the line length it defaults back to the
76          * line length.
77          */
78         public uint character { get; set; default = -1; }
79
80         public int compare_to (Position other) {
81             return line > other.line ? 1 :
82                 (line == other.line ?
83                  (character > other.character ? 1 :
84                   (character == other.character ? 0 : -1)) : -1);
85         }
86
87         public string to_string () {
88             return @"$line:$character";
89         }
90
91         public Position.from_libvala (Vala.SourceLocation sloc) {
92             line = sloc.line - 1;
93             character = sloc.column;
94         }
95
96         public Position dup () {
97             return this.translate ();
98         }
99
100         public Position translate (int dl = 0, int dc = 0) {
101             return new Position () {
102                 line = this.line + dl,
103                 character = this.character + dc
104             };
105         }
106     }
107
108     public class Range : Object, Gee.Hashable<Range>, Gee.Comparable<Range> {
109         /**
110          * The range's start position.
111          */
112         public Position start { get; set; }
113
114         /**
115          * The range's end position.
116          */
117         public Position end { get; set; }
118
119         private string? filename;
120
121         public string to_string () { return (filename != null ? @"$filename:" : "") + @"$start -> $end"; }
122
123         public Range.from_pos (Position pos) {
124             this.start = pos;
125             this.end = pos.dup ();
126         }
127
128         public Range.from_sourceref (Vala.SourceReference sref) {
129             this.start = new Position.from_libvala (sref.begin);
130             this.end = new Position.from_libvala (sref.end);
131             this.start.character -= 1;
132             this.filename = sref.file.filename;
133         }
134
135         public uint hash () {
136             return this.to_string ().hash ();
137         }
138
139         public bool equal_to (Range other) { return this.to_string () == other.to_string (); }
140
141         public int compare_to (Range other) {
142             return start.compare_to (other.start);
143         }
144
145         /**
146          * Return a new range that includes `this` and `other`.
147          */
148         public Range union (Range other) {
149             var range = new Range () {
150                 start = start.compare_to (other.start) < 0 ? start : other.start,
151                 end = end.compare_to (other.end) < 0 ? other.end : end,
152             };
153             if (filename == other.filename)
154                 range.filename = filename;
155             return range;
156         }
157
158         public bool contains (Position pos) {
159             return start.compare_to (pos) <= 0 && pos.compare_to (end) <= 0;
160         }
161     }
162
163     public class Diagnostic : Object {
164         /**
165          * The range at which the message applies.
166          */
167         public Range range { get; set; }
168
169         /**
170          * The diagnostic's severity. Can be omitted. If omitted it is up to the
171          * client to interpret diagnostics as error, warning, info or hint.
172          */
173         public DiagnosticSeverity severity { get; set; }
174
175         /**
176          * The diagnostic's code. Can be omitted.
177          */
178         public string? code { get; set; }
179
180         /**
181          * A human-readable string describing the source of this
182          * diagnostic, e.g. 'typescript' or 'super lint'.
183          */
184         public string? source { get; set; }
185
186         /**
187          * The diagnostic's message.
188          */
189         public string message { get; set; }
190         
191         
192         public string category {
193                 get { 
194                         switch(this.severity) {
195
196                                 case DiagnosticSeverity.Error : 
197                                         return "ERR";
198                                 case DiagnosticSeverity.Warning : 
199                                         return this.message.contains("deprecated") ? "DEPR" : "WARN";
200                                 default : 
201                                         return "WARN";
202                         }
203                 }
204                 private set {}
205                 
206         }
207     }
208
209     /**
210      * An event describing a change to a text document. If range and rangeLength are omitted
211      * the new text is considered to be the full content of the document.
212      */
213     public class TextDocumentContentChangeEvent : Object {
214         public Range? range    { get; set; }
215         public int rangeLength { get; set; }
216         public string text     { get; set; }
217     }
218
219     public enum MessageType {
220         /**
221          * An error message.
222          */
223         Error = 1,
224         /**
225          * A warning message.
226          */
227         Warning = 2,
228         /**
229          * An information message.
230          */
231         Info = 3,
232         /**
233          * A log message.
234          */
235         Log = 4
236     }
237
238     public class TextDocumentIdentifier : Object {
239         public string uri { get; set; }
240     }
241
242     public class VersionedTextDocumentIdentifier : TextDocumentIdentifier {
243         /**
244          * The version number of this document. If a versioned text document identifier
245          * is sent from the server to the client and the file is not open in the editor
246          * (the server has not received an open notification before) the server can send
247          * `null` to indicate that the version is known and the content on disk is the
248          * master (as speced with document content ownership).
249          *
250          * The version number of a document will increase after each change, including
251          * undo/redo. The number doesn't need to be consecutive.
252          */
253         public int version { get; set; default = -1; }
254     }
255
256     public class TextDocumentPositionParams : Object {
257         public TextDocumentIdentifier textDocument { get; set; }
258         public Position position { get; set; }
259     }
260
261     public class ReferenceParams : TextDocumentPositionParams {
262         public class ReferenceContext : Object {
263             public bool includeDeclaration { get; set; }
264         }
265         public ReferenceContext? context { get; set; }
266     }
267
268     public class Location : Object {
269         public string uri { get; set; }
270         public Range range { get; set; }
271
272         public Location.from_sourceref (Vala.SourceReference sref) {
273             this (sref.file.filename, new Range.from_sourceref (sref));
274         }
275
276         public Location (string filename, Range range) {
277             this.uri = File.new_for_commandline_arg (filename).get_uri ();
278             this.range = range;
279         }
280     }
281
282     [CCode (default_value = "LSP_DOCUMENT_HIGHLIGHT_KIND_Text")]
283     public enum DocumentHighlightKind {
284         Text = 1,
285         Read = 2,
286         Write = 3
287     }
288
289     public class DocumentHighlight : Object {
290         public Range range { get; set; }
291         public DocumentHighlightKind kind { get; set; }
292     }
293
294     public class DocumentSymbolParams: Object {
295         public TextDocumentIdentifier textDocument { get; set; }
296     }
297
298     public class DocumentSymbol : Object, Json.Serializable {
299         private Vala.SourceReference? _source_reference;
300         public string name { get; set; }
301         public string? detail { get; set; }
302         public SymbolKind kind { get; set; }
303         public bool deprecated { get; set; }
304         private Range? _initial_range;
305         public Range range {
306             owned get {
307                 if (_initial_range == null)
308                     _initial_range = new Range.from_sourceref (children.first ()._source_reference);
309                 
310                 return children.fold<Range> ((child, current_range) => current_range.union (child.range), _initial_range);
311             }
312         }
313         public Range selectionRange { get; set; }
314         public Gee.List<DocumentSymbol> children { get; private set; default = new Gee.LinkedList<DocumentSymbol> (); }
315         public string? parent_name;
316
317         private DocumentSymbol () {}
318
319         /**
320          * @param type the data type containing this symbol, if there was one (not available for Namespaces, for example)
321          * @param sym the symbol
322          */
323          /*
324         public DocumentSymbol.from_vala_symbol (Vala.DataType? type, Vala.Symbol sym, SymbolKind kind) {
325             this.parent_name = sym.parent_symbol != null ? sym.parent_symbol.name : null;
326             this._initial_range = new Range.from_sourceref (sym.source_reference);
327             if (sym is Vala.Subroutine) {
328                 var sub = (Vala.Subroutine) sym;
329                 var body_sref = sub.body != null ? sub.body.source_reference : null;
330                 // debug ("subroutine %s found (body @ %s)", sym.get_full_name (),
331                 //         body_sref != null ? body_sref.to_string () : null);
332                 if (body_sref != null && (body_sref.begin.line < body_sref.end.line ||
333                                           body_sref.begin.line == body_sref.end.line && body_sref.begin.pos <= body_sref.end.pos)) {
334                     this._initial_range = this._initial_range.union (new Range.from_sourceref (body_sref));
335                 }
336             }
337             this.name = sym.name;
338             this.detail = Vls.CodeHelp.get_symbol_representation (type, sym, null, false);
339             this.kind = kind;
340             this.selectionRange = new Range.from_sourceref (sym.source_reference);
341             this.deprecated = sym.version.deprecated;
342         }
343         */
344         public new void Json.Serializable.set_property (ParamSpec pspec, Value value) {
345             base.set_property (pspec.get_name (), value);
346         }
347
348         public new Value Json.Serializable.get_property (ParamSpec pspec) {
349             Value val = Value (pspec.value_type);
350             base.get_property (pspec.get_name (), ref val);
351             return val;
352         }
353
354         public unowned ParamSpec? find_property (string name) {
355             return this.get_class ().find_property (name);
356         }
357
358         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
359             if (property_name != "children")
360                 return default_serialize_property (property_name, value, pspec);
361             var node = new Json.Node (Json.NodeType.ARRAY);
362             node.init_array (new Json.Array ());
363             var array = node.get_array ();
364             foreach (var child in children)
365                 array.add_element (Json.gobject_serialize (child));
366             return node;
367         }
368
369         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
370             error ("deserialization not supported");
371         }
372     }
373
374     public class SymbolInformation : Object {
375         public string name { get; set; }
376         public SymbolKind kind { get; set; }
377         public Location location { get; set; }
378         public string? containerName { get; set; }
379
380         public SymbolInformation.from_document_symbol (DocumentSymbol dsym, string uri) {
381             this.name = dsym.name;
382             this.kind = dsym.kind;
383             this.location = new Location (uri, dsym.range);
384             this.containerName = dsym.parent_name;
385         }
386     }
387
388     [CCode (default_value = "LSP_SYMBOL_KIND_Variable")]
389     public enum SymbolKind {
390         File = 1,
391         Module = 2,
392         Namespace = 3,
393         Package = 4,
394         Class = 5,
395         Method = 6,
396         Property = 7,
397         Field = 8,
398         Constructor = 9,
399         Enum = 10,
400         Interface = 11,
401         Function = 12,
402         Variable = 13,
403         Constant = 14,
404         String = 15,
405         Number = 16,
406         Boolean = 17,
407         Array = 18,
408         Object = 19,
409         Key = 20,
410         Null = 21,
411         EnumMember = 22,
412         Struct = 23,
413         Event = 24,
414         Operator = 25,
415         TypeParameter = 26
416     }
417
418         public class CompletionList : Object, Json.Serializable {
419         public bool isIncomplete { get; set; }
420         public Gee.List<CompletionItem> items { get; private set; default = new Gee.LinkedList<CompletionItem> (); }
421
422         public new void Json.Serializable.set_property (ParamSpec pspec, Value value) {
423             base.set_property (pspec.get_name (), value);
424         }
425
426         public new Value Json.Serializable.get_property (ParamSpec pspec) {
427             Value val = Value(pspec.value_type);
428             base.get_property (pspec.get_name (), ref val);
429             return val;
430         }
431
432         public unowned ParamSpec? find_property (string name) {
433             return this.get_class ().find_property (name);
434         }
435
436         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
437             if (property_name != "items")
438                 return default_serialize_property (property_name, value, pspec);
439             var node = new Json.Node (Json.NodeType.ARRAY);
440             node.init_array (new Json.Array ());
441             var array = node.get_array ();
442             foreach (var child in items)
443                 array.add_element (Json.gobject_serialize (child));
444             return node;
445         }
446
447         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
448             error ("deserialization not supported");
449         }
450     }
451
452     [CCode (default_value = "LSP_COMPLETION_TRIGGER_KIND_Invoked")]
453     public enum CompletionTriggerKind {
454         /**
455              * Completion was triggered by typing an identifier (24x7 code
456              * complete), manual invocation (e.g Ctrl+Space) or via API.
457              */
458         Invoked = 1,
459
460         /**
461              * Completion was triggered by a trigger character specified by
462              * the `triggerCharacters` properties of the `CompletionRegistrationOptions`.
463              */
464         TriggerCharacter = 2,
465
466         /**
467              * Completion was re-triggered as the current completion list is incomplete.
468              */
469         TriggerForIncompleteCompletions = 3
470     }
471
472     public class CompletionContext : Object {
473         public CompletionTriggerKind triggerKind { get; set;}
474         public string? triggerCharacter { get; set; }
475     }
476
477     public class CompletionParams : TextDocumentPositionParams {
478         /**
479          * The completion context. This is only available if the client specifies
480          * to send this using `ClientCapabilities.textDocument.completion.contextSupport === true`
481          */
482         public CompletionContext? context { get; set; }
483     }
484
485     public enum CompletionItemTag {
486         // Render a completion as obsolete, usually using a strike-out.
487         Deprecated = 1,
488     }
489
490     [CCode (default_value = "LSP_INSERT_TEXT_FORMAT_PlainText")]
491     public enum InsertTextFormat {
492         /**
493          * The primary text to be inserted is treated as a plain string.
494          */
495         PlainText = 1,
496
497         /**
498          * The primary text to be inserted is treated as a snippet.
499          *
500          * A snippet can define tab stops and placeholders with `$1`, `$2`
501          * and `${3:foo}`. `$0` defines the final tab stop, it defaults to
502          * the end of the snippet. Placeholders with equal identifiers are linked,
503          * that is typing in one will update others too.
504          */
505         Snippet = 2,
506     }
507
508     public class CompletionItem : Object, Gee.Hashable<CompletionItem>, Json.Serializable {
509         public string label { get; set; }
510         public CompletionItemKind kind { get; set; }
511         public string detail { get; set; }
512         public MarkupContent? documentation { get; set; }
513         public bool deprecated { get; set; }
514         public Gee.List<CompletionItemTag> tags { get; private set; default = new Gee.ArrayList<CompletionItemTag> (); }
515         public string? insertText { get; set; }
516         public InsertTextFormat insertTextFormat { get; set; default = InsertTextFormat.PlainText; }
517         private uint _hash;
518
519         private CompletionItem () {}
520
521         public CompletionItem.keyword (string keyword, string? insert_text = null, string? documentation = null) {
522             this.label = keyword;
523             this.kind = CompletionItemKind.Keyword;
524             this.insertText = insert_text;
525             if (insert_text != null && (insert_text.contains ("$0") || insert_text.contains ("${0")))
526                 this.insertTextFormat = InsertTextFormat.Snippet;
527             if (documentation != null)
528                 this.documentation = new MarkupContent.from_plaintext (documentation);
529             this._hash = @"$label $kind".hash ();
530         }
531
532         /**
533          * A completion suggestion from an existing Vala symbol.
534          * 
535          * @param instance_type the parent data type of data type of the expression where this symbol appears, or null
536          * @param sym the symbol itself
537          * @param scope the scope to display this in
538          * @param kind the kind of completion to display
539          * @param documentation the documentation to display
540          * @param label_override if non-null, override the displayed symbol name with this
541          */
542          /*
543         public CompletionItem.from_symbol (Vala.DataType? instance_type, Vala.Symbol sym, Vala.Scope? scope,
544             CompletionItemKind kind,
545             Vls.DocComment? documentation, string? label_override = null) {
546             this.label = label_override ?? sym.name;
547             this.kind = kind;
548             this.detail = Vls.CodeHelp.get_symbol_representation (instance_type, sym, scope, true, null, label_override, false);
549             this._hash = @"$label $kind".hash ();
550
551             if (documentation != null)
552                 this.documentation = new MarkupContent.from_markdown (documentation.body);
553
554             var version = sym.get_attribute ("Version");
555             if (version != null && (version.get_bool ("deprecated") || version.get_string ("deprecated_since") != null)) {
556                 this.tags.add (CompletionItemTag.Deprecated);
557                 this.deprecated = true;
558             }public
559         }
560                 */
561         /**
562          * A completion suggestion from a data type and a synthetic symbol name.
563          *
564          * @param symbol_type       the data type of the symbol
565          * @param symbol_name       the name of the synthetic symbol
566          * @param scope             the scope that this completion item is displayed in, or null
567          * @param kind              the type of completion to display
568          * @param documentation     the documentation for this symbol, or null
569          */
570          /*
571         public CompletionItem.from_synthetic_symbol (Vala.DataType symbol_type, string symbol_name, Vala.Scope? scope,
572                                                      CompletionItemKind kind, Vls.DocComment? documentation) {
573             this.label = symbol_name;
574             this.kind = kind;
575             this.detail = @"$(Vls.CodeHelp.get_symbol_representation (symbol_type, null, scope, true, null, null, false)) $symbol_name";
576             this._hash = @"$label $kind".hash ();
577
578             if (documentation != null)
579                 this.documentation = new MarkupContent.from_markdown (documentation.body);
580         }
581         */
582                 /*
583         public CompletionItem.from_unimplemented_symbol (Vala.Symbol sym, 
584                                                          string label, CompletionItemKind kind,
585                                                          string insert_text,
586                                                          Vls.DocComment? documentation) {
587             this.label = label;
588             this.kind = kind;
589             this.insertText = insert_text;
590             if (insert_text.contains ("$0") || insert_text.contains ("${0"))
591                 this.insertTextFormat = InsertTextFormat.Snippet;
592             this._hash = @"$label $kind".hash ();
593             if (documentation != null)
594                 this.documentation = new MarkupContent.from_markdown (documentation.body);
595         }
596         */
597
598         public uint hash () {
599             return this._hash;
600         }
601
602         public bool equal_to (CompletionItem other) {
603             return other.label == this.label && other.kind == this.kind;
604         }
605
606         public new void Json.Serializable.set_property (ParamSpec pspec, Value value) {
607             base.set_property (pspec.get_name (), value);
608         }
609
610         public new Value Json.Serializable.get_property (ParamSpec pspec) {
611             Value val = Value(pspec.value_type);
612             base.get_property (pspec.get_name (), ref val);
613             return val;
614         }
615
616         public unowned ParamSpec? find_property (string name) {
617             return this.get_class ().find_property (name);
618         }
619
620         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
621             if (property_name != "tags")
622                 return default_serialize_property (property_name, value, pspec);
623
624             var node = new Json.Node (Json.NodeType.ARRAY);
625             node.init_array (new Json.Array ());
626             var array = node.get_array ();
627             foreach (var tag in this.tags) {
628                 array.add_int_element (tag);
629             }
630
631             return node;
632         }
633         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) 
634         {
635                 if (property_name != "tags") {
636                 return default_deserialize_property (property_name, out value, pspec, property_node);
637             }
638             if (property_node.get_node_type () != Json.NodeType.ARRAY) {
639                 warning ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
640                 return false;
641             }
642
643             var arguments = new Gee.ArrayList<CompletionItemTag>();
644
645             property_node.get_array ().foreach_element ((array, index, element) => {
646                 try {
647                     arguments.add ((CompletionItemTag) Json.gvariant_deserialize (element, null).get_int32() );
648                 } catch (Error e) {
649                     warning ("argument %u to command could not be deserialized: %s", index, e.message);
650                 }
651             });
652
653             value.set_boxed (arguments);
654             return true;
655        }
656     }
657
658     public class MarkupContent : Object {
659         public string kind { get; set; }
660         public string value { get; set; }
661
662         private MarkupContent () {}
663
664         /**
665          * Create a MarkupContent with plain text.
666          */
667         public MarkupContent.from_plaintext (string doc) {
668             this.kind = "plaintext";
669             this.value = doc;
670         }
671
672         /**
673          * Create a MarkupContent with markdown text.
674          */
675         public MarkupContent.from_markdown (string doc) {
676             this.kind = "markdown";
677             this.value = doc;
678         }
679     }
680     
681     [CCode (default_value = "LSP_COMPLETION_ITEM_KIND_Text")]
682     public enum CompletionItemKind {
683         Text = 1,
684         Method = 2,
685         Function = 3,
686         Constructor = 4,
687         Field = 5,
688         Variable = 6,
689         Class = 7,
690         Interface = 8,
691         Module = 9,
692         Property = 10,
693         Unit = 11,
694         Value = 12,
695         Enum = 13,
696         Keyword = 14,
697         Snippet = 15,
698         Color = 16,
699         File = 17,
700         Reference = 18,
701         Folder = 19,
702         EnumMember = 20,
703         Constant = 21,
704         Struct = 22,
705         Event = 23,
706         Operator = 24,
707         TypeParameter = 25
708     }
709     
710     /**
711      * Capabilities of the client/editor for `textDocument/documentSymbol`
712      */
713     public class DocumentSymbolCapabilities : Object {
714         public bool hierarchicalDocumentSymbolSupport { get; set; }
715     }
716
717     /**
718      * Capabilities of the client/editor for `textDocument/rename`
719      */
720     public class RenameClientCapabilities : Object {
721         public bool prepareSupport { get; set; }
722     }
723
724     /**
725      * Capabilities of the client/editor pertaining to language features.
726      */
727     public class TextDocumentClientCapabilities : Object {
728         public DocumentSymbolCapabilities documentSymbol { get; set; default = new DocumentSymbolCapabilities ();}
729         public RenameClientCapabilities rename { get; set; default = new RenameClientCapabilities (); }
730     }
731
732     /**
733      * Capabilities of the client/editor.
734      */
735     public class ClientCapabilities : Object {
736         public TextDocumentClientCapabilities textDocument { get; set; default = new TextDocumentClientCapabilities (); }
737     }
738
739     public class InitializeParams : Object {
740         public int processId { get; set; }
741         public string? rootPath { get; set; }
742         public string? rootUri { get; set; }
743         public ClientCapabilities capabilities { get; set; default = new ClientCapabilities (); }
744     }
745
746     public class SignatureInformation : Object, Json.Serializable {
747         public string label { get; set; }
748         public MarkupContent documentation { get; set; }
749
750         public Gee.List<ParameterInformation> parameters { get; private set; default = new Gee.LinkedList<ParameterInformation> (); }
751
752         public new void Json.Serializable.set_property (ParamSpec pspec, Value value) {
753             base.set_property (pspec.get_name (), value);
754         }
755
756         public new Value Json.Serializable.get_property (ParamSpec pspec) {
757             Value val = Value(pspec.value_type);
758             base.get_property (pspec.get_name (), ref val);
759             return val;
760         }
761
762         public unowned ParamSpec? find_property (string name) {
763             return this.get_class ().find_property (name);
764         }
765
766         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
767             if (property_name != "parameters")
768                 return default_serialize_property (property_name, value, pspec);
769             var node = new Json.Node (Json.NodeType.ARRAY);
770             node.init_array (new Json.Array ());
771             var array = node.get_array ();
772             foreach (var child in parameters)
773                 array.add_element (Json.gobject_serialize (child));
774             return node;
775         }
776
777         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
778             error ("deserialization not supported");
779         }
780     }
781
782     public class SignatureHelp : Object, Json.Serializable {
783         public Gee.Collection<SignatureInformation> signatures { get; set; default = new Gee.ArrayList<SignatureInformation> (); }
784         public int activeSignature { get; set; }
785         public int activeParameter { get; set; }
786
787         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
788             if (property_name != "signatures")
789                 return default_serialize_property (property_name, value, pspec);
790
791             var node = new Json.Node (Json.NodeType.ARRAY);
792             node.init_array (new Json.Array ());
793             var array = node.get_array ();
794             foreach (var child in signatures)
795                 array.add_element (Json.gobject_serialize (child));
796             return node;
797         }
798
799         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
800             error ("deserialization not supported");
801         }
802     }
803
804     public class ParameterInformation : Object {
805         public string label { get; set; }
806         public MarkupContent documentation { get; set; }
807     }
808
809    public  class MarkedString : Object {
810         public string language { get; set; }
811         public string value { get; set; }
812     }
813
814     public class Hover : Object, Json.Serializable {
815         public Gee.List<MarkedString> contents { get; set; default = new Gee.ArrayList<MarkedString> (); }
816         public Range range { get; set; }
817
818         public new void Json.Serializable.set_property (ParamSpec pspec, Value value) {
819             base.set_property (pspec.get_name (), value);
820         }
821
822         public new Value Json.Serializable.get_property (ParamSpec pspec) {
823             Value val = Value(pspec.value_type);
824             base.get_property (pspec.get_name (), ref val);
825             return val;
826         }
827
828         public unowned ParamSpec? find_property (string name) {
829             return this.get_class ().find_property (name);
830         }
831
832         public Json.Node serialize_property (string property_name, Value value, ParamSpec pspec) {
833             if (property_name != "contents")
834                 return default_serialize_property (property_name, value, pspec);
835             var node = new Json.Node (Json.NodeType.ARRAY);
836             node.init_array (new Json.Array ());
837             var array = node.get_array ();
838             foreach (var child in contents) {
839                 if (child.language != null)
840                     array.add_element (Json.gobject_serialize (child));
841                 else
842                     array.add_element (new Json.Node (Json.NodeType.VALUE).init_string (child.value));
843             }
844             return node;
845         }
846
847         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
848             error ("deserialization not supported");
849         }
850     }
851
852     /**
853      * A textual edit applicable to a text document.
854      */
855     public class TextEdit : Object {
856         /**
857          * The range of the text document to be manipulated. To insert
858          * text into a document create a range where ``start === end``.
859          */
860         public Range range { get; set; }
861
862         /**
863          * The string to be inserted. For delete operations use an
864          * empty string.
865          */
866         public string newText { get; set; }
867
868         public TextEdit (Range range, string new_text = "") {
869             this.range = range;
870             this.newText = new_text;
871         }
872     }
873
874     /** 
875      * Describes textual changes on a single text document. The text document is
876      * referred to as a {@link VersionedTextDocumentIdentifier} to allow clients to
877      * check the text document version before an edit is applied. A
878      * {@link TextDocumentEdit} describes all changes on a version ``Si`` and after they are
879      * applied move the document to version ``Si+1``. So the creator of a
880      * {@link TextDocumentEdit} doesn’t need to sort the array of edits or do any kind
881      * of ordering. However the edits must be non overlapping.
882      */
883     public class TextDocumentEdit : Object, Json.Serializable {
884         /**
885          * The text document to change.
886          */
887         public VersionedTextDocumentIdentifier textDocument { get; set; }
888
889         /**
890          * The edits to be applied.
891          */
892         public Gee.ArrayList<TextEdit> edits { get; set; default = new Gee.ArrayList<TextEdit> (); }
893
894         public TextDocumentEdit (VersionedTextDocumentIdentifier text_document) {
895             this.textDocument = text_document;
896         }
897
898         public Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
899             if (property_name != "edits")
900                 return default_serialize_property (property_name, value, pspec);
901             
902             var node = new Json.Node (Json.NodeType.ARRAY);
903             node.init_array (new Json.Array ());
904             var array = node.get_array ();
905             foreach (var text_edit in edits) {
906                 array.add_element (Json.gobject_serialize (text_edit));
907             }
908             return node;
909         }
910
911         public bool deserialize_property (string property_name, out GLib.Value value, GLib.ParamSpec pspec, Json.Node property_node) {
912             error ("deserialization not supported");
913         }
914     }
915
916     public abstract class CommandLike : Object, Json.Serializable {
917         /**
918          * The identifier of the actual command handler.
919          */
920         public string command { get; set; }
921
922         /**
923          * Arguments that the command handler should be invoked with.
924          */
925         public Array<Variant>? arguments { get; set; }
926
927         public Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
928             if (property_name != "arguments" || arguments == null)
929                 return default_serialize_property (property_name, value, pspec);
930
931             var array = new Json.Array ();
932             for (int i = 0; i < arguments.length; i++)
933                 array.add_element (Json.gvariant_serialize (arguments.index (i)));
934
935             var node = new Json.Node (Json.NodeType.ARRAY);
936             node.set_array (array);
937             return node;
938         }
939
940         public bool deserialize_property (string property_name, out GLib.Value value, GLib.ParamSpec pspec, Json.Node property_node) {
941             if (property_name == "arguments") {
942                 value = Value (typeof (Array));
943                 if (property_node.get_node_type () != Json.NodeType.ARRAY) {
944                     warning ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
945                     return false;
946                 }
947
948                 var arguments = new Array<Variant> ();
949
950                 property_node.get_array ().foreach_element ((array, index, element) => {
951                     try {
952                         arguments.append_val (Json.gvariant_deserialize (element, null));
953                     } catch (Error e) {
954                         warning ("argument %u to command could not be deserialized: %s", index, e.message);
955                     }
956                 });
957
958                 value.set_boxed (arguments);
959                 return true;
960             } else if (property_name == "command") {
961                 // workaround for json-glib < 1.5.2 (Ubuntu 20.04 / eOS 6)
962                 if (property_node.get_value_type () != typeof (string)) {
963                     value = "";
964                     warning ("unexpected property node type for 'commands' %s", property_node.get_node_type ().to_string ());
965                     return false;
966                 }
967
968                 value = property_node.get_string ();
969                 return true;
970             } else {
971                 return default_deserialize_property (property_name, out value, pspec, property_node);
972             }
973         }
974     }
975
976     public class ExecuteCommandParams : CommandLike {
977     }
978
979     /**
980      * Represents a reference to a command. Provides a title which will be used
981      * to represent a command in the UI. Commands are identified by a string
982      * identifier. The recommended way to handle commands is to implement their
983      * execution on the server side if the client and server provides the
984      * corresponding capabilities. Alternatively the tool extension code could
985      * handle the command. The protocol currently doesn’t specify a set of
986      * well-known commands.
987      */
988     public class Command : CommandLike {
989         /**
990          * The title of the command, like `save`.
991          */
992         public string title { get; set; }
993     }
994
995     /**
996      * A code lens represents a command that should be shown along with
997      * source text, like the number of references, a way to run tests, etc.
998      *
999      * A code lens is _unresolved_ when no command is associated to it. For
1000      * performance reasons the creation of a code lens and resolving should be done
1001      * in two stages.
1002      */
1003     public class CodeLens : Object {
1004         /**
1005          * The range in which this code lens is valid. Should only span a single
1006          * line.
1007          */
1008         public Range range { get; set; }
1009
1010         /**
1011          * The command this code lens represents.
1012          */
1013         public Command? command { get; set; }
1014     }
1015     
1016     public class DocumentRangeFormattingParams : Object {
1017         public TextDocumentIdentifier textDocument { get; set; }
1018         public Range? range { get; set; }
1019         public FormattingOptions options { get; set; }
1020     }
1021
1022     public class FormattingOptions : Object {
1023         public uint tabSize { get; set; }
1024         public bool insertSpaces { get; set; }
1025         public bool trimTrailingWhitespace { get; set; }
1026         public bool insertFinalNewline { get; set; }
1027         public bool trimFinalNewlines { get; set; }
1028     }
1029
1030     public class CodeActionParams : Object {
1031         public TextDocumentIdentifier textDocument { get; set; }
1032         public Range range { get; set; }
1033         public CodeActionContext context { get; set; }
1034     }
1035
1036
1037     public class CodeActionContext : Object, Json.Serializable {
1038         public Gee.List<Diagnostic> diagnostics { get; set; default = new Gee.ArrayList<Diagnostic> (); }
1039         public string[]? only { get; set; }
1040 /*
1041         public bool deserialize_property (string property_name, out Value value, ParamSpec pspec, Json.Node property_node) {
1042             if (property_name != "diagnostics")
1043                 return default_deserialize_property (property_name, out value, pspec, property_node);
1044             var diags = new Gee.ArrayList<Diagnostic> ();
1045             property_node.get_array ().foreach_element ((array, index, element) => {
1046                 try {
1047                     diags.add (Vls.Util.parse_variant<Diagnostic> (Json.gvariant_deserialize (element, null)));
1048                 } catch (Error e) {
1049                     warning ("argument %u could not be deserialized: %s", index, e.message);
1050                 }
1051             });
1052             value = diags;
1053             return true;
1054         }
1055         */
1056     }
1057
1058
1059         public class Diagnostics : Object, Json.Serializable 
1060         {
1061                 public string uri { get; set; }
1062
1063                 public int version  { get; set; default = 0; }
1064         public Gee.ArrayList<Diagnostic>? diagnostics { get; set; }
1065                  
1066                 public string filename { 
1067                         owned get {
1068                                 return File.new_for_uri (this.uri).get_path();
1069                         }
1070                         private set {}
1071                 }
1072                 
1073                 public bool deserialize_property (string property_name, out GLib.Value val, GLib.ParamSpec pspec, Json.Node property_node) {
1074                         if (property_name == "diagnostics") {
1075                                 var diags =  new Gee.ArrayList<Diagnostic> ();
1076                                 if (property_node.get_node_type () != Json.NodeType.ARRAY) {
1077                                         val = diags;
1078                                         warning ("unexpected property node type for 'arguments' %s", property_node.get_node_type ().to_string ());
1079                                         return false;
1080                                 }
1081
1082                                 
1083
1084                                 property_node.get_array ().foreach_element ((array, index, element) => {
1085                                          
1086                                                 diags.add (Json.gobject_deserialize (typeof (Lsp.Diagnostic), element) as Diagnostic );
1087                                          
1088                                                 //warning ("argument %u to command could not be deserialized: %s", index, e.message);
1089                                          
1090                                 });
1091                                 val = diags;
1092                                  
1093                                 return true;
1094                         }   
1095                          
1096                         return default_deserialize_property (property_name, out val, pspec, property_node);
1097                          
1098                 }
1099
1100                 
1101         }
1102
1103
1104    public  class CodeAction : Object, Json.Serializable {
1105         public string title { get; set; }
1106         public string? kind { get; set; }
1107         public Gee.Collection<Diagnostic>? diagnostics { get; set; }
1108         public bool isPreferred { get; set; }
1109         public WorkspaceEdit? edit { get; set; }
1110         public Command? command { get; set; }
1111         public Object? data { get; set; }
1112
1113         protected void add_diagnostic (Diagnostic diag) {
1114             if (diagnostics == null)
1115                 diagnostics = new Gee.ArrayList<Diagnostic> ();
1116             diagnostics.add (diag);
1117         }
1118
1119         public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
1120             if (property_name != "diagnostics")
1121                 return default_serialize_property (property_name, value, pspec);
1122
1123             var array = new Json.Array ();
1124             if (diagnostics != null)
1125                 foreach (var text_edit in diagnostics)
1126                     array.add_element (Json.gobject_serialize (text_edit));
1127             return new Json.Node.alloc ().init_array (array);
1128         }
1129     }
1130
1131     public class WorkspaceEdit : Object, Json.Serializable {
1132         public Gee.List<TextDocumentEdit>? documentChanges { get; set; }
1133
1134         public Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
1135             if (property_name != "documentChanges")
1136                 return default_serialize_property (property_name, value, pspec);
1137
1138             var node = new Json.Node (Json.NodeType.ARRAY);
1139             node.init_array (new Json.Array ());
1140             if (documentChanges != null) {
1141                 var array = node.get_array ();
1142                 foreach (var text_edit in documentChanges) {
1143                     array.add_element (Json.gobject_serialize (text_edit));
1144                 }
1145             }
1146             return node;
1147         }
1148     }
1149
1150     [Flags]
1151     public enum SymbolTags {
1152         NONE,
1153         DEPRECATED
1154     }
1155
1156     public class CallHierarchyItem : Object, Json.Serializable {
1157         public string name { get; set; }
1158         public SymbolKind kind { get; set; }
1159         public SymbolTags tags { get; set; }
1160         public string? detail { get; set; }
1161         public string uri { get; set; }
1162         public Range range { get; set; }
1163         public Range selectionRange { get; set; }
1164
1165         public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
1166             if (property_name != "tags")
1167                 return default_serialize_property (property_name, value, pspec);
1168             var array = new Json.Array ();
1169             if (SymbolTags.DEPRECATED in tags)
1170                 array.add_int_element (SymbolTags.DEPRECATED);
1171             return new Json.Node.alloc ().init_array (array);
1172         }
1173 /*
1174         public CallHierarchyItem.from_symbol (Vala.Symbol symbol) {
1175             this.name = symbol.get_full_name ();
1176             if (symbol is Vala.Method) {
1177                 if (symbol.parent_symbol is Vala.Namespace)
1178                     this.kind = SymbolKind.Function;
1179                 else
1180                     this.kind = SymbolKind.Method;
1181             } else if (symbol is Vala.Signal) {
1182                 this.kind = SymbolKind.Event;
1183             } else if (symbol is Vala.Constructor) {
1184                 this.kind = SymbolKind.Constructor;
1185             } else {
1186                 this.kind = SymbolKind.Method;
1187             }
1188             var version = symbol.get_attribute ("Version");
1189             if (version != null && (version.get_bool ("deprecated") || version.get_string ("deprecated_since") != null)) {
1190                 this.tags |= SymbolTags.DEPRECATED;
1191             }
1192             this.detail = Vls.CodeHelp.get_symbol_representation (null, symbol, null, true);
1193             this.uri = File.new_for_commandline_arg (symbol.source_reference.file.filename).get_uri ();
1194             this.range = new Range.from_sourceref (symbol.source_reference);
1195             if (symbol.comment != null)
1196                 this.range = new Range.from_sourceref (symbol.comment.source_reference).union (this.range);
1197             if (symbol is Vala.Subroutine && ((Vala.Subroutine)symbol).body != null)
1198                 this.range = new Range.from_sourceref (((Vala.Subroutine)symbol).body.source_reference).union (this.range);
1199             this.selectionRange = new Range.from_sourceref (symbol.source_reference);
1200         }
1201         */
1202     }
1203
1204     public class CallHierarchyIncomingCall : Json.Serializable, Object {
1205         /**
1206          * The method that calls the query method.
1207          */
1208         public CallHierarchyItem from { get; set; }
1209
1210         /**
1211          * The ranges at which the query method is called by `from`.
1212          */
1213         public Gee.ArrayList<Range> fromRanges { get; set; default = new Gee.ArrayList<Range> (); }
1214
1215         public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
1216             if (property_name == "from")
1217                 return default_serialize_property (property_name, value, pspec);
1218             var array = new Json.Array ();
1219             foreach (var range in fromRanges)
1220                 array.add_element (Json.gobject_serialize (range));
1221             return new Json.Node.alloc ().init_array (array);
1222         }
1223     }
1224
1225     public class CallHierarchyOutgoingCall : Json.Serializable, Object {
1226         /**
1227          * The method that the query method calls.
1228          */
1229         public CallHierarchyItem to { get; set; }
1230
1231         /**
1232          * The ranges at which the method is called by the query method.
1233          */
1234         public Gee.ArrayList<Range> fromRanges { get; set; default = new Gee.ArrayList<Range> (); }
1235
1236         public override Json.Node serialize_property (string property_name, GLib.Value value, GLib.ParamSpec pspec) {
1237             if (property_name == "to")
1238                 return default_serialize_property (property_name, value, pspec);
1239             var array = new Json.Array ();
1240             foreach (var range in fromRanges)
1241                 array.add_element (Json.gobject_serialize (range));
1242             return new Json.Node.alloc ().init_array (array);
1243         }
1244     }
1245
1246     public class InlayHintParams : Json.Serializable, Object {
1247         public TextDocumentIdentifier textDocument { get; set; }
1248         public Range range { get; set; }
1249     }
1250
1251     public enum InlayHintKind {
1252         UNSET,
1253         TYPE,
1254         PARAMETER
1255     }
1256
1257     public class InlayHint : Object {
1258         public Position position { get; set; }
1259         public string label { get; set; }
1260         public InlayHintKind kind { get; set; }
1261         public string? tooltip { get; set; }
1262         public bool paddingLeft { get; set; }
1263         public bool paddingRight { get; set; }
1264     }
1265
1266    public  class TypeHierarchyItem : Object, Json.Serializable {
1267         /**
1268          * The name of this item
1269          */
1270         public string name { get; set; }
1271
1272         /**
1273          * The kind of this item
1274          */
1275         public SymbolKind kind { get; set; }
1276
1277         /**
1278          * Tags for this item
1279          */
1280         public SymbolTags tags { get; set; }
1281
1282         /**
1283          * More detail for this item, e.g. the signature of a function.
1284          */
1285         public string? detail { get; set; }
1286
1287         /**
1288          * The resource identifier of this item.
1289          */
1290         public string uri { get; set; }
1291
1292         /**
1293          * The range enclosing this symbol not including leading/trailing
1294          * whitespace, but everything else, e.g. comments and code.
1295          */
1296         public Range range { get; set; }
1297
1298         /**
1299          * The range that should be selected and revealed when this symbol
1300          * is being picked, e.g. the name of a function. Must be contained
1301          * by {@link TypeHierarchyItem.range}
1302          */
1303         public Range selectionRange { get; set; }
1304
1305         private TypeHierarchyItem () {}
1306 /*
1307         public TypeHierarchyItem.from_symbol (Vala.TypeSymbol symbol) {
1308             this.name = symbol.get_full_name ();
1309             if (symbol is Vala.Class)
1310                 this.kind = SymbolKind.Class;
1311             else if (symbol is Vala.Delegate)
1312                 this.kind = SymbolKind.Interface;
1313             else if (symbol is Vala.Enum)
1314                 this.kind = SymbolKind.Enum;
1315             else if (symbol is Vala.ErrorCode)
1316                 this.kind = SymbolKind.EnumMember;
1317             else if (symbol is Vala.ErrorDomain)
1318                 this.kind = SymbolKind.Enum;
1319             else if (symbol is Vala.Interface)
1320                 this.kind = SymbolKind.Interface;
1321             else if (symbol is Vala.Struct)
1322                 this.kind = SymbolKind.Struct;
1323             else if (symbol is Vala.TypeParameter)
1324                 this.kind = SymbolKind.TypeParameter;
1325             else {
1326                 this.kind = SymbolKind.Module;
1327                 warning ("unexpected symbol kind in type hierarchy: `%s'", symbol.type_name);
1328             }
1329
1330             var version = symbol.get_attribute ("Version");
1331             if (version != null && (version.get_bool ("deprecated") || version.get_string ("deprecated_since") != null)) {
1332                 this.tags |= SymbolTags.DEPRECATED;
1333             }
1334             this.detail = Vls.CodeHelp.get_symbol_representation (null, symbol, null, true);
1335             this.uri = File.new_for_commandline_arg (symbol.source_reference.file.filename).get_uri ();
1336             this.range = new Range.from_sourceref (symbol.source_reference);
1337             this.selectionRange = this.range;
1338
1339             // widen range to include all members
1340             if (symbol is Vala.ObjectTypeSymbol) {
1341                 foreach (var member in ((Vala.ObjectTypeSymbol)symbol).get_members ()) {
1342                     if (member.source_reference != null)
1343                         this.range = this.range.union (new Range.from_sourceref (member.source_reference));
1344                 }
1345             } else if (symbol is Vala.Enum) {
1346                 foreach (var member in ((Vala.Enum)symbol).get_values ()) {
1347                     if (member.source_reference != null)
1348                         this.range = this.range.union (new Range.from_sourceref (member.source_reference));
1349                 }
1350                 foreach (var method in ((Vala.Enum)symbol).get_methods ()) {
1351                     if (method.source_reference != null)
1352                         this.range = this.range.union (new Range.from_sourceref (method.source_reference));
1353                 }
1354             } else if (symbol is Vala.ErrorDomain) {
1355                 foreach (var member in ((Vala.ErrorDomain)symbol).get_codes ()) {
1356                     if (member.source_reference != null)
1357                         this.range = this.range.union (new Range.from_sourceref (member.source_reference));
1358                 }
1359                 foreach (var method in ((Vala.ErrorDomain)symbol).get_methods ()) {
1360                     if (method.source_reference != null)
1361                         this.range = this.range.union (new Range.from_sourceref (method.source_reference));
1362                 }
1363             } else if (symbol is Vala.Struct) {
1364                 foreach (var field in ((Vala.Struct)symbol).get_fields ()) {
1365                     if (field.source_reference != null)
1366                         this.range = this.range.union (new Range.from_sourceref (field.source_reference));
1367                 }
1368                 foreach (var method in ((Vala.Struct)symbol).get_methods ()) {
1369                     if (method.source_reference != null)
1370                         this.range = this.range.union (new Range.from_sourceref (method.source_reference));
1371                 }
1372             }
1373         }
1374         */
1375     }
1376 }