Skip to content
This repository was archived by the owner on Sep 29, 2023. It is now read-only.

Commit f31670f

Browse files
author
jchapuis
committed
using comma separations when merging non-json meta-data
1 parent 044196f commit f31670f

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/main/scala/com/nexthink/utils/parsing/combinator/completion/CompletionSupport.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ trait CompletionSupport extends Parsers with CompletionTypes {
177177
Parser(this, in => updateCompletionsTag(this.completions(in), None, None, Some(tagDescription), None))
178178

179179
/** An operator to specify the completion tag meta-data of a parser (empty by default).
180+
* Note that meta-data is merged with comma separations when combining two equivalent entries.
180181
* @param tagMeta the completion tag meta-data (to be used e.g. to specify the visual style for a completion tag in the menu)
181182
* @return wrapper `Parser` instance specifying the completion tag meta-data
182183
*/
@@ -192,17 +193,18 @@ trait CompletionSupport extends Parsers with CompletionTypes {
192193
*/
193194
def %%(tagMeta: JValue): Parser[T] = %%(compact(render(tagMeta)))
194195

195-
/** An operator to specify the meta-data for completions of a parser (empty by default)
196+
/** An operator to specify the meta-data for completions of a parser (empty by default).
197+
* Note that meta-data is merged with comma separations when combining two equivalent entries.
196198
* @param meta the completion meta-data (to be used e.g. to specify the visual style for a completion entry in the menu)
197199
* @return wrapper `Parser` instance specifying the completion meta-data
198200
*/
199201
def %-%(meta: String): Parser[T] =
200202
Parser(this, in => updateCompletions(this.completions(in), Some(meta)))
201203

202204
/** An operator to specify the meta-data for completions of a parser in JSON format (empty by default)
203-
* Note that if the meta-data is encoded in JSON, it is automatically merged when combining two equivalent tags
205+
* Note that if the meta-data is encoded in JSON, it is automatically merged when combining two equivalent entries
204206
* (i.e. bearing the same label, but with a different payload). This allows for more flexibility when defining the grammar:
205-
* various parsers can return the same completion entries with an additive effect on the meta-data.
207+
* various parsers can return the same completion entries with an additive effect on the meta-data.
206208
* @param meta the JValue for completion meta-data (to be used e.g. to specify the visual style for a completion entry in the menu)
207209
* @return wrapper `Parser` instance specifying the completion meta-data
208210
*/

src/main/scala/com/nexthink/utils/parsing/combinator/completion/CompletionTypes.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,12 @@ trait CompletionTypes {
162162
override def toString: String = pretty(render(serializeJson))
163163
def toJson: String = compact(render(serializeJson))
164164

165+
165166
private def mergeMetaData(left: Option[String], right: Option[String]) = (left, right) match {
166167
case (Some(l), Some(r)) =>
167168
(parseOpt(l), parseOpt(r)) match {
168169
case (Some(lJson), Some(rJson)) => Some(compact(render(lJson merge rJson)))
169-
case _ => Some(l + r)
170+
case _ => Some(Seq(l, r).mkString(", "))
170171
}
171172
case (Some(l), None) => Some(l)
172173
case (None, Some(r)) => Some(r)

src/test/scala/com/nexthink/utils/parsing/combinator/completion/CompletionTypesTest.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ class CompletionTypesTest extends CompletionTypes {
1717

1818
val setA = CompletionSet(
1919
CompletionTag("A", 10, Some("description"), Some(compact(render("type" -> "a-type")))),
20-
Set(Completion("a", 2), Completion("b", 1, Some(compact(render(("objects" -> Seq("devices")) ~ ("themes" -> Seq("some")))))))
20+
Set(Completion("a", 2, Some("meta1")), Completion("b", 1, Some(compact(render(("objects" -> Seq("devices")) ~ ("themes" -> Seq("some")))))))
2121
)
2222
val setB = CompletionSet(CompletionTag("B", 5), Set(Completion("c", 4), Completion("d", 3)))
2323
val setC = CompletionSet("C", Completion("e", 10))
2424
val setAPrime = CompletionSet(
2525
CompletionTag("A", 10, None, Some(compact(render("style" -> "highlight")))),
26-
Set(Completion("a", 4), Completion("b", 1, Some(compact(render(("objects" -> Seq("users", "packages")) ~ ("themes" -> Seq("other")))))), Completion("aa"))
26+
Set(
27+
Completion("a", 4, Some("meta2")),
28+
Completion("b", 1, Some(compact(render(("objects" -> Seq("users", "packages")) ~ ("themes" -> Seq("other")))))),
29+
Completion("aa")
30+
)
2731
)
2832

2933
@Test
@@ -58,7 +62,7 @@ class CompletionTypesTest extends CompletionTypes {
5862
CompletionSet(
5963
CompletionTag("A", 10, Some("description"), Some(compact(render(("type" -> "a-type") ~ ("style" -> "highlight"))))),
6064
Set(
61-
Completion("a", 4),
65+
Completion("a", 4, Some("meta1, meta2")),
6266
Completion("b", 1, Some(compact(render(("objects" -> Seq("devices", "users", "packages")) ~ ("themes" -> Seq("some", "other")))))),
6367
Completion("aa")
6468
)

0 commit comments

Comments
 (0)