parent
5e648b2b39
commit
78cf08ec88
54
mode/perl.py
54
mode/perl.py
|
@ -493,51 +493,41 @@ class PerlWrapParagraph(method.WrapParagraph):
|
|||
|
||||
class PerlSemanticComplete(method.introspect.TokenComplete):
|
||||
_mini_prompt = 'Semantic Complete'
|
||||
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):
|
||||
def _min_completion(self, w, x1, x2, y):
|
||||
a = w.application
|
||||
a.methods['iperl-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())
|
||||
line = w.buffer.lines[y]
|
||||
candidates = b.readline_completions(x1, x2, line)
|
||||
if not candidates:
|
||||
return ([], 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)
|
||||
i = 0
|
||||
while i < len(candidates[0]):
|
||||
for s in candidates:
|
||||
if len(s) <= i or s[i] != candidates[0][i]:
|
||||
break
|
||||
i += 1
|
||||
return (candidates, candidates[0][:i])
|
||||
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")
|
||||
b = w.buffer
|
||||
x2, y = w.logical_cursor().xy()
|
||||
if y >= len(b.lines):
|
||||
return
|
||||
|
||||
(candidates, result) = self._min_completion(w, t)
|
||||
x1 = x2
|
||||
while x1 > 0 and b.lines[y][x1 - 1] in "$@%*&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_:":
|
||||
x1 -= 1
|
||||
assert x1 >= 0, "%r %r %r" % (x1, x2, len(b.lines[y]))
|
||||
|
||||
(candidates, result) = self._min_completion(w, x1, x2, y)
|
||||
|
||||
if candidates:
|
||||
p1 = Point(t.x, t.y)
|
||||
p2 = Point(t.end_x(), t.y)
|
||||
p1 = Point(x1, y)
|
||||
p2 = Point(x2, y)
|
||||
w.buffer.delete(p1, p2)
|
||||
w.insert_string(p1, result)
|
||||
|
||||
|
|
24
tools/iperl
24
tools/iperl
|
@ -72,6 +72,7 @@ sub readline_complete {
|
|||
my ($word, $line, $x) = @_;
|
||||
my $full_word = readline_full_word($word, $line, $x);
|
||||
my $delta = length($full_word) - length($word);
|
||||
#print STDERR "XYZ '$word' '$full_word' '$delta'\n";
|
||||
my @candidates = complete($full_word);
|
||||
return map { substr($_, $delta) } @candidates;
|
||||
}
|
||||
|
@ -131,8 +132,9 @@ sub sigil_complete {
|
|||
return @names;
|
||||
}
|
||||
sub pkg_complete {
|
||||
my ($pkg, $sep, $name) = @_;
|
||||
my @pkgs = _pkg_complete($pkg, $sep, $name, !($pkg && $sep));
|
||||
my ($pkg, $sep, $name, $returnself) = @_;
|
||||
$returnself = defined($returnself) ? $returnself : !($pkg && $sep);
|
||||
my @pkgs = _pkg_complete($pkg, $sep, $name, $returnself);
|
||||
my $base = $pkg . $sep . $name;
|
||||
return grep {$_ =~ m/^$base/ } @pkgs;
|
||||
}
|
||||
|
@ -179,12 +181,13 @@ sub complete {
|
|||
} elsif($word =~ m/^${hash_re}(.*)$/) {
|
||||
# literal hash
|
||||
my ($hash, $key) = ($1, $2);
|
||||
#print STDERR "\n$hash|$key\n";
|
||||
my $obj = eval("\\\%$hash");
|
||||
if($obj) {
|
||||
@words = map { "\$${hash}{$_" } grep { $_ =~ m/^$key/ } keys(%$obj);
|
||||
my @keys = keys(%$obj);
|
||||
unless(@keys) {
|
||||
$obj = eval("\\\%${PKG}::${hash}");
|
||||
@keys = keys(%$obj);
|
||||
}
|
||||
#print STDERR Dumper(\@words);
|
||||
@words = map { "\$${hash}{$_" } grep { $_ =~ m/^$key/ } @keys;
|
||||
} elsif($word =~ m/^${hash_ref_re}(.*)$/) {
|
||||
# hashref
|
||||
my ($hash, $key) = ($1, $2);
|
||||
|
@ -211,12 +214,14 @@ sub complete {
|
|||
@words = sigil_complete($sigil, $sigil, $pkg, $sep, $name);
|
||||
} else {
|
||||
push(@words, pkg_complete($pkg, $sep, $name));
|
||||
push(@words, pkg_complete($name, '', '', 0)) unless $oldpkg;
|
||||
push(@words, sigil_complete('&', '', $pkg, $sep, $name));
|
||||
}
|
||||
if($oldpkg ne $pkg) {
|
||||
@words = map { $_ =~ s/^${pkg}${sep}//; $_ } @words;
|
||||
@words = map { $_ =~ s/${pkg}${sep}//; $_ } @words;
|
||||
}
|
||||
}
|
||||
#print STDERR "XYZ" . join("|", @words) . "\n";
|
||||
return sort(@words);
|
||||
}
|
||||
|
||||
|
@ -341,7 +346,8 @@ sub run {
|
|||
my $x = $2;
|
||||
my $line = $3;
|
||||
my $word = substr($line, $1, $x - $1);
|
||||
my @candidates = readline_complete($line, $word, $x);
|
||||
#print STDERR "ABC '$1' '$2' '$3' '$word'\n";
|
||||
my @candidates = readline_complete($word, $line, $x);
|
||||
draw_completions(@candidates);
|
||||
next;
|
||||
} else {
|
||||
|
@ -387,7 +393,7 @@ sub run {
|
|||
} else {
|
||||
# we're dealing with a complete statement, so execute and display
|
||||
$input .= $line;
|
||||
my @results = map { repr($_) } eval($input);
|
||||
my @results = map { repr($_) } eval("package $PKG; $input");
|
||||
if($@) {
|
||||
print $@;
|
||||
} elsif(scalar(@results) == 0) {
|
||||
|
|
Loading…
Reference in New Issue