Skip to content

Commit 5666a6c

Browse files
committed
first release
1 parent c361b86 commit 5666a6c

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

Diff for: Default (Windows).sublime-keymap

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "keys": ["ctrl+alt+right"], "command": "select_next_quotes", "args": {"char": "\"", "move_forward": true, "jumps": 2}},
3+
{ "keys": ["ctrl+alt+left"], "command": "select_next_quotes", "args": {"char": "\"", "move_forward": false, "jumps": 2}}
4+
]

Diff for: Default.sublime-commands

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"caption": "Select next double quotes",
4+
"command": "select_next_quotes",
5+
"args": {
6+
"char": "\"",
7+
"move_forward": true,
8+
"jumps": 2
9+
},
10+
},
11+
{
12+
"caption": "Select next single quotes",
13+
"command": "select_next_quotes",
14+
"args": {
15+
"char": "'",
16+
"move_forward": true,
17+
"jumps": 2
18+
}
19+
}
20+
]

Diff for: README.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# Select-next-quotes
22
Select next quotes - plugin for Sublime Text 3
3+
4+
## Usage
5+
* <kbd>Ctrl+Alt+Right</kbd> to select next double-quoted content
6+
* <kbd>Ctrl+Alt+Left</kbd> to select previous double-quoted content
7+
8+
## In action
9+
![In action animation](https://user-images.githubusercontent.com/5006548/69914975-98b39080-1452-11ea-9e87-0884ec331ec7.gif)

Diff for: main.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)