Sublime Forum

Lists of list in show_quick_panel

#1

I have the following snippet :

import sublime
import sublime_plugin

class ExampleTestCommand(sublime_plugin.WindowCommand):
    choices = ['Foo', 'Foo Bar']

    def run(self):
        self.window.show_quick_panel(self.choices, self.on_done)

    def on_done(self, id):
        if id >= 0:
            choice = self.choices[id]
            self.window.run_command("insert", {"characters": choice})

Is there a way for me to show a quick panel in the following way :
Foo >> Choosing Foo displays another show_input_panel
Foo Bar >> Choosing Bar may display another show_input_panel etc

0 Likes

#2

You can do it like this:

import sublime_plugin

CHOICES = [
    ('First', ['A', 'B', 'C']),
    ('Second', ['X', 'Y', 'Z']),
]

class ExampleTestCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.show_quick_panel(
            [key for key, values in CHOICES],
            self.on_done_1
        )

    def on_done_1(self, id):
        if id >= 0:
            key, values = CHOICES[id]
            self.window.show_quick_panel(values, lambda id: self.on_done_2(values, id))

    def on_done_2(self, values, id):
        if id >= 0:
            value = values[id]
            print(value)

Alternatively, sublime_lib has a show_selection_panel method that removes some of the boilerplate:

import sublime_plugin
from sublime_lib import show_selection_panel

CHOICES = [
    ('First', ['A', 'B', 'C']),
    ('Second', ['X', 'Y', 'Z']),
]

class ExampleTestCommand(sublime_plugin.WindowCommand):
    def run(self):
        show_selection_panel(
            self.window,
            items=[value for key, value in CHOICES],
            labels=[key for key, value in CHOICES],
            on_select=self.on_done_1
        )

    def on_done_1(self, values):
        show_selection_panel(
            self.window,
            items=values,
            on_select=self.on_done_2,
        )

    def on_done_2(self, value):
        print(value)

If you want a more complex hierarchy, you can do it like this:

import sublime_plugin
from sublime_lib import show_selection_panel

CHOICES = [
    ('First', [
        ('A', 'You chose A'),
        ('B', 'You chose B'),
        ('C', 'You chose C'),
    ]),
    ('Second', [
        ('X', 'You chose X'),
        ('Y', [
            ('Y1', 'You chose Y1'),
            ('Y2', 'You chose Y2'),
            ('Y3', 'You chose Y3'),
        ]),
        ('Z', 'You chose Z'),
    ]),
]

class ExampleTestCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.select(CHOICES)

    def select(self, choices):
        show_selection_panel(
            self.window,
            items=[value for key, value in choices],
            labels=[key for key, value in choices],
            on_select=self.on_select
        )

    def on_select(self, choice):
        if isinstance(choice, str):
            print(choice)
        else:
            self.select(choice)
1 Like

#3

Thanks for the snippets @ThomSmith.

0 Likes