forked from jvantuyl/sublime_diagram_plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
diagram_plugin.py
151 lines (106 loc) · 4.15 KB
/
diagram_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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import re
import time
import sublime
import sublime_plugin
import threading
from sublime_plugin import TextCommand
from sublime import error_message, version
import plantuml_connection
from debug_tools import getLogger
log = getLogger(1, __package__)
try:
from .diagram import setup, process
except ValueError:
from diagram import setup, process
g_is_there_new_changes = False
try:
all_views_active
except NameError:
all_views_active = {}
def process_diagram_image(view):
log(1, "Processing diagrams in %s...", view.file_name())
if not process(view):
error_message("No diagrams overlap selections.\n\n" \
"Nothing to process.")
class DiagramContinueCreationThread(threading.Thread):
def __init__(self, view):
self.view = view
all_views_active[view.id()] = self
threading.Thread.__init__(self)
# Force it to be refreshed on the first time
self.change_count = -1
self.last_user_key_change = time.time()
self.open_image = True
self.sleepEvent = threading.Event()
def run(self):
global g_is_there_new_changes
view = self.view
window = view.window()
current_time = time.time()
elapsed_time = 0.1
default_time = 1.0
while True:
log(4, "current_time: %s, elapsed_time: %s", current_time, elapsed_time)
# Wait a little to not generate the diagram/image while the user is typing
while g_is_there_new_changes:
g_is_there_new_changes = False
self.sleepEvent.wait( 1.0 )
# Run until it closes
if view not in window.views():
log(1, "Exiting continuous thread...")
del all_views_active[view.id()]
return
elif self.change_count != view.change_count() \
and window == sublime.active_window() \
and view == window.active_view():
current_time = time.time()
self.change_count = view.change_count()
open_image = self.open_image
# active_sheet = window.active_sheet()
# group, view_index = window.get_view_index(view)
try:
process(view, self)
except plantuml_connection.PlantUMLSyntaxError as error:
log(1, "Syntax error on diagram: %s",
re.findall(r'X-PlantUML-Diagram-Description:((?:.|\n)*?)X-Powered-By', str(error)))
# Allowed the image view to be focused on the first time it is opened
if not open_image:
window.focus_view( view )
# window.focus_group( group )
# window.focus_sheet( active_sheet )
elapsed_time = time.time() - current_time
self.sleepEvent.wait( default_time )
class DisplayDiagramsContinually(TextCommand):
def run(self, edit):
view = self.view
log(1, "Processing diagrams in %s...", self.view)
# Force the view to the reprocessed as it associated image could have been closed
if view.id() in all_views_active:
continuous_processor = all_views_active[view.id()]
continuous_processor.open_image = True
continuous_processor.change_count = -1
else:
continuous_thread = DiagramContinueCreationThread( view )
continuous_thread.start()
class DisplayDiagramsContinuallyEventListener(sublime_plugin.EventListener):
def on_modified(self, view):
global g_is_there_new_changes
g_is_there_new_changes = True
class DisplayDiagrams(TextCommand):
def run(self, edit):
process_diagram_image(self.view)
def isEnabled(self):
return True
if version()[0] == '2':
setup()
else:
def plugin_loaded():
"""Sublime Text 3 callback to do after-loading initialization"""
try:
setup()
except Exception as error:
print("Unable to load diagram plugin, check console for details.\n\n%s" % error)
raise