-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPrintHtml.py
1166 lines (1051 loc) · 57.9 KB
/
PrintHtml.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sublime, sublime_plugin
from os import path
import os, datetime, tempfile, desktop, re, sys, pickle
from plistlib import readPlist
PACKAGE_SETTINGS = "PrintHtml.sublime-settings"
if sublime.platform() == "linux":
# Try and load Linux Python2.6 lib. Default path is for Ubuntu.
linux_lib = sublime.load_settings(PACKAGE_SETTINGS).get("linux_python2.6_lib", "/usr/lib/python2.6/lib-dynload")
if not linux_lib in sys.path and path.exists(linux_lib):
sys.path.append(linux_lib)
OUTLINED = sublime.DRAW_OUTLINED if sublime.load_settings(PACKAGE_SETTINGS).get("use_outline", True) else 0
# whether to use an outline, or block, when highlighting comments: defaults to True == use an outline
ICON = "../PrintHtml/icon" if sublime.load_settings(PACKAGE_SETTINGS).get("use_icon", True) else ""
# a small icon to appear in the gutter - defaults to true (can interfere with ST-bookmarks)
ICONSCOPE = sublime.load_settings(PACKAGE_SETTINGS).get("icon_scope", "comment")
# affects the colour of the gutter icon
WORD, CMT, LINE, STAMP = range(4) # indices for the vcomments dictionary
UTF8 = ('utf-8', 'xmlcharrefreplace') # arguments for encode() function
HEADER = \
"""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>%(fname)s</title>
"""
CSS_MAIN = \
"""
<style type="text/css">
body { color: %(fcolor)s; background-color: %(bcolor)s; font: %(fsize)dpt '%(fface)s', Consolas, Monospace; }
#preCode { border: 0; margin: 0; margin-top: 1em; padding: 0; font: %(fsize)dpt '%(fface)s', Consolas, Monospace; }
span { color: %(fcolor)s; background-color: %(bcolor)s; display: inline; border: 0; margin: 0; padding: 0; }
* html a:hover { background: transparent; }
"""
CSS_COMMENTS = \
"""
.tooltip {
border-bottom: 1px dotted %(dot_colour)s;
outline: none; text-decoration: none;
position: relative;
}
.tooltip .comment {
border-radius: 5px 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 5px 5px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 5px 5px rgba(0, 0, 0, 0.1);
white-space: -moz-pre-wrap; /* Mozilla */
white-space: -hp-pre-wrap; /* HP printers */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: pre-wrap; /* CSS 2.1 */
white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
word-wrap: break-word; /* IE */
position: absolute; z-index: 99;
width: 250px; left: 1em; top: 2em; padding: 0.8em 1em;
margin-left: -999em;
color: blue; background: #FFFFAA; border: 1px solid #FFAD33;
font-family: Calibri, Tahoma, Geneva, sans-serif;
font-size: %(fsize)dpt; font-weight: bold;
}
.tooltip:hover .comment { margin-left: 0pt; }
.comment .stamp { position: absolute; top: 0;
color: black; background: #FFFFAA; font-size: x-small; }
#divComments { display: none; height: 400px; width: 540px; overflow: auto; margin: 0; }
#divComments.inpage { position: static; }
#divComments.inpage #bottom_row { display: none; }
#divComments.overlay { position: fixed; z-index: 99; height: 400px; right:50px;
top: 100px; bottom: auto; left: auto; }
#divComments.overlay_bl { position: fixed; z-index: 99; height: 200px; right: auto;
top: auto; bottom: 50px; left: 50px; overflow-y: scroll; }
#divComments.overlay #bottom_row, #divComments.overlay_bl #bottom_row { display: table-row; }
#tblComments {
box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 5px 5px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 5px 5px rgba(0, 0, 0, 0.1);
border-collapse: collapse; margin: 0;
font-family: Calibri, Tahoma, Geneva, sans-serif;
color: #000000; background-color: lightyellow;
}
#tblComments th, #tblComments td { border: thin solid; padding: 5px; }
td.nos { width: 50px; text-align: right; padding-right: 10px !important; }
td.stamps {text-align: center; font-size: smaller; }
td.cmts { min-width: 400px; max-width: 600px; }
"""
CKBs_COMMENTS = \
"""
<p>Show table of comments:<input type="checkbox" name="ckbComments" id="ckbComments" value="1" onclick="listComments()">
Overlay table of comments:<input type="checkbox" name="ckbOverlay" id="ckbOverlay" value="1" onclick="overlayComments()">
Show/hide comments (disables hover):<input type="checkbox" name="ckbToggle" id="ckbToggle" value="1" onclick="toggleComments()"></p>
"""
SCOPED = \
"""<span>%(t_text)s</span>"""
SCOPEDCOLOR = \
"""<span style="color:%(colour)s;">%(t_text)s</span>"""
SCOPEDCOMMENT = \
"""<a class="tooltip" href="#" onclick="return false;">%(scoped)s<span class="comment"><span class="stamp">%(stamp)s</span>%(comment)s</span></a>"""
JS_STANDARD = \
""" function dismissChecks() {
var chks = document.getElementById("dChecks");
chks.style.display = "none";
}
function tidySpaces() {
var olCode, spans, i, span_textnode, span_text, span_next, offLeft, newLeft;
if (document.getElementsByClassName) {
spans = document.getElementsByClassName('tidy');
if (spans != 'undefined' && spans.length) {
for ( i = 0; i < spans.length; i++ )
spans[i].style.paddingLeft = (spans[i].style.paddingLeft == '0px') ? spans[i].prevValue : '0px';
return;
}
}
olCode = document.getElementById('olCode');
spans = olCode.getElementsByTagName('span');
for (i = 0; i < spans.length; i++) {
if ( spans[i].previousSibling ) {
if ( spans[i].className && spans[i].className == 'comment')
continue;
span_textnode = spans[i].firstChild;
span_text = span_textnode.data;
tidied = span_text.replace(/\s{2,}$/,'');
if (span_text.length && (span_text.length > tidied.length)) {
if ( spans[i].nextSibling ) {
span_next = spans[i];
while ( (span_next = span_next.nextSibling) && span_next.className
&& span_next.className == 'comment' && span_next.nextSibling )
; // do nothing, get next span (or 'a' tag)
if ( span_next ) {
offLeft = span_next.offsetLeft;
newLeft = (parseInt(offLeft / 60)) * 60 + 60;
span_next.style.paddingLeft = (newLeft - offLeft) + 'px';
span_next.className = 'tidy';
span_next.prevValue = span_next.style.paddingLeft;
}
}
}
}
}
}
"""
JS_COMMENTS = \
""" function listComments() {
var comments_div = document.getElementById('divComments');
if (!comments_div.style.display || comments_div.style.display == 'none') {
if (!comments_div.className || comments_div.className == 'inpage')
document.getElementById('preCode').style.display = 'none';
comments_div.style.display = 'block';
}
else {
comments_div.style.display = 'none';
document.getElementById('preCode').style.display = 'block';
document.getElementById('ckbComments').checked = false;
}
}
function overlayComments() {
var comments_div = document.getElementById('divComments');
document.getElementById('preCode').style.display = 'block';
if (document.getElementById('ckbOverlay').checked) {
comments_div.className = (document.getElementById('ckbBottom').checked) ?
'overlay_bl' : 'overlay';
}
else {
comments_div.className = 'inpage';
}
}
function tableToBottom() {
document.getElementById('divComments').className =
(document.getElementById('ckbBottom').checked) ? 'overlay_bl' : 'overlay';
}
function toggleComments() {
var comments, newMargin, i;
if (document.getElementsByClassName) {
comments = document.getElementsByClassName('comment');
}
else if (document.querySelectorAll) {
comments = document.querySelectorAll('.comment');
}
else {
return false;
}
newMargin = (document.getElementById('ckbToggle').checked) ? '0pt' : '-999em';
for (i = 0; i < comments.length; i++) {
comments[i].style.marginLeft = newMargin;
}
}
function gotoLine(line_no) {
var code_list = document.getElementById('olCode');
var code_lines = code_list.getElementsByTagName('li');
// scroll the line(-5) into view (so it's not right at the top)
line_no = (line_no - 5 + 1) * (line_no - 5 + 1 > 0);
code_lines[line_no].scrollIntoView();
return false;
}
function addComment(e) {
var span_target, awrapper, new_comment, comment_text;
if (!e) e = window.event;
span_target = e.target || e.srcElement;
if (span_target.nodeName != 'SPAN' || span_target.className == 'comment') {
return false;
}
awrapper = document.createElement('a');
awrapper.href = '#';
awrapper.onclick = function () { return false; };
awrapper.className = 'tooltip';
new_comment = document.createElement('textarea');
new_comment.className = 'comment';
new_comment.style.marginLeft = '0pt';
new_comment.style.backgroundColor = 'yellow';
comment_text = document.createTextNode('New comment text');
new_comment.appendChild(comment_text);
awrapper.appendChild(span_target.cloneNode(true));
awrapper.appendChild(new_comment);
span_target.parentNode.replaceChild(awrapper, span_target);
return false;
}
window.onload = function () {
document.getElementById('olCode').ondblclick = addComment;
};
"""
COMMENTS_TBLHEAD = \
"""
<div id="divComments" class="inpage">
<table id="tblComments">
<tr><th>Line</th><th>dd/mm</th><th>The Comment</th></tr>
"""
COMMENTS_TBLROW = \
"""
<tr><td class="nos"><a href="#" onclick="gotoLine(%(line_adj)s);return false;">%(line_no)s</a></td>
<td class="stamps">%(stamp)s</td><td class="cmts">%(comment)s</td></tr>
"""
COMMENTS_TBLEND = \
"""
<tr id="bottom_row"><td colspan="3" style=\"font-size:smaller; text-align:right;\"><a href="#top">Top</a>
<a href="#" onclick="listComments();return false;">Close</a>
Position: bottom-left <input type="checkbox" name="ckbBottom" id="ckbBottom" value="1" onclick="tableToBottom()"></td>
</tr>
</table>
</div>
"""
def dt_stamp():
return datetime.datetime.now().strftime("%d/%m %H:%M")
def entity_ref(text, reverse=False):
if reverse:
return text.replace('<', '<').replace('>', '>').replace('&', '&')
else:
return text.replace('&', '&').replace('<', '<').replace('>', '>')
class CommentHtmlCommand(sublime_plugin.TextCommand):
sensible_word = re.compile(r"""[a-zA-Z_]{1}[a-zA-Z_0-9]+""")
def check_suitability(self, word): # returns (unsuitable, unsuitable_err)
if len(word) < 2:
return (True, "Cursor should be within a word (2 characters min): %s" % word)
elif not re.match(self.sensible_word, word):
return (True, "Place cursor within a 'sensible' word (not starting with a number): %s" % word)
else:
return (False, '') # ok, word is suitable to attach comment to
def get_metrics(self, pt = None): # return word, begin(), etc., at cursor or point
try:
if pt is None:
sels = self.view.sel()
sel = sels[0]
pt = sel.begin()
word_region = self.view.word(pt)
word = self.view.substr(word_region)
word_pt, word_end = (word_region.begin(), word_region.end())
line, col = self.view.rowcol(word_pt)
except Exception:
return {}
return locals()
def same_word(self, key_pt): # is the comment-point still on the same word?
try: # key_pt should be/will be pointing to the beginning
word_region = self.view.word(key_pt) # of the comment's word.
curr_word = self.view.substr(word_region)
except Exception:
return False
return (self.view.vcomments[key_pt][WORD] == curr_word)
def adjust_comments(self): # utility fn - move all comment-pts to beginning of their current
eov = self.view.size() # word, but remember the previous word (so it can be moved)
for key_pt in [ pt for pt in self.view.vcomments.keys() if pt < eov ]:
current = self.get_metrics(key_pt)
if not current or current['word_pt'] in self.view.vcomments:
continue # there is already a comment at the word's begin-point
existing_wd, comment, _line, _stamp = self.view.vcomments[key_pt]
self.view.vcomments[current['word_pt']] = (existing_wd, comment, current['line'], dt_stamp())
del self.view.vcomments[key_pt] # delete comment from its previous position
def get_comment(self): # return comment-text at cursor, or ''
if not hasattr(self.view, 'vcomments') or not self.view.vcomments:
return ''
self.adjust_comments() # first, position all comments at beginning of their 'word'
selection = self.get_metrics()
if selection and selection['word_pt'] in self.view.vcomments:
prev_word = self.view.vcomments[selection['word_pt']][WORD]
if selection['word'] != prev_word:
sublime.status_message("The comment-word has changed: %s" % prev_word)
return (entity_ref(self.view.vcomments[selection['word_pt']][CMT], True)) # the comment text
else:
return ''
def run(self, edit):
window = sublime.active_window()
view = window.active_view() if window != None else None
if view is None or view.id() != self.view.id():
sublime.status_message('Click into the view/tab first.')
return
if hasattr(self.view, 'ip_comments') and view.id() == self.view.ip_comments.id():
sublime.status_message('The input panel is already active?! Click into view/tab first.')
return
if not hasattr(self.view, 'vcomments'): # on first run (for this view)
self.view.vcomments = {}
curr_comment = ''
try:
self.view.erase_regions("comments") # in case they have persisted
self.view.erase_regions("comment_errs")
self.view.erase_regions("hidden_cmts")
except Exception:
pass
fname = self.view.file_name()
if fname == None or not path.exists(fname):
sublime.status_message("It is recommended to save the file before creating comments.")
else:
sublime.status_message("Use 'load' command to load saved comments.")
else:
curr_comment = self.get_comment()
if not hasattr(self.view, 'highlighted'):
self.view.highlighted = False
self.more_comments = True # panel will continue to be displayed
self.just_added = False # haven't just added a new comment
self.show_comment_panel(curr_comment) # show panel and comment at cursor (or '')
def select_comments(self): # clears current selection and highlights, and deletes
sels = self.view.sel() # any comments that are no longer on the same word
if sels:
sel_orig = sels[0]
sels.clear()
else:
sel_orig = sublime.Region(0, 0) # default to beginning of view
_ = self.remove_highlights()
eov = self.view.size()
for key_pt in sorted(self.view.vcomments.keys()):
prev_wd, prev_comment, prev_line, _stamp = self.view.vcomments[key_pt]
if key_pt >= eov: # delete comments past end of the view
del self.view.vcomments[key_pt]
print "Comment past end-of-view deleted: %s (was line %d)" % (prev_comment, prev_line + 1)
continue
current = self.get_metrics(key_pt)
if current and self.same_word(key_pt):
sels.add(current['word_region'])
if len(sels) == 1: # show 1st comment region
self.view.show(current['word_region'])
else:
print "DELETED: Commented word was '%s' on line %d comment: %s" \
% (prev_wd, prev_line + 1, prev_comment)
del self.view.vcomments[key_pt] # delete mis-positioned comment
if not sels: # nothing selected, so re-instate original (1st) selection,
sels.add(sel_orig) # or beginning of view
if not self.view.vcomments:
return 'Comments no longer in original positions - deleted.'
def select_next(self, direction = 'down', start_at = -99): # start at a certain pt in view
message = None
if start_at > -99: # could be -1 (from beginning of view), view.size()+1, etc.
curr_pt = start_at
else:
selection = self.get_metrics()
if not selection:
return 'Ensure the cursor is somewhere in the view.'
curr_pt = selection['pt']
if direction == 'down':
sorted_pts = (key_pt for key_pt in sorted(self.view.vcomments.keys()) if key_pt > curr_pt)
else:
sorted_pts = (key_pt for key_pt in reversed(sorted(self.view.vcomments.keys())) if key_pt < curr_pt)
try:
next_pt = sorted_pts.next()
except StopIteration:
next_pt = None
if next_pt is not None:
next_wd, next_comment, next_line, _stamp = self.view.vcomments[next_pt]
if next_pt >= self.view.size(): # next comment is beyond the view-size
print "Comment past end-of-view: %s (was line %d)" % (next_comment, next_line + 1)
return "Comment is past end-of-view - use 'recover' command: %s (was line %d)" \
% (next_comment, next_line + 1)
current = self.get_metrics(next_pt)
if not current: # problem reading points' word, etc.
return "Unable to read details at next comment point."
if not self.same_word(next_pt):
message = "The word has changed - was '%s'" % next_wd
print "Commented word was '%s' on line %d now '%s' on line %d comment: %s" \
% (next_wd, next_line + 1, current['word'], current['line'] + 1, next_comment)
sels = self.view.sel()
sels.clear()
sels.add(current['word_region'])
self.view.show(current['word_region'])
else:
message = 'No comment %s cursor.' % ({"down": "after", "up": "before"}.get(direction))
return message
def highlight_comments(self): # highlight all comments, including mis-positioned ones
_ = self.remove_highlights()
comment_regions = []
comment_errors = []
eov = self.view.size()
beyond_eov = False # are their any comments beyond the view-size?
for key_pt in sorted(self.view.vcomments.keys()):
prev_wd, prev_comment, prev_line, _stamp = self.view.vcomments[key_pt]
if key_pt >= eov: # comment is beyond the view-size
print "Comment is past end-of-view - use 'recover' command: %s" % (prev_comment)
beyond_eov = True
continue
current = self.get_metrics(key_pt)
if not current: # problem reading points' word, etc.
print "DELETED: Could not find a location for comment: %s" % (prev_comment)
del self.view.vcomments[key_pt]
continue
if self.same_word(key_pt):
if not comment_regions and not comment_errors:
self.view.show(current['word_region']) # show the 1st highlighted region
comment_regions.append(current['word_region'])
else:
print "Commented word was '%s' now '%s' on line %d comment: %s" \
% (prev_wd, current['word'], current['line'] + 1, prev_comment)
if not comment_regions and not comment_errors:
self.view.show(current['word_region']) # show the 1st highlighted region
comment_errors.append(current['word_region'])
if comment_regions:
self.view.add_regions("comments", comment_regions, "comment", OUTLINED)
self.view.highlighted = True
if comment_errors:
self.view.add_regions("comment_errs", comment_errors, "invalid", OUTLINED)
self.view.highlighted = True
if beyond_eov:
return "There are comment(s) beyond the view-size - use 'recover' command."
elif self.view.highlighted:
return 'Highlights updated.'
else:
return 'No commment-highlights found.'
def remove_highlights(self):
try:
self.view.erase_regions("comments")
self.view.erase_regions("comment_errs")
self.view.highlighted = False
return 'Removed comment highlighting.'
except Exception:
return None
def remove_all_hidden(self): # utility fn - not called as a 'command'
try:
self.view.erase_regions("hidden_cmts")
except Exception:
pass
def remove_highlight(self, pt): # utility fn - not called as a 'command'
self.remove_hidden(pt)
if not self.view.highlighted:
return
high_cs = self.view.get_regions("comments")
high_errs = self.view.get_regions("comment_errs") # the highlight might be an error-region
if not high_cs and not high_errs: # nothing to remove
return
comment_regions = []
for reg in [r for r in high_cs if not r.contains(pt)]:
comment_regions.append(reg) # add regions other than the current one
self.view.erase_regions("comments")
if comment_regions:
self.view.add_regions("comments", comment_regions, "comment", OUTLINED)
comment_errors = [] # repeat sequence for error regions
for reg in [r for r in high_errs if not r.contains(pt)]:
comment_errors.append(reg)
self.view.erase_regions("comment_errs")
if comment_errors:
self.view.add_regions("comment_errs", comment_errors, "invalid", OUTLINED)
def remove_hidden(self, pt): # utility fn - not called as a 'command'
hidden = self.view.get_regions("hidden_cmts")
if not hidden:
return
hidden_temp = []
for reg in [r for r in hidden if not r.contains(pt)]:
hidden_temp.append(reg)
self.view.erase_regions("hidden_cmts")
if hidden_temp:
self.view.add_regions("hidden_cmts", hidden_temp, ICONSCOPE, ICON, sublime.HIDDEN)
def add_highlight(self, new_region, error = False): # utility fn - not called as a 'command'
self.add_hidden(new_region)
if not self.view.highlighted:
return
if error: # error == True: add as error region
high_reg, high_scope = ("comment_errs", "invalid")
else:
high_reg, high_scope = ("comments", "comment")
highs = self.view.get_regions(high_reg) or []
highs.append(new_region)
self.view.add_regions(high_reg, highs, high_scope, OUTLINED)
def add_hidden(self, new_region):
hidden = self.view.get_regions("hidden_cmts") or []
hidden.append(new_region)
self.view.add_regions("hidden_cmts", hidden, ICONSCOPE, ICON, sublime.HIDDEN)
def follow_highlights(self): # attempt to re-position comments to highlighted regions
if not self.view.highlighted: # (if there are the same number of comments as highlights)
return 'View comments are not currently highlighted.'
high_cs = self.view.get_regions("comments")
high_errs = self.view.get_regions("comment_errs")
if not high_cs and not high_errs:
return 'There are no highlighted regions to follow.'
if high_errs:
return 'There are errors highlighted which need to be corrected.'
if len(high_cs) != len(self.view.vcomments):
return 'There are not the same number of comments as highlights.'
_ = self.remove_highlights() # will set self.view.highlighted = False
comment_regions = []
comment_errors = []
for pt, area in zip(sorted(self.view.vcomments.keys()), high_cs):
prev_wd, prev_comment, _line, _stamp = self.view.vcomments[pt]
c_highlight = self.get_metrics(area.begin())
if not c_highlight:
continue # unable to read metrics at highlight
if c_highlight['word'] == prev_wd:
comment_regions.append(c_highlight['word_region'])
else:
comment_errors.append(c_highlight['word_region'])
if c_highlight['word_pt'] != pt: # if not already there, move comment
self.view.vcomments[c_highlight['word_pt']] = \
(prev_wd, prev_comment, c_highlight['line'], dt_stamp())
del self.view.vcomments[pt]
message = 'Unable to re-position comments.'
if comment_regions:
self.view.add_regions("comments", comment_regions, "comment", OUTLINED)
self.view.highlighted = True
message = 'Comments re-positioned to highlights.'
if comment_errors:
self.view.add_regions("comment_errs", comment_errors, "invalid", OUTLINED)
self.view.highlighted = True
message = 'Some comments are in the wrong position.'
return message
def correct_to_hidden(self): # if there are the same number of comments as hidden regions
hidden = self.view.get_regions("hidden_cmts")
if not hidden or (len(hidden) != len(self.view.vcomments)):
return 'The number of comments and hidden regions differ.'
for pt, area in zip(sorted(self.view.vcomments.keys()), hidden):
prev_wd, prev_comment, _line, _stamp = self.view.vcomments[pt]
c_hidden = self.get_metrics(area.begin())
if not c_hidden:
continue # unable to read metrics at hidden region
if c_hidden['word_pt'] != pt: # if not already there, move comment
self.view.vcomments[c_hidden['word_pt']] = \
(prev_wd, prev_comment, c_hidden['line'], dt_stamp())
del self.view.vcomments[pt]
if self.view.highlighted:
return self.highlight_comments()
else:
return "Use 'highlight' command to check comment' positions."
def delete_comment(self): # at cursor position (or within selection)
selection = self.get_metrics()
if not selection:
return 'Unable to read details at cursor.'
if selection['sel'].empty(): # delete any comment(s) within the current word
begin = selection['word_pt']; end = selection['word_end']
else: # delete comments within the selection
begin = selection['sel'].begin(); end = selection['sel'].end()
deleted = False
for cmt in [pt for pt in self.view.vcomments.keys() if begin <= pt <= end]:
del self.view.vcomments[cmt]
deleted = True # at least one comment deleted
self.remove_highlight(cmt)
return 'Comment(s) deleted.' if deleted else 'No comment(s) found to delete.'
def delete_all_comments(self):
_ = self.remove_highlights()
self.remove_all_hidden()
self.view.vcomments = {}
return 'All comments deleted.'
def push_comments(self, direction = 'down'): # move selected comment(s) down or up
selection = self.get_metrics() # or recover ones beyond the view-size
if not selection:
return 'Ensure the cursor is somewhere in the view.'
sels, curr_sel = (selection['sels'], selection['sel'])
if curr_sel.empty() and direction != 'recover': # deleting a single comment
if selection['word_pt'] in self.view.vcomments:
pt_begin = pt_end = selection['word_pt']
else:
return 'No comment found at cursor.'
elif direction == 'recover': # bring back comments that are beyond end-of-view
pt_begin = self.view.size()
pt_end = max(self.view.vcomments)
if pt_end < pt_begin:
return 'There are no comments beyond the view-size.'
else:
pt_begin, pt_end = (curr_sel.begin(), curr_sel.end()) # pushing comments within selection
if pt_begin == pt_end:
sorted_pts = [pt_begin] # just the current comment to move (use single list-item)
else:
sorted_pts = [key_pt for key_pt in sorted(self.view.vcomments.keys()) if pt_begin <= key_pt <= pt_end]
if direction == 'down':
sorted_pts = reversed(sorted_pts)
sels.clear()
for next_pt in sorted_pts:
prev_wd, prev_comment, _line, _stamp = self.view.vcomments[next_pt]
if direction == 'down': # find next occurrence of the comment-word
new_region = self.view.find(prev_wd, next_pt + 1, sublime.LITERAL)
else: # find previous occurrence of comment-word
new_regions = (r for r in reversed(self.view.find_all(prev_wd, sublime.LITERAL)) \
if r.begin() < next_pt)
try:
new_region = new_regions.next()
except StopIteration:
continue
if new_region: # was the comment-word found further up/down?
new_region_begin = new_region.begin()
new_comment_line, _ = self.view.rowcol(new_region_begin)
if new_region_begin in self.view.vcomments:
_, old_comment, old_line, _stamp = self.view.vcomments[new_region_begin]
sels.add(new_region)
self.view.show(new_region)
print "Already a comment at line %d comment: %s" % (new_comment_line + 1, old_comment)
return "Already a comment at line %d comment: %s" % (new_comment_line + 1, old_comment)
self.view.vcomments[new_region_begin] = (prev_wd, prev_comment, new_comment_line, dt_stamp())
self.add_highlight(new_region, False)
del self.view.vcomments[next_pt] # delete the comment from its previous position
self.remove_highlight(next_pt)
if not sels: # show the 1st region (highlighted or not)
sels.add(new_region)
self.view.show(new_region)
elif not self.view.highlighted:
sels.add(new_region)
if not sels:
if curr_sel is not None: # re-instate original (1st) selection
sels.add(curr_sel)
return 'Was unable to move any comment(s).'
def pull_comment(self, direction = 'down'): # pull next or previous comment to cursor position
selection = self.get_metrics()
if not selection:
return 'Ensure the cursor is in a word in the view.'
if len(selection['sels']) > 1:
return "'Pull' doesn't work with multi-selection."
unsuitable, unsuitable_err = self.check_suitability(selection['word'])
if unsuitable: # not a suitable word to attach a comment to
return unsuitable_err
curr_pt = selection['word_pt']
if curr_pt in self.view.vcomments:
return 'There is already a comment at the cursor.'
if direction == 'down':
sorted_pts = (key_pt for key_pt in reversed(sorted(self.view.vcomments.keys())) if key_pt < curr_pt)
else:
sorted_pts = (key_pt for key_pt in sorted(self.view.vcomments.keys()) if key_pt > curr_pt)
try:
next_pt = sorted_pts.next()
except StopIteration:
return "There is no comment to pull %s to the cursor position." % (direction)
# The comment we are moving may not be in it's correctly highlighted position. So,
discard_message = self.correct_to_hidden()
next_wd, next_comment, _line, _stamp = self.view.vcomments[next_pt]
self.view.vcomments[curr_pt] = (selection['word'], next_comment, selection['line'], dt_stamp())
del self.view.vcomments[next_pt]
self.remove_highlight(next_pt) # that is, from its 'previous' position
self.add_highlight(selection['word_region'], False)
sels = selection['sels']
sels.clear()
sels.add(selection['word_region']) # select the 'pulled' comments' region
self.view.show(selection['word_region'])
return "Comment pulled %s to cursor position." % (direction)
def add_comment(self, text, code = False): # at cursor position
curr = self.get_metrics()
if not curr:
return 'Unable to read word at cursor.'
unsuitable, unsuitable_err = self.check_suitability(curr['word'])
if unsuitable: # not a suitable word to attach a comment to
return unsuitable_err
if code: # use the line's code as the comment-text
text = self.view.substr(self.view.line(curr['word_pt'])).strip()[:60]
comment = entity_ref(text)
comment = comment.replace('\t', ' ' * 4).strip()
if curr['word_pt'] in self.view.vcomments and \
self.view.vcomments[curr['word_pt']][CMT] == comment: # it's the same comment..
if self.view.vcomments[curr['word_pt']][WORD] != curr['word']:
# .. but it's a different word, so they are correcting the comment
self.view.vcomments[curr['word_pt']] = \
(curr['word'], comment, curr['line'], dt_stamp())
print "Comment-word corrected at line %d: %s" % (curr['line'] + 1, comment)
self.remove_highlight(curr['word_pt'])
self.add_highlight(curr['word_region'], False) # False == not an error region
return 'Comment corrected to current word.'
select_msg = self.select_next('down') # ..otherwise, move to the next comment..
if select_msg is not None and select_msg.startswith('No comment'):
select_msg = self.select_next('down', -1) # ..loop to top of view
return select_msg
else: # create a new comment at the cursor
self.view.vcomments[curr['word_pt']] = \
(curr['word'], comment, curr['line'], dt_stamp())
print "New comment at line %d: %s" % (curr['line'] + 1, comment)
self.add_highlight(curr['word_region'], False) # False == not an error region
self.just_added = True # don't re-display the comment text
return "Comment added: %s" % (self.view.vcomments[curr['word_pt']][CMT])
def save_comments(self): # the same filename/location, with 'cmts' added at end
fname = self.view.file_name()
if fname == None or not path.exists(fname):
fname = "Untitled."
try:
fname_dict = open(fname + 'cmts', 'wb')
pickle.dump(self.view.vcomments, fname_dict)
except IOError:
return "Could not create comments file: %s" % (fname + 'cmts')
fname_dict.close()
if fname == "Untitled.": # getcwd() - 'current working directory'
print "File not saved, so comments saved as: %s%sUntitled.cmts" % (os.getcwd(), os.path.sep)
return "File not saved, so comments saved as: %s%sUntitled.cmts" % (os.getcwd(), os.path.sep)
else:
print "Saved as %s" % (fname + 'cmts')
return "Saved as %s" % (fname + 'cmts')
def load_comments(self): # the same filename/location, with 'cmts' added at end
fname = self.view.file_name()
if fname == None or not path.exists(fname):
return "The filename is not available, so comments cannot be loaded."
try:
fname_dict = open(fname + 'cmts', 'rb')
the_comments = pickle.load(fname_dict)
except IOError:
return "Could not find or read comments file: %s" % (fname + 'cmts')
fname_dict.close()
if not the_comments:
return "No comments found in %s" % (fname + 'cmts')
_ = self.remove_highlights()
self.remove_all_hidden()
self.view.vcomments = the_comments
return "Comments loaded - use 'Select' or 'Highlight' command."
def process_commentary(self, text, caller_id): # on_done for comments panel
self.more_comments = False # assume there is a problem with commentary
window = sublime.active_window()
view = window.active_view() if window != None else None
if view is None:
sublime.status_message('No active view.')
return
elif view.id() != caller_id:
sublime.status_message('Not the same view - cancelled.')
return
text = text.strip()
if not text: # They pressed Enter, attempt to display comment at cursor
if self.get_comment() != '':
self.more_comments = True
self.show_again()
return # the comments-panel will close and self.more_comments remains False
self.more_comments = True # okay to continue displaying the panel
comment_command = text.strip().upper()
has_comments = hasattr(self.view, 'vcomments') and self.view.vcomments
message = None # possible message to display in the status bar
if comment_command in ('SELECT','SEL','SEL ALL','SELECT ALL'): # select commented words
message = self.select_comments() if has_comments else 'There are no comments to select.'
elif comment_command in ('NEXT','NXT','DOWN','DWN','SELECT NEXT'): # select the next comment
message = self.select_next('down') if has_comments else 'There are no comments to select.'
elif comment_command in ('PREV','PREVIOUS','UP','SELECT' 'PREV'): # select the previous comment
message = self.select_next('up') if has_comments else 'There are no comments to select.'
elif comment_command == 'FIRST': # select the first comment
message = self.select_next('down', -1) if has_comments else 'There are no comments to select.'
elif comment_command == 'LAST': # select the last comment
message = self.select_next('up', max(self.view.vcomments) + 1) if has_comments \
else 'There are no comments to select.'
elif comment_command in ('HIGH','HIGHLIGHT'): # highlight all comments
message = self.highlight_comments() if has_comments else 'There are no comments to highlight.'
elif comment_command in ('REMOVE','REMOVE HIGHLIGHT','REMOVE HIGHLIGHTS'): # remove highlights
message = self.remove_highlights()
elif comment_command in ('FOLLOW','FOLLOW HIGHLIGHTS'): # move/adjust comments to highlighted regions
message = self.follow_highlights() if has_comments else 'There are no comments for the current view.'
elif comment_command == 'CORRECT': # attempt to align comments at hidden regions
message = self.correct_to_hidden() if has_comments else 'There are no comments for the current view.'
elif comment_command in ('DEL','DELETE'): # delete comment at cursor
message = self.delete_comment() if has_comments else 'There are no comments to delete.'
elif comment_command in ('DEL ALL','DELALL','DELETE ALL','DELETEALL'): # delete all comments
message = self.delete_all_comments() if has_comments else 'There are no comments to delete.'
elif comment_command in ('PUSH','PUSH DOWN','PUSH D','PUSH DWN','PUSHDOWN'): # push comment(s) downwards
message = self.push_comments('down') if has_comments else 'No comments to push down.'
elif comment_command in ('PUSH UP','PUSH U','PUSHUP'): # push comment(s) upwards
message = self.push_comments('up') if has_comments else 'No comments to push up.'
elif comment_command in ('PULL','PULL DOWN','PULL D','PULLDOWN'): # pull comment down to cursor
message = self.pull_comment('down') if has_comments else 'No comments to pull down.'
elif comment_command in ('PULL UP','PULL U','PULLUP'): # pull comment up to cursor
message = self.pull_comment('up') if has_comments else 'No comments to pull up.'
elif comment_command in ('RECOVER','RECOVER COMMENTS','RECOVER COMMENT'): # recover comments beyond view
message = self.push_comments('recover') if has_comments else 'No comments in the current view.'
elif comment_command in ('SAVE','SAVE COMMENTS','SAVECOMMENTS'):
message = self.save_comments() if has_comments else 'No comments to save.'
elif comment_command in ('LOAD','LOAD COMMENTS','LOAD COMMENT'):
message = self.load_comments()
elif comment_command.isdigit() or (comment_command[0] == '-' and comment_command[1:].isdigit()):
try:
line = int(comment_command)
self.view.run_command("goto_line", {"line": line} )
except Exception:
pass
elif comment_command in ("CODE","USE CODE", ";"):
message = self.add_comment(text, True) # use the code's text for the comment
else:
message = self.add_comment(text) # add new (or correct) comment at cursor
if message is not None:
sublime.status_message(message)
self.show_again() # the "commments panel"
def show_comment_panel(self, existing_comment):
caller_id = self.view.id()
self.view.ip_comments = self.view.window().show_input_panel('Comment>', existing_comment, \
lambda txt: self.process_commentary(txt, caller_id), None, self.hide_it)
def show_again(self): # the input panel
if self.more_comments:
if self.just_added: # don't show comment text if it has just been added
self.just_added = False
curr_comment = ''
else:
curr_comment = self.get_comment()
self.show_comment_panel(curr_comment)
def hide_it(self): # the input panel
self.more_comments = False
class QuickCommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
window = sublime.active_window()
view = window.active_view() if window != None else None
if view is None or view.id() != self.view.id():
sublime.status_message('Click into the view/tab first.')
elif not hasattr(self.view, 'vcomments') or not self.view.vcomments:
sublime.status_message('No comments for this view.')
else:
the_comments = []
for key_pt in sorted(self.view.vcomments.keys()):
the_comments.append("%s Line: %03d %s" % ( self.view.vcomments[key_pt][STAMP],
self.view.vcomments[key_pt][LINE] + 1,
entity_ref(self.view.vcomments[key_pt][CMT], True)))
window.show_quick_panel(the_comments, self.on_chosen)
def on_chosen(self, index):
if index == -1: return
sorted_keys = (k for (i, k) in enumerate(sorted(self.view.vcomments.keys())) if i == index)
try:
the_key = sorted_keys.next()
except StopIteration:
sublime.status_message("Comment-point not found.")
return
if the_key > self.view.size():
sublime.status_message("Comment is no longer within the view-size - use 'recover' command.")
return
comment_region = self.view.word(the_key)
sels = self.view.sel()
sels.clear()
sels.add(comment_region)
self.view.show(comment_region)
if self.view.substr(comment_region) != self.view.vcomments[the_key][WORD]:
sublime.status_message("The comment is no longer on its original word.")
else:
sublime.status_message("Comment: %s" % (entity_ref(self.view.vcomments[the_key][CMT], True)))
class PrintHtmlCommand(sublime_plugin.TextCommand):
def setup(self, numbers):
path_packages = sublime.packages_path()
if not hasattr(self.view, 'vcomments'):
self.view.vcomments = {} # create empty dictionary anyway
self.has_comments = False
else:
self.has_comments = (len(self.view.vcomments) > 0)
self.file_name = self.view.file_name()
if self.file_name == None or not path.exists(self.file_name):
self.file_name = "Untitled"
# Get general document preferences from sublime preferences
settings = sublime.load_settings('Preferences.sublime-settings')
self.font_size = settings.get('font_size', 10)
self.font_face = settings.get('font_face', 'Consolas')
self.tab_size = settings.get('tab_size', 4)
self.padd_top = settings.get('line_padding_top', 0)
self.padd_bottom = settings.get('line_padding_bottom', 0)
self.numbers = numbers
# Get color scheme
alt_scheme = sublime.load_settings(PACKAGE_SETTINGS).get("alternate_scheme", False)
scheme_file = settings.get('color_scheme') if alt_scheme == False else alt_scheme
colour_scheme = path.normpath(scheme_file)
plist_file = readPlist(path_packages + colour_scheme.replace('Packages', ''))
colour_settings = plist_file["settings"][0]["settings"]
# Get general theme colors from color scheme file
self.bground = colour_settings.get('background', '#FFFFFF')
self.fground = colour_settings.get('foreground', '#000000')
self.gfground = colour_settings.get('gutterForeground', self.fground)
# Determine start and end points and whether to parse whole file or selection
curr_sel = self.view.sel()[0]
if curr_sel.empty() or len(self.view.lines(curr_sel)) < 2: # don't print just 1 line
self.size = self.view.size()
self.pt, self.end, self.curr_row, self.partial = (0, 1, 1, False) # partial = False: print entire view
else:
self.size = curr_sel.end()
self.pt = curr_sel.begin()
self.end = self.pt + 1
self.curr_row = self.view.rowcol(self.pt)[0] + 1
self.partial = True # printing selection
if self.has_comments: # are there any comments within the selection?
within_sel = (pt for pt in self.view.vcomments.keys() if self.pt <= pt <= self.size)
try:
any_pt = within_sel.next()
except StopIteration:
self.has_comments = False # that is, none within selection
# Create scope colour-mapping from colour-scheme file
self.colours = { self.view.scope_name(self.end).split(' ')[0]: self.fground }
for item in plist_file["settings"]:
scope = item.get('scope', None)
if 'settings' in item and 'foreground' in item['settings']:
colour = item['settings']['foreground']
else:
colour = None
if scope != None and colour != None:
self.colours[scope] = colour
def guess_colour(self, the_key):
the_colour = None
if the_key in self.colours:
the_colour = self.colours[the_key]
else:
best_match = 0
for key in self.colours:
if self.view.score_selector(self.pt, key) > best_match:
best_match = self.view.score_selector(self.pt, key)
the_colour = self.colours[key]
self.colours[the_key] = the_colour or self.fground
return the_colour or self.fground
def add_comments_table(self, the_html):
the_html.write((COMMENTS_TBLHEAD).encode(*UTF8))
for (line_no, comment, stamp) in self.comments_list:
the_html.write((COMMENTS_TBLROW % {"line_adj": str(line_no - self.curr_row), \
"line_no": str(line_no + 1), "stamp": stamp, "comment": comment}).encode(*UTF8))
the_html.write((COMMENTS_TBLEND).encode(*UTF8))
def write_header(self, the_html):
the_html.write((HEADER % {"fname": self.file_name}).encode(*UTF8))
the_html.write((CSS_MAIN % {"fcolor": self.fground, "bcolor": self.bground, \
"fsize": self.font_size, "fface": self.font_face}).encode(*UTF8))