-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPHPCodeIntel.py
More file actions
184 lines (129 loc) · 6.71 KB
/
PHPCodeIntel.py
File metadata and controls
184 lines (129 loc) · 6.71 KB
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
import sublime
import sublime_plugin
import sys
import os
from os.path import join, exists
import re
import threading
import time
from pprint import pprint
import PHPCodeIntel.preferences as preferences
import PHPCodeIntel.phpdaemon as phpdaemon
##############################################################################################################################
# debug
##############################################################################################################################
def debugMsg(prefs, msg):
if prefs.debug_enabled == True:
print("[PHPCodeIntel] " + str(msg))
def warnMsg(msg):
print("[PHPCodeIntel] WARN: " + str(msg))
##############################################################################################################################
# base plugin
##############################################################################################################################
class PhpCodeIntelBase:
def rescanFile(self, prefs, view, src_file):
scan_dirs = prefs.getProjectScanDirs(view)
db_file = prefs.getDBFilePath(view)
# scan as async command - we don't care about the results
sublime.status_message("PHPCI: scan file")
phpdaemon.runRemoteCommandInPHPDaemon(prefs, 'scanFile', [src_file, scan_dirs, prefs.exclude_patterns, db_file])
##############################################################################################################################
# Plugin Commands
##############################################################################################################################
# scans a single file
class PhpCodeIntelScanFileCommand(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
self.rescanFile(prefs, self.view, self.view.file_name())
# scans a project
class PhpCodeIntelScanProjectCommand(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
scan_dirs = prefs.getProjectScanDirs(self.view)
db_file = prefs.getDBFilePath(self.view)
sublime.status_message("PHPCI: scanning project")
phpdaemon.runAsyncRemoteCommandInPHPDaemon(prefs, 'scanProject', [scan_dirs, prefs.exclude_patterns, db_file])
# tells the daemon to stop
class PhpCodeIntelShutdownDaemonCommand(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
phpdaemon.runRemoteCommandInPHPDaemon(prefs, 'quit', [])
# does a 3 second sleep in the daemon. This is used to test async commands.
class PhpCodeIntelDebugSleepCommand(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
phpdaemon.runAsyncRemoteCommandInPHPDaemon(prefs, 'debugSleep', [3])
# view.run_command('pci_test')
class PciTestCommand(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
# project = get_project(self.view)
# debugMsg(prefs, 'poject is '+str(project))
# db_file_path = prefs.getDBFilePath(self.view)
# debugMsg(prefs, 'getDBFilePath = '+str(db_file_path))
# folders = self.view.window().folders()
# debugMsg(prefs, 'folders: '+str(folders))
# self.get_expanded_region()
##############################################################################################################################
# Quick Panel Completions
##############################################################################################################################
class PhpCodeIntelShowCompletions(PhpCodeIntelBase, sublime_plugin.TextCommand):
def run(self, edit):
prefs = preferences.load(self.view)
items = self.loadCompletions(prefs)
if len(items) == 0:
sublime.status_message("PHPCI: No completions found.")
return
def response_fn(chosen_offset):
if chosen_offset < 0:
return
chosen_completion = items[chosen_offset][1]
self.view.run_command('php_code_intel_insert_completion', {'completion': chosen_completion})
self.view.window().show_quick_panel(items, response_fn)
def loadCompletions(self, prefs):
completions = []
view = self.view
sel = view.sel()[0]
pos = sel.end()
if view.score_selector(pos, "source.php") == 0:
debugMsg(prefs, "not in a source.php scope")
sublime.status_message("PHPCI: not in PHP scope")
return []
intel_db_filepath = prefs.getDBFilePath(view)
content = view.substr(sublime.Region(0, view.size()))
completions_array = phpdaemon.runRemoteCommandInPHPDaemon(prefs, 'autoComplete', [content, pos, intel_db_filepath])
if completions_array == None:
debugMsg(prefs, "completions_array was None");
return
return completions_array;
class PhpCodeIntelInsertCompletion(sublime_plugin.TextCommand):
def run(self, edit, selection=False, encoding='utf-8', kill=False, completion=''):
region = self.get_expanded_region()
self.view.replace(edit, region, completion)
def get_expanded_region(self):
view = self.view
sel = view.sel()
region = sel[0]
end_classification = view.classify(region.end())
if sublime.CLASS_WORD_END & end_classification:
# at the end of a word, add the whole word
region = view.word(region)
elif not (end_classification & (sublime.CLASS_WORD_START | sublime.CLASS_WORD_END | sublime.CLASS_LINE_START | sublime.CLASS_LINE_END | sublime.CLASS_PUNCTUATION_START | sublime.CLASS_PUNCTUATION_END | sublime.CLASS_SUB_WORD_START | sublime.CLASS_SUB_WORD_END | sublime.CLASS_EMPTY_LINE)):
# in the middle of a word
region = view.word(region)
return region
##############################################################################################################################
# Event Listener
##############################################################################################################################
completionsCache = {}
class PhpCodeIntel(PhpCodeIntelBase, sublime_plugin.EventListener):
############################################
# rescan file on save
def on_post_save_async(self, view):
prefs = preferences.load(view)
if prefs.rescan_on_save == True:
debugMsg(prefs, "Saving (async)")
extension_types = prefs.php_extensions
for extension in prefs.php_extensions:
if view.file_name().endswith(extension):
self.rescanFile(prefs, view, view.file_name())