pmacs3/method/introspect.py

150 lines
5.2 KiB
Python

import os, commands, re, sets, tempfile
from subprocess import Popen, PIPE, STDOUT
import buffer, default, dirutil, regex, util, window
from point import Point
from method import DATATYPES, Method, Argument
class DumpRegions(Method):
'''debug region highlighting'''
def _execute(self, w, **vargs):
lines = []
for hr in w.application.highlighted_ranges:
(w, p1, p2, fg, bg) = hr
lines.append("%r %s %s" % (w, p1, p2))
output = "\n".join(lines)
w.application.data_buffer("region-dump", output, switch_to=True)
class DumpMarkers(Method):
'''Dump all tab markers (tab debugging)'''
def _execute(self, w, **vargs):
lines = []
if w.mode.tabber:
keys = w.mode.tabber.lines.keys()
keys.sort()
for i in keys:
line = w.mode.tabber.lines[i]
lines.append("LINE %d: %r" % (i, line))
lines.append(" %s" % repr(w.mode.tabber.record[i]))
else:
lines.append("no tokens")
output = "\n".join(lines)
w.application.data_buffer("marker-dump", output, switch_to=True)
class DumpTokens(Method):
'''Dump all lexical tokens (syntax highlighting debugging)'''
def _execute(self, w, **vargs):
modename = w.mode.name()
lines = []
if modename in w.buffer.highlights:
tokens = w.buffer.highlights[modename].tokens
for i in range(0, len(tokens)):
lines.append("LINE %d" % i)
group = tokens[i]
for token in group:
fqname = token.fqname()
p1 = Point(token.x, token.y)
if token.parent is None:
pcoord = ''
else:
pcoord = '[%d, %d]' % (token.parent.x, token.parent.y)
if fqname in w.mode.ghist and p1 in w.mode.ghist[fqname]:
g = '[' + w.mode.ghist[fqname][p1].name() + ']'
else:
g = ''
fields = (str(p1), pcoord, token.fqname(), g, token.string)
lines.append(' %-10s %-10s %-20s %-10s %r' % fields)
else:
lines.append("no tokens")
output = "\n".join(lines)
w.application.data_buffer("token-dump", output, switch_to=True)
class GetToken(Method):
'''View type and data of the "current" token'''
def _execute(self, w, **vargs):
token = w.get_token()
if token is None:
w.set_error('No Token')
else:
w.set_error('Token: %r (%s)' % (token.string, token.fqname()))
class TokenComplete(Method):
'''Complete token names based on other tokens in the buffer'''
name_overrides = {}
def _min_completion(self, w, t):
h = w.get_highlighter()
minlen = None
if t.name in self.name_overrides:
ok = name_overrides[t.name]
else:
ok = (t.name,)
strings = {}
for line in h.tokens:
for t2 in line:
if t2 is t:
continue
elif False and t2.name not in ok:
continue
elif t2.string.startswith(t.string):
strings[t2.string] = 1
if minlen is None:
minlen = len(t2.string)
else:
minlen = min(minlen, len(t2.string))
strings = strings.keys()
if not strings:
return ([], t.string)
i = len(t.string)
while i < minlen:
c = strings[0][i]
for s in strings:
if s[i] != c:
return (strings, strings[0][:i])
i += 1
return (strings, strings[0][:minlen])
def _execute(self, w, **vargs):
t = w.get_token2()
if t is None:
w.set_error("No token to complete!")
return
elif regex.reserved_token_names.match(t.name):
w.set_error("Will not complete reserved token")
return
(candidates, result) = self._min_completion(w, t)
if candidates:
p1 = Point(t.x, t.y)
p2 = Point(t.end_x(), t.y)
w.buffer.delete(p1, p2)
w.insert_string(p1, result)
if not candidates:
w.set_error("No completion: %r" % result)
elif len(candidates) == 1:
w.set_error("Unique completion: %r" % result)
elif result in candidates:
w.set_error("Ambiguous completion: %r" % candidates)
else:
w.set_error("Partial completion: %r" % candidates)
class OpenConsole(Method):
'''Evaluate python expressions (for advanced use and debugging only)'''
def execute(self, w, **vargs):
a = w.application
if not a.has_buffer_name('*Console*'):
b = buffer.ConsoleBuffer()
a.add_buffer(b)
window.Window(b, a)
b = a.bufferlist.get_buffer_by_name('*Console*')
if a.window().buffer is not b:
a.switch_buffer(b)
f = lambda x: None
w.application.open_mini_buffer('>>> ', f, self, None, 'consolemini')