Home Download Buy Blog Forum Support

expandSelectionTo string

Re: expandSelectionTo string

Postby FalconNL on Fri Jun 11, 2010 3:10 pm

Here's a first stab at using syntax info for doing this. The obvious downside is that it doesn't work in plain text files or syntax schemes that don't adhere to the convention of naming strings string.quoted.double/single. Still, it should work for most languages (tried C, C++, C#, Java, Ruby, Python, Haskell and PHP).

Code: Select all
import re
import sublime, sublimeplugin

class QuoteSelectCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        for region in view.sel():
            syntax = view.syntaxName(region.begin())
            if re.match(".*string.quoted.double", syntax): self.select(view, region, '"')
            if re.match(".*string.quoted.single", syntax): self.select(view, region, "'")
   
    def select(self, view, region, char):
        begin = region.begin() - 1
        end = region.begin()
        while view.substr(begin) != char or view.substr(begin - 1) == '\\': begin -= 1
        while view.substr(end) != char or view.substr(end - 1) == '\\': end += 1
        view.sel().subtract(region)
        view.sel().add(sublime.Region(begin + 1, end))


If anyone finds any cases where it doesn't work, please let me know.
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Previous

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 3 guests