pmacs3/method/introspect.py

136 lines
4.9 KiB
Python

import os, commands, re, sets, tempfile
from subprocess import Popen, PIPE, STDOUT
import buffer, completer, default, dirutil, regex, util, window
import mode.mini
from point import Point
from method import DATATYPES, Method, Argument
class DumpContext(Method):
'''debug context'''
def _execute(self, w, **vargs):
lines = []
if w.mode.context:
for i in range(0, len(w.mode.context.namelines)):
lines.append("LINE %d: %r" % (i + 1, repr(w.mode.context.namelines[i])))
else:
lines.append("no context")
output = "\n".join(lines)
w.application.data_buffer("context-dump", output, switch_to=True)
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 + 1, 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 + 1))
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'''
_mini_prompt = 'Token Complete'
def _execute(self, w, **vargs):
self.old_window = w
tabber = completer.TokenCompleter()
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
class Dummy(object): pass
dw = Dummy()
dw.buffer = Dummy()
dw.buffer.method = self
(s2, exists, complete) = tabber.tab_string(t.string, dw)
p1 = Point(t.x, t.y)
p2 = Point(t.end_x(), t.y)
def callback(s):
w.buffer.delete(p1, p2)
w.insert_string(p1, s)
w.application.close_mini_buffer()
if exists and complete:
w.set_error("Unique completion: %r" % s2)
callback(s2)
return
else:
if exists:
pass
w.application.open_mini_buffer("%s: " % self._mini_prompt, callback,
method=self, tabber=tabber,
startvalue=s2)
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')