|
| 1 | +import sublime |
| 2 | +import sublime_plugin |
| 3 | +#import time |
| 4 | + |
| 5 | + |
| 6 | +# TODO: multiple selections |
| 7 | +class SelectNextQuotesCommand(sublime_plugin.TextCommand): |
| 8 | + def run(self, edit, char='"', move_forward=True, jumps=1): |
| 9 | + #start = time.time() |
| 10 | + # --------------------------------------- |
| 11 | + if jumps < 1: |
| 12 | + return |
| 13 | + region = None |
| 14 | + for i in range(1, jumps + 1): |
| 15 | + if region == None: |
| 16 | + cursor_point = self.view.sel()[0].end() |
| 17 | + else: |
| 18 | + cursor_point = region.end() |
| 19 | + if move_forward: |
| 20 | + region = self.find_forward(char, cursor_point) |
| 21 | + else: |
| 22 | + region = self.find_prev(char, cursor_point) |
| 23 | + if region == None: |
| 24 | + return |
| 25 | + self.view.sel().clear() |
| 26 | + self.view.sel().add(region) |
| 27 | + # --------------------------------------- |
| 28 | + #end = time.time() |
| 29 | + #print("perf:") |
| 30 | + #print(end - start) |
| 31 | + |
| 32 | + def find_forward(self, char, cursor_point): |
| 33 | + region = self.find_region_forward(char, cursor_point) |
| 34 | + if self.is_start(region): |
| 35 | + #print("debug: reached end of text buffer") |
| 36 | + return |
| 37 | + return region |
| 38 | + |
| 39 | + def find_prev(self, char, cursor_point, flags=0): |
| 40 | + region = self.find_region_prev(char, cursor_point) |
| 41 | + if self.is_start(region): |
| 42 | + #print("debug: reached start of text buffer") |
| 43 | + return |
| 44 | + return region |
| 45 | + |
| 46 | + def find_region_forward(self, char, cursor_point): |
| 47 | + return self.view.find("(?<={0})([^{0}]*)(?={0})".format(char), cursor_point) |
| 48 | + |
| 49 | + def is_start(self, region): |
| 50 | + return (region.a == -1 and region.b == -1) |
| 51 | + |
| 52 | + def search_in_range(self, what, start, end, flags=0): |
| 53 | + match = self.view.find(what, start, flags) |
| 54 | + if match and ((match.begin() >= start) and (match.end() <= end)): |
| 55 | + return True |
| 56 | + |
| 57 | + def reverse_search(self, what, cursor_point, flags=0): |
| 58 | + ''' binary search ''' |
| 59 | + last_match = None |
| 60 | + lo = 0 |
| 61 | + hi = cursor_point |
| 62 | + while True: |
| 63 | + middle = int((lo + hi) / 2) |
| 64 | + # Don't search forever the same line. |
| 65 | + last_match = sublime.Region(lo, hi) |
| 66 | + if last_match and last_match.size() <= len(what): |
| 67 | + return last_match.begin() |
| 68 | + if self.search_in_range(what, middle, hi, flags): |
| 69 | + lo = middle |
| 70 | + elif self.search_in_range(what, lo, middle, flags): |
| 71 | + hi = middle |
| 72 | + else: |
| 73 | + return -1 |
| 74 | + |
| 75 | + def find_region_prev(self, char, cursor_point): |
| 76 | + pos = self.reverse_search(char, cursor_point) |
| 77 | + if pos != -1: |
| 78 | + pos = self.reverse_search(char, pos) |
| 79 | + if pos != -1: |
| 80 | + return self.view.find("(?<={0})([^{0}]*)(?={0})".format(char), pos) |
| 81 | + return sublime.Region(-1, -1) |
0 commit comments