-
Notifications
You must be signed in to change notification settings - Fork 7
/
namespaces.py
67 lines (59 loc) · 2.27 KB
/
namespaces.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
import os
def is_php_file(view):
if view.file_name().endswith('.php'):
return True
return False
def is_file_opened(window, view):
for wview in window.views():
if wview.id() == view.id():
return True
return False
def close_overlay(window, currentView):
if not is_file_opened(window, window.active_view()):
window.run_command('close', [])
else:
window.focus_view(currentView)
window.run_command('hide_overlay', [])
def get_namespace(window):
rg = window.active_view().find('namespace (.*);', 0)
snamespace = window.active_view().substr(rg)[10:][:-1]
rg = window.active_view().find('\n([a-z ]+)?(class|interface|trait) [a-zA-Z0-9]+', 0)
sclass = window.active_view().substr(rg)
sclass = sclass[(sclass.rindex(' ')+1):]
return snamespace+'\\'+sclass
def build_namespace(view):
settings = view.settings()
limits = settings.get('php_namespace.stop_folders')
folders = view.file_name().split(os.sep)
for limit in limits:
if limit in folders:
folders = folders[folders.index(limit):]
return "\\".join(folders[1:-1])
def insert_use_statement(view, namespace):
instruct = 'use '+namespace+';'
edit = view.begin_edit()
regions = view.find_all('\nuse (.*);', 0)
if 0 == len(regions):
region = view.find('namespace (.*);', 0)
region = view.full_line(region)
text = view.substr(region)
view.replace(edit, region, text + "\n" + instruct + "\n")
else:
region = view.full_line(regions[-1])
text = view.substr(region)
view.replace(edit, region, text + instruct + "\n")
def insert_namespace_statement(view, edit, namespace):
if '' != namespace:
full_namespace = "namespace " + namespace + ";"
region = view.find('namespace (.*);', 0)
if region is None:
regions = view.find_all('<\?php', 0)
if 0 == len(regions):
for sel in view.sel():
view.insert(edit, sel.begin(), full_namespace + "\n")
else:
region = view.line(regions[-1])
view.insert(edit, region.end(), "\n\n" + full_namespace)
else:
region = view.line(region)
view.replace(edit, region, full_namespace)