-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathZnunyQuoteWithMarker.py
85 lines (64 loc) · 2.78 KB
/
ZnunyQuoteWithMarker.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from datetime import date
import sublime
import sublime_plugin
def plugin_loaded():
global settings
settings = sublime.load_settings("Znuny.sublime-settings")
class ZnunyQuoteWithMarkerCommand(sublime_plugin.TextCommand):
def run(self, edit):
quote_char_start = ""
quote_char_end = ""
code_marker_replace = ""
code_marker = settings.get("znuny_code_marker") or "Znuny"
current_time = date.today()
day = current_time.strftime("%d")
month = current_time.strftime("%m")
year = current_time.strftime("%Y")
code_marker = code_marker.replace("${year}", year)
code_marker = code_marker.replace("${month}", month)
code_marker = code_marker.replace("${day}", day)
meta = self.view.meta_info("shellVariables", 0)
for var in meta:
if var["name"] == "TM_COMMENT_START":
quote_char_start = var["value"]
if var["name"] == "TM_COMMENT_END":
quote_char_end = " " + var["value"]
# Loop over all selections.
for region in self.view.sel():
# Skip empty selections.
if region.empty():
next
# Get the selected text.
selection = self.view.substr(region)
# Start custom maker.
code_marker_replace = """{quote_char_start}---{quote_char_end}
{quote_char_start}{code_marker}{quote_char_end}
{quote_char_start}---{quote_char_end}
"""
# Add QuoteCharStart to every single line.
for line in selection.split("\n"):
if len(line) == 0:
continue
# Add quote.
code_marker_replace += "{quote_char_start}"
# Add old line and an linebreak.
code_marker_replace += line + "{quote_char_end}\n"
# Add old selection and trailing code marker.
code_marker_replace += selection
code_marker_replace += "\n\n{quote_char_start}---{quote_char_end}\n"
code_marker_replace = code_marker_replace.replace(
"{quote_char_start}", quote_char_start
)
code_marker_replace = code_marker_replace.replace(
"{quote_char_end}", quote_char_end
)
code_marker_replace = code_marker_replace.replace(
"{code_marker}", code_marker
)
# Replace the selection with transformed text
self.view.replace(edit, region, code_marker_replace)
# Clear selection regions / cursor position.
self.view.sel().clear()
# Set new regions to inserted custom marker package name.
for begin in self.view.find_all(code_marker + "\n"):
self.view.sel().add(sublime.Region(begin.a, begin.b - 1))