-
Notifications
You must be signed in to change notification settings - Fork 14
/
current_state.py
169 lines (126 loc) · 4.88 KB
/
current_state.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
"""
manages current state, which include: current filename of view, current project folder if any, project folder
does not manage settings, but sends a message with the current project folder to settings
"""
ID = "CurrentState"
import sublime
import os
import re
import FuzzyFilePath.common.settings as settings
from FuzzyFilePath.project.FileCache import FileCache
import FuzzyFilePath.common.path as Path
import FuzzyFilePath.common.verbose as logger
is_enabled = False # set to true when plugin has initially updated settings
valid = False # if the current view is a valid project file
file_caches = {} # caches any file indices of each project folder
state = {} # saves current views state like filename, project_folder, cache and settings
def update():
""" call me anytime a new view has gained focus. This includes activation of a new window, which should have an
active view
"""
global valid, is_enabled
if not is_enabled:
return False
temp = False
window = sublime.active_window()
if window is None:
logger.log(ID, "Abort -- no active window")
valid = False
return valid
view = window.active_view()
if view is None:
logger.log(ID, "Abort -- no active view")
valid = False
return valid
file = Path.posix(view.file_name())
if not file:
logger.log(ID, "Abort -- view has not yet been saved to file")
temp = True
return valid
if state.get("file") == file:
logger.log(ID, "Abort -- view already updated")
return valid
folders = list(map(lambda f: Path.posix(f), window.folders()))
project_folder = get_closest_folder(file, folders)
if project_folder is False:
logger.log(ID, "Abort -- file not part of a project (folder)")
valid = False
return valid
# notify settings of new project folder
if state.get("project_folder") != project_folder:
settings.update_project_settings()
settings.update_project_folder_settings(project_folder)
valid = True
# @TODO cache
state["file"] = file
state["directory"] = sanitize_directory(file, project_folder)
state["folders"] = folders
state["project_folder"] = project_folder
state["cache"] = get_file_cache(project_folder)
logger.start_block()
logger.verbose(ID, "Updated", state)
return valid
def sanitize_directory(file_name, project_folder):
directory = re.sub(project_folder, "", file_name)
directory = re.sub("^[\\\\/\.]*", "", directory)
return os.path.dirname(directory)
def get_project_directory():
return state.get("project_folder")
def get_directory():
return state.get("directory")
def update_settings():
if state.get("project_folder"):
# we expect settings to be already updated and thus only update the project folder settings
settings.update_project_folder_settings(state.get("project_folder"))
def is_valid():
return valid
def enable():
global is_enabled
is_enabled = True
def get_file_cache(folder):
if not folder in file_caches:
valid_file_extensions = get_valid_extensions(settings.get("trigger"))
logger.verbose(ID, "Build cache for " + folder + " (", valid_file_extensions , ") excluding", settings.get("exclude_folders"))
file_caches[folder] = FileCache(valid_file_extensions, settings.get("exclude_folders"), folder)
return file_caches.get(folder)
def rebuild_filecache(folder=None):
if not folder:
if state.get("cache"):
logger.verbose(ID, "rebuild current filecache of folder " + state.get("project_folder"))
state.get("cache").rebuild()
return
folder = Path.posix(folder)
if not folder in file_caches:
logger.log(ID, "Abort rebuild filecache -- folder " + folder + " not cached")
return False
logger.verbose(ID, "rebuild current filecache of folder " + folder)
file_caches.get(folder).rebuild()
def search_completions(needle, project_folder, valid_extensions, base_path=False):
return state.get("cache").search_completions(needle, project_folder, valid_extensions, base_path)
def find_file(file_name):
return state.get("cache").find_file(file_name)
def get_valid_extensions(triggers):
""" Returns a list of all file extensions found in scope triggers """
extensionsToSuggest = []
for scope in triggers:
ext = scope.get("extensions", [])
extensionsToSuggest += ext
# return without duplicates
return list(set(extensionsToSuggest))
def get_closest_folder(filepath, directories):
"""
Returns the (closest) project folder associated with the given file or False
# the rational behind this is as follows:
In nodejs we might work with linked node_modules. Each node_module is a separate project. Adding nested folders
to the root document thus owns the file and defines the project scope. A separated folder should never reach
out (via files) on its parents folders.
"""
folderpath = os.path.dirname(filepath)
current_folder = folderpath
closest_directory = False
for folder in directories:
distance = current_folder.replace(folder, "")
if len(distance) < len(folderpath):
folderpath = distance
closest_directory = folder
return closest_directory