pmacs3/method/introspect.py

157 lines
5.7 KiB
Python
Raw Normal View History

import os, commands, re, sets, tempfile
from subprocess import Popen, PIPE, STDOUT
import buffer, default, dirutil, regex, util, window
2008-05-29 16:35:21 -04:00
import mode.mini
from point import Point
from method import DATATYPES, Method, Argument
2008-05-12 18:12:56 -04:00
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]
2008-05-12 18:12:56 -04:00
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)):
2008-05-12 18:12:56 -04:00
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'''
2008-05-28 18:13:40 -04:00
def _prune_candidates(self, t, minlen, candidates):
if not candidates:
return ([], t.string)
i = len(t.string)
while i < minlen:
c = candidates[0][i]
for s in candidates:
if s[i] != c:
return (candidates, candidates[0][:i])
i += 1
return (candidates, candidates[0][:minlen])
def _min_completion(self, w, t):
h = w.get_highlighter()
minlen = None
2008-05-28 18:13:40 -04:00
candidates = {}
for line in h.tokens:
for t2 in line:
if t2 is t:
continue
2008-05-28 18:13:40 -04:00
elif False and t2.name != t.name:
continue
elif t2.string.startswith(t.string):
2008-05-28 18:13:40 -04:00
candidates[t2.string] = 1
if minlen is None:
minlen = len(t2.string)
else:
minlen = min(minlen, len(t2.string))
2008-05-28 18:13:40 -04:00
return self._prune_candidates(t, minlen, candidates.keys())
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)
2008-05-29 16:35:21 -04:00
#mode.mini.use_completion_window(w.application, result, candidates)
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')