1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
#![feature(proc_macro)]

#[macro_use]
extern crate enum_primitive;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

use std::collections::HashMap;
use serde::de::Error;
use serde_json::Value;

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CancelParams {
    /// The request id to cancel.
    pub id: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidChangeTextDocumentParams {
    /// The document that did change. The version number points
    /// to the version after all provided content changes have
    /// been applied.
    #[serde(rename="textDocument")]
    pub text_document: VersionedTextDocumentIdentifier,
    /// The actual content changes.
    #[serde(rename="contentChanges")]
    pub content_changes: Vec<TextDocumentContentChangeEvent>,
}

/// Text documents are identified using a URI. On the protocol level, URIs are passed as strings. The corresponding JSON structure looks like this:
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct TextDocumentIdentifier {
    /// The text document's URI.
    pub uri: String,
}

/// An identifier to denote a specific version of a text document.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct VersionedTextDocumentIdentifier {
    /// The text document's URI.
    pub uri: String,
    /// The version number of this document.
    pub version: u64,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct TextDocumentItem {
    /// The text document's URI.
    pub uri: String,

    /// The text document's language identifier.
    #[serde(rename="languageId")]
    pub language_id: String,

    /// The version number of this document (it will strictly increase after each
    /// change, including undo/redo).
    pub version: u64,

    /// The content of the opened text document.
    pub text: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidOpenTextDocumentParams {
    /// The document that was opened.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentItem,
}

/// An event describing a change to a text document. If range and rangeLength are omitted
/// the new text is considered to be the full content of the document.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct TextDocumentContentChangeEvent {
    /// The range of the document that changed.
    pub range: Option<Range>,
    /// The length of the range that got replaced.
    #[serde(rename="rangeLength")]
    pub range_length: Option<u64>,
    /// The new text of the document.
    pub text: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidCloseTextDocumentParams {
    /// The document that was closed.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidSaveTextDocumentParams {
    /// The document that was saved.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidChangeWatchedFilesParams {
    /// The actual file events.
    pub changes: Vec<FileEvent>,
}

/// The file event type.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum FileChangeType {
    /// The file got created.
    Created = 1,
    /// The file got changed.
    Changed = 2,
    /// The file got deleted.
    Deleted = 3,
}

impl serde::Deserialize for FileChangeType {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        Ok(match try!(u8::deserialize(deserializer)) {
            1 => FileChangeType::Created,
            2 => FileChangeType::Changed,
            3 => FileChangeType::Deleted,
            _ => {
                return Err(D::Error::invalid_value("Expected a value of 1, 2 or 3 to deserialze \
                                                    to FileChangeType"))
            }
        })
    }
}

impl serde::Serialize for FileChangeType {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

/// An event describing a file change.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct FileEvent {
    /// The file's URI.
    pub uri: String,
    /// The change type.
    pub typ: FileChangeType,
}

/// Position in a text document expressed as zero-based line and character offset.
#[derive(Debug, PartialEq, Copy, Clone, Default, Deserialize, Serialize)]
pub struct Position {
    /// Line position in a document (zero-based).
    pub line: u64,
    /// Character offset on a line in a document (zero-based).
    pub character: u64,
}

#[derive(Debug, PartialEq, Copy, Clone, Default, Deserialize, Serialize)]
pub struct Range {
    /// The range's start position.
    pub start: Position,
    /// The range's end position.
    pub end: Position,
}

/// Represents a location inside a resource, such as a line inside a text file.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Location {
    pub uri: String,
    pub range: Range,
}

/// A parameter literal used in requests to pass a text document and a position inside that document.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct TextDocumentPositionParams {
    /// The text document.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
    /// The position inside the text document.
    pub position: Position,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct InitializeParams {
    /// The process Id of the parent process that started
    /// the server.
    #[serde(rename="processId")]
    pub process_id: u64,

    /// The rootPath of the workspace. Is null
    /// if no folder is open.
    #[serde(rename="rootPath")]
    pub root_path: Option<String>,

    /// The capabilities provided by the client (editor)
    pub capabilities: ClientCapabilities,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ClientCapabilities {
    _dummy: Option<()>,
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct InitializeResult {
    pub capabilities: ServerCapabilities,
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct InitializeError {
    /// Indicates whether the client should retry to send the
    /// initilize request after showing the message provided
    /// in the ResponseError.
    pub retry: bool,
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct ServerCapabilities {
    /// Defines how text documents are synced.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="textDocumentSync")]
    pub text_document_sync: Option<TextDocumentSyncKind>,
    /// The server provides hover support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="hoverProvider")]
    pub hover_provider: Option<bool>,
    /// The server provides completion support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="completionProvider")]
    pub completion_provider: Option<CompletionOptions>,
    /// The server provides signature help support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="signatureHelpProvider")]
    pub signature_help_provider: Option<SignatureHelpOptions>,
    /// The server provides goto definition support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="definitionProvider")]
    pub definition_provider: Option<bool>,
    /// The server provides find references support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="referencesProvider")]
    pub references_provider: Option<bool>,
    /// The server provides document highlight support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="documentHighlightProvider")]
    pub document_highlight_provider: Option<bool>,
    /// The server provides document symbol support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="documentSymbolProvider")]
    pub document_symbol_provider: Option<bool>,
    /// The server provides workspace symbol support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="workspaceSymbolProvider")]
    pub workspace_symbol_provider: Option<bool>,
    /// The server provides code actions.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="codeActionProvider")]
    pub code_action_provider: Option<bool>,
    /// The server provides code lens.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="codeLensProvider")]
    pub code_lens_provider: Option<CodeLensOptions>,
    /// The server provides document formatting.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="documentFormattingProvider")]
    pub document_formatting_provider: Option<bool>,
    /// The server provides document range formatting.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="documentRangeFormattingProvider")]
    pub document_range_formatting_provider: Option<bool>,
    /// The server provides document formatting on typing.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="documentOnTypeFormattingProvider")]
    pub document_on_type_formatting_provider: Option<DocumentOnTypeFormattingOptions>,
    /// The server provides rename support.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="renameProvider")]
    pub rename_provider: Option<bool>,
}

/// Defines how the host (editor) should sync document changes to the language server.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TextDocumentSyncKind {
    /// Documents should not be synced at all.
    None = 0,
    /// Documents are synced by always sending the full content of the document.
    Full = 1,
    /// Documents are synced by sending the full content on open. After that only
    /// incremental updates to the document are sent.
    Incremental = 2,
}

impl serde::Deserialize for TextDocumentSyncKind {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        Ok(match try!(u8::deserialize(deserializer)) {
            0 => TextDocumentSyncKind::None,
            1 => TextDocumentSyncKind::Full,
            2 => TextDocumentSyncKind::Incremental,
            _ => {
                return Err(D::Error::invalid_value("Expected a value between 1 and 2 (inclusive) \
                                                    to deserialize to TextDocumentSyncKind"))
            }
        })
    }
}

impl serde::Serialize for TextDocumentSyncKind {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

/// Completion options.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct CompletionOptions {
    /// The server provides support to resolve additional information for a completion item.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="resolveProvider")]
    pub resolve_provider: Option<bool>,

    /// The characters that trigger completion automatically.
    #[serde(rename="triggerCharacters")]
    pub trigger_characters: Vec<String>,
}

/// Signature help options.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct SignatureHelpOptions {
    /// The characters that trigger signature help automatically.
    #[serde(skip_serializing_if="Vec::is_empty")]
    #[serde(rename="triggerCharacters")]
    pub trigger_characters: Vec<String>,
}

/// Code Lens options.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CodeLensOptions {
    /// Code lens has a resolve provider as well.
    #[serde(rename="resolveProvider")]
    pub resolve_provider: Option<bool>,
}

/// Format document on type options
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DocumentOnTypeFormattingOptions {
    /// A character on which formatting should be triggered, like `}`.
    #[serde(rename="firstTriggerCharacter")]
    pub first_trigger_character: String,
    /// More trigger characters.
    #[serde(skip_serializing_if="Vec::is_empty")]
    #[serde(rename="moreTriggerCharacter")]
    pub more_trigger_character: Vec<String>,
}

/// A textual edit applicable to a text document.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct TextEdit {
    /// The range of the text document to be manipulated. To insert
    /// text into a document create a range where start === end.
    pub range: Range,
    /// The string to be inserted. For delete operations use an
    /// empty string.
    #[serde(rename="newText")]
    pub new_text: String,
}

/// A workspace edit represents changes to many resources managed in the workspace.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct WorkspaceEdit {
    /// Holds changes to existing resources.
    pub changes: HashMap<String, Vec<TextEdit>>,
}

/// Represents a collection of [completion items](#CompletionItem) to be presented
/// in the editor.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct CompletionList {
    /// This list it not complete. Further typing should result in recomputing
    /// this list.
    #[serde(rename="isIncomplete")]
    pub is_incomplete: bool,
    /// The completion items.
    pub items: Vec<CompletionItem>,
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct CompletionItem {
    /// The label of this completion item. By default
    /// also the text that is inserted when selecting
    /// this completion.
    pub label: String,
    /// The kind of this completion item. Based of the kind
    /// an icon is chosen by the editor.
    #[serde(skip_serializing_if="Option::is_none")]
    pub kind: Option<CompletionItemKind>,
    /// A human-readable string with additional information
    /// about this item, like type or symbol information.
    #[serde(skip_serializing_if="Option::is_none")]
    pub detail: Option<String>,
    /// A human-readable string that represents a doc-comment.
    #[serde(skip_serializing_if="Option::is_none")]
    pub documentation: Option<String>,
    /// A string that shoud be used when comparing this item
    /// with other items. When `falsy` the label is used.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="sortText")]
    pub sort_text: Option<String>,
    /// A string that should be used when filtering a set of
    /// completion items. When `falsy` the label is used.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="filterText")]
    pub filter_text: Option<String>,
    /// A string that should be inserted a document when selecting
    /// this completion. When `falsy` the label is used.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="insertText")]
    pub insert_text: Option<String>,
    /// An edit which is applied to a document when selecting
    /// this completion. When an edit is provided the value of
    /// insertText is ignored.
    #[serde(skip_serializing_if="Option::is_none")]
    #[serde(rename="textEdit")]
    pub text_edit: Option<TextEdit>,
    /// An data entry field that is preserved on a completion item between
    /// a completion and a completion resolve request.
    #[serde(skip_serializing_if="Option::is_none")]
    pub data: Option<Value>,
}

enum_from_primitive!{
/// The kind of a completion entry.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum CompletionItemKind {
    Text = 1,
    Method = 2,
    Function = 3,
    Constructor = 4,
    Field = 5,
    Variable = 6,
    Class = 7,
    Interface = 8,
    Module = 9,
    Property = 10,
    Unit = 11,
    Value = 12,
    Enum = 13,
    Keyword = 14,
    Snippet = 15,
    Color = 16,
    File = 17,
    Reference = 18,
}
}

impl serde::Deserialize for CompletionItemKind {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        use enum_primitive::FromPrimitive;

        let i = try!(u8::deserialize(deserializer));
        CompletionItemKind::from_u8(i).ok_or_else(|| {
            D::Error::invalid_value("Expected a value between 1 and 18 (inclusive) to deserialize \
                                     to CompletionItemKind")
        })
    }
}

impl serde::Serialize for CompletionItemKind {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

/// The result of a hover request.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Hover {
    /// The hover's content
    pub contents: Vec<MarkedString>,

    /// An optional range
    pub range: Option<Range>,
}

#[derive(Debug, PartialEq)]
pub enum MarkedString {
    String(String),
    LanguageString { language: String, value: String },
}

impl serde::Deserialize for MarkedString {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        String::deserialize(deserializer)
            .map(MarkedString::String)
            .or_else(|_| {
                use std::borrow::Cow;

                #[derive(Deserialize, Serialize)]
                struct Variant<'s> {
                    language: Cow<'s, str>,
                    value: Cow<'s, str>,
                }
                Variant::deserialize(deserializer).map(|variant| {
                    MarkedString::LanguageString {
                        language: variant.language.into_owned(),
                        value: variant.value.into_owned(),
                    }
                })
            })
    }
}

impl serde::Serialize for MarkedString {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        match *self {
            MarkedString::String(ref s) => serializer.serialize_str(s),
            MarkedString::LanguageString { ref language, ref value } => {
                #[derive(Serialize)]
                struct Variant<'s> {
                    language: &'s str,
                    value: &'s str,
                }
                Variant {
                        language: language,
                        value: value,
                    }
                    .serialize(serializer)
            }
        }
    }
}

/// Signature help represents the signature of something
/// callable. There can be multiple signature but only one
/// active and only one active parameter.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct SignatureHelp {
    /// One or more signatures.
    pub signatures: Vec<SignatureInformation>,

    /// The active signature.
    #[serde(rename="activeSignature")]
    pub active_signature: Option<u64>,

    /// The active parameter of the active signature.
    #[serde(rename="activeParameter")]
    pub active_parameter: Option<u64>,
}

/// Represents the signature of something callable. A signature
/// can have a label, like a function-name, a doc-comment, and
/// a set of parameters.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct SignatureInformation {
    /// The label of this signature. Will be shown in
    /// the UI.
    pub label: String,

    /// The human-readable doc-comment of this signature. Will be shown
    /// in the UI but can be omitted.
    pub documentation: String,

    /// The parameters of this signature.
    #[serde(skip_serializing_if="Vec::is_empty")]
    pub parameters: Vec<ParameterInformation>,
}

/// Represents a parameter of a callable-signature. A parameter can
/// have a label and a doc-comment.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ParameterInformation {
    /// The label of this signature. Will be shown in
    /// the UI.
    pub label: String,

    /// The human-readable doc-comment of this signature. Will be shown
    /// in the UI but can be omitted.
    #[serde(skip_serializing_if="String::is_empty")]
    pub documentation: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ReferenceParams {
    /// The text document.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
    /// The position inside the text document.
    pub position: Position,

    pub context: ReferenceContext,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ReferenceContext {
    /// Include the declaration of the current symbol.
    #[serde(rename="includeDeclaration")]
    pub include_declaration: bool,
}

/// A document highlight is a range inside a text document which deserves
/// special attention. Usually a document highlight is visualized by changing
/// the background color of its range.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DocumentHighlight {
    /// The range this highlight applies to.
    pub range: Range,

    /// The highlight kind, default is DocumentHighlightKind.Text.
    pub kind: Option<DocumentHighlightKind>,
}

/// A document highlight kind.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum DocumentHighlightKind {
    /// A textual occurrance.
    Text = 1,

    /// Read-access of a symbol, like reading a variable.
    Read = 2,

    /// Write-access of a symbol, like writing to a variable.
    Write = 3,
}

impl serde::Deserialize for DocumentHighlightKind {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        Ok(match try!(u8::deserialize(deserializer)) {
            1 => DocumentHighlightKind::Text,
            2 => DocumentHighlightKind::Read,
            3 => DocumentHighlightKind::Write,
            _ => {
                return Err(D::Error::invalid_value("Expected a value of 1, 2, or 3 to \
                                                    deserialze to DocumentHighlightKiny"))
            }
        })
    }
}

impl serde::Serialize for DocumentHighlightKind {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DocumentSymbolParams {
    /// The text document.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
}

/// Represents information about programming constructs like variables, classes,
/// interfaces etc.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct SymbolInformation {
    /// The name of this symbol.
    pub name: String,

    /// The kind of this symbol.
    pub kind: SymbolKind,

    /// The location of this symbol.
    pub location: Location,

    /// The name of the symbol containing this symbol.
    #[serde(rename="containerName")]
    pub container_name: String,
}

/// A symbol kind.
enum_from_primitive!{
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum SymbolKind {
    File = 1,
    Module = 2,
    Namespace = 3,
    Package = 4,
    Class = 5,
    Method = 6,
    Property = 7,
    Field = 8,
    Constructor = 9,
    Enum = 10,
    Interface = 11,
    Function = 12,
    Variable = 13,
    Constant = 14,
    String = 15,
    Number = 16,
    Boolean = 17,
    Array = 18,
}
}

impl serde::Deserialize for SymbolKind {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        use enum_primitive::FromPrimitive;

        let i = try!(u8::deserialize(deserializer));
        SymbolKind::from_u8(i).ok_or_else(|| {
            D::Error::invalid_value("Expected a value between 1 and 18 (inclusive) to deserialize \
                                     to SymbolKind")
        })
    }
}

impl serde::Serialize for SymbolKind {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

/// The parameters of a Workspace Symbol Request.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct WorkspaceSymbolParams {
    /// A non-empty query string
    pub query: String,
}

/// Params for the CodeActionRequest
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CodeActionParams {
    /// The document in which the command was invoked.
    pub text_document: TextDocumentIdentifier,

    /// The range for which the command was invoked.
    pub range: Range,

    /// Context carrying additional information.
    pub context: CodeActionContext,
}

/// Contains additional diagnostic information about the context in which
/// a code action is run.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CodeActionContext {
    /// An array of diagnostics.
    pub diagnostics: Vec<Diagnostic>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CodeLensParams {
    /// The document to request code lens for.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,
}

/// A code lens represents a command that should be shown along with
/// source text, like the number of references, a way to run tests, etc.
/// A code lens is _unresolved_ when no command is associated to it. For performance
/// reasons the creation of a code lens and resolving should be done in two stages.
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CodeLens {
    /// The range in which this code lens is valid. Should only span a single line.
    pub range: Range,

    /// The command this code lens represents.
    pub command: Option<Command>,

    /// A data entry field that is preserved on a code lens item between
    /// a code lens and a code lens resolve request.
    pub data: Option<Value>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct RenameParams {
    /// The document to format.
    #[serde(rename="textDocument")]
    pub text_document: TextDocumentIdentifier,

    /// The position at which this request was sent.
    pub position: Position,

    /// The new name of the symbol. If the given name is not valid the
    /// request must return a [ResponseError](#ResponseError) with an
    /// appropriate message set.
    #[serde(rename="newName")]
    pub new_name: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ShowMessageParams {
    /// The message type. See {@link MessageType}
    #[serde(rename="type")]
    pub typ: MessageType,

    /// The actual message
    pub message: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct ShowMessageRequestParams {
    /// The message type. See {@link MessageType}
    #[serde(rename="type")]
    pub typ: MessageType,

    /// The actual message
    pub message: String,

    /// The message action items to present.
    #[serde(skip_serializing_if="Vec::is_empty")]
    pub actions: Vec<MessageActionItem>,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct MessageActionItem {
    /// A short title like 'Retry', 'Open Log' etc.
    pub title: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct LogMessageParams {
    /// The message type. See {@link MessageType}
    #[serde(rename="type")]
    pub typ: MessageType,

    /// The actual message
    pub message: String,
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct DidChangeConfigurationParams {
    /// The actual changed settings
    pub settings: Value,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MessageType {
    /// An error message.
    Error = 1,
    /// A warning message.
    Warning = 2,
    /// An information message.
    Info = 3,
    /// A log message.
    Log = 4,
}

impl serde::Deserialize for MessageType {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        Ok(match try!(u8::deserialize(deserializer)) {
            1 => MessageType::Error,
            2 => MessageType::Warning,
            3 => MessageType::Info,
            4 => MessageType::Log,
            _ => {
                return Err(D::Error::invalid_value("Expected a value of 1, 2, 3 or 4 to \
                                                    deserialze to MessageType"))
            }
        })
    }
}

impl serde::Serialize for MessageType {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct PublishDiagnosticsParams {
    /// The URI for which diagnostic information is reported.
    pub uri: String,

    /// An array of diagnostic information items.
    pub diagnostics: Vec<Diagnostic>,
}

#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct Diagnostic {
    /// The range at which the message applies
    pub range: Range,

    /// The diagnostic's severity. Can be omitted. If omitted it is up to the
    /// client to interpret diagnostics as error, warning, info or hint.
    pub severity: Option<DiagnosticSeverity>,

    /// The diagnostic's code. Can be omitted.
    pub code: String,
    /// number | string;

    /// A human-readable string describing the source of this
    /// diagnostic, e.g. 'typescript' or 'super lint'.
    pub source: Option<String>,

    /// The diagnostic's message.
    pub message: String,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DiagnosticSeverity {
    /// Reports an error.
    Error = 1,
    /// Reports a warning.
    Warning = 2,
    /// Reports an information.
    Information = 3,
    /// Reports a hint.
    Hint = 4,
}

impl serde::Deserialize for DiagnosticSeverity {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        Ok(match try!(u8::deserialize(deserializer)) {
            1 => DiagnosticSeverity::Error,
            2 => DiagnosticSeverity::Warning,
            3 => DiagnosticSeverity::Information,
            4 => DiagnosticSeverity::Hint,
            _ => {
                return Err(D::Error::invalid_value("Expected a value of 1, 2, 3 or 4 to \
                                                    deserialze to DiagnosticSeverity"))
            }
        })
    }
}

impl serde::Serialize for DiagnosticSeverity {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        serializer.serialize_u8(*self as u8)
    }
}

/// Represents a reference to a command. Provides a title which will be used to represent a command in the UI and, optionally, an array of arguments which will be passed to the command handler function when invoked.
#[derive(Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct Command {
    /// Title of the command, like `save`.
    pub title: String,
    /// The identifier of the actual command handler.
    pub command: String,
    /// Arguments that the command handler should be
    /// invoked with.
    #[serde(skip_serializing_if="Vec::is_empty")]
    pub arguments: Vec<Value>,
}