LCOV - code coverage report
Current view: top level - clang/tools/extra/clangd - Protocol.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 0 4 0.0 %
Date: 2017-09-14 15:23:50 Functions: 0 2 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===--- Protocol.h - Language Server Protocol Implementation ---*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file contains structs based on the LSP specification at
      11             : // https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md
      12             : //
      13             : // This is not meant to be a complete implementation, new interfaces are added
      14             : // when they're needed.
      15             : //
      16             : // Each struct has a parse and unparse function, that converts back and forth
      17             : // between the struct and a JSON representation.
      18             : //
      19             : //===----------------------------------------------------------------------===//
      20             : 
      21             : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H
      22             : #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H
      23             : 
      24             : #include "llvm/ADT/Optional.h"
      25             : #include "llvm/Support/YAMLParser.h"
      26             : #include <string>
      27             : #include <vector>
      28             : 
      29             : namespace clang {
      30             : namespace clangd {
      31             : 
      32           0 : struct URI {
      33             :   std::string uri;
      34             :   std::string file;
      35             : 
      36             :   static URI fromUri(llvm::StringRef uri);
      37             :   static URI fromFile(llvm::StringRef file);
      38             : 
      39             :   static URI parse(llvm::yaml::ScalarNode *Param);
      40             :   static std::string unparse(const URI &U);
      41             : 
      42             :   friend bool operator==(const URI &LHS, const URI &RHS) {
      43             :     return LHS.uri == RHS.uri;
      44             :   }
      45             : 
      46             :   friend bool operator!=(const URI &LHS, const URI &RHS) {
      47             :     return !(LHS == RHS);
      48             :   }
      49             : 
      50             :   friend bool operator<(const URI &LHS, const URI &RHS) {
      51             :     return LHS.uri < RHS.uri;
      52             :   }
      53             : };
      54             : 
      55             : struct TextDocumentIdentifier {
      56             :   /// The text document's URI.
      57             :   URI uri;
      58             : 
      59             :   static llvm::Optional<TextDocumentIdentifier>
      60             :   parse(llvm::yaml::MappingNode *Params);
      61             : };
      62             : 
      63             : struct Position {
      64             :   /// Line position in a document (zero-based).
      65             :   int line;
      66             : 
      67             :   /// Character offset on a line in a document (zero-based).
      68             :   int character;
      69             : 
      70             :   friend bool operator==(const Position &LHS, const Position &RHS) {
      71             :     return std::tie(LHS.line, LHS.character) ==
      72             :            std::tie(RHS.line, RHS.character);
      73             :   }
      74             :   friend bool operator<(const Position &LHS, const Position &RHS) {
      75             :     return std::tie(LHS.line, LHS.character) <
      76             :            std::tie(RHS.line, RHS.character);
      77             :   }
      78             : 
      79             :   static llvm::Optional<Position> parse(llvm::yaml::MappingNode *Params);
      80             :   static std::string unparse(const Position &P);
      81             : };
      82             : 
      83             : struct Range {
      84             :   /// The range's start position.
      85             :   Position start;
      86             : 
      87             :   /// The range's end position.
      88             :   Position end;
      89             : 
      90             :   friend bool operator==(const Range &LHS, const Range &RHS) {
      91             :     return std::tie(LHS.start, LHS.end) == std::tie(RHS.start, RHS.end);
      92             :   }
      93             :   friend bool operator<(const Range &LHS, const Range &RHS) {
      94             :     return std::tie(LHS.start, LHS.end) < std::tie(RHS.start, RHS.end);
      95             :   }
      96             : 
      97             :   static llvm::Optional<Range> parse(llvm::yaml::MappingNode *Params);
      98             :   static std::string unparse(const Range &P);
      99             : };
     100             : 
     101           0 : struct Location {
     102             :   /// The text document's URI.
     103             :   URI uri;
     104             :   Range range;
     105             : 
     106             :   friend bool operator==(const Location &LHS, const Location &RHS) {
     107             :     return LHS.uri == RHS.uri && LHS.range == RHS.range;
     108             :   }
     109             : 
     110             :   friend bool operator!=(const Location &LHS, const Location &RHS) {
     111             :     return !(LHS == RHS);
     112             :   }
     113             : 
     114             :   friend bool operator<(const Location &LHS, const Location &RHS) {
     115             :     return std::tie(LHS.uri, LHS.range) < std::tie(RHS.uri, RHS.range);
     116             :   }
     117             : 
     118             :   static std::string unparse(const Location &P);
     119             : };
     120             : 
     121             : struct Metadata {
     122             :   std::vector<std::string> extraFlags;
     123             : 
     124             :   static llvm::Optional<Metadata> parse(llvm::yaml::MappingNode *Params);
     125             : };
     126             : 
     127           0 : struct TextEdit {
     128             :   /// The range of the text document to be manipulated. To insert
     129             :   /// text into a document create a range where start === end.
     130             :   Range range;
     131             : 
     132             :   /// The string to be inserted. For delete operations use an
     133             :   /// empty string.
     134             :   std::string newText;
     135             : 
     136             :   static llvm::Optional<TextEdit> parse(llvm::yaml::MappingNode *Params);
     137             :   static std::string unparse(const TextEdit &P);
     138             : };
     139             : 
     140             : struct TextDocumentItem {
     141             :   /// The text document's URI.
     142             :   URI uri;
     143             : 
     144             :   /// The text document's language identifier.
     145             :   std::string languageId;
     146             : 
     147             :   /// The version number of this document (it will strictly increase after each
     148             :   int version;
     149             : 
     150             :   /// The content of the opened text document.
     151             :   std::string text;
     152             : 
     153             :   static llvm::Optional<TextDocumentItem>
     154             :   parse(llvm::yaml::MappingNode *Params);
     155             : };
     156             : 
     157             : struct DidOpenTextDocumentParams {
     158             :   /// The document that was opened.
     159             :   TextDocumentItem textDocument;
     160             : 
     161             :   /// Extension storing per-file metadata, such as compilation flags.
     162             :   llvm::Optional<Metadata> metadata;
     163             : 
     164             :   static llvm::Optional<DidOpenTextDocumentParams>
     165             :   parse(llvm::yaml::MappingNode *Params);
     166             : };
     167             : 
     168             : struct DidCloseTextDocumentParams {
     169             :   /// The document that was closed.
     170             :   TextDocumentIdentifier textDocument;
     171             : 
     172             :   static llvm::Optional<DidCloseTextDocumentParams>
     173             :   parse(llvm::yaml::MappingNode *Params);
     174             : };
     175             : 
     176             : struct TextDocumentContentChangeEvent {
     177             :   /// The new text of the document.
     178             :   std::string text;
     179             : 
     180             :   static llvm::Optional<TextDocumentContentChangeEvent>
     181             :   parse(llvm::yaml::MappingNode *Params);
     182             : };
     183             : 
     184             : struct DidChangeTextDocumentParams {
     185             :   /// The document that did change. The version number points
     186             :   /// to the version after all provided content changes have
     187             :   /// been applied.
     188             :   TextDocumentIdentifier textDocument;
     189             : 
     190             :   /// The actual content changes.
     191             :   std::vector<TextDocumentContentChangeEvent> contentChanges;
     192             : 
     193             :   static llvm::Optional<DidChangeTextDocumentParams>
     194             :   parse(llvm::yaml::MappingNode *Params);
     195             : };
     196             : 
     197             : struct FormattingOptions {
     198             :   /// Size of a tab in spaces.
     199             :   int tabSize;
     200             : 
     201             :   /// Prefer spaces over tabs.
     202             :   bool insertSpaces;
     203             : 
     204             :   static llvm::Optional<FormattingOptions>
     205             :   parse(llvm::yaml::MappingNode *Params);
     206             :   static std::string unparse(const FormattingOptions &P);
     207             : };
     208             : 
     209             : struct DocumentRangeFormattingParams {
     210             :   /// The document to format.
     211             :   TextDocumentIdentifier textDocument;
     212             : 
     213             :   /// The range to format
     214             :   Range range;
     215             : 
     216             :   /// The format options
     217             :   FormattingOptions options;
     218             : 
     219             :   static llvm::Optional<DocumentRangeFormattingParams>
     220             :   parse(llvm::yaml::MappingNode *Params);
     221             : };
     222             : 
     223             : struct DocumentOnTypeFormattingParams {
     224             :   /// The document to format.
     225             :   TextDocumentIdentifier textDocument;
     226             : 
     227             :   /// The position at which this request was sent.
     228             :   Position position;
     229             : 
     230             :   /// The character that has been typed.
     231             :   std::string ch;
     232             : 
     233             :   /// The format options.
     234             :   FormattingOptions options;
     235             : 
     236             :   static llvm::Optional<DocumentOnTypeFormattingParams>
     237             :   parse(llvm::yaml::MappingNode *Params);
     238             : };
     239             : 
     240             : struct DocumentFormattingParams {
     241             :   /// The document to format.
     242             :   TextDocumentIdentifier textDocument;
     243             : 
     244             :   /// The format options
     245             :   FormattingOptions options;
     246             : 
     247             :   static llvm::Optional<DocumentFormattingParams>
     248             :   parse(llvm::yaml::MappingNode *Params);
     249             : };
     250             : 
     251             : struct Diagnostic {
     252             :   /// The range at which the message applies.
     253             :   Range range;
     254             : 
     255             :   /// The diagnostic's severity. Can be omitted. If omitted it is up to the
     256             :   /// client to interpret diagnostics as error, warning, info or hint.
     257             :   int severity;
     258             : 
     259             :   /// The diagnostic's message.
     260             :   std::string message;
     261             : 
     262             :   friend bool operator==(const Diagnostic &LHS, const Diagnostic &RHS) {
     263             :     return std::tie(LHS.range, LHS.severity, LHS.message) ==
     264             :            std::tie(RHS.range, RHS.severity, RHS.message);
     265             :   }
     266             :   friend bool operator<(const Diagnostic &LHS, const Diagnostic &RHS) {
     267             :     return std::tie(LHS.range, LHS.severity, LHS.message) <
     268             :            std::tie(RHS.range, RHS.severity, RHS.message);
     269             :   }
     270             : 
     271             :   static llvm::Optional<Diagnostic> parse(llvm::yaml::MappingNode *Params);
     272             : };
     273             : 
     274             : struct CodeActionContext {
     275             :   /// An array of diagnostics.
     276             :   std::vector<Diagnostic> diagnostics;
     277             : 
     278             :   static llvm::Optional<CodeActionContext>
     279             :   parse(llvm::yaml::MappingNode *Params);
     280             : };
     281             : 
     282             : struct CodeActionParams {
     283             :   /// The document in which the command was invoked.
     284             :   TextDocumentIdentifier textDocument;
     285             : 
     286             :   /// The range for which the command was invoked.
     287             :   Range range;
     288             : 
     289             :   /// Context carrying additional information.
     290             :   CodeActionContext context;
     291             : 
     292             :   static llvm::Optional<CodeActionParams>
     293             :   parse(llvm::yaml::MappingNode *Params);
     294             : };
     295             : 
     296             : struct TextDocumentPositionParams {
     297             :   /// The text document.
     298             :   TextDocumentIdentifier textDocument;
     299             : 
     300             :   /// The position inside the text document.
     301             :   Position position;
     302             : 
     303             :   static llvm::Optional<TextDocumentPositionParams>
     304             :   parse(llvm::yaml::MappingNode *Params);
     305             : };
     306             : 
     307             : /// The kind of a completion entry.
     308             : enum class CompletionItemKind {
     309             :   Missing = 0,
     310             :   Text = 1,
     311             :   Method = 2,
     312             :   Function = 3,
     313             :   Constructor = 4,
     314             :   Field = 5,
     315             :   Variable = 6,
     316             :   Class = 7,
     317             :   Interface = 8,
     318             :   Module = 9,
     319             :   Property = 10,
     320             :   Unit = 11,
     321             :   Value = 12,
     322             :   Enum = 13,
     323             :   Keyword = 14,
     324             :   Snippet = 15,
     325             :   Color = 16,
     326             :   File = 17,
     327             :   Reference = 18,
     328             : };
     329             : 
     330             : /// Defines whether the insert text in a completion item should be interpreted
     331             : /// as plain text or a snippet.
     332             : enum class InsertTextFormat {
     333             :   Missing = 0,
     334             :   /// The primary text to be inserted is treated as a plain string.
     335             :   PlainText = 1,
     336             :   /// The primary text to be inserted is treated as a snippet.
     337             :   ///
     338             :   /// A snippet can define tab stops and placeholders with `$1`, `$2`
     339             :   /// and `${3:foo}`. `$0` defines the final tab stop, it defaults to the end
     340             :   /// of the snippet. Placeholders with equal identifiers are linked, that is
     341             :   /// typing in one will update others too.
     342             :   ///
     343             :   /// See also:
     344             :   /// https//github.com/Microsoft/vscode/blob/master/src/vs/editor/contrib/snippet/common/snippet.md
     345             :   Snippet = 2,
     346             : };
     347             : 
     348           0 : struct CompletionItem {
     349             :   /// The label of this completion item. By default also the text that is
     350             :   /// inserted when selecting this completion.
     351             :   std::string label;
     352             : 
     353             :   /// The kind of this completion item. Based of the kind an icon is chosen by
     354             :   /// the editor.
     355             :   CompletionItemKind kind = CompletionItemKind::Missing;
     356             : 
     357             :   /// A human-readable string with additional information about this item, like
     358             :   /// type or symbol information.
     359             :   std::string detail;
     360             : 
     361             :   /// A human-readable string that represents a doc-comment.
     362             :   std::string documentation;
     363             : 
     364             :   /// A string that should be used when comparing this item with other items.
     365             :   /// When `falsy` the label is used.
     366             :   std::string sortText;
     367             : 
     368             :   /// A string that should be used when filtering a set of completion items.
     369             :   /// When `falsy` the label is used.
     370             :   std::string filterText;
     371             : 
     372             :   /// A string that should be inserted to a document when selecting this
     373             :   /// completion. When `falsy` the label is used.
     374             :   std::string insertText;
     375             : 
     376             :   /// The format of the insert text. The format applies to both the `insertText`
     377             :   /// property and the `newText` property of a provided `textEdit`.
     378             :   InsertTextFormat insertTextFormat = InsertTextFormat::Missing;
     379             : 
     380             :   /// An edit which is applied to a document when selecting this completion.
     381             :   /// When an edit is provided `insertText` is ignored.
     382             :   ///
     383             :   /// Note: The range of the edit must be a single line range and it must
     384             :   /// contain the position at which completion has been requested.
     385             :   llvm::Optional<TextEdit> textEdit;
     386             : 
     387             :   /// An optional array of additional text edits that are applied when selecting
     388             :   /// this completion. Edits must not overlap with the main edit nor with
     389             :   /// themselves.
     390             :   std::vector<TextEdit> additionalTextEdits;
     391             : 
     392             :   // TODO(krasimir): The following optional fields defined by the language
     393             :   // server protocol are unsupported:
     394             :   //
     395             :   // command?: Command - An optional command that is executed *after* inserting
     396             :   //                     this completion.
     397             :   //
     398             :   // data?: any - A data entry field that is preserved on a completion item
     399             :   //              between a completion and a completion resolve request.
     400             :   static std::string unparse(const CompletionItem &P);
     401             : };
     402             : 
     403             : } // namespace clangd
     404             : } // namespace clang
     405             : 
     406             : #endif

Generated by: LCOV version 1.13