|
4 | 4 |
|
5 | 5 | class MultiFindAllCommand(sublime_plugin.TextCommand): |
6 | 6 |
|
7 | | - def run(self, edit): |
| 7 | + def run(self, edit, case=True, word=False, ignore_comments=False, expand=True): |
8 | 8 |
|
9 | 9 | view = self.view |
10 | 10 | newRegions = [] |
11 | 11 |
|
| 12 | + # filter selections in order to exclude duplicates since it can hang |
| 13 | + # Sublime if search is performed on dozens of selections, this doesn't |
| 14 | + # happen with built-in command because it works on a single selection |
| 15 | + initial = [sel for sel in view.sel()] |
| 16 | + regions, substrings = [], [] |
| 17 | + for region in view.sel(): |
| 18 | + if expand and region.empty(): |
| 19 | + # if expanding substring will be the word |
| 20 | + region = view.word(region.a) |
| 21 | + # add the region since nothing is selected yet |
| 22 | + view.sel().add(region) |
| 23 | + # filter by substring (word or not) |
| 24 | + substr = view.substr(region) |
| 25 | + if substr and substr not in substrings: |
| 26 | + regions.append(region) |
| 27 | + substrings.append(substr) |
| 28 | + view.sel().clear() |
| 29 | + if regions: |
| 30 | + for region in regions: |
| 31 | + view.sel().add(region) |
| 32 | + else: |
| 33 | + view.window().status_message("Multi Find All: nothing selected") |
| 34 | + for sel in initial: |
| 35 | + view.sel().add(sel) |
| 36 | + return |
| 37 | + |
| 38 | + selected_words = [view.substr(view.word(sel)).lower() for sel in view.sel()] |
| 39 | + |
12 | 40 | for region in view.sel(): |
13 | 41 | substr = view.substr(region) |
14 | | - for region in view.find_all(substr, sublime.LITERAL): |
15 | | - newRegions.append(region) |
| 42 | + |
| 43 | + if case: |
| 44 | + for region in view.find_all(substr, sublime.LITERAL): |
| 45 | + newRegions.append(region) |
| 46 | + else: |
| 47 | + for region in view.find_all(substr, sublime.LITERAL | sublime.IGNORECASE): |
| 48 | + newRegions.append(region) |
| 49 | + |
| 50 | + if word: |
| 51 | + deleted = [region for region in newRegions |
| 52 | + if view.substr(view.word(region)).lower() not in selected_words] |
| 53 | + newRegions = [region for region in newRegions if region not in deleted] |
| 54 | + |
| 55 | + if ignore_comments: |
| 56 | + deleted = [region for region in newRegions |
| 57 | + if re.search(r'\bcomment\b', view.scope_name(region.a))] |
| 58 | + newRegions = [region for region in newRegions if region not in deleted] |
16 | 59 |
|
17 | 60 | for region in newRegions: |
18 | 61 | view.sel().add(region) |
19 | 62 |
|
| 63 | +class MultiFindAllRegexCommand(sublime_plugin.TextCommand): |
| 64 | + |
| 65 | + def on_done(self, regex): |
| 66 | + |
| 67 | + case = sublime.IGNORECASE if not self.case else 0 |
| 68 | + regions = self.view.find_all(regex, case) |
| 69 | + |
| 70 | + # we don't clear the selection so it's additive, it's nice to just add a |
| 71 | + # regex search on top of a previous search |
| 72 | + if not self.subtract: |
| 73 | + for region in regions: |
| 74 | + self.view.sel().add(region) |
| 75 | + |
| 76 | + # the resulting regions will be subtracted instead |
| 77 | + else: |
| 78 | + for region in regions: |
| 79 | + self.view.sel().subtract(region) |
| 80 | + |
| 81 | + # remove empty selections in both cases, so there aren't loose cursors |
| 82 | + regions = [r for r in self.view.sel() if not r.empty()] |
| 83 | + self.view.sel().clear() |
| 84 | + for region in regions: |
| 85 | + self.view.sel().add(region) |
| 86 | + for region in self.view.sel(): |
| 87 | + print(region) |
| 88 | + |
| 89 | + def run(self, edit, case=True, subtract=False): |
| 90 | + |
| 91 | + self.edit = edit |
| 92 | + self.case = case |
| 93 | + self.subtract = subtract |
| 94 | + c = "Additive regex search:" if not subtract else "Subtractive regex search:" |
| 95 | + sublime.active_window().show_input_panel(c, "", self.on_done, None, None) |
| 96 | + |
| 97 | +class MultiFindMenuCommand(sublime_plugin.TextCommand): |
| 98 | + |
| 99 | + def run(self, edit): |
| 100 | + |
| 101 | + choice = [ |
| 102 | + "Find All Case + Word +", |
| 103 | + "Find All Case + Word -", |
| 104 | + "Find All Case - Word +", |
| 105 | + "Find All Case - Word -", |
| 106 | + "Find All Case + Word + (Ignore Comments)", |
| 107 | + "Find Regex (Additive)", |
| 108 | + "Find Regex (Subtractive)" |
| 109 | + ] |
| 110 | + |
| 111 | + def on_done(index): |
| 112 | + |
| 113 | + if index == -1: |
| 114 | + return |
| 115 | + if index == 0: |
| 116 | + self.view.run_command('multi_find_all', {"case": True, "word": True}) |
| 117 | + elif index == 1: |
| 118 | + self.view.run_command('multi_find_all', {"case": True}) |
| 119 | + elif index == 2: |
| 120 | + self.view.run_command('multi_find_all', {"case": False, "word": True}) |
| 121 | + elif index == 3: |
| 122 | + self.view.run_command('multi_find_all', {"case": False}) |
| 123 | + elif index == 4: |
| 124 | + self.view.run_command('multi_find_all', {"case": True, "word": True, "ignore_comments": True}) |
| 125 | + elif index == 5: |
| 126 | + self.view.run_command('multi_find_all_regex') |
| 127 | + elif index == 6: |
| 128 | + self.view.run_command('multi_find_all_regex', {"subtract": True}) |
| 129 | + |
| 130 | + self.view.window().show_quick_panel(choice, on_done, 1, 0, None) |
20 | 131 |
|
21 | 132 | class JumpToLastRegionCommand(sublime_plugin.TextCommand): |
22 | 133 |
|
|
0 commit comments