-
Notifications
You must be signed in to change notification settings - Fork 4
/
BladeAutoComplete.py
77 lines (59 loc) · 2.54 KB
/
BladeAutoComplete.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
import sublime_plugin
import sublime
from os import path, walk
import fnmatch
import re
class BladeAutoComplete(sublime_plugin.EventListener):
files = []
blade_files = []
def load_blade_files(self, view):
self.blade_files = []
views_folder = ''.join([
view.window().folders()[0],
path.sep,
path.join('resources', 'views'),
path.sep])
matches = []
for root, dirnames, filenames in walk(views_folder):
for filename in fnmatch.filter(filenames, '*.blade.php'):
matches.append(path.join(root, filename))
self.files = matches
for file in self.files:
file_name = file.replace(views_folder, '')
file_append_text = file_name.replace('.blade.php', '').replace(path.sep, '.')
t = ('{} \tBlade Autocomplete'.format(file_name), file_append_text)
self.blade_files.append(t)
def find_yields_in_layout(self, layout):
layout_path = None
for file in self.files:
if layout.replace('.', path.sep) in file:
layout_path = file
if layout_path is None:
return []
with open(layout_path, 'r') as file:
content = file.read()
file.close()
matches = re.findall(r'\@yield\((?:\'|\")(.*?)(?:\'|\")\)', content, re.M | re.I)
return matches
def on_query_completions(self, view, prefix, locations):
if (not view.file_name()
or not fnmatch.fnmatch(view.file_name(), '*.blade.php')
or len(view.window().folders()) < 1):
return None
self.load_blade_files(view)
line = view.substr(view.line(view.sel()[0])).strip()
if line.startswith('@extends'):
return (self.blade_files,
sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
elif line.startswith('@section'):
# place search cursor one word back
cursor = locations[0] - len(prefix) - 1
body = view.substr(sublime.Region(0, cursor))
matches = re.search(r'\@extends\((?:\'|\")(.*?)(?:\'|\")\)', body, re.M | re.I)
if not matches:
return []
layout = matches.group(1)
layout_yields = self.find_yields_in_layout(layout)
return ([('{} \tin {}'.format(section, layout), section) for section in layout_yields],
sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)
return None