| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2021-2022 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Object::Pad 0.75 ':experimental(init_expr adjust_params)'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | package Tickit::Widget::Entry::Plugin::Completion 0.02; | ||
| 10 | class Tickit::Widget::Entry::Plugin::Completion | ||
| 11 | :strict(params); | ||
| 12 | |||
| 13 | 1 | ▶ | use feature 'fc'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 0 | ▶ | use Tickit::Widget 0.55; # Can ->set_style on '<Tab>' key |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | |||
| 19 | use Tickit::Utils qw( textwidth ); | ||
| 20 | use List::Util qw( max ); | ||
| 21 | |||
| 22 | use constant PEN_UNDER => Tickit::Pen->new( u => 1 ); | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | C<Tickit::Widget::Entry::Plugin::Completion> - add word-completion logic to a L<Tickit::Widget::Entry> | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | use Tickit::Widget::Entry; | ||
| 31 | use Tickit::Widget::Entry::Plugin::Completion; | ||
| 32 | |||
| 33 | my $entry = Tickit::Widget::Entry->new( ... ); | ||
| 34 | Tickit::Widget::Entry::Plugin::Completion->apply( $entry, | ||
| 35 | words => [ make_words() ], | ||
| 36 | ); | ||
| 37 | |||
| 38 | ... | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This package applies code to a L<Tickit::Widget::Entry> instance to implement | ||
| 43 | word-completion logic while editing. This logic is activated by pressing the | ||
| 44 | C<< <Tab> >> key. | ||
| 45 | |||
| 46 | If the word currently being edited has a unique match in the list of words, | ||
| 47 | then the word is completed entirely, followed by a space. | ||
| 48 | |||
| 49 | If there are multiple words that could complete from the word at the cursor, | ||
| 50 | then a popup menu is presented showing the next available characters or | ||
| 51 | matches. The user can continue typing more characters to narrow down the | ||
| 52 | choice until a unique match is found. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | =head1 METHODS | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | =head2 apply | ||
| 61 | |||
| 62 | Tickit::Widget::Entry::Plugin::Completion->apply( $entry, %params ) | ||
| 63 | |||
| 64 | Applies the plugin code to the given L<Tickit::Widget::Entry> instance. | ||
| 65 | |||
| 66 | The following named parameters are recognised | ||
| 67 | |||
| 68 | =over 4 | ||
| 69 | |||
| 70 | =item gen_words => CODE | ||
| 71 | |||
| 72 | @words = $gen_words->( %args ) | ||
| 73 | |||
| 74 | A CODE reference to a subroutine used to generate the list of words at the | ||
| 75 | current position. It is passed the following name/value pairs to assist it: | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item word => STRING | ||
| 80 | |||
| 81 | The partial word currently being completed. | ||
| 82 | |||
| 83 | =item wordpos => INT | ||
| 84 | |||
| 85 | The position of the beginning of the word, within the line. Will be 0 for the | ||
| 86 | initial word of the line. | ||
| 87 | |||
| 88 | =item entry => C<Tickit::Widget::Entry> | ||
| 89 | |||
| 90 | The underlying entry widget instance. | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =item words => ARRAY | ||
| 95 | |||
| 96 | A shortcut to providing C<gen_words>; a reference to an array containing all | ||
| 97 | the possible words, in no particular order, that are offered for completion. | ||
| 98 | |||
| 99 | =item use_popup => BOOL | ||
| 100 | |||
| 101 | Optional. If false, do not display a popup menu. Defaults to true. | ||
| 102 | |||
| 103 | When this is disabled, the completion logic will apply longest-prefix matching | ||
| 104 | on the set of available words, but will not otherwise display or offer any | ||
| 105 | interactive UI on the list of matches. | ||
| 106 | |||
| 107 | =item ignore_case => BOOL | ||
| 108 | |||
| 109 | Optional. If true, word matching will be performed ignoring case, by using the | ||
| 110 | C</i> regexp flag. Defaults to false. When the completion logic has selected a | ||
| 111 | word to insert, it may change the case of the text already in the buffer to | ||
| 112 | match the completion word. | ||
| 113 | |||
| 114 | =item append_after_word => STRING | ||
| 115 | |||
| 116 | Optional. If set, append this string after a successful unique match. Defaults | ||
| 117 | to a single space. | ||
| 118 | |||
| 119 | =back | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | # Not an instance method | ||
| 124 | sub apply | ||
| 125 | { | ||
| 126 | my $class = shift; | ||
| 127 | my ( $entry, %opts ) = @_; | ||
| 128 | |||
| 129 | my $plugin = $class->new( entry => $entry, %opts ); | ||
| 130 | |||
| 131 | $entry->bind_keys( | ||
| 132 | Tab => sub { $plugin->key_complete }, | ||
| 133 | ); | ||
| 134 | |||
| 135 | # Need to disable style-applied keypress binding so this takes effect | ||
| 136 | $entry->set_style( '<Tab>' => "" ); | ||
| 137 | } | ||
| 138 | |||
| 139 | field $_ignore_case :param //= 0; | ||
| 140 | field $_use_popup :param //= 1; | ||
| 141 | field $_append_after_word :param //= " "; | ||
| 142 | |||
| 143 | field $_gen_words :param = undef; | ||
| 144 | |||
| 145 | ADJUST :params ( | ||
| 146 | :$words = undef, | ||
| 147 | ) { | ||
| 148 | if( $words and !$_gen_words ) { | ||
| 149 | $_gen_words = sub { return $words->@* }; | ||
| 150 | } | ||
| 151 | |||
| 152 | $_gen_words or | ||
| 153 | croak "Require either 'gen_words' or 'words'"; | ||
| 154 | } | ||
| 155 | |||
| 156 | field $_entry :param; | ||
| 157 | |||
| 158 | field $_popup_window; | ||
| 159 | |||
| 160 | method key_complete | ||
| 161 | { | ||
| 162 | my ( $partial ) = substr( $_entry->text, 0, $_entry->position ) =~ m/(\S*)$/; | ||
| 163 | my $plen = length $partial or return 1; | ||
| 164 | |||
| 165 | my $match = $_ignore_case ? qr/^\Q$partial\E/i : qr/^\Q$partial\E/; | ||
| 166 | my @completions = grep { $_ =~ $match } $_gen_words->( | ||
| 167 | word => $partial, | ||
| 168 | wordpos => $_entry->position - $plen, | ||
| 169 | entry => $_entry, | ||
| 170 | ); | ||
| 171 | @completions or return 1; | ||
| 172 | |||
| 173 | # Find the common prefix of all the matches | ||
| 174 | my $add = $completions[0]; | ||
| 175 | foreach my $s ( @completions[1 .. $#completions] ) { | ||
| 176 | my $diffpos = 1; | ||
| 177 | if( $_ignore_case ) { | ||
| 178 | $diffpos++ while fc substr( $add, 0, $diffpos ) eq fc substr( $s, 0, $diffpos ); | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | $diffpos++ while substr( $add, 0, $diffpos ) eq substr( $s, 0, $diffpos ); | ||
| 182 | } | ||
| 183 | |||
| 184 | return 1 if $diffpos == 1; | ||
| 185 | |||
| 186 | $add = substr( $add, 0, $diffpos - 1 ); | ||
| 187 | } | ||
| 188 | |||
| 189 | if( @completions == 1 ) { | ||
| 190 | # No other completions, so we have a complete match | ||
| 191 | $add .= $_append_after_word; | ||
| 192 | } | ||
| 193 | |||
| 194 | $_entry->text_splice( $_entry->position - $plen, $plen, $add ); | ||
| 195 | |||
| 196 | return 1 if @completions < 2; | ||
| 197 | return 1 unless $_use_popup; | ||
| 198 | |||
| 199 | # Split matches on next letter | ||
| 200 | my %next; | ||
| 201 | foreach ( @completions ) { | ||
| 202 | my $l = substr( $_, $plen, 1 ); | ||
| 203 | push @{ $next{$l} }, $_; | ||
| 204 | } | ||
| 205 | |||
| 206 | my @possibles = map { | ||
| 207 | @{ $next{$_} } == 1 ? $next{$_}[0] | ||
| 208 | : substr( $next{$_}[0], 0, $plen + 1 ) . "..." | ||
| 209 | } sort keys %next; | ||
| 210 | |||
| 211 | # Popup above, unless there's no room at which point, go below | ||
| 212 | my $popup_line = ( $_entry->window->abs_top >= @possibles ) ? -(scalar @possibles) : +1; | ||
| 213 | my $popup = $_entry->make_popup_at_cursor( | ||
| 214 | $popup_line, -$plen, | ||
| 215 | scalar @possibles, max( map { textwidth($_) } @possibles ), | ||
| 216 | ); | ||
| 217 | |||
| 218 | # TODO: Some style stuff here | ||
| 219 | $popup->pen->chattrs({ bg => 'green', fg => 'black' }); | ||
| 220 | |||
| 221 | $popup->bind_event( expose => sub ( $win, $, $info, @ ) { | ||
| 222 | my $rb = $info->rb; | ||
| 223 | |||
| 224 | foreach my $line ( 0 .. $#possibles ) { | ||
| 225 | my $str = $possibles[$line]; | ||
| 226 | |||
| 227 | $rb->goto( $line, 0 ); | ||
| 228 | |||
| 229 | $rb->text( substr( $str, 0, $plen + 1 ), PEN_UNDER ); | ||
| 230 | $rb->text( substr( $str, $plen + 1 ) ) if length $str > $plen + 1; | ||
| 231 | $rb->erase_to( $win->cols ); | ||
| 232 | } | ||
| 233 | } ); | ||
| 234 | $popup->bind_event( key => sub ( $win, $, $info, @ ) { | ||
| 235 | my $redo_complete; | ||
| 236 | |||
| 237 | my $str = $info->str; | ||
| 238 | |||
| 239 | if( $info->type eq "text" ) { | ||
| 240 | $_entry->text_splice( $_entry->position, 0, $str ); | ||
| 241 | $redo_complete++; | ||
| 242 | } | ||
| 243 | elsif( $str eq "Backspace" ) { | ||
| 244 | $_entry->text_splice( $_entry->position - 1, 1, "" ); | ||
| 245 | $redo_complete++; | ||
| 246 | } | ||
| 247 | elsif( $str eq "Escape" ) { | ||
| 248 | # OK, just dismiss | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | # TODO: Handle at least Enter, maybe arrows to select? | ||
| 252 | print STDERR "TODO: Unsure how to handle key $str in popup menu\n"; | ||
| 253 | } | ||
| 254 | |||
| 255 | $popup->hide; | ||
| 256 | undef $_popup_window; | ||
| 257 | $_entry->take_focus; | ||
| 258 | |||
| 259 | $self->key_complete if $redo_complete; | ||
| 260 | return 1; | ||
| 261 | } ); | ||
| 262 | $popup->cursor_at( 0, $plen ); | ||
| 263 | $popup->take_focus; | ||
| 264 | |||
| 265 | $popup->show; | ||
| 266 | |||
| 267 | $_popup_window = $popup; | ||
| 268 | |||
| 269 | return 1; | ||
| 270 | } | ||
| 271 | |||
| 272 | =head1 AUTHOR | ||
| 273 | |||
| 274 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | 0x55AA; | ||