Home Download Buy Blog Forum Support

on_modified replacement loop

on_modified replacement loop

Postby Alvaro on Sat Dec 08, 2012 4:40 pm

Im coding a plugin where I need to dynamically replace some placeholders after inserting a snippet.
Im using on_modified eventlistener for this.
So in the on_modified listener Im replacing the placeholder with a UUID.

The problem is that if I undo after the snippet insertion, Im not undoing the snippet insertion but the UUID replacement done by the on_modified handler. So that triggers another on_modified event that replace the placeholder with a new UUID.

In summary, after inserting a snippet, a new UUID is generated, which is fine.
If I press control-z instead of undoing the snippet inertion, Im replacing the UUID with a new UUID.

Is there any way to solve this?
Can I disable "undo" for the on_modified handler?

Thanks
Alvaro
 
Posts: 9
Joined: Wed Dec 05, 2012 10:49 am

Re: on_modified replacement loop

Postby sublimator on Sun Dec 09, 2012 12:17 am

Not sure what you are doing exactly. It's not possible to use the api to insert the snippet with uuid already? How is the snippet inserted? You can use view.command_history possibly to detect 'undo' within on_modified
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 648
Joined: Thu Mar 20, 2008 5:41 am

Re: on_modified replacement loop

Postby Alvaro on Sun Dec 09, 2012 4:15 pm

As far as I know, you cannot insert dynamic content via snippets (like a uuid), so what Im doing is inserting a placeholder %UUID% and then replace it in the on_modified handler. If there is any way to call a python method from the snippet, please let me know.
I will try to use the history to prevent sublime to go into this loop when undoing the snippet insertion.

Thanks!
Alvaro
 
Posts: 9
Joined: Wed Dec 05, 2012 10:49 am

Re: on_modified replacement loop

Postby agibsonsw on Sun Dec 09, 2012 7:24 pm

If you are modifying the snippet to include %UUID% anyway then can you not just embed the snippet's result in your plug-in?

Code: Select all
sublime.run_command("insert_snippet", { "contents": "My snippet %s" % UUID }) # haven't checked if this is the correct syntax

Or, if appropriate, just modify the view to achieve the same result as the snippet. But it's difficult to tell without seeing some code.

You could even collect the snippets you need to use at the top of your plug-in (.py) as HEREDOC strings:
Code: Select all
HEADER = \
"""<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>%(fname)s</title>
"""

sublime.run_command("insert_snippet", { "contents": HEADER % { "fname": UUID } } )

You should, I believe, be able to embed ${1:something} in the HEREDOC so that it still behaves as a snippet, because the HEREDOC and snippet syntax for parameters differs.

Added: Believe I may have just created a dynamic snippet - Santa should be pleased with me :)
"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: on_modified replacement loop

Postby agibsonsw on Sun Dec 09, 2012 7:53 pm

Yes, a dynamic snippet:

Code: Select all
import sublime, sublime_plugin

MYSNIPPET = \
"""
Do this %(name)s and
then ${1:this}.
"""
class DynamicSnip(sublime_plugin.TextCommand):
    def run(self, edit):
        someName = 'Bob'
        self.view.run_command("insert_snippet", { "contents" : (MYSNIPPET % { "name": someName }) })
"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: on_modified replacement loop

Postby Alvaro on Mon Dec 10, 2012 9:50 am

That was my initial approach (but using a external file instead of a variable). The problem is that I want the users to be able to use sublime snippets capabilities: definitiaion syntax and search capabilities, so it was not an option for me.
I ended up using on_modified event to replace the placeholder by the dynamic generated UUID and then using command history to avoid the undo loop
Alvaro
 
Posts: 9
Joined: Wed Dec 05, 2012 10:49 am

Re: on_modified replacement loop

Postby agibsonsw on Mon Dec 10, 2012 6:42 pm

I know this is no longer relevant for the OP but it may be useful to others. It is possible to insert variables into (this version of a..) snippet and have them occupy the snippet's field positions:

Code: Select all
import sublime, sublime_plugin

MYSNIPPET = \
"""
Do this ${1:%(name1)s} and %(name2)s.$0
"""
class DynamicSnip(sublime_plugin.TextCommand):
    def run(self, edit):
        someName = 'Bob'
        otherName = 'Ted'
        self.view.run_command("insert_snippet", { "contents" : (MYSNIPPET % \
            { "name1": someName, "name2": otherName }) })
"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


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 5 guests

cron