[PATCH] D52616: Introduce completionItemKind capability support.

2018-09-27 Thread Kadir Cetinkaya via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rCTE343237: Introduce completionItemKind capability support. 
(authored by kadircet, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D52616?vs=167346=167347#toc

Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52616

Files:
  clangd/ClangdLSPServer.cpp
  clangd/ClangdLSPServer.h
  clangd/Protocol.cpp
  clangd/Protocol.h

Index: clangd/Protocol.h
===
--- clangd/Protocol.h
+++ clangd/Protocol.h
@@ -233,13 +233,65 @@
 };
 bool fromJSON(const llvm::json::Value &, CompletionItemClientCapabilities &);
 
+/// The kind of a completion entry.
+enum class CompletionItemKind {
+  Missing = 0,
+  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,
+  Folder = 19,
+  EnumMember = 20,
+  Constant = 21,
+  Struct = 22,
+  Event = 23,
+  Operator = 24,
+  TypeParameter = 25,
+};
+bool fromJSON(const llvm::json::Value &, CompletionItemKind &);
+
+struct CompletionItemKindCapabilities {
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional> valueSet;
+};
+// Discards unknown CompletionItemKinds.
+bool fromJSON(const llvm::json::Value &, std::vector &);
+bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &);
+
+constexpr auto CompletionItemKindMin =
+static_cast(CompletionItemKind::Text);
+constexpr auto CompletionItemKindMax =
+static_cast(CompletionItemKind::TypeParameter);
+using CompletionItemKindBitset = std::bitset;
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset );
+
 struct CompletionClientCapabilities {
   /// Whether completion supports dynamic registration.
   bool dynamicRegistration = false;
   /// The client supports the following `CompletionItem` specific capabilities.
   CompletionItemClientCapabilities completionItem;
-  // NOTE: not used by clangd at the moment.
-  // llvm::Optional completionItemKind;
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional completionItemKind;
 
   /// The client supports to send additional context information for a
   /// `textDocument/completion` request.
@@ -305,6 +357,7 @@
   /// value.
   llvm::Optional> valueSet;
 };
+// Discards unknown SymbolKinds.
 bool fromJSON(const llvm::json::Value &, std::vector &);
 bool fromJSON(const llvm::json::Value &, SymbolKindCapabilities &);
 SymbolKind adjustKindToCapability(SymbolKind Kind,
@@ -683,36 +736,6 @@
 };
 llvm::json::Value toJSON(const Hover );
 
-/// The kind of a completion entry.
-enum class CompletionItemKind {
-  Missing = 0,
-  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,
-  Folder = 19,
-  EnumMember = 20,
-  Constant = 21,
-  Struct = 22,
-  Event = 23,
-  Operator = 24,
-  TypeParameter = 25,
-};
-
 /// Defines whether the insert text in a completion item should be interpreted
 /// as plain text or a snippet.
 enum class InsertTextFormat {
Index: clangd/Protocol.cpp
===
--- clangd/Protocol.cpp
+++ clangd/Protocol.cpp
@@ -496,6 +496,57 @@
   return std::move(Result);
 }
 
+bool fromJSON(const json::Value , CompletionItemKind ) {
+  if (auto T = E.getAsInteger()) {
+if (*T < static_cast(CompletionItemKind::Text) ||
+*T > static_cast(CompletionItemKind::TypeParameter))
+  return false;
+Out = static_cast(*T);
+return true;
+  }
+  return false;
+}
+
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset ) {
+  auto KindVal = static_cast(Kind);
+  if (KindVal >= CompletionItemKindMin &&
+  KindVal <= supportedCompletionItemKinds.size() &&
+  supportedCompletionItemKinds[KindVal])
+return Kind;
+
+  switch (Kind) {
+  // Provide some fall backs for common kinds that are close enough.
+  case CompletionItemKind::Folder:
+return CompletionItemKind::File;
+  case CompletionItemKind::EnumMember:
+return CompletionItemKind::Enum;
+  case CompletionItemKind::Struct:
+return CompletionItemKind::Class;
+  default:
+return CompletionItemKind::Text;
+  }
+}
+
+bool fromJSON(const json::Value , std::vector 

[PATCH] D52616: Introduce completionItemKind capability support.

2018-09-27 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet updated this revision to Diff 167346.
kadircet marked 3 inline comments as done.
kadircet added a comment.

- Address comments.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52616

Files:
  clangd/ClangdLSPServer.cpp
  clangd/ClangdLSPServer.h
  clangd/Protocol.cpp
  clangd/Protocol.h

Index: clangd/Protocol.h
===
--- clangd/Protocol.h
+++ clangd/Protocol.h
@@ -233,13 +233,65 @@
 };
 bool fromJSON(const llvm::json::Value &, CompletionItemClientCapabilities &);
 
+/// The kind of a completion entry.
+enum class CompletionItemKind {
+  Missing = 0,
+  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,
+  Folder = 19,
+  EnumMember = 20,
+  Constant = 21,
+  Struct = 22,
+  Event = 23,
+  Operator = 24,
+  TypeParameter = 25,
+};
+bool fromJSON(const llvm::json::Value &, CompletionItemKind &);
+
+struct CompletionItemKindCapabilities {
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional> valueSet;
+};
+// Discards unknown CompletionItemKinds.
+bool fromJSON(const llvm::json::Value &, std::vector &);
+bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &);
+
+constexpr auto CompletionItemKindMin =
+static_cast(CompletionItemKind::Text);
+constexpr auto CompletionItemKindMax =
+static_cast(CompletionItemKind::TypeParameter);
+using CompletionItemKindBitset = std::bitset;
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset );
+
 struct CompletionClientCapabilities {
   /// Whether completion supports dynamic registration.
   bool dynamicRegistration = false;
   /// The client supports the following `CompletionItem` specific capabilities.
   CompletionItemClientCapabilities completionItem;
-  // NOTE: not used by clangd at the moment.
-  // llvm::Optional completionItemKind;
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional completionItemKind;
 
   /// The client supports to send additional context information for a
   /// `textDocument/completion` request.
@@ -305,6 +357,7 @@
   /// value.
   llvm::Optional> valueSet;
 };
+// Discards unknown SymbolKinds.
 bool fromJSON(const llvm::json::Value &, std::vector &);
 bool fromJSON(const llvm::json::Value &, SymbolKindCapabilities &);
 SymbolKind adjustKindToCapability(SymbolKind Kind,
@@ -683,36 +736,6 @@
 };
 llvm::json::Value toJSON(const Hover );
 
-/// The kind of a completion entry.
-enum class CompletionItemKind {
-  Missing = 0,
-  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,
-  Folder = 19,
-  EnumMember = 20,
-  Constant = 21,
-  Struct = 22,
-  Event = 23,
-  Operator = 24,
-  TypeParameter = 25,
-};
-
 /// Defines whether the insert text in a completion item should be interpreted
 /// as plain text or a snippet.
 enum class InsertTextFormat {
Index: clangd/Protocol.cpp
===
--- clangd/Protocol.cpp
+++ clangd/Protocol.cpp
@@ -496,6 +496,57 @@
   return std::move(Result);
 }
 
+bool fromJSON(const json::Value , CompletionItemKind ) {
+  if (auto T = E.getAsInteger()) {
+if (*T < static_cast(CompletionItemKind::Text) ||
+*T > static_cast(CompletionItemKind::TypeParameter))
+  return false;
+Out = static_cast(*T);
+return true;
+  }
+  return false;
+}
+
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset ) {
+  auto KindVal = static_cast(Kind);
+  if (KindVal >= CompletionItemKindMin &&
+  KindVal <= supportedCompletionItemKinds.size() &&
+  supportedCompletionItemKinds[KindVal])
+return Kind;
+
+  switch (Kind) {
+  // Provide some fall backs for common kinds that are close enough.
+  case CompletionItemKind::Folder:
+return CompletionItemKind::File;
+  case CompletionItemKind::EnumMember:
+return CompletionItemKind::Enum;
+  case CompletionItemKind::Struct:
+return CompletionItemKind::Class;
+  default:
+return CompletionItemKind::Text;
+  }
+}
+
+bool fromJSON(const json::Value , std::vector ) {
+  if (auto *A = E.getAsArray()) {
+Out.clear();
+for (size_t I = 0; I < A->size(); ++I) {
+  CompletionItemKind 

[PATCH] D52616: Introduce completionItemKind capability support.

2018-09-27 Thread Sam McCall via Phabricator via cfe-commits
sammccall accepted this revision.
sammccall added inline comments.
This revision is now accepted and ready to land.



Comment at: clangd/ClangdLSPServer.cpp:108
+  if (Params.capabilities.textDocument.completion.completionItemKind)
+for (CompletionItemKind Kind : *Params.capabilities.textDocument.completion
+.completionItemKind->valueSet)

also check if valueSet is None before dereferencing it?



Comment at: clangd/Protocol.h:265
+};
+
+bool fromJSON(const llvm::json::Value &, CompletionItemKind &);

(nit: drop blank line for consistency)



Comment at: clangd/Protocol.h:274
+};
+bool fromJSON(const llvm::json::Value &, std::vector &);
+bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &);

wasn't obvious why this is necessary vs the default...
Maybe add a comment `// Discards unknown kinds` (and to SymbolKind)?


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52616



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D52616: Introduce completionItemKind capability support.

2018-09-27 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet updated this revision to Diff 167332.
kadircet added a comment.

- Minimum CompletionItemKind is Text.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52616

Files:
  clangd/ClangdLSPServer.cpp
  clangd/ClangdLSPServer.h
  clangd/Protocol.cpp
  clangd/Protocol.h

Index: clangd/Protocol.h
===
--- clangd/Protocol.h
+++ clangd/Protocol.h
@@ -233,13 +233,65 @@
 };
 bool fromJSON(const llvm::json::Value &, CompletionItemClientCapabilities &);
 
+/// The kind of a completion entry.
+enum class CompletionItemKind {
+  Missing = 0,
+  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,
+  Folder = 19,
+  EnumMember = 20,
+  Constant = 21,
+  Struct = 22,
+  Event = 23,
+  Operator = 24,
+  TypeParameter = 25,
+};
+
+bool fromJSON(const llvm::json::Value &, CompletionItemKind &);
+
+struct CompletionItemKindCapabilities {
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional> valueSet;
+};
+bool fromJSON(const llvm::json::Value &, std::vector &);
+bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &);
+
+constexpr auto CompletionItemKindMin =
+static_cast(CompletionItemKind::Text);
+constexpr auto CompletionItemKindMax =
+static_cast(CompletionItemKind::TypeParameter);
+using CompletionItemKindBitset = std::bitset;
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset );
+
 struct CompletionClientCapabilities {
   /// Whether completion supports dynamic registration.
   bool dynamicRegistration = false;
   /// The client supports the following `CompletionItem` specific capabilities.
   CompletionItemClientCapabilities completionItem;
-  // NOTE: not used by clangd at the moment.
-  // llvm::Optional completionItemKind;
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional completionItemKind;
 
   /// The client supports to send additional context information for a
   /// `textDocument/completion` request.
@@ -683,36 +735,6 @@
 };
 llvm::json::Value toJSON(const Hover );
 
-/// The kind of a completion entry.
-enum class CompletionItemKind {
-  Missing = 0,
-  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,
-  Folder = 19,
-  EnumMember = 20,
-  Constant = 21,
-  Struct = 22,
-  Event = 23,
-  Operator = 24,
-  TypeParameter = 25,
-};
-
 /// Defines whether the insert text in a completion item should be interpreted
 /// as plain text or a snippet.
 enum class InsertTextFormat {
Index: clangd/Protocol.cpp
===
--- clangd/Protocol.cpp
+++ clangd/Protocol.cpp
@@ -496,6 +496,57 @@
   return std::move(Result);
 }
 
+bool fromJSON(const json::Value , CompletionItemKind ) {
+  if (auto T = E.getAsInteger()) {
+if (*T < static_cast(CompletionItemKind::Text) ||
+*T > static_cast(CompletionItemKind::TypeParameter))
+  return false;
+Out = static_cast(*T);
+return true;
+  }
+  return false;
+}
+
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset ) {
+  auto KindVal = static_cast(Kind);
+  if (KindVal >= CompletionItemKindMin &&
+  KindVal <= supportedCompletionItemKinds.size() &&
+  supportedCompletionItemKinds[KindVal])
+return Kind;
+
+  switch (Kind) {
+  // Provide some fall backs for common kinds that are close enough.
+  case CompletionItemKind::Folder:
+return CompletionItemKind::File;
+  case CompletionItemKind::EnumMember:
+return CompletionItemKind::Enum;
+  case CompletionItemKind::Struct:
+return CompletionItemKind::Class;
+  default:
+return CompletionItemKind::Text;
+  }
+}
+
+bool fromJSON(const json::Value , std::vector ) {
+  if (auto *A = E.getAsArray()) {
+Out.clear();
+for (size_t I = 0; I < A->size(); ++I) {
+  CompletionItemKind KindOut;
+  if (fromJSON((*A)[I], KindOut))
+Out.push_back(KindOut);
+}
+return true;
+  }
+  return false;
+}
+
+bool fromJSON(const json::Value , CompletionItemKindCapabilities ) {
+  json::ObjectMapper O(Params);
+  return O && O.map("valueSet", R.valueSet);
+}
+
 json::Value toJSON(const CompletionItem ) {
   

[PATCH] D52616: Introduce completionItemKind capability support.

2018-09-27 Thread Kadir Cetinkaya via Phabricator via cfe-commits
kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added subscribers: cfe-commits, arphaman, jkorous, ioeric, ilya-biryukov.

Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52616

Files:
  clangd/ClangdLSPServer.cpp
  clangd/ClangdLSPServer.h
  clangd/Protocol.cpp
  clangd/Protocol.h

Index: clangd/Protocol.h
===
--- clangd/Protocol.h
+++ clangd/Protocol.h
@@ -233,13 +233,65 @@
 };
 bool fromJSON(const llvm::json::Value &, CompletionItemClientCapabilities &);
 
+/// The kind of a completion entry.
+enum class CompletionItemKind {
+  Missing = 0,
+  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,
+  Folder = 19,
+  EnumMember = 20,
+  Constant = 21,
+  Struct = 22,
+  Event = 23,
+  Operator = 24,
+  TypeParameter = 25,
+};
+
+bool fromJSON(const llvm::json::Value &, CompletionItemKind &);
+
+struct CompletionItemKindCapabilities {
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional> valueSet;
+};
+bool fromJSON(const llvm::json::Value &, std::vector &);
+bool fromJSON(const llvm::json::Value &, CompletionItemKindCapabilities &);
+
+constexpr auto CompletionItemKindMin =
+static_cast(CompletionItemKind::Text);
+constexpr auto CompletionItemKindMax =
+static_cast(CompletionItemKind::TypeParameter);
+using CompletionItemKindBitset = std::bitset;
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset );
+
 struct CompletionClientCapabilities {
   /// Whether completion supports dynamic registration.
   bool dynamicRegistration = false;
   /// The client supports the following `CompletionItem` specific capabilities.
   CompletionItemClientCapabilities completionItem;
-  // NOTE: not used by clangd at the moment.
-  // llvm::Optional completionItemKind;
+  /// The CompletionItemKinds that the client supports. If not set, the client
+  /// only supports <= CompletionItemKind::Reference and will not fall back to a
+  /// valid default value.
+  llvm::Optional completionItemKind;
 
   /// The client supports to send additional context information for a
   /// `textDocument/completion` request.
@@ -683,36 +735,6 @@
 };
 llvm::json::Value toJSON(const Hover );
 
-/// The kind of a completion entry.
-enum class CompletionItemKind {
-  Missing = 0,
-  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,
-  Folder = 19,
-  EnumMember = 20,
-  Constant = 21,
-  Struct = 22,
-  Event = 23,
-  Operator = 24,
-  TypeParameter = 25,
-};
-
 /// Defines whether the insert text in a completion item should be interpreted
 /// as plain text or a snippet.
 enum class InsertTextFormat {
Index: clangd/Protocol.cpp
===
--- clangd/Protocol.cpp
+++ clangd/Protocol.cpp
@@ -496,6 +496,57 @@
   return std::move(Result);
 }
 
+bool fromJSON(const json::Value , CompletionItemKind ) {
+  if (auto T = E.getAsInteger()) {
+if (*T < static_cast(CompletionItemKind::File) ||
+*T > static_cast(CompletionItemKind::TypeParameter))
+  return false;
+Out = static_cast(*T);
+return true;
+  }
+  return false;
+}
+
+CompletionItemKind
+adjustKindToCapability(CompletionItemKind Kind,
+   CompletionItemKindBitset ) {
+  auto KindVal = static_cast(Kind);
+  if (KindVal >= CompletionItemKindMin &&
+  KindVal <= supportedCompletionItemKinds.size() &&
+  supportedCompletionItemKinds[KindVal])
+return Kind;
+
+  switch (Kind) {
+  // Provide some fall backs for common kinds that are close enough.
+  case CompletionItemKind::Folder:
+return CompletionItemKind::File;
+  case CompletionItemKind::EnumMember:
+return CompletionItemKind::Enum;
+  case CompletionItemKind::Struct:
+return CompletionItemKind::Class;
+  default:
+return CompletionItemKind::Text;
+  }
+}
+
+bool fromJSON(const json::Value , std::vector ) {
+  if (auto *A = E.getAsArray()) {
+Out.clear();
+for (size_t I = 0; I < A->size(); ++I) {
+  CompletionItemKind KindOut;
+  if (fromJSON((*A)[I], KindOut))
+Out.push_back(KindOut);
+}
+return true;
+  }
+  return false;
+}
+
+bool fromJSON(const json::Value , CompletionItemKindCapabilities ) {
+  json::ObjectMapper O(Params);
+  return O && O.map("valueSet", R.valueSet);
+}
+
 json::Value toJSON(const