-
Notifications
You must be signed in to change notification settings - Fork 29
/
visibilityHandler.py
86 lines (60 loc) · 2.62 KB
/
visibilityHandler.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
from .common import *
# whether all bookmarks (even unrelated) should be shown
def SHOW_ALL_BOOKMARKS():
return "Show All Bookmarks"
def SHOW_ONLY_PROJECT_BOOKMARKS():
return "Show Only Project Bookmarks"
def SHOW_ONLY_FILE_BOOKMARKS():
return "Show Only File Bookmarks"
def shouldShowBookmark(window, activeView, bookmark, bookmarkMode):
# 1. there is no current project now. Show all bookmarks
# 2. current project matches bookmark path
def isValidProject(currentProjectPath, bookmarkProjectPath):
return currentProjectPath == NO_PROJECT or \
currentProjectPath == bookmarkProjectPath
currentFilePath = activeView.file_name()
currentProjectPath = window.project_file_name()
# free bookmarks can be shown. We don't need a criteria
if bookmarkMode == SHOW_ALL_BOOKMARKS():
return True
elif bookmarkMode == SHOW_ONLY_PROJECT_BOOKMARKS() and \
isValidProject(currentProjectPath, bookmark.getProjectPath()):
return True
elif bookmarkMode == SHOW_ONLY_FILE_BOOKMARKS() and \
bookmark.getFilePath() == currentFilePath:
return True
else:
# There are no bookmarks in the current file
return False
return False
def ___sortBookmarks(visibleBookmarks, currentFile):
from collections import defaultdict
def lineSortFn(bookmark):
return bookmark.getLineNumber()
def sortByLineNumber(bookmarks):
return sorted(bookmarks, key=lineSortFn)
fileBookmarks = defaultdict(list)
sortedBookmarks = []
for bookmark in visibleBookmarks:
filePath = bookmark.getFilePath()
fileBookmarks[filePath].append(bookmark)
# take all bookmarks in current file,
# sort them,
# remove the current file from the list of files
currentFileBookmarks = fileBookmarks[currentFile]
sortedBookmarks = sortedBookmarks + sortByLineNumber(currentFileBookmarks)
del fileBookmarks[currentFile]
# Iterate over all list of bookmarks in each file
# sort them according to line number
for bookmarkList in fileBookmarks.values():
sortedBookmarkList = sortByLineNumber(bookmarkList)
sortedBookmarks = sortedBookmarks + sortedBookmarkList
return sortedBookmarks
def getVisibleBookmarks(bookmarks, window, activeView, bookmarkMode):
visibleBookmarks = []
for bookmark in bookmarks:
if shouldShowBookmark(window, activeView, bookmark, bookmarkMode):
visibleBookmarks.append(bookmark)
sortedBookmarks = ___sortBookmarks(visibleBookmarks,
activeView.file_name())
return sortedBookmarks