-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSublimeTRFTextCommand.py
More file actions
154 lines (119 loc) · 3.92 KB
/
SublimeTRFTextCommand.py
File metadata and controls
154 lines (119 loc) · 3.92 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sublime
import sublime_plugin
# import re
def getCursorPos(view):
sel = view.sel()
if sel: return sel[0]
def getLineRegionAtPoint(view, point):
return view.line(point)
def getLineAtPoint(view, point):
region = getLineRegionAtPoint(view, point)
return view.substr(region)
def getCurrentLineRegion(view):
point = getCursorPos(view)
return getLineRegionAtPoint(view, point)
def getCurrentLine(view):
point = getCursorPos(view)
return getLineAtPoint(view, point)
def replaceLineWithText(view, edit, text):
region = getCurrentLineRegion(view)
view.replace(edit, region, text)
def addPhantomToCurrentLine(view, html, key = 'trf-phantastic!', single = False):
if single: view.erase_phantoms(key)
region = getCurrentLineRegion(view)
region = sublime.Region(region.a, region.b)
print(region)
return view.add_phantom(key, region, html, sublime.PhantomLayout.BLOCK)
def removePhantoms(view, key = 'trf-phantastic!'):
view.erase_phantoms(key)
def removePhantom(view, pid):
view.erase_phantom_by_id(pid)
def compareRegions(a, b):
return a.a == b.a and a.b == b.b
def isPhantomAtCurrentLine(view, pid):
if pid is None: return False
cregion = getCurrentLineRegion(view)
pregion = view.query_phantom(pid)[0]
return compareRegions(cregion, pregion)
def findPhantomAtCurrentLine(view, pids):
for pid in pids:
if isPhantomAtCurrentLine(view, pid):
return pid
def togglePhantomAtCurrentLine(view, pids, html, *aa, **kk):
pid = findPhantomAtCurrentLine(view, pids)
if pid:
removePhantom(view, pid)
else:
pid = addPhantomToCurrentLine(view, html, *aa, **kk)
pids.append(pid)
HTML = '''
<body id="trf-hint">
<style>
div.hint {
background-color: #333;
color: hotpink;
padding: 0;
}
</style>
<div class="hint">%s</div>
</body>
'''
def html(text, padded = True):
return HTML % text.replace(' ', ' ')
TOURNAMENT_START_DATE = ' DD/MM/YYYY'
# PLAYER_LINE = '001 1 m g Mirzoev Azer 2527 AZE 13400304 1978 4.0 1 26 w 1 13 b 1 8 w 1 4 b 1'
PLAYER_LINE = '001 1 m g Name 2700 FED 007 1968 3.0 1 2 w 1 3 b 1 4 w 1 5 b 1 6 w 1 7 b 1 8 b 1 9 w 1 10 b 1'
class Phantoms:
def __init__(self, view):
self.pids = []
# removeAll / clear / reset
# setAtCurrentLine / set
# removeFromCurrentLine / remove
# isSetAtCurrentLine / IsSet
# setAtLine / setAt
# removeFromLine / removeAt
# isSetAtLine / isSetAt
# toggleAtLine / toggleAt
# toggleAtCurrentLine / toggle
# self.view.add_regions('annotastic!',
# [sublime.Region(0, 100)],
# scope='invalid',
# annotations=[minihtml],
# flags=(sublime.DRAW_NO_FILL),
# on_close=self.hide_annotations)
#
# view.erase_regions('annotastic!')
# view.hide_popup()
class SublimeTrfTextCommand(sublime_plugin.TextCommand):
def __init__(self, *aa, **kk):
super().__init__(*aa, **kk)
self.pid = None
self.pids = []
def run(self, edit, action = ''):
self.edit = edit
# <div class="error"> DD/MM/YYYY</div>
if action == '042':
# This is highly problematic
# replaceLineWithText(self.view, edit, '042 DD/MM/YYYY')
pass
elif action == 'hint': # <F1>
line = getCurrentLine(self.view)
if line.startswith('042') or line.startswith('052'):
togglePhantomAtCurrentLine(self.view, self.pids, html(TOURNAMENT_START_DATE))
elif line.startswith('001'):
togglePhantomAtCurrentLine(self.view, self.pids, html(PLAYER_LINE))
elif self.pid:
if isPhantomAtCurrentLine(self.view, self.pid):
# removePhantoms(self.view)
removePhantom(self.view, self.pid)
self.pid = None
else:
self.pid = addPhantomToCurrentLine(self.view, html('Hello again!'))
else:
removePhantoms(self.view)
self.pid = addPhantomToCurrentLine(self.view, html('Hello!'))
elif action == 'unhint': # <S-F1>
removePhantoms(self.view)
self.pid = None
def onPhantomLinkClicked(self, href):
print(href)