/workspace_commands.py

https://github.com/andreikop/completer
Python | 237 lines | 133 code | 43 blank | 61 comment | 21 complexity | 7f56054bcada325c677764a0283894c1 MD5 | raw file
  1. """
  2. workspace_commands --- Open, SaveAs, GotoLine commands
  3. ======================================================
  4. """
  5. import os.path
  6. import glob
  7. from pyparsing import CharsNotIn, Combine, Keyword, Literal, Optional, Or, ParseException, \
  8. StringEnd, Suppress, White, Word, nums
  9. from pathcompleter import makeSuitableCompleter, PathCompleter
  10. from locator import AbstractCommand
  11. class CommandGotoLine(AbstractCommand):
  12. """Go to line command implementation
  13. """
  14. @staticmethod
  15. def signature():
  16. """Command signature. For Help
  17. """
  18. return '[l] [LINE]'
  19. @staticmethod
  20. def description():
  21. """Command description. For Help
  22. """
  23. return 'Go to line'
  24. @staticmethod
  25. def pattern():
  26. """Pyparsing pattern
  27. """
  28. line = Word(nums)("line")
  29. pat = (Literal('l ') + Suppress(Optional(White())) + Optional(line)) ^ line
  30. pat.leaveWhitespace()
  31. pat.setParseAction(CommandGotoLine.create)
  32. return pat
  33. @staticmethod
  34. def create(str, loc, tocs):
  35. """Callback for pyparsing. Creates an instance of command
  36. """
  37. if tocs.line:
  38. line = int(tocs.line)
  39. else:
  40. line = None
  41. return [CommandGotoLine(line)]
  42. @staticmethod
  43. def isAvailable():
  44. """Check if command is currently available
  45. """
  46. return True
  47. def __init__(self, line):
  48. self._line = line
  49. def isReadyToExecute(self):
  50. """Check if command is complete and ready to execute
  51. """
  52. return self._line is not None
  53. def execute(self):
  54. """Execute the command
  55. """
  56. print 'goto', self._line
  57. class CommandOpen(AbstractCommand):
  58. @staticmethod
  59. def signature():
  60. """Command signature. For Help
  61. """
  62. return '[f] PATH [LINE]'
  63. @staticmethod
  64. def description():
  65. """Command description. For Help
  66. """
  67. return 'Open file. Globs are supported'
  68. @staticmethod
  69. def pattern():
  70. """pyparsing pattern
  71. """
  72. def attachLocation(s, loc, tocs):
  73. """pyparsing callback. Saves path position in the original string
  74. """
  75. return [(loc, tocs[0])]
  76. path = CharsNotIn(" \t")("path")
  77. path.setParseAction(attachLocation)
  78. longPath = CharsNotIn(" \t", min=2)("path")
  79. longPath.setParseAction(attachLocation)
  80. slashPath = Combine(Literal('/') + Optional(CharsNotIn(" \t")))("path")
  81. slashPath.setParseAction(attachLocation)
  82. pat = ((Literal('f ') + Optional(White()) + Optional(path)) ^ longPath ^ slashPath) + \
  83. Optional(White() + Word(nums)("line"))
  84. pat.leaveWhitespace()
  85. pat.setParseAction(CommandOpen.create)
  86. return pat
  87. @staticmethod
  88. def create(str, loc, tocs):
  89. """pyparsing callback. Creates an instance of command
  90. """
  91. if tocs.path:
  92. pathLocation, path = tocs.path
  93. else:
  94. pathLocation, path = 0, ''
  95. if tocs.line:
  96. line = int(tocs.line)
  97. else:
  98. line = None
  99. return [CommandOpen(pathLocation, path, line)]
  100. def __init__(self, pathLocation, path, line):
  101. self._path = path
  102. self._pathLocation = pathLocation
  103. self._line = line
  104. def completer(self, text, pos):
  105. """Command completer.
  106. If cursor is after path, returns PathCompleter or GlobCompleter
  107. """
  108. if pos == self._pathLocation + len(self._path) or \
  109. (not self._path and pos == len(text)):
  110. return makeSuitableCompleter(self._path, pos - self._pathLocation)
  111. else:
  112. return None
  113. def constructCommand(self, completableText):
  114. """Construct command by path
  115. """
  116. command = 'f ' + completableText
  117. if self._line is not None:
  118. command += ' %d' % self._line
  119. return command
  120. def isReadyToExecute(self):
  121. """Check if command is complete and ready to execute
  122. """
  123. files = glob.glob(os.path.expanduser(self._path))
  124. return len(files) > 0 and \
  125. all([os.path.isfile(p) for p in files])
  126. def execute(self):
  127. """Execute the command
  128. """
  129. for path in glob.iglob(os.path.expanduser(self._path)):
  130. print 'open file', path, self._line
  131. class CommandSaveAs(AbstractCommand):
  132. """Save As Locator command
  133. """
  134. @staticmethod
  135. def signature():
  136. """Command signature. For Help
  137. """
  138. return 's PATH'
  139. @staticmethod
  140. def description():
  141. """Command description. For Help
  142. """
  143. return 'Save file As'
  144. @staticmethod
  145. def pattern():
  146. """pyparsing pattern of the command
  147. """
  148. def attachLocation(s, loc, tocs):
  149. return [(loc, tocs[0])]
  150. path = CharsNotIn(" \t")("path")
  151. path.setParseAction(attachLocation)
  152. pat = (Literal('s ') + Optional(White()) + Optional(path))
  153. pat.leaveWhitespace()
  154. pat.setParseAction(CommandSaveAs.create)
  155. return pat
  156. @staticmethod
  157. def create(str, loc, tocs):
  158. """Callback for pyparsing. Creates an instance
  159. """
  160. if tocs.path:
  161. pathLocation, path = tocs.path
  162. else:
  163. pathLocation, path = 0, ''
  164. return [CommandSaveAs(pathLocation, path)]
  165. @staticmethod
  166. def isAvailable():
  167. """Check if command is available.
  168. It is available, if at least one document is opened
  169. """
  170. return True
  171. def __init__(self, pathLocation, path):
  172. self._path = path
  173. self._pathLocation = pathLocation
  174. def completer(self, text, pos):
  175. """Command Completer.
  176. Returns PathCompleter, if cursor stays after path
  177. """
  178. if pos == self._pathLocation + len(self._path) or \
  179. (not self._path and pos == len(text)):
  180. return PathCompleter(self._path, pos - self._pathLocation)
  181. else:
  182. return None
  183. def constructCommand(self, completableText):
  184. """Construct command by path
  185. """
  186. return 'f ' + completableText
  187. def isReadyToExecute(self):
  188. """Check if command is complete and ready to execute
  189. """
  190. return len(self._path) > 0 and not os.path.isdir(self._path)
  191. def execute(self):
  192. """Execute command
  193. """
  194. print 'save file as', self._path