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().add(sublime.Region(begin + 1, end))

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


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: No registered users and 6 guests