forked from roboshoes/SmartDuplicate-Sublime
/
SmartDuplicateCommand.py
51 lines (35 loc) · 1.89 KB
/
SmartDuplicateCommand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sublime, sublime_plugin, re
class SmartDuplicateCommand( sublime_plugin.TextCommand ):
def run( self, edit ):
for region in self.view.sel():
if region.empty():
line = self.view.line( region )
line_contents = self.scan( '\n' + self.view.substr( line ) )
self.view.insert( edit, line.end(), line_contents )
else:
self.view.insert( edit, region.begin(), self.view.substr( region ) )
def scan( self, string ):
def hardReplace( match ):
if ( match.group( 0 ) == ".x" ): return ".y"
elif ( match.group( 0 ) == ".y" ): return ".x"
def fillReplace( match ):
value = match.group( 0 )
if ( re.match( r"\w+X", value ) ): return value[ 0 : -1 ] + "Y"
if ( re.match( r"\w+Y", value ) ): return value[ 0 : -1 ] + "X"
def softReplace( match ):
value = match.group( 0 )
if ( value.istitle() ): transform = lambda string: string.title();
elif ( value.isupper() ): transform = lambda string: string.upper();
elif ( value.islower() ): transform = lambda string: string.lower();
else: transform = lambda string: string
value = value.lower();
if ( value == "height" ): return transform( "width" )
elif ( value == "width" ): return transform( "height" )
elif ( value == "left" ): return transform( "right" )
elif ( value == "right" ): return transform( "left" )
elif ( value == "top" ): return transform( "bottom" )
elif ( value == "bottom" ): return transform( "top" )
string = re.sub( r"(\.x|\.y)", hardReplace, string )
string = re.sub( r"(?i)(width|height|left|right|top|bottom)", softReplace, string )
string = re.sub( r"(\w+X|\w+Y)", fillReplace, string )
return string