Home Download Buy Blog Forum Support

relative indentation on pasting and snipppet $SELECTION

Re: relative indentation on pasting and snipppet $SELECTION

Postby jps on Fri Apr 18, 2008 12:25 pm

Sounds cool... It should be simpler to do it this way though:

Code: Select all
class RelativeIndentPasteCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        view.runCommand('insertInlineSnippet', ['$PARAM1', sublime.getClipboard()])


You could almost get away with just view.runCommand('insertInlineSnippet', [sublime.getClipboard()]), but then the contents of the clipboard will be interpreted as a snippet, which is probably a bad idea.

It's also worth noting that there are couple of extra things that copy/paste do in sublime, that aren't accessible via getClipboard/setClipboard:
- An extra flag is stored in the clipboard indicating if it should be pasted as a stand alone line. This is used to make Ctrl+C, Ctrl+V function as 'duplicate line' when the selection is empty.
- The syntax of the text copied to the clipboard is also stored there for meta info, so when pasting into a new buffer, the syntax will be applied.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: relative indentation on pasting and snipppet $SELECTION

Postby jps on Sat Apr 19, 2008 5:29 am

I see now, the difference is that your plugin above will first strip the leading spaces off the text on the clipboard.

If you changed it to remove the common leading whitespace off the contents of the clipboard, and not add the indentation at the cursor, instead inserting via:

Code: Select all
view.runCommand('insertInlineSnippet', ['$PARAM1', selection])


rather than:

Code: Select all
sublime.setClipboard(selection)
view.runCommand('paste')   
sublime.setClipboard(cache)


Then it'd play better with multiple selections, and work with tabs too.

Cool plugin btw.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: relative indentation on pasting and snipppet $SELECTION

Postby jps on Sat Apr 19, 2008 5:46 am

$PARAM1, $PARAMA etc are available when a snippet is expanded, they correspond to extra parameters passed to the insertSnippet and insertInlineSnippet commands. E.g., binding a key to:

Code: Select all
insertSnippet foo.sublime-snippet "hello, world!" "1234"


Would expand foo.sublime-snippet, with $PARAM1 evaluating to "hello, world" and $PARAM2 expanding to 1234.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: relative indentation on pasting and snipppet $SELECTION

Postby jps on Sat Apr 19, 2008 5:59 am

Looks good to me... it won't work entirely correctly if spaces and tabs are mixed inconsistently across lines in the clipboard, but that's certainly not worth worrying about.
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Next

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 12 guests