-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurtext_pythonista.py
More file actions
391 lines (318 loc) · 11.8 KB
/
urtext_pythonista.py
File metadata and controls
391 lines (318 loc) · 11.8 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
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
from urtext.project_list import ProjectList
from .urtext_syntax import UrtextSyntax
from sublemon.editor import BaseEditor
from objc_util import *
import clipboard
import console
import time
import os
import ui
import re
class UrtextEditor(BaseEditor):
name = "Pythonista Urtext"
syntax = UrtextSyntax
def __init__(self, args):
super().__init__(args)
self.setup(args)
def setup(self, args):
self._UrtextProjectList = None
self.current_open_file = None
self.urtext_project_path = ''
if 'path' in args:
self.urtext_project_path = args['path']
self.initial_project = None
if 'initial_project' in args:
self.initial_project = args['initial_project']
editor_methods = {
'open_file_to_position' : self.open_file_to_position,
'error_message' : self.error_message,
'insert_text' : self.insert_text,
'save_current' : self.urtext_save,
'save_file' : self.urtext_save,
'get_open_files' : self.get_open_files,
'set_clipboard' : self.set_clipboard,
'write_to_console' : print,
'open_external_file' : self.open_in,
'open_file_in_editor' : self.open_file,
'get_buffer' : self.get_buffer,
'set_buffer': self.set_buffer,
'replace' : self.replace_text,
'refresh_files' : self.refresh_files,
'show_panel': self.show_panel,
'get_current_filename': self.get_current_filename,
'get_position': self.get_position,
'set_position': self.set_position,
'get_line_and_cursor': self.get_line_and_cursor,
'info_message' : self.info_message,
'close_file' : self.close_file,
'get_selection' : self.get_selection,
'select_file_or_folder': self.select_file_or_folder,
#'open_file_dialog': self.open_file_dialog
}
self._UrtextProjectList = ProjectList(self.urtext_project_path, editor_methods=editor_methods)
self._UrtextProjectList.set_current_project(self.urtext_project_path)
self.saved = None
self.buttons = {}
self.updating_history = False
self.setup_syntax_highlighter()
self.setup_buttons({
'/' : self.open_link,
'?' : self.search_node_title,
'<' : self.nav_back,
'>' : self.nav_forward,
'h' : self.urtext_home,
';' : self.new_node,
'S' : self.manual_save,
'{..}' : self.new_inline_node,
'->': self.tab,
'::': self.meta_autocomplete,
'M' : self.all_actions,
'-' : self.insert_meta_dash,
'↓' : self.hide_keyboard,
'#' : self.add_hash_meta,
't' : self.timestamp,
'<..>' : self.manual_timestamp,
'o' : self.select_project,
'[' : self.insert_dynamic_def,
'*' : self.search_all_projects,
'c' : self.copy_link_to_here,
#'^c': self.copy_link_to_here_with_project,
'| >': self.link_to_new_node,
']]' : self.jump_to_def
})
self.setup_autocomplete()
launch_actions = {'new_node' : self.new_node}
if 'launch_action' in args and args['launch_action'] in launch_actions:
launch_actions[args['launch_action']](None)
self.show()
def urtext_home(self, sender):
self._UrtextProjectList.run_action('open_home')
def all_actions(self, sender):
self._UrtextProjectList.run_action('show_all_actions')
def timestamp(self, sender):
self._UrtextProjectList.run_action('insert_timestamp')
def insert_text(self, text):
self.tv.replace_range(self.tv.selected_range, text)
def replace_text(self, filename='', start=0, end=0, full_line=False, replacement_text=''):
if full_line is True:
_, _, _, [start, end] = self.get_line_and_cursor()
self.tv.replace_range((start, end), replacement_text)
def get_buffer(self, filename):
if filename == self.current_open_file:
return self.tv.text
def get_current_filename(self):
return self.current_open_file
def set_buffer(self, filename, contents):
if filename == self.current_open_file:
position = self.get_position()
self.tv.scroll_enabled = False
self.tv.text = ''
self.tv.text = contents
# Ensure position is within bounds for the new content
if contents:
if position >= len(contents):
position = len(contents) - 1
elif position < 0:
position = 0
else:
position = 0
self.tv.selected_range = (position, position)
self.tv.scroll_enabled = True
self.saved = False
self.refresh_syntax_highlighting(initial=True)
return True
def get_current_filename(self):
return self.current_open_file
def get_selection(self):
return self.tv.text[self.tv.selected_range[0]:self.tv.selected_range[1]], self.tv.selected_range[0]
def close_file(self, filename, save=True):
if filename == self.current_open_file:
if save:
self.urtext_save(filename)
self.tv.text = ''
self.current_open_file = None
self.saved = None
return True
def copy_link_to_here(self, sender):
self._UrtextProjectList.run_action('copy_link_to_here')
def get_open_files(self):
return { self.current_open_file: False if self.saved else True }
def open_in(self, filename):
if not os.path.isfile(filename):
console.hud_alert('Path is not file, cannot open in Urtext', 'error')
return None
console.open_in(filename)
def info_message(self, message):
ui.delay(lambda: console.hud_alert(message, 'info', 2), 0)
def set_clipboard(self, text):
clipboard.set(text)
console.hud_alert(text + ' copied to the clipboard', 'info', 2)
self.refresh_syntax_highlighting(initial=True) # necessary for some reason
def insert_at_next_line(self, contents):
pass #future
def hide_keyboard(self, sender):
self.tv.end_editing()
def search_all_projects(self, sender):
self._UrtextProjectList.run_action('select_project')
def show_panel(self, selections, callback, on_highlight=None):
self.autoCompleter.set_items(selections, 'quick_panel')
self.autoCompleter.set_action(callback)
self.autoCompleter.show()
def get_position(self):
return self.tv.selected_range[0]
def set_position(self, position):
self.tv.selected_range = (position, position)
def get_line_and_cursor(self):
file_pos = self.get_position()
line_start = 0
for line in self.tv.text.split('\n'):
if line_start + len(line) > file_pos:
break
line_start += len(line) + 1
full_line, position_in_line = get_full_line(file_pos, self.tv)
start = line_start
end = line_start + len(full_line)
return full_line, position_in_line, file_pos, [start, end]
def insert_dynamic_def(self, sender):
position = self.get_position()
self.tv.replace_range(
self.tv.selected_range,
'\n\n[[ >(| >)\n+( ) +( )\n-( ) -( )\n ]]')
self.tv.selected_range = (position + 9, position + 9)
def tab(self, sender):
self.tv.replace_range(self.tv.selected_range, '\t')
def add_hash_meta(self, sender):
self.hash_values = self._UrtextProjectList.current_project.get_all_values_for_key(
self._UrtextProjectList.current_project.get_single_setting('hash_key').text)
self.autoCompleter.set_items(self.hash_values, 'hash_values')
self.autoCompleter.set_action(self.insert_hash_meta)
self.autoCompleter.show()
def insert_hash_meta(self, value):
self.tv.replace_range(
self.tv.selected_range,
'#'+self.hash_values[value]+' ')
self.tv.begin_editing()
def manual_timestamp(self, sender):
self._UrtextProjectList.run_action('insert_timestamp')
def error_message(self, message):
ui.delay(lambda: console.hud_alert(message, 'error', 5), 0)
print(message)
def select_project(self, sender):
self._UrtextProjectList.run_action('select_project')
def manual_save(self, sender):
self.urtext_save(self.current_open_file)
console.hud_alert('Saved','success',0.5)
def urtext_save(self, filename):
if filename == self.current_open_file:
self.save(None, save_as=False, handle_changed_contents=False)
if self._UrtextProjectList:
self._UrtextProjectList.on_modified(self.current_open_file)
def refresh_files(self, file_list):
if not isinstance(file_list, list):
file_list = [file_list]
for filename in file_list:
if filename == self.current_open_file:
self.open_file_to_position(self.current_open_file, character=self.get_position())
def refresh_syntax_highlighting(self, highlight_range=None, initial=False):
self.syntax_highlighter.refresh(highlight_range=highlight_range, initial=initial)
def _open_file(self, filename):
if not os.path.exists(filename):
console.hud_alert('FILE not found. Synced?', 'error', 1)
return None
if not os.path.isfile(filename):
console.hud_alert('Path is not file, cannot open in Urtext', 'error')
return None
if self.current_open_file and self.current_open_file != filename:
self.urtext_save(self.current_open_file)
contents = self.get_file_contents(filename)
if self._UrtextProjectList:
self._UrtextProjectList.visit_file(filename)
self.tv.text=''
self.tv.text=contents
self.current_open_file = filename
def open_file_to_position(self, filename, line=None, character=None, highlight_range=None, new_window=False):
if filename != self.current_open_file:
self._open_file(filename)
if line:
pass
if character:
if character >= len(self.tv.text):
character = max(0, len(self.tv.text) - 1)
else:
character = 0
# Ensure character is within valid bounds before creating NSRange
text_length = len(self.tv.text)
if text_length == 0:
character = 0
elif character >= text_length:
character = text_length - 1
elif character < 0:
character = 0
self.tv.selected_range = (character, character)
if text_length > 0: # Only scroll if there's text to scroll to
self.tvo.scrollRangeToVisible(NSRange(character, 0))
self.refresh_syntax_highlighting(highlight_range=highlight_range)
self.tv.begin_editing()
def open_link(self, sender):
line, cursor,_,_ = self.get_line_and_cursor()
self._UrtextProjectList.handle_link(line, self.current_open_file, self.get_position(), col_pos=cursor)
def new_inline_node(self, sender, locate_inside=True):
selection = self.tv.selected_range
selected_text = self.tv.text[selection[0]:selection[1]]
self.tv.replace_range(selection, ''.join([
'{ ', selected_text,' }']))
self.tv.selected_range = (selection[0]+2, selection[0]+2)
def new_node(self, sender):
self._UrtextProjectList.run_action('new_file_node')
def meta_autocomplete(self, sender):
self._UrtextProjectList.run_action('browse_metadata')
def search_node_title(self, sender):
self._UrtextProjectList.run_action('node_browser')
def _refresh_node_browser_until_compiled(self):
while not self._UrtextProjectList.current_project.compiled:
time.sleep(1)
if self.autoCompleter.showing == 'node_browser':
self.autoCompleter.set_items(
self._UrtextProjectList.current_project.sort_for_node_browser(),
'node_browser')
def insert_meta_dash(self, sender):
self.tv.replace_range(self.tv.selected_range, ' - ')
def link_to_new_node(self, sender):
self._UrtextProjectList.run_action('insert_link_to_new_node')
def nav_back(self, sender):
self._UrtextProjectList.run_action('nav_back')
def nav_forward(self, sender):
self._UrtextProjectList.run_action('nav_forward')
def find_end_of_line(self, position):
contents = self.tv.text
if contents:
while ( position < len(contents) - 1) and (
contents[position] != '\n'):
position += 1
return position
def jump_to_def(self, sender):
self._UrtextProjectList.run_action('go_to_frame')
def select_file_or_folder(self, callback):
path = dialogs.pick_document()
callback(path)
def _open_file_dialog(self, callback, allow_folders=None):
pass
path = dialogs.pick_document(types=[])
# hangs
def get_full_line(position, tv):
if not tv.text:
return ('', 0)
# Ensure position is within bounds
position = max(0, min(position, len(tv.text) - 1))
lines = tv.text.split('\n')
total_length = 0
for line in lines:
total_length += len(line) + 1
if total_length > position:
distance_from_end_of_line = total_length - position
position_in_line = len(line) - distance_from_end_of_line
return (line, position_in_line)
# Fallback if position is at the very end
if lines:
return (lines[-1], len(lines[-1]))
return ('', 0)