Sublime Forum

Feed edit object into show_quick_panel

#1

This not work:[code]import sublime, sublime_plugin

class examplCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.active_window().show_quick_panel(‘Y’, self._on_select)

def _on_select(self, idx):
    for sel in self.view.sel():
        self.view.insert(edit, sel.begin(), 'sample text')[/code]

I know that I need to feed edit object into _on_select(), I tried all the ways with _on_select(self, edit, idx); _on_select(self, idx, edit), still not work.?!

Please help.

0 Likes

#2

[code]import sublime, sublime_plugin

class examplCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.active_window().show_quick_panel(‘Y’, self._on_select)
def _on_select(self, idx):
for sel in self.view.sel():
self.view.insert(edit, sel.begin(), ‘sample text’)
[/code]

this works

0 Likes

#3

You changed indentation level of def _on_select(self, idx) ?
Sorry but not works for me.

0 Likes

#4

Try this (untested but should work).

[code]import sublime, sublime_plugin

class examplCommand(sublime_plugin.TextCommand):
def run(self, edit):
sublime.active_window().show_quick_panel(‘Y’, lambda idx: self._on_select(idx, edit))

def _on_select(self, idx, edit):
    for sel in self.view.sel():
        self.view.insert(edit, sel.begin(), 'sample text')

[/code]

0 Likes

#5

Sorry, but still not work. (Quick panel shown up but self.view.insert(edit, sel.begin(), ‘sample text’) not do it job).
As far as I know, edit object is destroyed when quick_panel pop up, so the only way to initiate edit object is by calling TextCommand again

0 Likes

#6

Sorry…I didn’t really look at what you were trying to but was trying to solve how to pass the parameter to the callback.

I had a similar problem in one of my plugins that I solved by using the following code:

    self.view.run_command('append', {'characters': message+'\n', 'force': True, 'scroll_to_end': True})

Maybe you could try something like this to insert the text?

    self.view.run_command('insert', {'characters': 'sample text'})

Have a look here for more info on the available commands: http://docs.sublimetext.info/en/latest/reference/commands.html

0 Likes

#7

OK, solved here: https://sublimetext.com/forum/viewtopic.php?f=6&p=45530#p45559

0 Likes