Skip to content

Commit

Permalink
Added atomic ops, improved example.
Browse files Browse the repository at this point in the history
  • Loading branch information
bduggan committed Jan 23, 2018
1 parent cda6537 commit 9a96198
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -112,6 +112,8 @@ alias iperl6='jupyter-console --kernel=perl6'

* autocompleting ` **` or a superscript will give you superscripts (for typing exponents).

* 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).

* 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
58 changes: 40 additions & 18 deletions eg/hello-world.ipynb
Expand Up @@ -49,7 +49,7 @@
{
"data": {
"text/plain": [
"-1+1.22464679914735e-16i"
"170141183460469231731687303715884105727"
]
},
"execution_count": 2,
Expand All @@ -58,7 +58,8 @@
}
],
"source": [
"e ** (i * π)"
"# To type this, you can type '2 **' and then press tab to help type exponents.\n",
"my $n = 2¹²⁷ - 1;"
]
},
{
Expand All @@ -69,7 +70,7 @@
{
"data": {
"text/plain": [
"[...]"
"True"
]
},
"execution_count": 3,
Expand All @@ -78,7 +79,8 @@
}
],
"source": [
"my @fib = 1, 1, * + * ... ∞"
"# _ refers to the last output\n",
"is-prime( _ )"
]
},
{
Expand All @@ -89,7 +91,7 @@
{
"data": {
"text/plain": [
"(1 1 2 3 5 8 13 21 34 55 89)"
"127"
]
},
"execution_count": 4,
Expand All @@ -98,25 +100,18 @@
}
],
"source": [
"@fib[0..10]"
"( Out[2] + 1).log(2); # you can also refer to Out[...]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here is some output\n"
]
},
{
"data": {
"text/plain": [
"42"
"[...]"
]
},
"execution_count": 5,
Expand All @@ -125,8 +120,7 @@
}
],
"source": [
"say \"here is some output\";\n",
"42"
"my @fib = 1, 1, * + * ... ∞"
]
},
{
Expand All @@ -137,7 +131,7 @@
{
"data": {
"text/plain": [
"(1 2 4 8 16 32 64 128 256 512)"
"(1 1 2 3 5 8 13 21 34 55 89)"
]
},
"execution_count": 6,
Expand All @@ -146,7 +140,35 @@
}
],
"source": [
"1,2,4...1000"
"@fib[0..10]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"this goes to stdout\n"
]
},
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"say \"this goes to stdout\";\n",
"42 # this is an output expression"
]
}
],
Expand Down
7 changes: 7 additions & 0 deletions lib/Jupyter/Kernel/Sandbox/Autocomplete.pm6
Expand Up @@ -12,6 +12,7 @@ constant equality-operators = << ≠ ≅ == != <=> =:= === =~= >>;
constant less-than-operators = << < ≤ <= >>;
constant greater-than-operators = << > ≥ >= >>;
constant superscripts = <⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ⁱ ⁺ ⁻ ⁼ ⁽ ⁾ ⁿ>;
constant atomic-operators = <⚛= ⚛ ++⚛ ⚛++ --⚛ ⚛-- ⚛+= ⚛-= ⚛−=>;

method complete-ops($str, $next = '') {
return unless $str.chars > 0;
Expand Down Expand Up @@ -67,6 +68,12 @@ method complete($str,$cursor-pos,$sandbox) {
my ($prefix,$last) = extract-last-word($str.substr(0,$cursor-pos));
my $identifier = add-sigil-or-twigil($prefix, $last);

# Special handling for atomic (subs that are operators).
if $last eq 'atomic' {
return $cursor-pos - 'atomic'.chars,
$cursor-pos, atomic-operators;
}

with self.complete-syntactic( $prefix, $str.substr($cursor-pos)) -> $got {
my ($start, $end, $completions) = @$got;
return $prefix.chars-$start, $str.chars + $end, $completions;
Expand Down
8 changes: 7 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 8;
plan 10;

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

Expand All @@ -30,4 +30,10 @@ my $c = Jupyter::Kernel::Sandbox::Autocomplete.new;
is $c.complete-ops('*'), (0, << * × >>), 'multiplication';
is $c.complete-ops('<'), (0, << < ≤ <= >>), 'less than';

{
my ($pos,$offset,$atomic) = $c.complete('$a atomic','$a atomic'.chars,Nil);
ok @$atomic > 0, 'got some atomic ops';
ok <⚛= ⚛> $atomic, 'atomic ops contains ⚛= ⚛';
}

# vim: syn=perl6

0 comments on commit 9a96198

Please sign in to comment.