Skip to content

Commit 2a0b2e9

Browse files
committed
added l.posX calculator command
1 parent e941a0e commit 2a0b2e9

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

commands/Default.sublime-commands

+4
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@
2323
{
2424
"caption": "MVT: Entity → Variable",
2525
"command": "mvt_entity_convert_to_variable"
26+
},
27+
{
28+
"caption": "MVT: l.pos Calculator",
29+
"command": "mvt_pos_calculator"
2630
}
2731
]

mvt-pos-calculator.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sublime, sublime_plugin, re
2+
3+
class MvtPosCalculatorCommand( sublime_plugin.TextCommand ):
4+
def run( self, edit ):
5+
6+
selections = self.view.sel()
7+
8+
for selection in selections:
9+
10+
# get region from start of file to first point in selection
11+
search_region = sublime.Region( 0, selection.a )
12+
13+
# get actual text of region
14+
search_text = self.view.substr( search_region )
15+
16+
# find all matches of "open" tags
17+
open_tags = re.findall( r'(?i)(<mvt:foreach)', search_text )
18+
close_tags = re.findall( r'(?i)(<\/mvt:foreach)', search_text )
19+
20+
# calculate the difference between the number of open and closed tags
21+
open_close_difference = len( open_tags ) - len( close_tags )
22+
23+
# check the difference
24+
if ( open_close_difference >= 1 ):
25+
26+
# generate output string
27+
output = 'l.pos' + str( open_close_difference );
28+
29+
# Replace the Variable selection with the generated l.posX
30+
self.view.replace( edit, selection, output )
31+
32+
else:
33+
34+
# Output error message
35+
self.view.set_status( mvt_error_status_key, 'No valid <mvt:foreach> tags detected' )
36+
threading.Timer( 3, self.view.erase_status, args=[mvt_error_status_key] ).start()

0 commit comments

Comments
 (0)