iperl mode tab completion

--HG--
branch : pmacs2
This commit is contained in:
moculus 2008-05-20 13:54:19 +00:00
parent 2712aa11e1
commit e382dcf8bb
2 changed files with 35 additions and 26 deletions

View File

@ -35,34 +35,30 @@ class IperlTab(method.Method):
a = w.application
s = w.buffer.make_string()
x = w.logical_cursor().x
if not s or s[:x].isspace():
x2 = w.logical_cursor().x
if not s or s[:x2].isspace():
w.insert_string_at_cursor(' ' * w.mode.tabwidth)
return
l = lex.Lexer(w.mode, PerlGrammar)
tokens = list(l.lex([s]))
r = re.compile('^[a-zA-Z0-9_:$@*&%]$')
line = s
x1 = x2
while x1 > 0 and r.match(s[x1 - 1]):
x1 -= 1
word = line[x1:x2]
t = None
for t2 in tokens:
if t2.x < x and t2.end_x() >= x:
t = t2
break
if t is None:
return
s = t.string
w.mode.pipe.stdin.write("COMPLETE:%s\n" % s)
w.mode.pipe.stdin.write("COMPLETE2:%d:%d:%s\n" % (x1, x2, s))
w.mode.pipe.stdin.flush()
(typ_, value) = w.mode._readline()
assert typ_ == 'COMPLETIONS'
assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value)
candidates = [x for x in value.split('|') if x]
w.mode._read()
if candidates:
s = completer.find_common_string(candidates)
w.buffer.delete(Point(t.x, 0), Point(t.end_x(), 0), force=True)
#w.buffer.delete(Point(t.x, 0), Point(t.end_x(), 0), force=True)
w.buffer.delete(Point(x1, 0), Point(x2, 0), force=True)
w.insert_string_at_cursor(s)
mode.mini.use_completion_window(a, s, candidates)
@ -108,8 +104,6 @@ class IperlMini(mode.Fundamental):
if type_ == 'PROMPT':
self.window.application.set_mini_buffer_prompt(value + ' ')
break
if type_ in ('ERROR', 'RESULT'):
value = value.replace('\\n', '\n').replace('\\\\', '\\')
value.rstrip()
if value:
output.append(value)

View File

@ -150,13 +150,16 @@ sub resolve_ref {
# use sigilfind to get completions for particular word(s)
sub complete {
my ($word, $line, $x1, $x2) = @_;
my ($word, $line, $x) = @_;
#print STDERR "'$word' '$line' $x\n";
$word =~ m/^([&\$%@\*]?)(.*)$/;
my $sigil = $1;
my $name = $2;
my $pre = substr($line, 0, $x1);
my $pre = substr($line, 0, $x);
if(length($line) == $x1 && $pre =~ m/^[ \t]*$/) {
if(length($line) == $x && $pre =~ m/^[ \t]*$/) {
# hack to handle the case where we really do want a tab
return ("\t");
}
@ -200,6 +203,8 @@ sub complete {
# completions.
@candidates = map {
"${sigil}${_}"
} grep {
$_ =~ m/^$name/
} sigilread($pkgname, $sigil);
} else {
# if we don't have a sigil, the user might wanna call a function,
@ -218,7 +223,7 @@ sub complete {
# display completions to the user
sub draw_completions {
my (@items) = @_;
my($prefix, $delim) = $pipe ? ("COMPLETIONS", "|") : ("", "\n");
my($prefix, $delim) = $pipe ? ("COMPLETIONS:", "|") : ("", "\n");
print $prefix . join($delim, @items) . "\n";
}
@ -307,13 +312,23 @@ sub main {
# if we're in pipe-mode then we expect input in a special form
if($pipe) {
if($line =~ m/ENTER:(.*)$/) {
if($line =~ m/^ENTER:(.*)$/) {
$line = $1;
} elsif($line =~ m/COMPLETE:(.*)$/) {
draw_completions(complete($1));
} elsif($line =~ m/^COMPLETE:(.*)$/) {
my $line = $1;
while($line =~ m/\G(?<=[^a-zA-Z0-9_])[a-zA-Z0-9_]/g) {}
my $x = pos($line) ? pos($line) : 0;
my $word = substr($line, $x);
draw_completions(complete($line, $word, $x));
next;
} elsif($line =~ m/^COMPLETE2:(\d+):(\d+):(.*)$/) {
my $x = $2;
my $line = $3;
my $word = substr($line, $1, $x - $1);
draw_completions(complete($line, $word, $x));
next;
} else {
print "malformed pipe input line\n";
print "malformed pipe input line: $line\n";
next;
}
}