forked from derickc/Fountainhead
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacters.py
221 lines (202 loc) · 12.1 KB
/
characters.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
import sublime
import sublime_plugin
import re
import os
# import sys
# import platform
# from .sublime_helper import *
try:
from .sublime_helper import SublimeHelper
except (ImportError, ValueError):
from sublime_helper import SublimeHelper
user = ''
# user_os = platform.system()
class Characters(sublime_plugin.EventListener):
characters = []
person = ''
lower_characters = []
camel_characters = []
current_character = ''
previous_line = 0
current_line = 0
filename = ''
def modified_character(self, view):
if view.settings().get('syntax') == 'Packages/Fountainhead/Fountainhead.tmLanguage':
# if 'Fountainhead.tmLanguage' in view.settings().get('syntax'):
# if sublime.load_settings('Fountainhead.sublime-settings').get('characters', True):
if view.settings().get('characters', True):
if self.characters == []:
self.on_activated(view)
view.set_status('CharacterList', '')
if view.rowcol(view.sel()[0].end())[0] != self.current_line:
self.previous_line = self.current_line
self.current_line = view.rowcol(view.sel()[0].end())[0]
if view.scope_name(view.text_point(self.previous_line, 0)) == 'text.fountain string entity.name.class ':
# get character name from line
s = SublimeHelper()
self.current_character = view.substr(view.line(view.text_point(self.previous_line, 0)))
character = s.line_string(view)
name = self.current_character.split(' (O.S.)')[0]
name = name.split(' (V.O.)')[0]
name = name.split(' (OS)')[0]
name = name.split(' (VO)')[0]
name = name.split(" (CONT'D)")[0]
if name[0] == ' ' or name[0] == '\t':
name = re.split(r'^\s*', name)[1]
if name not in self.characters:
self.characters.append(name)
# Create Fountainhead directory if it doesn't exist
packages_directory = sublime.packages_path() + '/User/Fountainhead/'
if not os.path.exists(packages_directory):
os.mkdir(packages_directory)
completions_file = packages_directory + 'Characters.sublime-completions'
# if user_os == 'Windows':
# print("Sorry, not supported at this time.")
# elif user_os == 'Darwin':
if name[0] != '@':
if name.lower() not in self.lower_characters:
self.lower_characters.append(name.lower())
self.lower_characters = sorted(self.lower_characters)
# proc_env = os.environ.copy()
# encoding = sys.getfilesystemencoding()
# for k, v in proc_env.items():
# proc_env[k] = os.path.expandvars(v).encode(encoding)
# user = (proc_env['HOME']).decode(encoding='UTF-8')
# completions = open(user + '/Library/Application Support/Sublime Text 3/Packages/Fountainhead/Characters.sublime-completions', 'w')
completions = open(completions_file, 'w')
completions.write('{\n\t\t"scope": "text.fountain - comment - string - entity.other.attribute-name - entity.other.inherited-class - foreground - meta.diff - entity.name.function - entity.name.tag - entity.name.class - variable.parameter",\n\n\t\t"completions":\n\t\t[')
length = len(self.lower_characters)
character_counter = 0
for character in self.lower_characters:
if character_counter < length - 1:
completions.write('"%s",' % character)
character_counter += 1
else:
completions.write('"%s"' % character)
completions.write(']\n}')
completions.close()
elif name[0] == '@':
if name not in self.lower_characters:
self.lower_characters.append(name)
self.lower_characters = sorted(self.lower_characters)
# proc_env = os.environ.copy()
# encoding = sys.getfilesystemencoding()
# for k, v in proc_env.items():
# proc_env[k] = os.path.expandvars(v).encode(encoding)
# user = (proc_env['HOME']).decode(encoding='UTF-8')
# completions = open(user + '/Library/Application Support/Sublime Text 3/Packages/Fountainhead/Characters.sublime-completions', 'w')
completions = open(completions_file, 'w')
completions.write('{\n\t\t"scope": "text.fountain - comment - string - entity.other.attribute-name - entity.other.inherited-class - foreground - meta.diff - entity.name.function - entity.name.tag - entity.name.class - variable.parameter",\n\n\t\t"completions":\n\t\t[')
length = len(self.lower_characters)
character_counter = 0
for character in self.lower_characters:
if character_counter < length - 1:
completions.write('"%s",' % character)
character_counter += 1
else:
completions.write('"%s"' % character)
completions.write(']\n}')
completions.close()
# Clear out character list message
view.set_status('CharacterList',
'')
def on_modified_async(self, view):
if int(sublime.version()) >= 3000:
self.modified_character(view)
def on_modified(self, view):
if int(sublime.version()) < 3000:
self.modified_character(view)
def on_activated(self, view):
if view.settings().get('syntax') == 'Packages/Fountainhead/Fountainhead.tmLanguage':
# s = view.settings().get('syntax')
# while s is None:
# s = view.settings().get('syntax')
# if 'Fountainhead.tmLanguage' in s:
# if sublime.load_settings('Fountainhead.sublime-settings').get('characters', True):
if view.settings().get('characters', True):
if self.filename == view.file_name() and len(self.characters) > 0:
pass
# print(view.file_name())
else:
view.set_status('CharacterList',
'FINDING CHARACTERS...')
self.characters = []
self.lower_characters = []
counter = 0
self.filename = view.file_name()
try:
while counter >= 0:
character = view.substr(view.find_by_selector(
'text.fountain string entity.name.class ')[counter])
name = character.split(' (O.S.)')[0]
name = name.split(' (V.O.)')[0]
name = name.split(' (OS)')[0]
name = name.split(' (VO)')[0]
name = name.split(" (CONT'D)")[0]
if name[0] == ' ' or name[0] == '\t':
name = (re.split(r'^\s*', name))[1]
if name not in self.characters:
self.characters.append(name)
counter += 1
except IndexError:
pass
# if user_os == 'Windows':
# print("Sorry, not supported at this time.")
# elif user_os == 'Darwin':
for character in self.characters:
if character[0] != '@':
self.lower_characters.append(character.lower())
if character[0] == '@':
self.lower_characters.append(character)
self.lower_characters = sorted(self.lower_characters)
# proc_env = os.environ.copy()
# encoding = sys.getfilesystemencoding()
# for k, v in proc_env.items():
# proc_env[k] = os.path.expandvars(v).encode(encoding)
# user = (proc_env['HOME']).decode(encoding='UTF-8')
# completions = open(user + '/Library/Application Support/Sublime Text 3/Packages/Fountainhead/Characters.sublime-completions', 'w')
# packages_directory = sublime.packages_path()
# completions_file = packages_directory + '/Fountainhead/Characters.sublime-completions'
# Create Fountainhead directory if it doesn't exist
packages_directory = sublime.packages_path() + '/User/Fountainhead/'
if not os.path.exists(packages_directory):
os.mkdir(packages_directory)
completions_file = packages_directory + 'Characters.sublime-completions'
completions = open(completions_file, 'w')
completions.write('{\n\t\t"scope": "text.fountain - comment - string - entity.other.attribute-name - entity.other.inherited-class - foreground - meta.diff - entity.name.function - entity.name.tag - entity.name.class - variable.parameter",\n\n\t\t"completions":\n\t\t[')
length = len(self.lower_characters)
character_counter = 0
for character in self.lower_characters:
if character_counter < length - 1:
completions.write('"%s",' % character)
character_counter += 1
else:
completions.write('"%s"' % character)
completions.write(']\n}')
completions.close()
# Print confirmation message
view.set_status('CharacterList',
'CHARACTERS FOUND!')
ShowCharactersCommand.unsorted_characters = self.characters
class UpdateCharacterListCommand(sublime_plugin.TextCommand):
characters = []
filename = ''
def run(self, edit):
self.characters = []
c = Characters()
c.on_activated(self.view)
class ShowCharactersCommand(sublime_plugin.TextCommand):
person = ''
unsorted_characters = []
sorted_characters = []
def run(self, edit):
# if sublime.load_settings('Fountainhead.sublime-settings').get('characters', True) and int(sublime.version()) >= 3000:
if self.view.settings().get('characters', True) and int(sublime.version()) >= 3000:
self.sorted_characters = sorted(self.unsorted_characters)
self.view.show_popup_menu(self.sorted_characters, self.on_done)
self.view.run_command('insert', {"characters": self.person})
def on_done(self, index):
if index == -1:
self.person = ''
else:
self.person = self.sorted_characters[index]