@@ -319,6 +319,102 @@ Try for yourself!
319
319
:::
320
320
321
321
322
+ ### Accumulating Inputs from a List
323
+
324
+ It is also possible to accumulate multiple inputs dynamically.
325
+ We can achieve this
326
+ by using lists as the values
327
+ of our ListInputHandler's ` list_items ` return value.
328
+
329
+ To allow the user to signal that they want to select more values,
330
+ we define the ` want_event ` method and ` return True ` from it.
331
+ This tells Sublime Text to add an [ ` event ` argument] [ Event ]
332
+ to the ` validate ` and ` confirm ` methods,
333
+ which we use to determine
334
+ if a certain modifier key was held
335
+ and whether to return another input handler
336
+ in the ` next_input ` method.
337
+
338
+ ::: tip
339
+ Due to [ a bug in the plugin API] [ core-6258 ] ,
340
+ we need to define * both* methods,
341
+ ` validate ` and ` confirm ` ,
342
+ and have them accept this additional ` event ` argument,
343
+ even when we don't need them.
344
+ :::
345
+
346
+ <<< ./code/accumulate.py {13-15,25-28,34-35,38-40,47-48}
347
+
348
+ Here is what it looks like in action:
349
+
350
+ <video controls src =" ./images/accumulate.mp4 " />
351
+
352
+ In this example,
353
+ we "generate" a list of choices
354
+ that we pass to our ` ItemsInputHandler ` 's constructor (lines 47-48).
355
+ These choices will be used as the basis for each prompt.
356
+
357
+ When providing a list of items for ST to display
358
+ in ` list_items ` (lines 22-28),
359
+ we return a 2-element ` tuple ` .
360
+ The first item is the list of items,
361
+ which in turn are more 2-element tuples.
362
+ The first value of the inner tuples
363
+ tells ST what to show inside the item list,
364
+ while the second value is what ST will use
365
+ when invoking the ` validate ` and ` confirm ` methods
366
+ and also what will get used as the final value
367
+ provided by this event handler.
368
+ The second item will become relevant later.
369
+ Refer to [ the documentation for ` list_items ` ] [ list_items ] for more details.
370
+
371
+ [ list_items ] : https://www.sublimetext.com/docs/api_reference.html#sublime_plugin.ListInputHandler.list_items
372
+
373
+ Next, we take a look at the ` confirm ` method (lines 30-35).
374
+ The method is invoked
375
+ with the ` value ` of the selected list item
376
+ but there is the aforementioned additional [ ` event ` argument] [ Event ] .
377
+ We inspect the ` event ` to check for the Alt key,
378
+ record the result and the selected value in an instance attribute
379
+ and move on to ` next_input ` .
380
+
381
+ [ core-6258 ] : https://github.com/sublimehq/sublime_text/issues/6258
382
+ [ Event ] : https://www.sublimetext.com/docs/api_reference.html#sublime.Event
383
+
384
+ As discussed before,
385
+ ST calls the ` next_input `
386
+ to check for the next input handler to open
387
+ and this is where the magic happens (lines 38-40).
388
+ If the alt modified key has been held while selecting an item,
389
+ we return a new instance of * the same input handler class*
390
+ and with the following values:
391
+
392
+ 1 . the same list of choices,
393
+ 1 . the accumulated value list (` self.selected ` ), and
394
+ 1 . the index of the just-selected item.
395
+
396
+ The list of choices is self-explanatory,
397
+ the accumulated value list is needed
398
+ to generate the next set of items in ` list_items ` ,
399
+ and the ` selected_index ` is used
400
+ to open the next input handler
401
+ with the previously selected item preselected.
402
+ If the alt key has not been held,
403
+ we simply return ` None `
404
+ and conclude the collection of arguments.
405
+
406
+ In the end, the result of
407
+ the last input handler on the stack of each argument
408
+ (here: ` items ` , determined from the class name ` ItemsInputHandler ` )
409
+ will be collected and used in the command's ` run ` method invocation.
410
+ Because we used the * same input handler name* for all our input handlers,
411
+ we receive the accumulated list of selected items
412
+ from the last instance.
413
+
414
+ Note that this is just one method of achieving this behavior.
415
+ You may find that another works better for you.
416
+
417
+
322
418
## Code Archive
323
419
324
420
The final code examples presented on this page
0 commit comments