forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorgdb.py
606 lines (532 loc) · 20.5 KB
/
orgdb.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
import sublime
import sublime_plugin
import datetime
import re
from pathlib import Path
import os
import fnmatch
import OrgExtended.orgparse.loader as loader
import OrgExtended.orgparse.node as node
import OrgExtended.orgutil.util as util
import OrgExtended.orgutil.navigation as nav
import OrgExtended.orgutil.template as templateEngine
import logging
import sys
import traceback
import OrgExtended.orgfolding
import OrgExtended.asettings as sets
import OrgExtended.pymitter as evt
log = logging.getLogger(__name__)
headingRe = re.compile("^([*]+) (.+)")
class FileInfo:
def __init__(self, file, parsed, orgPaths):
self.org = parsed
self.filename = file
self.key = file.lower() if file else None
self.change_count = 0
self.org.setFile(self)
displayFn = self.key
oldLen = len(displayFn) if displayFn else 0
if(not displayFn):
self.displayFn = "<BUFFER>"
return
for prefix in orgPaths:
displayFn = displayFn.replace(prefix,"")
displayFn = displayFn.replace(prefix.lower(),"")
# Max Slashes!
# No prefixes. We should count the slashes and truncate
# if there are to many.
maxSlash = 3
if(oldLen == len(displayFn)):
scount = displayFn.count('/')
if(scount > maxSlash):
llist = displayFn.split('/')
displayFn = '/'.join(llist[-maxSlash:])
scount = displayFn.count('\\')
if(scount > maxSlash):
llist = displayFn.split('\\')
displayFn = '\\'.join(llist[-maxSlash:])
if(len(displayFn) > 1 and (displayFn[0] == '\\' or displayFn[0] == '/')):
displayFn = displayFn[1:]
self.displayName = displayFn
def Root(self):
return self.org[0]
def LoadS(self,view):
bufferContents = view.substr(sublime.Region(0, view.size()))
self.org = loader.loads(bufferContents,view.file_name() if view.file_name() else "<string>")
self.org.setFile(self)
# Keep track of last change count.
self.change_count = view.change_count()
def Reload(self):
self.org = loader.load(self.filename)
self.org.setFile(self)
def ResetChangeCount(self):
self.change_count = 0
def HeadingCount(self):
return len(self.org) - 1
def Save(self):
f = open(self.filename,"w+",encoding="utf-8")
for item in self.org:
f.write(str(item))
f.close()
def ReloadIfChanged(self,view,db):
if(self.HasChanged(view)):
self.LoadS(view)
db.RebuildAllIdsForFile(self)
#def FindInfoAndReloadIfChanged(self, view, db):
# if(self.HasChanged(view)):
# self.LoadS(view)
# db.RebuildAllIdsForFile(self)
# return self.FindInfo(view)
def HasChanged(self,view):
return self.change_count < view.change_count()
def At(self, row):
return self.org.at(row)
def AtPt(self, view, pt, db):
self.ReloadIfChanged(view, db)
row,col = view.rowcol(pt)
return self.org.at(row)
def AtRegion(self, view, reg):
row,col = view.rowcol(reg.begin())
return self.org.at(row)
def AtInView(self, view, db):
self.ReloadIfChanged(view, db)
(row,col) = view.curRowCol()
return self.org.at(row)
def AgendaFilenameTag(self):
return os.path.splitext(os.path.basename(self.filename))[0] + ":"
def FindOrCreateNode(self, heading):
for n in self.org[1:]:
if(heading == n.full_heading):
return n
# Okay got here and didn't find the node, have to make it.
m = headingRe.search(heading)
if(m == None):
log.error("FindorCreateNode: failed to parse heading: " + heading)
return None
levelGroup = m.group(1)
level = len(levelGroup)
cur = self.org[0]
parentLevel = level-1
while(cur.level < parentLevel):
if(cur.num_children == 0):
tree = loader.loads("* " + str(datetime.datetime()))
cur.insert_child(tree[1])
cur = cur.get_last_child()
if(heading == None or heading.isspace() or heading.strip() == ""):
return cur
else:
tree = loader.loads(heading)
cur.insert_child(tree[1])
cur = cur.get_last_child()
return cur
class OrgFileId:
def __init__(self, file, id, index):
self.file =file
self.id = id
self.index = index
class OrgDb:
def __init__(self):
self.files = {}
self.Files = []
self.orgPaths = None
self.customids = []
self.customidmaps = {}
self.ids = []
self.idmaps = {}
self.tags = set()
def OnTags(self, tags):
for i in tags:
self.tags.add(i)
def RebuildCustomIdsForFile(self,file):
for id in file.org.env.customids:
if(not id in self.customidmaps):
index = len(self.customids)
fid = OrgFileId(file,id,index)
self.customids.append(fid)
self.customidmaps[id] = fid
def RebuildIdsForFile(self,file):
for id in file.org.env.ids:
if(not id in self.idmaps):
index = len(self.ids)
fid = OrgFileId(file,id,index)
self.ids.append(fid)
self.idmaps[id] = fid
def RebuildAllIdsForFile(self,file):
self.RebuildIdsForFile(file)
self.RebuildCustomIdsForFile(file)
def RebuildIds(self):
self.ids = []
self.idmaps = {}
self.customids = []
self.customidmaps = {}
for file in self.Files:
self.RebuildAllIdsForFile(file)
def LoadNew(self, fileOrView):
if(fileOrView == None):
return None
if(not hasattr(self,'orgPaths') or self.orgPaths == None):
self.orgPaths = sets.Get("orgDirs",None)
filename = self.FilenameFromFileOrView(fileOrView)
if(util.isPotentialOrgFile(filename)):
file = FileInfo(filename, loader.load(filename), self.orgPaths)
self.AddFileInfo(file)
return file
elif(util.isView(fileOrView) and util.isOrgSyntax(fileOrView)):
bufferContents = fileOrView.substr(sublime.Region(0, fileOrView.size()))
file = FileInfo(filename if filename else util.getKey(fileOrView), loader.loads(bufferContents), self.orgPaths)
self.AddFileInfo(file)
return file
else:
log.debug("File is not an org file, not loading into the database: " + str(filename))
return None
def Remove(self, fileOrView):
if(type(fileOrView) is sublime.View):
filename = fileOrView.file_name().lower()
else:
filename = fileOrView.lower()
for i in range(len(self.Files)-1,-1,-1):
if(self.Files[i].key == filename):
del self.Files[i]
if(filename in self.files):
del self.files[filename]
#self.files.pop(filename,None)
def Reload(self, fileOrView):
self.orgPaths = sets.Get("orgDirs",None)
fi = self.FindInfo(fileOrView)
if(fi != None):
fi.Reload()
self.RebuildIds()
return fi
else:
rv = self.LoadNew(fileOrView)
self.RebuildIds()
return rv
def GetIndentForRegion(self, view, region):
node = self.AtRegion(view, region)
return node.level + 1
def FilenameFromFileOrView(self,fileOrView):
filename = None
if(type(fileOrView) is sublime.View):
filename = fileOrView.file_name()
else:
filename = fileOrView
return filename
def AddFileInfo(self, fi):
if(self.files == None):
self.files = {}
self.files[fi.key] = fi
if(self.Files == None):
self.Files = []
self.Files.append(fi)
self.SortFiles()
def SortFiles(self):
self.Files.sort(key=lambda x: x.key)
@staticmethod
def IsExcluded(filename, excludedPaths, excludedFiles):
if(excludedPaths):
excludedPaths = [x.lower().replace('\\','/') for x in excludedPaths]
mypath = os.path.dirname(filename).lower().replace('\\','/')
for item in excludedPaths:
if mypath.startswith(item):
return True
if(excludedFiles):
excludedFiles = [x.lower().replace('\\','/') for x in excludedFiles]
myfile = os.path.basename(filename).lower().replace('\\','/')
for item in excludedFiles:
if(item == myfile):
return True
return False
def RebuildDb(self):
if(evt.Get().listeners('tagsfound')):
evt.Get().clear_listeners('tagsfound')
evt.Get().on("tagsfound",self.OnTags)
self.Files = []
self.files = {}
self.orgPaths = sets.Get("orgDirs",None)
self.orgFiles = sets.Get("orgFiles",None)
self.orgExcludePaths = sets.Get("orgExcludeDirs",None)
self.orgExcludeFiles = sets.Get("orgExcludeFiles",None)
matches = []
if(self.orgPaths):
for orgPath in self.orgPaths:
orgPath = orgPath.replace('\\','/')
globSuffix = sets.Get("validOrgExtensions",[".org"])
for suffix in globSuffix:
if('archive' in suffix):
continue
suffix = "*" + suffix
dirGlobPos = orgPath.find("*")
if(dirGlobPos > 0):
suffix = os.path.join(orgPath[dirGlobPos:],suffix)
orgPath = orgPath[0:dirGlobPos]
if("*" in orgPath):
log.error(" orgDirs only supports double star style directory wildcards! Anything else is not supported: " + str(orgPath))
if(sublime.active_window().active_view()):
sublime.active_window().active_view().set_status("Error: ","orgDirs only supports double star style directory wildcards! Anything else is not supported: " + str(orgPath))
log.error(" skipping orgDirs value: " + str(orgPath))
continue
if not Path(orgPath).exists():
log.warning('orgDir path {} does not exist!'.format(orgPath))
continue
try:
for path in Path(orgPath).glob(suffix):
if OrgDb.IsExcluded(str(path), self.orgExcludePaths, self.orgExcludeFiles):
continue
try:
filename = str(path)
file = FileInfo(filename,loader.load(filename), self.orgPaths)
self.AddFileInfo(file)
except Exception as e:
#x = sys.exc_info()
log.warning("FAILED PARSING: %s\n %s",str(path),traceback.format_exc())
except Exception as e:
log,logging.warning("ERROR globbing {}\n{}".format(orgPath, traceback.format_exc()))
if(self.orgFiles):
for orgFile in self.orgFiles:
path = orgFile.replace('\\','/')
if OrgDb.IsExcluded(str(path), self.orgExcludePaths, self.orgExcludeFiles):
continue
try:
filename = str(path)
file = FileInfo(filename,loader.load(filename), self.orgPaths)
self.AddFileInfo(file)
except Exception as e:
#x = sys.exc_info()
log.warning("FAILED PARSING: %s\n %s",str(path),traceback.format_exc())
self.SortFiles()
self.RebuildIds()
def FindInfo(self, fileOrView):
try:
if(not fileOrView):
return None
key = util.getKey(fileOrView).lower()
if(key and key in self.files):
f = self.files[key]
else:
f = self.LoadNew(fileOrView)
if(f and util.isView(fileOrView)):
f.ReloadIfChanged(fileOrView, self)
return f
except:
try:
#log.debug("Trying to load file anew")
f = self.LoadNew(fileOrView)
if(type(fileOrView) is sublime.View):
f.ReloadIfChanged(fileOrView, self)
return f
except:
log.warning("FAILED PARSING: \n %s",traceback.format_exc())
return None
def Find(self, fileOrView):
n = self.FindInfo(fileOrView)
if(n != None):
return n.org
return None
def At(self, fileOrView, line):
x = self.Find(fileOrView)
if(x != None):
return x.at(line)
return None
def AtInView(self, view):
(row,col) = view.curRowCol()
return self.At(view, row)
def AtPt(self, view, pt):
file = self.FindInfo(view)
return file.AtPt(view, pt, self)
def AtRegion(self, view, reg):
file = self.FindInfo(view)
return file.AtRegion(view, reg)
def NodeAtIndex(self, fileOrView, index):
return self.Find(fileOrView).node_at(index + 1)
def Headings(self, view):
f = self.Find(view)
headings = []
if(None != f):
for n in f[1:]:
headings.append((". " * (n.level)) + n.heading)
return headings
# This is paired with FindFileInfo
def AllHeadings(self, view):
headings = []
for o in self.Files:
displayFn = o.displayName
f = o.org
for n in f[1:]:
formattedHeading = "{0:35}::{1}{2}".format(displayFn , (". " * (n.level)) , n.heading)
#print(formattedHeading)
headings.append(formattedHeading)
return headings
# This is paired with FindFileInfo
def AllHeadingsWContext(self, view):
headings = []
count = 0
for o in self.Files:
displayFn = o.displayName
f = o.org
for n in f[1:]:
parents = ""
t = n
while(type(t.parent) != node.OrgRootNode and t.parent != None):
t = t.parent
parents = t.heading + ":" + parents
#formattedHeading = "{0:35}::{1}{2}".format(displayFn , parents, n.heading)
formattedHeading = ["{0}{1}".format(parents,n.heading),displayFn]
#print(formattedHeading)
headings.append(formattedHeading)
count += 1
return headings
# This is a pair with AllHeadings, it can go from an index in that BACK to the
# fileinfo
def FindFileInfoByAllHeadingsIndex(self, index):
curVal = 0
for o in self.Files:
if(index >= curVal and index < (curVal + o.HeadingCount())):
return (o, (index - curVal) + 1) # remember to account for header
curVal += o.HeadingCount()
return None
# Try to find a node by filename and locator
def FindNode(self, filename, locator):
file = self.FindInfo(filename)
if(not file):
return None
# Basic locator search through the headings
headings = locator.split(":")
cur = file.org[0]
for index in range(len(headings)):
heading = headings[index]
for n in cur.children:
if(n.heading == heading):
cur = n
break
# Did not find it darn
if(cur.heading != heading):
break
heading = headings[len(headings)-1]
if(cur.heading == heading):
return cur
if(len(headings) > 1):
parent = headings[-1]
bestMatch = None
# fuzzy search, heading must match (hopefully)
for n in file.org[1:]:
if(n.heading == heading):
bestMatch = n
if(n.parent and n.parent.heading == parent):
return n
return bestMatch
def JumpToCustomId(self, id):
path = None
file, at = self.FindByCustomId(id)
if(file != None):
path = "{0}:{1}".format(file.filename,at + 1)
if(path):
#print("Found Custom ID jumping to it: " + path)
sublime.active_window().open_file(path, sublime.ENCODED_POSITION)
return True
else:
log.info("Could not locate Custom ID failed to jump there")
return False
def JumpToId(self, id):
path = None
file, at = self.FindById(id)
if(file != None):
path = "{0}:{1}".format(file.filename,at + 1)
if(path):
#print("Found Normal ID jumping to it: " + path)
sublime.active_window().open_file(path, sublime.ENCODED_POSITION)
return True
else:
log.info("Could not locate ID failed to jump there")
return False
def JumpToAnyId(self, id):
if(not self.JumpToId(id)):
return self.JumpToCustomId(id)
return True
def FindByAnyId(self, id):
v = self.FindById(id)
if(not v or v[0] == None):
return self.FindByCustomId(id)
return v
def FindNodeByAnyId(self, id):
v = self.FindByAnyId(id)
print(str(v))
if(v and v[0]):
return v[0].At(v[1])
return None
def FindFileByFilename(self,filename):
for f in self.Files:
if(filename in f.filename):
return f
return None
def FindByCustomId(self, id):
if(id in self.customidmaps):
fid = self.customidmaps[id]
file = fid.file
at = file.org.env.customids[id][1]
return (file,at)
return (None, None)
def FindById(self, id):
if(id in self.idmaps):
fid = self.idmaps[id]
file = fid.file
at = file.org.env.ids[id][1]
return (file,at)
return (None, None)
# EXPORTED ORGDB
orgDb = OrgDb()
def Get():
global orgDb
return orgDb
# rebuild our org database from our org directory
class OrgRebuildDbCommand(sublime_plugin.TextCommand):
def run(self,edit):
Get().RebuildDb()
# Just reload the current file.
class OrgReloadFileCommand(sublime_plugin.TextCommand):
def run(self,edit):
file = Get().FindInfo(self.view)
if(file):
file.LoadS(self.view)
orgDb.RebuildIds()
else:
log.debug("FAILED TO FIND FILE INFO?")
class OrgJumpToCustomIdCommand(sublime_plugin.TextCommand):
def on_done_st4(self,index,modifers):
self.on_done(index)
def on_done(self, index):
if(index < 0 or index >= len(orgDb.customids)):
return
fid = orgDb.customids[index]
file = fid.file
id = fid.id
at = file.org.env.customids[id][1]
path = "{0}:{1}".format(file.filename,at + 1)
self.view.window().open_file(path, sublime.ENCODED_POSITION)
def run(self, edit):
if(int(sublime.version()) <= 4096):
self.view.window().show_quick_panel(orgDb.customids, self.on_done, -1, -1)
else:
self.view.window().show_quick_panel(orgDb.customids, self.on_done_st4, -1, -1)
class OrgJumpToIdCommand(sublime_plugin.TextCommand):
def on_done_st4(self,index,modifers):
self.on_done(index)
def on_done(self, index):
if(index < 0 or index >= len(orgDb.ids)):
return
fid = orgDb.ids[index]
file = fid.file
id = fid.id
at = file.org.env.ids[id][1]
path = "{0}:{1}".format(file.filename,at + 1)
self.view.window().open_file(path, sublime.ENCODED_POSITION)
def run(self, edit):
if(int(sublime.version()) <= 4096):
self.view.window().show_quick_panel(orgDb.ids, self.on_done, -1, -1)
else:
self.view.window().show_quick_panel(orgDb.ids, self.on_done_st4, -1, -1)
class OrgJumpToTodayCommand(sublime_plugin.TextCommand):
def run(self, edit):
file, at = Get().FindByCustomId("TODAY")
path = "{0}:{1}".format(file.filename,at + 1)
self.view.window().open_file(path, sublime.ENCODED_POSITION)