Skip to content

Commit

Permalink
Add autocompletion for arbitrary unicode chars
Browse files Browse the repository at this point in the history
* also added travis_retry
* and documentation
  • Loading branch information
bduggan committed Feb 8, 2018
1 parent 9a96198 commit cff537b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Expand Up @@ -2,12 +2,12 @@ language: perl6
env:
- MVM_SPESH_DISABLE=1 P6_JUPYTER_TEST_AUTOCOMPLETE=1
perl6:
- 2017.08
- 2018.01
before_install:
- wget https://github.com/zeromq/libzmq/releases/download/v4.2.2/zeromq-4.2.2.tar.gz
- tar -xzvf zeromq-4.2.2.tar.gz
- pushd zeromq-4.2.2 && ./configure --prefix=/usr && make && sudo make install && popd
install:
- rakudobrew build zef
- zef install --depsonly .

- travis_retry zef install --depsonly .
sudo: false
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -114,6 +114,11 @@ alias iperl6='jupyter-console --kernel=perl6'

* the word 'atomic' autocompletes to the [atomic operators](https://docs.perl6.org/type/atomicint#Operators). (Use `atomic-` or `atom` to get the subroutines with their ASCII names).

* a colon followed by a sequence of word characters will autocomplete
to characters whose unicode name contains that string. Dashes are
treated as spaces.
e.g. :straw will find 🍓 ("STRAWBERRY") or 🥤 ("CUP WITH STRAW") and :smiling-face-with-smiling-eye will find 😊 ("SMILING FACE WITH SMILING EYES")

* All cells are evaluated in item context. Outputs are then saved to an array
named `$Out`. You can read from this directly or:

Expand Down
17 changes: 17 additions & 0 deletions lib/Jupyter/Kernel/Sandbox/Autocomplete.pm6
Expand Up @@ -104,6 +104,23 @@ method complete($str,$cursor-pos,$sandbox) {
return $prefix.chars, $cursor-pos, $found if $found;
}

# Unicode lookup
my regex uniname { [ \w | '-' ]+ }
if $str ~~ / ':' $<word>=<uniname> $/ {
my $word = "$<word>".fc;
my $alt;
$alt = $word.subst('-',' ', :global) if $word.contains('-');
my @chars = (1 .. 0x1ffff)
.map({.chr})
.grep({
my $u = .uniname.fc;
$u.contains($word)
or ($alt and $u.contains(' ') and $u.subst('-',' ', :g).contains($alt))
}).head(10);
my $pos = $str.chars - $word.chars - 1;
return ( $pos, $pos + $word.chars + 1, @chars ) if @chars;
}

my @possible = CORE::.keys.grep({ /^ '&' / }).map( { .subst(/ ^ '&' /, '') } );
return $prefix.chars, $cursor-pos, [ @possible.grep( { / ^ "$last" / } ).sort ];
}
24 changes: 23 additions & 1 deletion t/05-autocomplete.t
Expand Up @@ -6,7 +6,7 @@ use Jupyter::Kernel::Sandbox::Autocomplete;

logger.add-tap( -> $msg { diag $msg<msg> } );

plan 10;
plan 17;

my $c = Jupyter::Kernel::Sandbox::Autocomplete.new;

Expand Down Expand Up @@ -36,4 +36,26 @@ is $c.complete-ops('<'), (0, << < ≤ <= >>), 'less than';
ok <⚛= ⚛> $atomic, 'atomic ops contains ⚛= ⚛';
}

{
my ($pos,$end,$beer) = $c.complete('some :beer','some :beer'.chars,Nil);
ok @$beer > 0, 'got some beer';
ok <🍺 🍻> $beer, 'beer contains🍺 and 🍻 ';
is $pos, 5, 'got right start';
is $end, '10', 'got right end';
}
{
my ($pos,$end,$beer) = $c.complete('some :b','some :b'.chars,Nil);
ok $beer.elems10, '10 or fewer results';
}
{
my ($pos,$end,$got) = $c.complete(':less-than',':less-than'.chars,Nil);
ok '' @$got, 'found less-than';
}
{
my ($pos,$end,$got) = $c.complete(':less-than-or-equal',':less-than'.chars,Nil);
ok '' @$got, 'found ≤';
}



# vim: syn=perl6

0 comments on commit cff537b

Please sign in to comment.