-
Notifications
You must be signed in to change notification settings - Fork 14
/
console_wrap.py
128 lines (94 loc) · 3 KB
/
console_wrap.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
import sublime
import sublime_plugin
try:
from .core.tools import *
from .core.settings import *
from .core.js_wrapper import JsWrapp
from .core.py_wrapper import PyWrapp
from .core.php_wrapper import PhpWrapp
from .core.go_wrapper import GoWrapp
except ValueError:
from core.tools import *
from core.settings import *
from core.js_wrapper import JsWrapp
from core.py_wrapper import PyWrapp
from core.php_wrapper import PhpWrapp
from core.go_wrapper import GoWrapp
def plugin_loaded():
print('[Console Wrap] ' + '*' * 30 + ' loaded ' + '*' * 30)
wrapConnector = {}
wrapConnector['js'] = JsWrapp()
wrapConnector['py'] = PyWrapp()
wrapConnector['php'] = PhpWrapp()
wrapConnector['go'] = GoWrapp()
def getFileTypeMap():
fileTypeMap = settings().get('fileTypeMap') or {
"text.html.vue": "js",
"source.ts": "js",
"source.tsx": "js",
"source.coffee": "js",
"text.html.basic": "js",
"text.html.blade": "js",
"text.html.twig": "js"
}
defMap = {
"embedding.php": "php",
"source.js": "js",
"source.python": "py",
"source.go": "go"
}
fileTypeMap.update(defMap)
return fileTypeMap
def supportedFile(view):
supported = False
fileTypeMap = getFileTypeMap()
cursors = view.sel()
if not list(cursors):
view.sel().add(0)
for cursor in cursors:
scope_name = view.scope_name(cursor.begin())
fileTypeIntersect = list(set(scope_name.split(' ')).intersection(fileTypeMap))
if fileTypeIntersect:
supported = True
return supported
def runCommand(view, edit, action, insert_before=False):
cursors = view.sel()
fileTypeMap = getFileTypeMap()
lastPos = float("inf")
if not list(cursors):
view.sel().add(0)
for cursor in cursors:
scope_name = view.scope_name(cursor.begin())
fileTypeIntersect = list(set(scope_name.split(' ')).intersection(fileTypeMap))[::-1]
if not fileTypeIntersect:
fileType = scope_name.split(' ')[0]
msg = 'Console Wrap: Not work in this file type ( {} )'.format(fileType)
sublime.status_message(msg)
continue
wrapperType = fileTypeMap.get(fileTypeIntersect[0], False)
if view.match_selector(cursor.begin(), 'source.php'):
wrapperType = 'php'
if view.match_selector(cursor.begin(), 'source.js'):
wrapperType = 'js'
wrapper = wrapConnector.get(wrapperType, None)
if wrapper:
if action == 'create':
end = getattr(wrapper, action)(view, edit, cursor, insert_before)
else:
end = getattr(wrapper, action)(view, edit, cursor)
if end:
lastPos = end if end < lastPos else lastPos
return lastPos
class ConsoleWrapCommand(sublime_plugin.TextCommand):
def run(self, edit, insert_before=False):
view = self.view
lastPos = runCommand(view, edit, 'create', insert_before)
if lastPos > 0 and lastPos < float("inf"):
view.sel().clear()
view.sel().add(sublime.Region(lastPos, lastPos))
self.view.run_command("move_to", {"to": "eol"})
class ConsoleActionCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return supportedFile(self.view)
def run(self, edit, action=False):
runCommand(self.view, edit, action)