@@ -117,7 +117,11 @@ trait CompletionSupport extends Parsers with CompletionTypes {
117
117
this ,
118
118
in => {
119
119
val completions = this .completions(in)
120
- Completions (completions.position, completions.sets.mapValues(s => CompletionSet (s.tag, s.entries.toList.sortBy(_.score).reverse.take(n).toSet)).toSeq)
120
+ Completions (
121
+ completions.position,
122
+ completions.meta,
123
+ completions.sets.mapValues(s => CompletionSet (s.tag, s.entries.toList.sortBy(_.score).reverse.take(n).toSet)).toSeq
124
+ )
121
125
}
122
126
)
123
127
@@ -126,22 +130,22 @@ trait CompletionSupport extends Parsers with CompletionTypes {
126
130
* @return wrapper `Parser` instance specifying the completion tag
127
131
*/
128
132
def % (tag : String ): Parser [T ] =
129
- Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), None , None , None ))
133
+ Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag)))
130
134
131
135
/** An operator to specify the completions tag score of a parser (0 by default)
132
136
* @param tagScore the completion tag score (to be used e.g. to order sections in a completion menu)
133
137
* @return wrapper `Parser` instance specifying the completion tag score
134
138
*/
135
139
def % (tagScore : Int ): Parser [T ] =
136
- Parser (this , in => updateCompletionsTag(this .completions(in), None , Some (tagScore), None , None ))
140
+ Parser (this , in => updateCompletionsTag(this .completions(in), None , Some (tagScore)))
137
141
138
142
/** An operator to specify the completion tag and score of a parser
139
143
* @param tag the completion tag
140
144
* @param tagScore the completion tag score
141
145
* @return wrapper `Parser` instance specifying the completion tag
142
146
*/
143
147
def % (tag : String , tagScore : Int ): Parser [T ] =
144
- Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore), None , None ))
148
+ Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore)))
145
149
146
150
/** An operator to specify the completion tag, score and description of a parser
147
151
* @param tag the completion tag
@@ -150,17 +154,17 @@ trait CompletionSupport extends Parsers with CompletionTypes {
150
154
* @return wrapper `Parser` instance specifying completion tag
151
155
*/
152
156
def % (tag : String , tagScore : Int , tagDescription : String ): Parser [T ] =
153
- Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore), Some (tagDescription), None ))
157
+ Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore), Some (tagDescription)))
154
158
155
159
/** An operator to specify the completion tag, score, description and meta of a parser
156
160
* @param tag the completion tag
157
161
* @param tagScore the completion tag score
158
162
* @param tagDescription the completion tag description
159
- * @param tagKind the completion tag meta
163
+ * @param tagMeta the completion tag meta
160
164
* @return wrapper `Parser` instance specifying completion tag
161
165
*/
162
- def % (tag : String , tagScore : Int , tagDescription : String , tagKind : String ): Parser [T ] =
163
- Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore), Some (tagDescription), Some (tagKind )))
166
+ def % (tag : String , tagScore : Int , tagDescription : String , tagMeta : String ): Parser [T ] =
167
+ Parser (this , in => updateCompletionsTag(this .completions(in), Some (tag), Some (tagScore), Some (tagDescription), Some (tagMeta )))
164
168
165
169
/** An operator to specify the completion tag
166
170
* @param tag the completion tag
@@ -174,7 +178,7 @@ trait CompletionSupport extends Parsers with CompletionTypes {
174
178
* @return wrapper `Parser` instance specifying the completion description
175
179
*/
176
180
def %? (tagDescription : String ): Parser [T ] =
177
- Parser (this , in => updateCompletionsTag(this .completions(in), None , None , Some (tagDescription), None ))
181
+ Parser (this , in => updateCompletionsTag(this .completions(in), None , None , Some (tagDescription)))
178
182
179
183
/** An operator to specify the completion tag meta-data of a parser (empty by default).
180
184
* Note that meta-data is merged with comma separations when combining two equivalent entries.
@@ -191,15 +195,16 @@ trait CompletionSupport extends Parsers with CompletionTypes {
191
195
* @param tagMeta the JValue for completion tag meta-data (to be used e.g. to specify the visual style for a completion tag in the menu)
192
196
* @return wrapper `Parser` instance specifying the completion tag meta-data
193
197
*/
194
- def %% (tagMeta : JValue ): Parser [T ] = %% (compact(render(tagMeta)))
198
+ def %% (tagMeta : JValue ): Parser [T ] =
199
+ Parser (this , in => updateCompletionsSets(this .completions(in), set => CompletionSet (set.tag.updateMeta(tagMeta), set.completions)))
195
200
196
201
/** An operator to specify the meta-data for completions of a parser (empty by default).
197
202
* Note that meta-data is merged with comma separations when combining two equivalent entries.
198
203
* @param meta the completion meta-data (to be used e.g. to specify the visual style for a completion entry in the menu)
199
204
* @return wrapper `Parser` instance specifying the completion meta-data
200
205
*/
201
206
def %-% (meta : String ): Parser [T ] =
202
- Parser (this , in => updateCompletions (this .completions(in), Some ( meta)))
207
+ Parser (this , in => updateCompletionsSets (this .completions(in), set => CompletionSet (set.tag, set.entries.map(e => e.updateMeta( meta)) )))
203
208
204
209
/** An operator to specify the meta-data for completions of a parser in JSON format (empty by default)
205
210
* Note that if the meta-data is encoded in JSON, it is automatically merged when combining two equivalent entries
@@ -210,6 +215,26 @@ trait CompletionSupport extends Parsers with CompletionTypes {
210
215
*/
211
216
def %-% (meta : JValue ): Parser [T ] = %-% (compact(render(meta)))
212
217
218
+ /**
219
+ * An operator to specify the meta-data for the whole set of completions (empty by default)
220
+ * Note that meta-data is merged with comma separations when combining two equivalent entries.
221
+ * @param globalMeta the meta-data (to be used e.g. to specify the visual style for the completion menu)
222
+ * @return wrapper `Parser` instance specifying the completions meta-data
223
+ */
224
+ def %%% (globalMeta : String ): Parser [T ] =
225
+ Parser (this , in => this .completions(in).updateMeta(globalMeta))
226
+
227
+ /**
228
+ * An operator to specify the meta-data for the whole set of completions (empty by default)
229
+ * Note that if the meta-data is encoded in JSON, it is automatically merged when combining multiple completion sets.
230
+ * This allows for more flexibility when defining the grammar: various parsers can define the global completion meta-data
231
+ * with an additive effect.
232
+ * @param globalMeta the JValue for completions meta-data (to be used e.g. to specify the visual style for the completion menu)
233
+ * @return wrapper `Parser` instance specifying the completions meta-data
234
+ */
235
+ def %%% (globalMeta : JValue ): Parser [T ] =
236
+ Parser (this , in => this .completions(in).updateMeta(globalMeta))
237
+
213
238
def flatMap [U ](f : T => Parser [U ]): Parser [U ] =
214
239
Parser (super .flatMap(f), completions)
215
240
@@ -233,26 +258,27 @@ trait CompletionSupport extends Parsers with CompletionTypes {
233
258
234
259
private def updateCompletionsSets (completions : Completions , updateSet : CompletionSet => CompletionSet ) = {
235
260
Completions (completions.position,
261
+ completions.meta,
236
262
completions.sets.values
237
263
.map(updateSet)
238
264
.map(s => s.tag.label -> s)
239
265
.toSeq)
240
266
}
241
267
242
268
private def updateCompletionsTag (completions : Completions ,
243
- newTagLabel : Option [String ],
244
- newTagScore : Option [Int ],
245
- newTagDescription : Option [String ],
246
- newTagKind : Option [String ]) = {
269
+ newTagLabel : Option [String ] = None ,
270
+ newTagScore : Option [Int ] = None ,
271
+ newTagDescription : Option [String ] = None ,
272
+ newTagKind : Option [String ] = None ) = {
247
273
def updateSet (existingSet : CompletionSet ) =
248
274
CompletionSet (existingSet.tag.update(newTagLabel, newTagScore, newTagDescription, newTagKind), existingSet.completions)
249
275
250
276
updateCompletionsSets(completions, updateSet)
251
277
}
252
278
253
- private def updateCompletions (completions : Completions , newCompletionKind : Option [ String ] ) = {
279
+ private def updateCompletionsMeta (completions : Completions , newMeta : String ) = {
254
280
def updateSet (existingSet : CompletionSet ) =
255
- CompletionSet (existingSet.tag, existingSet.entries.map(e => e.updateKind(newCompletionKind )))
281
+ CompletionSet (existingSet.tag, existingSet.entries.map(e => e.updateMeta(newMeta )))
256
282
updateCompletionsSets(completions, updateSet)
257
283
}
258
284
0 commit comments