2009-02-09 09:37:04 -05:00
|
|
|
import commands, os.path, re, string, sys, traceback
|
2009-04-05 22:30:40 -04:00
|
|
|
import color, completer, context, default, mode, method, regex, tab
|
|
|
|
import method.introspect
|
2007-10-21 20:50:11 -04:00
|
|
|
from point import Point
|
2009-02-05 10:21:24 -05:00
|
|
|
from render import RenderString
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule, OverridePatternRule
|
2009-02-04 08:48:58 -05:00
|
|
|
from parse import Any, And, Or, Optional, Name, Match, Matchs
|
2009-04-26 22:13:57 -04:00
|
|
|
from method import Method, arg, Argument
|
2009-03-08 00:19:58 -05:00
|
|
|
from method.shell import Exec
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2008-10-01 16:58:43 -04:00
|
|
|
class StringGrammar1(Grammar):
|
2007-07-21 11:40:53 -04:00
|
|
|
rules = [
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('octal', r'\\[0-7]{3}'),
|
|
|
|
PatternRule('hex', r'\\x[0-9a-fA-F]{2}'),
|
|
|
|
PatternRule('escaped', r'\\.'),
|
|
|
|
PatternRule('data', r"[^\\']+"),
|
2008-10-01 16:58:43 -04:00
|
|
|
]
|
|
|
|
class StringGrammar2(Grammar):
|
|
|
|
rules = [
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('octal', r'\\[0-7]{3}'),
|
|
|
|
PatternRule('hex', r'\\x[0-9a-fA-F]{2}'),
|
|
|
|
PatternRule('escaped', r'\\.'),
|
|
|
|
PatternRule('data', r'[^\\"]+'),
|
2008-10-01 16:58:43 -04:00
|
|
|
]
|
|
|
|
class StringGrammar3(Grammar):
|
|
|
|
rules = [
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('octal', r'\\[0-7]{3}'),
|
|
|
|
PatternRule('hex', r'\\x[0-9a-fA-F]{2}'),
|
|
|
|
PatternRule('escaped', r'\\.'),
|
|
|
|
PatternRule('data', r"(?:[^\\']|'(?!')|''(?!'))+"),
|
2008-10-01 16:58:43 -04:00
|
|
|
]
|
|
|
|
class StringGrammar4(Grammar):
|
|
|
|
rules = [
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('octal', r'\\[0-7]{3}'),
|
|
|
|
PatternRule('hex', r'\\x[0-9a-fA-F]{2}'),
|
|
|
|
PatternRule('escaped', r'\\.'),
|
|
|
|
PatternRule('data', r'(?:[^\\"]|"(?!")|""(?!"))+'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
class PythonGrammar(Grammar):
|
|
|
|
rules = [
|
2009-05-03 23:51:52 -04:00
|
|
|
PatternRule('python.def', '(?<=def )[a-zA-Z_][a-zA-Z0-9_]*'),
|
|
|
|
PatternRule('python.class', '(?<=class )[a-zA-Z_][a-zA-Z0-9_]*'),
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('python.reserved', '(?:True|None|False|Exception|self)(?![a-zA-Z0-9_])'),
|
|
|
|
PatternRule('python.keyword', '(?:yield|with|while|try|return|raise|print|pass|or|not|lambda|is|in|import|if|global|from|for|finally|exec|except|else|elif|del|def|continue|class|break|assert|as|and)(?![a-zA-Z0-9_])'),
|
2009-04-11 00:39:34 -04:00
|
|
|
PatternRule(r"python.builtin", r'(?<!\.)(?:zip|xrange|vars|unicode|unichr|type|tuple|super|sum|str|staticmethod|sorted|slice|setattr|set|round|repr|reduce|raw_input|range|property|pow|ord|open|oct|object|max|min|map|long|locals|list|len|iter|issubclass|isinstance|int|input|id|hex|hash|hasattr|globals|getattr|frozenset|float|filter|file|execfile|eval|enumerate|divmod|dir|dict|delattr|complex|compile|coerce|cmp|classmethod|chr|callable|bool)(?![a-zA-Z0-9_])'),
|
2009-05-03 23:51:52 -04:00
|
|
|
PatternRule('python.method', r'(?<=\. )[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
|
|
|
|
PatternRule('python.function', r'[a-zA-Z_][a-zA-Z0-9_]*(?= *\()'),
|
|
|
|
|
|
|
|
PatternRule('python.system_identifier', '__[a-zA-Z0-9_]+__'),
|
|
|
|
PatternRule('python.private_identifier', '__[a-zA-Z0-9_]*'),
|
|
|
|
PatternRule('python.hidden_identifier', '_[a-zA-Z0-9_]*'),
|
2009-05-04 20:33:49 -04:00
|
|
|
PatternRule('python.identifier', '[a-zA-Z_][a-zA-Z0-9_]*(?![a-zA-Z0-9_\'"])'),
|
2007-10-15 01:41:01 -04:00
|
|
|
|
2009-04-13 20:46:40 -04:00
|
|
|
RegionRule('rawstring', 'r"""', StringGrammar4, '"""'),
|
|
|
|
RegionRule('rawstring', "r'''", StringGrammar3, "'''"),
|
|
|
|
RegionRule('rawstring', 'r"', StringGrammar2, '"'),
|
|
|
|
RegionRule('rawstring', "r'", StringGrammar1, "'"),
|
|
|
|
RegionRule('string', 'u?"""', StringGrammar4, '"""'),
|
|
|
|
RegionRule('string', "u?'''", StringGrammar3, "'''"),
|
|
|
|
RegionRule('string', 'u?"', StringGrammar2, '"'),
|
|
|
|
RegionRule('string', "u?'", StringGrammar1, "'"),
|
2007-10-15 01:41:01 -04:00
|
|
|
|
2009-04-13 20:46:40 -04:00
|
|
|
PatternRule('delimiter', r'\(|\)|\[|\]|{|}|@|,|:|\.|`|=|;|\+=|-=|\*=|/=|//=|%=|&=|\|=|\^=|>>=|<<=|\*\*='),
|
2009-05-03 23:51:52 -04:00
|
|
|
PatternRule(r"python.integer", r"(?<![\.0-9a-zA-Z_])(?:0|-?[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?(?![\.0-9a-zA-Z_])"),
|
|
|
|
PatternRule(r"python.float", r"(?<![\.0-9a-zA-Z_])(?:-?[0-9]+\.[0-9]*|-?\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|-?\.[0-9]+)[eE][\+-]?[0-9]+)(?![\.0-9a-zA-Z_])"),
|
|
|
|
PatternRule(r"python.imaginary", r"(?<![\.0-9a-zA-Z_])(?:[0-9]+|(?:[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+)[jJ])(?![\.0-9a-zA-Z_])"),
|
2007-10-15 01:41:01 -04:00
|
|
|
|
2009-05-03 23:51:52 -04:00
|
|
|
PatternRule(r"python.operator", r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"),
|
2008-03-16 16:08:37 -04:00
|
|
|
|
2009-04-13 20:46:40 -04:00
|
|
|
OverridePatternRule('comment', '#@@:(?P<token>[.a-zA-Z0-9_]+):(?P<mode>[.a-zA-Z0-9_]+) *$'),
|
|
|
|
PatternRule('comment', '#.*$'),
|
|
|
|
PatternRule('continuation', r'\\\n$'),
|
|
|
|
PatternRule('spaces', ' +'),
|
|
|
|
PatternRule('eol', r'\n$'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2007-10-21 20:55:29 -04:00
|
|
|
class PythonTabber(tab.StackTabber):
|
2007-07-21 11:40:53 -04:00
|
|
|
# NOTE: yield might initially seem like an endlevel name, but it's not one.
|
2009-04-11 00:39:34 -04:00
|
|
|
# NOTE: return should be an endlevel name but for now it can't be one.
|
2008-11-12 00:00:40 -05:00
|
|
|
endlevel_names = ('pass', 'raise', 'break', 'continue')
|
2007-07-21 11:40:53 -04:00
|
|
|
startlevel_names = ('if', 'try', 'class', 'def', 'for', 'while', 'try')
|
|
|
|
def __init__(self, m):
|
2007-10-21 20:55:29 -04:00
|
|
|
tab.StackTabber.__init__(self, m)
|
2007-07-21 11:40:53 -04:00
|
|
|
self.base_level = 0
|
|
|
|
|
|
|
|
def is_base(self, y):
|
|
|
|
if y == 0:
|
|
|
|
# we always know that line 0 is indented at the 0 level
|
|
|
|
return True
|
|
|
|
tokens = self.get_tokens(y)
|
2009-04-11 00:39:34 -04:00
|
|
|
if tokens[0].matchs('python.keyword', self.startlevel_names):
|
|
|
|
# if a line has no whitespace and begins with something like
|
2007-07-21 11:40:53 -04:00
|
|
|
# 'while','class','def','if',etc. then we can start at it
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# otherwise, we can't be sure that its level is correct
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_level(self, y):
|
|
|
|
self._calc_level(y)
|
|
|
|
return self.lines.get(y)
|
|
|
|
|
|
|
|
def _calc_level(self, y):
|
|
|
|
# ok, so first remember where we are going, and find our starting point
|
|
|
|
target = y
|
2007-10-19 11:17:00 -04:00
|
|
|
y = max(0, y - 1)
|
2007-07-21 11:40:53 -04:00
|
|
|
while not self.is_base(y) and y > 0:
|
|
|
|
y -= 1
|
|
|
|
|
|
|
|
# ok, so clear out our stack and then loop over each line
|
|
|
|
self.popped = False
|
|
|
|
self.markers = []
|
|
|
|
while y <= target:
|
|
|
|
self.continued = False
|
|
|
|
self.last_popped = self.popped
|
|
|
|
self.popped = False
|
|
|
|
tokens = self.get_tokens(y)
|
|
|
|
currlvl = self.get_curr_level()
|
|
|
|
# if we were continuing, let's pop that previous continuation token
|
|
|
|
# and note that we're continuing
|
|
|
|
if self.markers and self.markers[-1].name == 'cont':
|
|
|
|
self.continued = True
|
|
|
|
self._pop()
|
|
|
|
# if we haven't reached the target-line yet, we can detect how many
|
|
|
|
# levels of unindention, if any, the user chose on previous lines
|
|
|
|
if y < target and len(tokens) > 2:
|
|
|
|
if self.token_is_space(y, 0):
|
|
|
|
l = len(tokens[0].string)
|
|
|
|
else:
|
|
|
|
l = 0
|
|
|
|
while currlvl > l:
|
|
|
|
self._pop()
|
|
|
|
currlvl = self.get_curr_level()
|
|
|
|
self.popped = True
|
2009-04-05 22:30:40 -04:00
|
|
|
# ok, having done all that, we can now process each token
|
|
|
|
# on the line
|
2007-07-21 11:40:53 -04:00
|
|
|
for i in range(0, len(tokens)):
|
|
|
|
currlvl = self._handle_token(currlvl, y, i)
|
|
|
|
# so let's store the level for this line, as well as some debugging
|
|
|
|
self.lines[y] = currlvl
|
|
|
|
self.record[y] = tuple(self.markers)
|
|
|
|
y += 1
|
|
|
|
|
|
|
|
def _handle_close_token(self, currlvl, y, i):
|
|
|
|
try:
|
2007-10-21 20:55:29 -04:00
|
|
|
return tab.StackTabber._handle_close_token(self, currlvl, y, i)
|
2007-07-21 11:40:53 -04:00
|
|
|
except:
|
|
|
|
return currlvl
|
|
|
|
|
|
|
|
def _handle_other_token(self, currlvl, y, i):
|
2008-04-02 19:06:52 -04:00
|
|
|
w = self.mode.tabwidth
|
2007-07-21 11:40:53 -04:00
|
|
|
token = self.get_token(y, i)
|
|
|
|
fqname = token.fqname()
|
|
|
|
if fqname == 'continuation':
|
|
|
|
# we need to pop the indentation level over, unless last line was
|
|
|
|
# also a continued line
|
|
|
|
if self.continued:
|
2009-02-05 10:21:24 -05:00
|
|
|
self._opt_append('cont', currlvl, y)
|
2007-07-21 11:40:53 -04:00
|
|
|
else:
|
2009-02-05 10:21:24 -05:00
|
|
|
self._opt_append('cont', currlvl + w, y)
|
2007-07-21 11:40:53 -04:00
|
|
|
elif fqname == 'string.start':
|
|
|
|
# while inside of a string, there is no indention leve
|
2009-02-05 10:21:24 -05:00
|
|
|
self._opt_append('string', None, y)
|
2007-07-21 11:40:53 -04:00
|
|
|
elif fqname == 'string.end':
|
|
|
|
# since we're done with the string, resume our indentation level
|
|
|
|
self._opt_pop('string')
|
|
|
|
elif fqname == 'delimiter':
|
|
|
|
# we only really care about a colon as part of a one-line statement,
|
|
|
|
# i.e. "while ok: foo()" or "if True: print 3"
|
|
|
|
if token.string == ':':
|
2009-02-03 14:19:48 -05:00
|
|
|
if self.markers and self.markers[-1].name in ('[', '{', '('):
|
2007-07-21 11:40:53 -04:00
|
|
|
pass
|
|
|
|
elif self.is_rightmost_token(y, i):
|
|
|
|
pass
|
|
|
|
else:
|
2009-02-03 14:19:48 -05:00
|
|
|
self._pop()
|
2009-04-11 00:39:34 -04:00
|
|
|
elif fqname == 'python.keyword':
|
2009-04-05 22:30:40 -04:00
|
|
|
s = token.string
|
|
|
|
if s in self.endlevel_names and self.is_leftmost_token(y, i):
|
2007-07-21 11:40:53 -04:00
|
|
|
# we know we'll unindent at least once
|
|
|
|
self._pop()
|
|
|
|
self.popped = True
|
2009-04-05 22:30:40 -04:00
|
|
|
elif s in self.startlevel_names and self.is_leftmost_token(y, i):
|
2007-07-21 11:40:53 -04:00
|
|
|
# we know we will indent exactly once
|
2009-04-05 22:30:40 -04:00
|
|
|
self._append(s, currlvl + w, y)
|
|
|
|
elif s in ('elif', 'else') and self.is_leftmost_token(y, i):
|
2007-07-21 11:40:53 -04:00
|
|
|
# we know we'll unindent at least to the first if/elif
|
2008-10-22 18:25:45 -04:00
|
|
|
if not self.popped and not self.last_popped and self._peek_until('if', 'elif'):
|
2007-07-21 11:40:53 -04:00
|
|
|
self._pop_until('if', 'elif')
|
|
|
|
currlvl = self.get_curr_level()
|
2009-04-05 22:30:40 -04:00
|
|
|
self._append(s, currlvl + w, y)
|
|
|
|
elif s == 'except' and self.is_leftmost_token(y, i):
|
2007-07-21 11:40:53 -04:00
|
|
|
# we know we'll unindent at least to the first try
|
|
|
|
if not self.popped and not self.last_popped:
|
|
|
|
self._pop_until('try')
|
|
|
|
currlvl = self.get_curr_level()
|
2009-04-05 22:30:40 -04:00
|
|
|
self._append(s, currlvl + w, y)
|
|
|
|
elif s == 'finally' and self.is_leftmost_token(y, i):
|
2007-07-21 11:40:53 -04:00
|
|
|
# we know we'll unindent at least to the first try/except
|
|
|
|
if not self.popped and not self.last_popped:
|
|
|
|
self._pop_until('try', 'except')
|
|
|
|
currlvl = self.get_curr_level()
|
2009-04-05 22:30:40 -04:00
|
|
|
self._append(s, currlvl + w, y)
|
2007-07-21 11:40:53 -04:00
|
|
|
return currlvl
|
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonCheckSyntax(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
'''Check the syntax of the current python file'''
|
|
|
|
def _execute(self, w, **vargs):
|
2008-03-21 02:08:17 -04:00
|
|
|
pythonlib = w.application.config.get('python.lib')
|
|
|
|
if pythonlib:
|
|
|
|
sys.path.insert(0, pythonlib)
|
2007-07-22 22:37:30 -04:00
|
|
|
source = w.buffer.make_string()
|
|
|
|
try:
|
|
|
|
code = compile(source, w.buffer.path, 'exec')
|
|
|
|
w.set_error("Syntax OK")
|
|
|
|
except Exception, e:
|
|
|
|
output = traceback.format_exc()
|
2008-12-03 00:15:22 -05:00
|
|
|
w.application.data_buffer("*PythonSyntax*", output, switch_to=True,
|
|
|
|
modename='error')
|
2007-10-18 11:31:12 -04:00
|
|
|
del sys.path[0]
|
2007-07-21 11:40:53 -04:00
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonDictCleanup(Method):
|
2007-07-21 11:40:53 -04:00
|
|
|
'''Align assignment blocks and literal dictionaries'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
cursor = w.logical_cursor()
|
|
|
|
b = w.buffer
|
|
|
|
|
|
|
|
# so this is where we will store the groups that we find
|
|
|
|
groups_by_line = {}
|
|
|
|
|
|
|
|
# the regex we will try
|
|
|
|
regexes = [regex.python_dict_cleanup,
|
|
|
|
regex.python_assign_cleanup]
|
|
|
|
|
|
|
|
# if we aren't in a hash, inform the user and exit
|
|
|
|
line = b.lines[cursor.y]
|
|
|
|
myregex = None
|
|
|
|
for r in regexes:
|
|
|
|
if r.match(line):
|
|
|
|
myregex = r
|
|
|
|
|
|
|
|
if myregex is None:
|
|
|
|
raise Exception, "Not a python dict line"
|
|
|
|
|
|
|
|
groups_by_line[cursor.y] = myregex.match(line).groups()
|
|
|
|
|
|
|
|
# find the beginning of this hash block
|
|
|
|
start = 0
|
|
|
|
i = cursor.y - 1
|
|
|
|
while i >= 0:
|
|
|
|
line = b.lines[i]
|
|
|
|
m = myregex.match(line)
|
|
|
|
if not m:
|
|
|
|
start = i + 1
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
groups_by_line[i] = m.groups()
|
|
|
|
i -= 1
|
|
|
|
|
|
|
|
# find the end of this hash block
|
|
|
|
end = len(b.lines) - 1
|
|
|
|
i = cursor.y + 1
|
|
|
|
while i < len(b.lines):
|
|
|
|
line = b.lines[i]
|
|
|
|
m = myregex.match(line)
|
|
|
|
if not m:
|
|
|
|
end = i - 1
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
groups_by_line[i] = m.groups()
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
# assume that the least indented line is correct
|
|
|
|
indent_w = min([len(groups_by_line[k][0]) for k in groups_by_line])
|
|
|
|
|
|
|
|
# find the longest hash key to base all the other padding on
|
|
|
|
key_w = max([len(groups_by_line[k][1]) for k in groups_by_line])
|
|
|
|
|
|
|
|
# for each line, format it correctly
|
|
|
|
keys = groups_by_line.keys()
|
|
|
|
keys.sort()
|
|
|
|
data = ''
|
|
|
|
for i in keys:
|
|
|
|
indent_pad = ' ' * indent_w
|
|
|
|
key = groups_by_line[i][1]
|
|
|
|
sep = groups_by_line[i][3]
|
|
|
|
value = groups_by_line[i][5]
|
|
|
|
key_pad = ' ' * (key_w - len(key))
|
|
|
|
if sep == '=':
|
2009-04-05 22:30:40 -04:00
|
|
|
data += indent_pad + key + key_pad + ' ' + sep + ' '
|
2007-07-21 11:40:53 -04:00
|
|
|
else:
|
2009-04-05 22:30:40 -04:00
|
|
|
data += indent_pad + key + sep + ' ' + key_pad
|
|
|
|
data += value + '\n'
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
# remove the old text and add the new
|
|
|
|
start_p = Point(0, start)
|
2007-08-21 09:07:39 -04:00
|
|
|
if end + 1 < len(w.buffer.lines):
|
|
|
|
end_p = Point(0, end + 1)
|
|
|
|
else:
|
|
|
|
end_p = Point(len(w.buffer.lines[-1]), len(w.buffer.lines) - 1)
|
2008-11-11 00:15:47 -05:00
|
|
|
w.delete(start_p, end_p)
|
2007-07-21 11:40:53 -04:00
|
|
|
w.insert_string(start_p, data)
|
2007-10-19 02:41:33 -04:00
|
|
|
|
2009-03-08 00:19:58 -05:00
|
|
|
class PythonHelp(Exec):
|
|
|
|
'''Generate a help page on a python object'''
|
|
|
|
args = [arg('name', t="string", p="Name: ", h='name to get help on')]
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
name = vargs['name']
|
2009-04-02 10:17:22 -04:00
|
|
|
stmt = 'try:\n import %s\nexcept:\n pass\nhelp(%s)' % (name, name)
|
|
|
|
self._doit(w, None, 'python -c "%s"' % stmt)
|
2009-03-08 00:19:58 -05:00
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonInsertTripleSquotes(Method):
|
2008-04-01 14:35:09 -04:00
|
|
|
'''Insert a triple-quoted string using single-quotes'''
|
2008-05-11 19:40:06 -04:00
|
|
|
_q = "'''"
|
2008-04-01 14:35:09 -04:00
|
|
|
def _execute(self, w, **vargs):
|
2008-05-11 19:59:35 -04:00
|
|
|
w.insert_string_at_cursor('%s%s' % (self._q, self._q))
|
2008-04-01 14:35:09 -04:00
|
|
|
for i in range(0, 3):
|
|
|
|
w.backward()
|
2009-03-08 00:19:58 -05:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
class PythonInsertTripleDquotes(PythonInsertTripleSquotes):
|
2008-04-01 14:35:09 -04:00
|
|
|
'''Insert a triple-quoted string using double-quotes'''
|
2008-05-11 19:40:06 -04:00
|
|
|
_q = '"""'
|
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonInitNames(Method):
|
2008-05-11 19:40:06 -04:00
|
|
|
'''Jump to a function defined in this module'''
|
2008-04-01 14:35:09 -04:00
|
|
|
def _execute(self, w, **vargs):
|
2008-05-11 19:40:06 -04:00
|
|
|
w.mode.context.build_name_map()
|
|
|
|
w.application.set_error("Initialized name maps")
|
|
|
|
|
2008-06-05 21:55:17 -04:00
|
|
|
class PythonSemanticComplete(method.introspect.TokenComplete):
|
|
|
|
_mini_prompt = 'Semantic Complete'
|
|
|
|
def _min_completion(self, w, t):
|
|
|
|
a = w.application
|
|
|
|
a.methods['ipython-path-start'].execute(w, switch=False)
|
|
|
|
|
|
|
|
name = buffer.IperlBuffer.create_name(w.buffer)
|
|
|
|
b = a.get_buffer_by_name(name)
|
|
|
|
|
|
|
|
line = w.buffer.lines[t.y]
|
|
|
|
(x1, x2) = (t.x, t.end_x())
|
|
|
|
candidates = [t.string + s for s in b.completions(line[x1:x2])]
|
|
|
|
|
|
|
|
minlen = None
|
|
|
|
for candidate in candidates:
|
|
|
|
if minlen is None:
|
|
|
|
minlen = len(candidate)
|
|
|
|
else:
|
|
|
|
minlen = min(minlen, len(candidate))
|
|
|
|
|
|
|
|
return self._prune_candidates(t, minlen, candidates)
|
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonGotoName(Method):
|
2008-05-11 19:40:06 -04:00
|
|
|
'''Jump to a class or function defined in this module'''
|
2009-04-26 22:13:57 -04:00
|
|
|
args = [Argument("name", type(""), "pythonname", "Goto Name: ")]
|
2008-05-11 19:40:06 -04:00
|
|
|
title = 'Name'
|
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.mode.context.get_names()
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
name = vargs['name']
|
|
|
|
d = self._get_dict(w)
|
|
|
|
if name in d:
|
|
|
|
w.goto(Point(0, d[name]))
|
|
|
|
else:
|
|
|
|
w.application.set_error("%r %r was not found" % (title, name))
|
2009-03-08 00:19:58 -05:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
class PythonGotoFunction(PythonGotoName):
|
|
|
|
'''Jump to a function defined in this module'''
|
2009-04-26 22:13:57 -04:00
|
|
|
args = [Argument("name", type(""), "pythonfunction", "Goto Function: ")]
|
2008-05-11 19:40:06 -04:00
|
|
|
title = 'Function'
|
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.mode.context.get_functions()
|
2009-03-08 00:19:58 -05:00
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonGotoClass(Method):
|
2008-05-11 19:40:06 -04:00
|
|
|
'''Jump to a class defined in this module'''
|
2009-04-26 22:13:57 -04:00
|
|
|
args = [Argument("name", type(""), "pythonclass", "Goto Class: ")]
|
2008-05-11 19:40:06 -04:00
|
|
|
title = 'Class'
|
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.mode.context.get_classes()
|
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonListNames(Method):
|
2008-05-11 19:40:06 -04:00
|
|
|
'''Show the user all functions defined in this module'''
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
names = w.mode.context.get_names()
|
|
|
|
output = '\n'.join(sorted(names)) + "\n"
|
|
|
|
w.application.data_buffer("*Python-List-Names*", output, switch_to=True)
|
2008-04-01 14:35:09 -04:00
|
|
|
|
2009-04-26 22:13:57 -04:00
|
|
|
class PythonBrmFindReferences(Method):
|
2008-10-28 15:48:44 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
if w.mode.brm is None:
|
|
|
|
w.set_error('bicycle repairman not installed')
|
|
|
|
return
|
2009-04-02 10:17:22 -04:00
|
|
|
|
|
|
|
base = os.getcwd()
|
2008-10-28 15:48:44 -04:00
|
|
|
path = w.buffer.path
|
|
|
|
cursor = w.logical_cursor()
|
2009-04-02 10:17:22 -04:00
|
|
|
line, col = cursor.y + 1, cursor.x + 1
|
|
|
|
refs = w.mode.brm.findReferencesByCoordinates(path, line, col)
|
|
|
|
|
|
|
|
l, count, tokens = 0, 0, []
|
|
|
|
if not base.endswith('/'): base += '/'
|
|
|
|
|
2008-10-28 15:48:44 -04:00
|
|
|
for r in refs:
|
|
|
|
f, n, c = r.filename, r.lineno, r.confidence
|
2009-04-02 10:17:22 -04:00
|
|
|
f = f.replace(base, '')
|
|
|
|
label = '%s:%d:' % (f, n)
|
|
|
|
l = max(len(label), l)
|
|
|
|
tokens.append((label, c))
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
for tpl in tokens:
|
|
|
|
lines.append('%-*s %3d%% confidence' % (l, tpl[0], tpl[1]))
|
|
|
|
|
2009-05-06 00:33:45 -04:00
|
|
|
if not tokens:
|
2008-10-28 15:48:44 -04:00
|
|
|
w.set_error('no references found')
|
|
|
|
return
|
|
|
|
|
|
|
|
data = '\n'.join(lines)
|
|
|
|
w.application.data_buffer("*References*", data, switch_to=True)
|
2009-04-02 10:17:22 -04:00
|
|
|
if count == 1:
|
2008-10-28 15:48:44 -04:00
|
|
|
w.set_error('1 reference found')
|
|
|
|
else:
|
2009-04-02 10:17:22 -04:00
|
|
|
w.set_error('%d references found' % count)
|
2008-10-28 15:48:44 -04:00
|
|
|
|
2008-04-25 10:20:40 -04:00
|
|
|
class PythonNameCompleter(completer.Completer):
|
2008-05-11 19:40:06 -04:00
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.buffer.method.old_window.mode.context.get_names()
|
2008-04-25 10:20:40 -04:00
|
|
|
def get_candidates(self, s, w=None):
|
2008-05-11 19:40:06 -04:00
|
|
|
return [n for n in self._get_dict(w) if n.startswith(s)]
|
|
|
|
class PythonFunctionCompleter(PythonNameCompleter):
|
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.buffer.method.old_window.mode.context.get_functions()
|
|
|
|
class PythonClassCompleter(completer.Completer):
|
|
|
|
def _get_dict(self, w):
|
|
|
|
return w.buffer.method.old_window.mode.context.get_classes()
|
|
|
|
|
|
|
|
class PythonContext(context.Context):
|
2009-02-04 08:48:58 -05:00
|
|
|
empty_match = And(Optional(Name('spaces')), Name('eol'))
|
|
|
|
class_match = And(Optional(Name('spaces')),
|
2009-04-11 00:39:34 -04:00
|
|
|
Match('python.keyword', 'class'),
|
2009-02-04 08:48:58 -05:00
|
|
|
Name('spaces'),
|
2009-05-03 23:51:52 -04:00
|
|
|
Name('python.class'))
|
2009-02-04 08:48:58 -05:00
|
|
|
func_match = And(Optional(Name('spaces')),
|
2009-04-11 00:39:34 -04:00
|
|
|
Match('python.keyword', 'def'),
|
2009-02-04 08:48:58 -05:00
|
|
|
Name('spaces'),
|
2009-05-03 23:51:52 -04:00
|
|
|
Name('python.def'))
|
2008-05-11 19:40:06 -04:00
|
|
|
def __init__(self, mode):
|
|
|
|
self.mode = mode
|
|
|
|
self.names = None
|
|
|
|
self.namelines = None
|
|
|
|
self.classes = None
|
|
|
|
self.functions = None
|
|
|
|
|
|
|
|
# new object methods
|
|
|
|
def get_functions(self):
|
|
|
|
if self.functions is None:
|
|
|
|
self.build_name_map()
|
|
|
|
return self.functions
|
|
|
|
def get_classes(self):
|
|
|
|
if self.classes is None:
|
|
|
|
self.build_name_map()
|
|
|
|
return self.classes
|
|
|
|
def get_function_list(self):
|
|
|
|
return self._ordered_dict(self.get_functions())
|
|
|
|
def get_class_list(self):
|
|
|
|
return self._ordered_dict(self.get_classes())
|
|
|
|
|
|
|
|
# overridden object methods
|
|
|
|
def _init_name_map(self):
|
|
|
|
self.names = {}
|
|
|
|
self.classes = {}
|
|
|
|
self.functions = {}
|
2008-05-12 11:34:06 -04:00
|
|
|
self.namelines = [(None, None)] * len(self.mode.window.buffer.lines)
|
|
|
|
def _del_name(self, y, name):
|
|
|
|
if name:
|
|
|
|
if name in self.names:
|
|
|
|
del self.names[name]
|
|
|
|
if name in self.classes:
|
|
|
|
del self.classes[name]
|
|
|
|
if name in self.functions:
|
|
|
|
del self.functions[name]
|
|
|
|
self.namelines[y] = (None, None)
|
2008-05-11 19:40:06 -04:00
|
|
|
def _build_name_map(self, y1, y2, last, curr, stack):
|
|
|
|
blen = len(self.mode.window.buffer.lines)
|
|
|
|
highlights = self.mode.window.get_highlighter()
|
|
|
|
i = y1
|
|
|
|
while i < y2:
|
2009-02-04 08:48:58 -05:00
|
|
|
tokens = highlights.tokens[i]
|
2008-05-11 19:40:06 -04:00
|
|
|
g = highlights.tokens[i]
|
2009-02-04 08:48:58 -05:00
|
|
|
if self.empty_match.match(tokens):
|
2008-05-11 19:40:06 -04:00
|
|
|
if last is None:
|
|
|
|
last = i
|
|
|
|
i += 1
|
|
|
|
continue
|
|
|
|
|
2008-09-30 01:47:20 -04:00
|
|
|
if g[0].name == 'spaces':
|
2008-05-11 19:40:06 -04:00
|
|
|
j, lvl = 1, len(g[0].string)
|
|
|
|
else:
|
|
|
|
j, lvl = 0, 0
|
2009-02-04 08:48:58 -05:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
while stack and lvl <= stack[-1][0]:
|
|
|
|
stack.pop(-1)
|
|
|
|
|
|
|
|
if last is not None:
|
|
|
|
curr = '.'.join([x[1] for x in stack])
|
|
|
|
if curr:
|
|
|
|
for k in range(last, i):
|
2008-05-12 11:34:06 -04:00
|
|
|
self.namelines[k] = (curr, None)
|
2008-05-11 19:40:06 -04:00
|
|
|
last = None
|
|
|
|
|
|
|
|
if len(g[j:]) > 3:
|
|
|
|
d, found = None, False
|
2009-04-11 00:39:34 -04:00
|
|
|
if g[j].name == 'python.keyword' and g[j].string == 'class':
|
2008-05-11 19:40:06 -04:00
|
|
|
d, found = self.classes, True
|
2009-04-11 00:39:34 -04:00
|
|
|
elif g[j].name == 'python.keyword' and g[j].string == 'def':
|
2008-05-11 19:40:06 -04:00
|
|
|
d, found = self.functions, True
|
|
|
|
if found:
|
|
|
|
stack.append([lvl, g[j+2].string])
|
|
|
|
curr = '.'.join([x[1] for x in stack])
|
|
|
|
d[curr] = i
|
|
|
|
self.names[curr] = i
|
|
|
|
else:
|
|
|
|
curr = '.'.join([x[1] for x in stack])
|
|
|
|
|
2008-05-12 11:34:06 -04:00
|
|
|
if i == y2 - 1 and curr != self.namelines[i][0] and y2 < blen:
|
2008-05-11 19:40:06 -04:00
|
|
|
y2 += 1
|
|
|
|
if curr:
|
2008-05-12 11:34:06 -04:00
|
|
|
self.namelines[i] = (curr, None)
|
2008-05-11 19:40:06 -04:00
|
|
|
i += 1
|
|
|
|
|
|
|
|
if last is not None and y2 < len(self.namelines):
|
2008-06-23 09:24:10 -04:00
|
|
|
if self.namelines[y2] and self.namelines[y2][0]:
|
2008-05-12 11:34:06 -04:00
|
|
|
n = len(self.namelines[y2][0].split('.'))
|
2008-05-11 19:40:06 -04:00
|
|
|
curr = '.'.join([x[1] for x in stack[:n]])
|
|
|
|
if curr:
|
|
|
|
for k in range(last, y2):
|
2008-05-12 11:34:06 -04:00
|
|
|
self.namelines[k] = (curr, None)
|
2008-04-25 10:20:40 -04:00
|
|
|
|
2008-04-17 10:55:02 -04:00
|
|
|
class Python(mode.Fundamental):
|
2009-04-02 10:17:22 -04:00
|
|
|
description = '''
|
|
|
|
This programming mode is designed to edit Python source files. It
|
|
|
|
features parenthesis matching, syntax highlighting, indentation
|
|
|
|
assistance and syntax checking. It can also find classes and functions
|
|
|
|
by name, provide scope context in the status bar, and has an optional
|
|
|
|
context header. Finally, it can semantically complete tokens and
|
|
|
|
provide help output via the python interpreter.
|
|
|
|
'''
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'Python'
|
2008-04-17 10:55:02 -04:00
|
|
|
extensions = ['.py']
|
|
|
|
detection = ['python']
|
|
|
|
tabbercls = PythonTabber
|
|
|
|
grammar = PythonGrammar
|
|
|
|
opentokens = ('delimiter',)
|
|
|
|
opentags = {'(': ')', '[': ']', '{': '}'}
|
|
|
|
closetokens = ('delimiter',)
|
|
|
|
closetags = {')': '(', ']': '[', '}': '{'}
|
2009-02-15 12:06:35 -05:00
|
|
|
commentc = '#'
|
2008-04-17 10:55:02 -04:00
|
|
|
colors = {
|
2009-05-03 23:51:52 -04:00
|
|
|
'python.def': ('blue', 'default', 'bold'),
|
|
|
|
'python.class': ('yellow', 'default', 'bold'),
|
|
|
|
'python.reserved': ('magenta', 'default', 'bold'),
|
|
|
|
'python.keyword': ('cyan', 'default', 'bold'),
|
|
|
|
'python.builtin': ('cyan', 'default', 'bold'),
|
|
|
|
'python.method': ('default', 'default'),
|
|
|
|
'python.function': ('default', 'default'),
|
|
|
|
'python.system_identifier': ('cyan', 'default', 'bold'),
|
|
|
|
'python.private_identifier': ('default', 'default'),
|
|
|
|
'python.hidden_identifier': ('default', 'default'),
|
|
|
|
'python.identifier': ('default', 'default'),
|
|
|
|
'python.integer': ('default', 'default'),
|
|
|
|
'python.float': ('default', 'default'),
|
|
|
|
'python.imaginary': ('default', 'default'),
|
|
|
|
'python.operator': ('default', 'default'),
|
|
|
|
'rawstring.start': ('green', 'default', 'bold'),
|
|
|
|
'rawstring.data': ('green', 'default', 'bold'),
|
|
|
|
'rawstring.null': ('green', 'default', 'bold'),
|
|
|
|
'rawstring.escaped': ('magenta', 'default', 'bold'),
|
|
|
|
'rawstring.end': ('green', 'default', 'bold'),
|
2008-04-17 10:55:02 -04:00
|
|
|
}
|
|
|
|
config = {
|
|
|
|
'python.lib': '.',
|
|
|
|
}
|
2008-10-15 17:17:16 -04:00
|
|
|
lconfig = {
|
2009-05-27 15:28:04 -04:00
|
|
|
'ignore_suffix': ['.pyc', '.pyo'],
|
2008-10-15 17:17:16 -04:00
|
|
|
}
|
2009-03-08 00:19:58 -05:00
|
|
|
actions = [PythonInitNames, PythonListNames, PythonGotoName, PythonHelp,
|
2008-04-25 10:20:40 -04:00
|
|
|
PythonGotoFunction, PythonGotoClass, PythonCheckSyntax,
|
2009-04-05 22:30:40 -04:00
|
|
|
PythonDictCleanup, PythonSemanticComplete,
|
|
|
|
PythonBrmFindReferences,
|
2008-04-25 10:20:40 -04:00
|
|
|
PythonInsertTripleSquotes, PythonInsertTripleDquotes]
|
|
|
|
completers = {
|
2008-10-15 17:17:16 -04:00
|
|
|
"pythonname": PythonNameCompleter(None),
|
|
|
|
"pythonfunction": PythonFunctionCompleter(None),
|
|
|
|
"pythonclass": PythonClassCompleter(None),
|
2008-04-25 10:20:40 -04:00
|
|
|
}
|
2008-09-21 01:15:33 -04:00
|
|
|
|
2009-04-05 22:30:40 -04:00
|
|
|
format = "%(flag)s %(bname)s (%(mname)s) %(indent)s %(cursor)s %(perc)s [%(name)s] %(vc-info)s"
|
2009-02-05 12:12:52 -05:00
|
|
|
header_size = 3
|
2008-09-21 01:15:33 -04:00
|
|
|
|
2008-05-11 19:40:06 -04:00
|
|
|
def get_status_names(self):
|
2008-09-21 01:15:33 -04:00
|
|
|
names = mode.Fundamental.get_status_names(self)
|
|
|
|
c = self.window.logical_cursor()
|
|
|
|
names['name'] = self.context.get_line_name(c.y)
|
2008-05-11 19:40:06 -04:00
|
|
|
return names
|
2008-09-21 01:15:33 -04:00
|
|
|
|
2009-02-05 10:21:24 -05:00
|
|
|
# xyz
|
|
|
|
def get_header(self):
|
2009-03-17 15:24:10 -04:00
|
|
|
fg, bg = "default", "blue"
|
2009-02-05 10:47:20 -05:00
|
|
|
|
|
|
|
if self.tabber is None:
|
|
|
|
s = "Header support is not available for this mode"
|
2009-03-06 11:39:47 -05:00
|
|
|
hs = [[RenderString(s=s, attrs=color.build(fg, bg))]]
|
2009-02-05 10:47:20 -05:00
|
|
|
while len(hs) < 3:
|
2009-03-06 11:39:47 -05:00
|
|
|
hs.insert(0, [RenderString(s='', attrs=color.build(fg, bg))])
|
2009-02-05 10:47:20 -05:00
|
|
|
return hs
|
|
|
|
|
2009-02-05 10:21:24 -05:00
|
|
|
w = self.window
|
|
|
|
y = self.window.first.y
|
|
|
|
if self.window.first.x > 0:
|
|
|
|
y += 1
|
|
|
|
lvl = self.tabber.get_level(y)
|
|
|
|
markers = self.tabber.record[y]
|
|
|
|
if w.buffer.is_whitespace(y):
|
|
|
|
ws = None
|
|
|
|
else:
|
|
|
|
ws = w.buffer.count_leading_whitespace(y)
|
|
|
|
|
|
|
|
hs = []
|
|
|
|
i = len(markers) - 1
|
|
|
|
while i >= 0 and len(hs) < 3:
|
|
|
|
marker = markers[i]
|
|
|
|
i -= 1
|
|
|
|
if marker.y == y:
|
|
|
|
continue
|
|
|
|
if ws and marker.level > ws:
|
|
|
|
continue
|
|
|
|
s = w.buffer.lines[marker.y][:w.width - 1]
|
2009-03-06 11:39:47 -05:00
|
|
|
hs.insert(0, [RenderString(s=s, attrs=color.build(fg, bg))])
|
2009-02-05 10:21:24 -05:00
|
|
|
while len(hs) < 3:
|
2009-03-06 11:39:47 -05:00
|
|
|
hs.insert(0, [RenderString(s='', attrs=color.build(fg, bg))])
|
2009-02-05 10:21:24 -05:00
|
|
|
return hs
|
|
|
|
|
2008-04-17 10:55:02 -04:00
|
|
|
def __init__(self, w):
|
|
|
|
mode.Fundamental.__init__(self, w)
|
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
2008-04-25 10:20:40 -04:00
|
|
|
self.add_bindings('python-goto-name', ('C-c M-g',))
|
|
|
|
self.add_bindings('python-goto-function', ('C-c M-f',))
|
|
|
|
self.add_bindings('python-goto-class', ('C-c M-c',))
|
2008-04-17 10:55:02 -04:00
|
|
|
self.add_bindings('python-check-syntax', ('C-c s',))
|
|
|
|
self.add_bindings('python-dict-cleanup', ('C-c h',))
|
|
|
|
self.add_bindings('python-insert-triple-squotes', ('C-c M-\'',))
|
|
|
|
self.add_bindings('python-insert-triple-dquotes', ('C-c M-"',))
|
2008-06-05 21:55:17 -04:00
|
|
|
self.add_bindings('python-semantic-complete', ('C-c TAB',))
|
2008-05-11 19:40:06 -04:00
|
|
|
self.context = PythonContext(self)
|
2008-04-17 10:55:02 -04:00
|
|
|
|
2008-10-28 15:48:44 -04:00
|
|
|
# bicycle repairman!
|
2009-04-02 10:17:22 -04:00
|
|
|
try:
|
|
|
|
import bike
|
2008-10-28 15:48:44 -04:00
|
|
|
self.brm = bike.init()
|
2009-04-02 10:17:22 -04:00
|
|
|
# turn off brm's annoying STDERR printing
|
|
|
|
f = open('/dev/null', 'w')
|
|
|
|
self.brm.setProgressLogger(f)
|
|
|
|
self.brm.setWarningLogger(f)
|
|
|
|
except ImportError:
|
2008-10-28 15:48:44 -04:00
|
|
|
self.brm = None
|
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = Python.install
|