-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorstop_plugin.py
805 lines (658 loc) · 24 KB
/
doorstop_plugin.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
import json
from pathlib import Path
import re
import sublime
import sublime_plugin
from .doorstop_util import Setting
from .doorstop_util import Settings
from . import doorstop_util
DOORSTOP_KEY = "doorstop"
settings = None
def trace():
# NOTE to future me:
# Start Sublime with:
# ▶ /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text
# then call this function to start debugger
import pdb
import sys
pdb.Pdb(stdout=sys.__stdout__).set_trace()
def plugin_loaded():
"""
Hook that is called by Sublime when plugin is loaded.
"""
global settings
settings = Settings()
doorstop_util.settings = settings
def plugin_unloaded():
"""
Hook that is called by Sublime when plugin is unloaded.
"""
global settings
settings.remove_callbacks()
for key in list(globals().keys()):
if "doorstop" in key.lower():
del globals()[key]
class DoorstopSetDoorstopPythonInterpreterCommand(sublime_plugin.ApplicationCommand):
"""
Command to set the python interpreter in settings that will be
used to run the script `doorstop_cli/doorstop_cli.py`
"""
def run(self, interpreter):
global settings
settings.set(Setting().INTERPRETER, interpreter)
settings.save()
# TODO: maybe use set_project_data(data) of WindowCommand instead?
def input(self, args):
return DoorstopPythonInterpreterInputHandler()
class DoorstopPythonInterpreterInputHandler(sublime_plugin.TextInputHandler):
"""
TextInputHandler that will ask the user to specify a valid python interpreter.
It only accepts input when it can run the python interpreter to import the
doorstop package.
"""
def name(self):
return "interpreter"
def preview(self, text):
return "Please select a valid python interpreter with doorstop available"
def validate(self, text):
import subprocess
return subprocess.call([text, "-c", "import doorstop"]) == 0
class DoorstopDebugCommand(sublime_plugin.TextCommand):
"""
Debug command that prints settings
"""
def run(self, edit):
global settings
for setting in Setting():
print("{}: {}".format(setting, settings.get(setting)))
class DoorstopAddItemCommand(sublime_plugin.WindowCommand):
"""
Adds an item to the specified doorstop document
"""
def _add_item(self, text):
args = ["--prefix", self.document]
if len(text) > 1:
args += ["--text", text]
new_item = doorstop_util.doorstop(self, "add_item", *args)
path = list(new_item.values())[0]
self.window.open_file(path)
def run(self, document):
self.document = document
self.window.show_input_panel(
"Specify (optional) text for new {} item".format(document),
"",
self._add_item,
None,
None,
)
def input(self, args):
project_dir = doorstop_util.doorstop_root(window=self.window)
return DoorstopFindDocumentInputHandler(project_dir)
def is_enabled(self, *args):
return doorstop_util.is_doorstop_configured(
view=self.window.active_view(), window=self.window
)
class DoorstopGotoAnyItemCommand(sublime_plugin.WindowCommand):
"""
GoTo any doorstop item.
"""
def run(self, document, item):
self.window.open_file(item)
def input(self, args):
project_dir = doorstop_util.doorstop_root(window=self.window)
return DoorstopFindDocumentInputHandler(
project_dir, DoorstopFindItemPathInputHandler
)
def is_enabled(self, *args):
return doorstop_util.is_doorstop_configured(
view=self.window.active_view(), window=self.window
)
class DoorstopCreateReferenceCommand(sublime_plugin.TextCommand):
"""
Add a new reference to an existing doorstop item.
"""
def run(self, edit, document, item):
reference = doorstop_util.reference(self.view)
result = doorstop_util.doorstop(
self, "add_reference", "--item", item, json.dumps(reference)
)
self.view.window().open_file(result["path"])
def is_enabled(self, *args):
return self.view.file_name() is not None
def input(self, args):
root = doorstop_util.doorstop_root(view=self.view)
return DoorstopFindDocumentInputHandler(root, DoorstopFindItemInputHandler)
class DoorstopAddLinkCommand(sublime_plugin.TextCommand):
"""
Links an item to the current doorstop item.
"""
def run(self, edit, document, item):
file_name = Path(self.view.file_name())
result = doorstop_util.doorstop(self, "link", file_name.stem, item)
self.view.window().open_file(result["path"])
def input(self, args):
root = doorstop_util.doorstop_root(view=self.view)
return DoorstopFindDocumentInputHandler(root, DoorstopFindItemInputHandler)
def is_enabled(self, *args):
return doorstop_util.is_doorstop_item_file(self.view.file_name())
class DoorstopFindDocumentInputHandler(sublime_plugin.ListInputHandler):
"""
List input handler that will show a list of doorstop documents
for the user to choose from.
"""
def __init__(self, root, next_input_type=None):
"""
root: str
Root path of doorstop
next_input_type: InputHandler
Handler to show when user has picked document
"""
self.root = root
self.next_input_type = next_input_type
def name(self):
return "document"
def list_items(self):
items = doorstop_util.doorstop(self.root, "documents")
if not items:
return []
return [item["prefix"] for item in items]
def next_input(self, args):
if self.next_input_type:
return self.next_input_type(self.root, args["document"])
return None
class DoorstopFindItemInputHandler(sublime_plugin.ListInputHandler):
"""
List input handler that will show a list of doorstop items
for the given document name for the user to choose from.
"""
def __init__(self, root, document, result_as_path=False):
"""
root: str
Root path of doorstop
document: str
Document prefix/name
"""
self.root = root
self.document = document
self.result_as_path = result_as_path
def name(self):
return "item"
def list_items(self):
items = doorstop_util.doorstop(self.root, "items", "--prefix", self.document)
if not items:
return []
return [
(
"{}: {}".format(item["uid"], item["text"]),
item["path"] if self.result_as_path else item["uid"],
)
for item in items
]
class DoorstopFindItemPathInputHandler(DoorstopFindItemInputHandler):
def __init__(self, root, document):
super().__init__(root, document, result_as_path=True)
class DoorstopReferencedLocationsListener(sublime_plugin.ViewEventListener):
@classmethod
def is_applicable(cls, settings):
# TODO: check for existance of doorstop stuff in project root?
# return "yaml" in settings.get("syntax").lower()
return True
def on_load_async(self):
"""
Called when the file is finished loading. Runs in a separate thread,
and does not block the application.
"""
self.update_referenced_locations()
def on_activated_async(self):
"""
Called when a view gains input focus.
"""
self.update_referenced_locations()
def on_close(self):
"""
Called when a view loses input focus.
"""
self.erase_regions()
def erase_regions(self):
self.view.erase_regions("doorstop:referenced")
# def on_modified_async(self):
# """
# Called after changes have been made to a view. Runs in a separate thread, and
# does not block the application.
# """
# self.update_referenced_locations()
def update_referenced_locations(self):
path = self.view.file_name()
root = doorstop_util.doorstop_root(view=self.view)
if not path or not root:
return
file = Path(path).relative_to(Path(root))
items = doorstop_util.doorstop(
self,
"find_references",
str(file),
)
for item in items:
keyword = item.get("keyword")
if not keyword:
continue
region = self.view.find(keyword.replace("(", "\\(").replace(")", "\\)"), 0)
if region.begin() == -1:
print("Could not find keyword: '{}'".format(keyword))
continue
item["region"] = region
self.referenced = items
self.view.add_regions(
"doorstop:referenced",
[item["region"] for item in self.referenced if "region" in item],
"string",
"bookmark",
sublime.DRAW_NO_FILL,
)
def on_hover(self, point, hover_zone):
"""
Called when the user's mouse hovers over a view for a short period.
"""
if not hasattr(self, "referenced"):
return
hovered_referenced = [
ref for ref in self.referenced if ref["region"].contains(point)
]
if not hovered_referenced:
return
hovered_reference = hovered_referenced[0]
self.view.show_popup(
"<a href='{}'>{}</a>".format(
hovered_reference["path"],
"{}: {}".format(hovered_reference["uid"], hovered_reference["text"]),
),
sublime.HIDE_ON_MOUSE_MOVE_AWAY,
point,
500,
500,
self.referenced_href_clicked,
)
def referenced_href_clicked(self, href):
self.view.window().open_file(href)
class DoorstopReferencesListener(sublime_plugin.ViewEventListener):
"""
ViewEventListener that will try to find and highlight references
in yaml/doorstop files.
"""
@classmethod
def is_applicable(cls, settings):
return "yaml" in settings.get("syntax").lower()
def on_load_async(self):
"""
Called when the file is finished loading. Runs in a separate thread,
and does not block the application.
"""
self.update_references_regions()
def on_activated_async(self):
"""
Called when a view gains input focus.
"""
self.update_references_regions(force=True)
def on_hover(self, point, hover_zone):
"""
Called when the user's mouse hovers over a view for a short period.
"""
if not hasattr(self, "references"):
return
hovered_references = [
ref for ref in self.references if ref.region.contains(point)
]
if len(hovered_references) == 0:
return
hovered_reference = hovered_references[0]
href = hovered_reference.file
if not href:
return
if hovered_reference.row:
href += (
":" + str(hovered_reference.row) + ":" + str(hovered_reference.column)
)
self.view.show_popup(
"<a href='{}'>{}</a>".format(href, hovered_reference.path),
sublime.HIDE_ON_MOUSE_MOVE_AWAY,
point,
500,
500,
self.reference_href_clicked,
)
def on_close(self):
"""
Called when a view loses input focus.
"""
self.erase_regions()
def on_modified_async(self):
"""
Called after changes have been made to a view. Runs in a separate thread, and
does not block the application.
"""
self.update_references_regions()
def erase_regions(self):
self.view.erase_regions("doorstop:references:invalid")
self.view.erase_regions("doorstop:references:valid")
def update_references_regions(self, force=False):
regions = regions_for_items_in_yaml_list(self.view, "references")
if regions is None:
self.erase_regions()
return
# Determine if anything has actually changed
if hasattr(self, "references") and not force:
refs = []
for region in regions:
content = self.view.substr(region)
for ref in self.references:
if ref.content == content:
refs.append(ref)
break
if len(refs) == len(self.references):
return
self.references = [
doorstop_util.region_to_reference(self.view, region) for region in regions
]
self.view.add_regions(
"doorstop:references:valid",
[ref.region for ref in self.references if ref.is_valid()],
"string",
"bookmark",
sublime.DRAW_NO_FILL,
)
self.view.add_regions(
"doorstop:references:invalid",
[ref.region for ref in self.references if not ref.is_valid()],
"invalid",
"bookmark",
sublime.DRAW_NO_FILL,
)
def reference_href_clicked(self, href):
root = Path(doorstop_util.doorstop_root(view=self.view))
try:
href.index(":")
except ValueError:
self.view.window().open_file(str(root / href))
return
self.view.window().open_file(
str(root / href), sublime.ENCODED_POSITION | sublime.TRANSIENT
)
class DoorstopGotoReferenceCommand(sublime_plugin.TextCommand):
"""
Text command that shows a list of all the references. Picking
a reference will open the file. When a keyword is specified,
it will try to look that up.
"""
def goto_reference(self, idx):
if idx < 0:
return
root = Path(doorstop_util.doorstop_root(view=self.view))
reference = self.references[idx]
if not reference.row:
self.view.window().open_file(str(root / reference.file), sublime.TRANSIENT)
else:
link = "{}:{}:{}".format(
str(root / reference.file), reference.row, reference.column
)
self.view.window().open_file(
link, sublime.ENCODED_POSITION | sublime.TRANSIENT
)
def run(self, edit):
regions = self.view.get_regions("doorstop:references:valid")
self.references = [
doorstop_util.region_to_reference(self.view, region) for region in regions
]
if len(self.references) == 1:
self.goto_reference(0)
else:
items = [
ref.path if not ref.keyword else "{}: {}".format(ref.path, ref.keyword)
for idx, ref in enumerate(self.references)
]
self.view.window().show_quick_panel(
items,
self.goto_reference,
)
def is_enabled(self, *args):
return len(self.view.get_regions("doorstop:references:valid")) >= 1
class DoorstopGotoAnyLinkCommand(sublime_plugin.TextCommand):
"""
Text command that shows a list of all the links from and to this doorstop item
"""
def run(self, edit):
file_name = Path(self.view.file_name())
self.parents = doorstop_util.doorstop(self, "parents", "--item", file_name.stem)
self.children = doorstop_util.doorstop(
self, "children", "--item", file_name.stem
)
self.links = doorstop_util.doorstop(self, "linked", "--item", file_name.stem)
all_items = []
for name, items in zip(
["Parent", "Child", "Other"], [self.parents, self.children, self.links]
):
all_items.extend(
[
"{}: {}: {}".format(name, link["uid"], link["text"])
for idx, link in enumerate(items)
]
)
self.view.window().show_quick_panel(
all_items,
self.goto_item,
)
# TODO: maybe show that no child can be found?
def is_enabled(self, *args):
return doorstop_util.is_doorstop_item_file(self.view.file_name())
def goto_item(self, idx):
if idx < 0:
return
items = self.parents + self.children + self.links
item = items[idx]
self.view.window().open_file(item["path"], sublime.TRANSIENT)
class DoorstopLinksListener(sublime_plugin.ViewEventListener):
"""
ViewEventListener that will try to find link from and to
the currently opened doorstop item.
"""
@classmethod
def is_applicable(cls, settings):
return "yaml" in settings.get("syntax").lower()
def on_load_async(self):
"""
Called when the file is finished loading. Runs in a separate thread,
and does not block the application.
"""
self.dirty = True
self.update_links_regions()
def on_modified_async(self):
"""
Called after changes have been made to a view. Runs in a separate thread,
and does not block the application.
"""
self.dirty = True
def on_activated_async(self):
"""
Called when a view gains input focus.
"""
self.update_links_regions()
def on_post_save_async(self):
"""
Called after changes have been made to a view. Runs in a separate thread, and
does not block the application.
"""
self.update_links_regions()
def on_close(self):
"""
Called when a view is closed (note, there may still be other views into
the same buffer).
"""
self.view.erase_regions("doorstop:links")
def on_hover(self, point, hover_zone):
"""
Called when the user's mouse hovers over a view for a short period.
"""
regions = self.view.get_regions("doorstop:links:direct")
hovered_regions = [region for region in regions if region.contains(point)]
if hovered_regions:
direct_links = self.direct_links
item_uid = self.view.substr(hovered_regions[0])
items = [link for link in direct_links if link["uid"] == item_uid]
if items and len(items) == 1:
item = items[0]
self.view.show_popup(
"<a href='{}'>{}: {}</a>".format(
item["path"], item["uid"], item["text"]
),
sublime.HIDE_ON_MOUSE_MOVE_AWAY,
point,
1000,
2000,
self.link_href_clicked,
)
return
regions = self.view.get_regions("doorstop:links")
if not regions:
return
if not regions[0].contains(point):
return
if (
not hasattr(self, "parents")
or not hasattr(self, "children")
or not hasattr(self, "other")
):
return
def item_to_link(item):
return "<a href='{}'>{}</a>".format(
item["path"],
"{}: {}".format(item["uid"], item["text"]),
)
sections = []
if self.parents:
sections.append("Parent(s):")
for parent in self.parents:
sections.append(item_to_link(parent))
if self.children:
sections.append("Child(ren):")
for child in self.children:
sections.append(item_to_link(child))
if self.other:
sections.append("Other:")
for item in self.other:
sections.append(item_to_link(item))
if not sections:
self.view.show_popup(
"No links found",
sublime.HIDE_ON_MOUSE_MOVE,
point,
1000,
2000,
None,
)
else:
self.view.show_popup(
"<br>".join(sections),
sublime.HIDE_ON_MOUSE_MOVE_AWAY,
point,
1000,
2000,
self.link_href_clicked,
)
def update_links_regions(self):
# TODO: lint during edits to links, not just after save
# Make sure we don't update those regions too often
if hasattr(self, "dirty") and not self.dirty:
return
links_regions = self.view.find_all(r"^links")
if len(links_regions) != 1:
self.view.erase_regions("doorstop:links")
return
link_regions = regions_for_items_in_yaml_list(self.view, "links")
if link_regions is None:
self.view.erase_regions("doorstop:links:direct")
self.view.erase_regions("doorstop:links:direct:invalid")
link_regions = []
uid_link_regions = []
invalid_link_regions = []
self.direct_links = []
for region in link_regions:
content = self.view.substr(region)
try:
index = content.rindex(": ")
uid_region = sublime.Region(region.begin() + 2, region.begin() + index)
item_uid = self.view.substr(uid_region)
try:
item = doorstop_util.doorstop(self, "item", item_uid)
self.direct_links.append(item)
# Valid region
uid_link_regions.append(uid_region)
except Exception:
invalid_link_regions.append(uid_region)
continue
except ValueError:
uid_link_regions.append(
sublime.Region(region.begin() + 2, region.end())
)
self.view.add_regions(
"doorstop:links:direct",
uid_link_regions,
"string", # keyword seems to be orange
"",
sublime.DRAW_NO_FILL
| sublime.DRAW_NO_OUTLINE
| sublime.DRAW_SOLID_UNDERLINE,
)
self.view.add_regions(
"doorstop:links:direct:invalid",
invalid_link_regions,
"invalid", # keyword seems to be orange
"",
sublime.DRAW_NO_FILL
| sublime.DRAW_NO_OUTLINE
| sublime.DRAW_SOLID_UNDERLINE,
)
file_name = Path(self.view.file_name())
item = file_name.stem
self.parents = doorstop_util.doorstop(self, "parents", "--item", item)
self.children = doorstop_util.doorstop(self, "children", "--item", item)
self.other = doorstop_util.doorstop(self, "linked", "--item", item)
is_normative = True
normative_region = self.view.find_all(r"^normative:.*$")
if len(normative_region) == 1 and "true" not in self.view.substr(
normative_region[0]
):
is_normative = False
links_region = links_regions[0]
self.view.add_regions(
"doorstop:links",
[links_region],
"string"
if self.parents or self.children or self.other or not is_normative
else "invalid",
"bookmark",
sublime.DRAW_NO_FILL
| sublime.DRAW_NO_OUTLINE
| sublime.DRAW_SOLID_UNDERLINE,
)
self.dirty = False
def link_href_clicked(self, href):
self.view.window().open_file(href, sublime.TRANSIENT)
def regions_for_items_in_yaml_list(view, keyword):
keyword_regions = view.find_all("^{}:$".format(keyword))
if len(keyword_regions) != 1:
return None
keyword_region = keyword_regions[0]
lines_that_start_with_word = view.find_all(r"^\w+")
attribute_after_references = None
for x in lines_that_start_with_word:
if x.begin() > keyword_region.begin():
attribute_after_references = x
break
regions = []
items = view.find_all(r"(?s)*(^- .*?)(?:(?!^[-|\w]).)*")
for item in items:
if item.begin() < keyword_region.begin():
continue
if item.begin() > attribute_after_references.begin():
continue
regions.append(sublime.Region(item.begin(), item.end()))
return regions