/
workspace_commands.py
237 lines (194 loc) · 6.45 KB
/
workspace_commands.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
workspace_commands --- Open, SaveAs, GotoLine commands
======================================================
"""
import os.path
import glob
from pyparsing import CharsNotIn, Combine, Keyword, Literal, Optional, Or, ParseException, \
StringEnd, Suppress, White, Word, nums
from pathcompleter import makeSuitableCompleter, PathCompleter
from locator import AbstractCommand
class CommandGotoLine(AbstractCommand):
"""Go to line command implementation
"""
@staticmethod
def signature():
"""Command signature. For Help
"""
return '[l] [LINE]'
@staticmethod
def description():
"""Command description. For Help
"""
return 'Go to line'
@staticmethod
def pattern():
"""Pyparsing pattern
"""
line = Word(nums)("line")
pat = (Literal('l ') + Suppress(Optional(White())) + Optional(line)) ^ line
pat.leaveWhitespace()
pat.setParseAction(CommandGotoLine.create)
return pat
@staticmethod
def create(str, loc, tocs):
"""Callback for pyparsing. Creates an instance of command
"""
if tocs.line:
line = int(tocs.line)
else:
line = None
return [CommandGotoLine(line)]
@staticmethod
def isAvailable():
"""Check if command is currently available
"""
return True
def __init__(self, line):
self._line = line
def isReadyToExecute(self):
"""Check if command is complete and ready to execute
"""
return self._line is not None
def execute(self):
"""Execute the command
"""
print 'goto', self._line
class CommandOpen(AbstractCommand):
@staticmethod
def signature():
"""Command signature. For Help
"""
return '[f] PATH [LINE]'
@staticmethod
def description():
"""Command description. For Help
"""
return 'Open file. Globs are supported'
@staticmethod
def pattern():
"""pyparsing pattern
"""
def attachLocation(s, loc, tocs):
"""pyparsing callback. Saves path position in the original string
"""
return [(loc, tocs[0])]
path = CharsNotIn(" \t")("path")
path.setParseAction(attachLocation)
longPath = CharsNotIn(" \t", min=2)("path")
longPath.setParseAction(attachLocation)
slashPath = Combine(Literal('/') + Optional(CharsNotIn(" \t")))("path")
slashPath.setParseAction(attachLocation)
pat = ((Literal('f ') + Optional(White()) + Optional(path)) ^ longPath ^ slashPath) + \
Optional(White() + Word(nums)("line"))
pat.leaveWhitespace()
pat.setParseAction(CommandOpen.create)
return pat
@staticmethod
def create(str, loc, tocs):
"""pyparsing callback. Creates an instance of command
"""
if tocs.path:
pathLocation, path = tocs.path
else:
pathLocation, path = 0, ''
if tocs.line:
line = int(tocs.line)
else:
line = None
return [CommandOpen(pathLocation, path, line)]
def __init__(self, pathLocation, path, line):
self._path = path
self._pathLocation = pathLocation
self._line = line
def completer(self, text, pos):
"""Command completer.
If cursor is after path, returns PathCompleter or GlobCompleter
"""
if pos == self._pathLocation + len(self._path) or \
(not self._path and pos == len(text)):
return makeSuitableCompleter(self._path, pos - self._pathLocation)
else:
return None
def constructCommand(self, completableText):
"""Construct command by path
"""
command = 'f ' + completableText
if self._line is not None:
command += ' %d' % self._line
return command
def isReadyToExecute(self):
"""Check if command is complete and ready to execute
"""
files = glob.glob(os.path.expanduser(self._path))
return len(files) > 0 and \
all([os.path.isfile(p) for p in files])
def execute(self):
"""Execute the command
"""
for path in glob.iglob(os.path.expanduser(self._path)):
print 'open file', path, self._line
class CommandSaveAs(AbstractCommand):
"""Save As Locator command
"""
@staticmethod
def signature():
"""Command signature. For Help
"""
return 's PATH'
@staticmethod
def description():
"""Command description. For Help
"""
return 'Save file As'
@staticmethod
def pattern():
"""pyparsing pattern of the command
"""
def attachLocation(s, loc, tocs):
return [(loc, tocs[0])]
path = CharsNotIn(" \t")("path")
path.setParseAction(attachLocation)
pat = (Literal('s ') + Optional(White()) + Optional(path))
pat.leaveWhitespace()
pat.setParseAction(CommandSaveAs.create)
return pat
@staticmethod
def create(str, loc, tocs):
"""Callback for pyparsing. Creates an instance
"""
if tocs.path:
pathLocation, path = tocs.path
else:
pathLocation, path = 0, ''
return [CommandSaveAs(pathLocation, path)]
@staticmethod
def isAvailable():
"""Check if command is available.
It is available, if at least one document is opened
"""
return True
def __init__(self, pathLocation, path):
self._path = path
self._pathLocation = pathLocation
def completer(self, text, pos):
"""Command Completer.
Returns PathCompleter, if cursor stays after path
"""
if pos == self._pathLocation + len(self._path) or \
(not self._path and pos == len(text)):
return PathCompleter(self._path, pos - self._pathLocation)
else:
return None
def constructCommand(self, completableText):
"""Construct command by path
"""
return 'f ' + completableText
def isReadyToExecute(self):
"""Check if command is complete and ready to execute
"""
return len(self._path) > 0 and not os.path.isdir(self._path)
def execute(self):
"""Execute command
"""
print 'save file as', self._path