Home Download Buy Blog Forum Support

Kick-start for plug-in

Re: Kick-start for plug-in

Postby agibsonsw on Mon Feb 20, 2012 6:56 pm

I'm making good progress and I'm using the console to run/feed a Snippet. At the moment I grab the console text, insert it into the view, run the Snippet, and then delete the line that was added.

The Snippet uses TM_CURRENT_LINE for its input. But want I really want to do is feed the console text directly to the Snippet. Is this possible please?

Code: Select all
import sublime, sublime_plugin

class AndyOutput(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel('Andy>','', self.on_done, self.on_change, self.on_cancel)
        pass        # empty statement

    def on_done(self, text):
        # if self.window.active_view(): # why?
        self.window.active_view().run_command("use_main_view", {"the_text": text})

    def on_change(self, text):
        pass

    def on_cancel(self):
        pass

class UseMainView(sublime_plugin.TextCommand):
    def run(self, edit, the_text):
        # self.view.insert(edit, 0, the_text)
        sel = self.view.sel()[0]
        pt = sel.end()
        self.view.insert(edit, pt, the_text)
        self.view.run_command("insert_snippet",{"name": "Packages/CSS/Andypropxs.sublime-snippet"})
        cur_linef = self.view.full_line(sel)
        self.view.erase(edit, cur_linef)
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Kick-start for plug-in

Postby agibsonsw on Mon Feb 20, 2012 8:54 pm

I'm getting somewhere :D If they type a space in the input panel, I show the quick panel. If they choose an item its text is added to the main window, and the focus is given back to the input panel, whe'hay.

But I'm still struggling to discover whether it's possible to feed text directly to a Snippet? Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Kick-start for plug-in

Postby agibsonsw on Tue Feb 21, 2012 9:11 pm

Hello. I'm assuming it's not possible to feed text directly to a snippet, so I'm moving on :) (although I could probably copy the entire snippet as 'contents' and insert the text into this string..).

I can track someone's editing of a css file, but I'm unable to use 'view.insert(edit, pt, 'ght')' to add additional text to the current view/region - because 'edit' is not defined within an event listener (code below).

This may be intentional, in that it's not possible to insert text within the modified event? I can see how this may be an issue (circular reference..). But perhaps it can be done by appending data to an internal buffer? Or by creating, and appending, a new region? Any suggestion is welcome :D

Code: Select all
class EditorTracking(sublime_plugin.EventListener):
    def __init__(self):
        pass

    def on_modified(self, view):
        sel = view.sel()[0]
        pt = sel.end()
        if not view.match_selector(pt, ('source.css meta.property-list.css')):
            # print 'not in css'
            return
        else:
            # print 'yes, in css property list'
            cur_line = view.line(sel)
            line_text = view.substr(cur_line)
            if line_text[-2:] == 'ri':
                view.insert(edit, pt, 'ght')

Added: 'view.is_read_only()' returns False, so maybe it is possible to add text??
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Kick-start for plug-in

Postby agibsonsw on Tue Feb 21, 2012 9:51 pm

No worries :D

Code: Select all
class EditorTracking(sublime_plugin.EventListener):
    def __init__(self):
        self.ignore = False

    def on_modified(self, view):
        if self.ignore:
            return
        sel = view.sel()[0]
        pt = sel.end()
        if not view.match_selector(pt, ('source.css meta.property-list.css')):
            # print 'not in css'
            return
        else:
            # print 'yes, in css property list'
            # print view.is_read_only()
            cur_line = view.line(sel)
            line_text = view.substr(cur_line)
            edit = view.begin_edit()
            try:
                if line_text[-2:] == 'ri':
                    view.insert(edit, pt, 'ght')
                pass
            finally:
                self.ignore = True
                view.end_edit(edit)
                self.ignore = False


Next task is to disable the auto-complete list (while my code is running) and to find some way to remove my event listener.. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Kick-start for plug-in

Postby agibsonsw on Wed Feb 22, 2012 6:53 pm

Hello. Is it possible to disable/detach my on_modified event-listener within my event's code? Or do I need to use a second key-binding that sets on_modified back to None?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Previous

Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], phil.b, Yahoo [Bot] and 21 guests