blib/lib/Term/Fabulous/ThemeEditor/KdlArea.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 156 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::ThemeEditor::KdlArea;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.05';
9  
10 use Object::Pad 0.825;
11  
12 use Term::Fabulous::Widget::TextArea;
13  
14 # The text area of the theme editor's text mode: a code editor for KDL
15 # that keeps Tab for indenting.
16 class Term::Fabulous::ThemeEditor::KdlArea :isa(Term::Fabulous::Widget::TextArea) :strict(params) {
17  
18 # A tab in the text is shown as a blank glyph two columns wide.
19 use constant TAB_GLYPH => "\x{3000}";
20  
21 # Tab and Shift+Tab indent; they do not move the focus (unless the text
22 # cannot be changed, when they move it as everywhere).
23 method captures_tab () {
24 return $self->read_only ? 0 : 1;
25 }
26  
27 method display_cluster :override ($cluster) {
28 return $cluster eq "\t" ? TAB_GLYPH : $self->SUPER::display_cluster($cluster);
29 }
30  
31 # The indentation one level adds: the first indentation of the text,
32 # or a tab.
33 method indent_unit () {
34 foreach my $line ( $self->editor->lines ) {
35 return $1 if $line =~ /\A([ \t]+)\S/;
36 }
37 return "\t";
38 }
39  
40 method handle_key :override ($event) {
41 my $name = $event->main_key_name // '';
42 return $self->SUPER::handle_key($event) if $self->read_only;
43 return $self->_indent_lines(1) if $name eq 'Tab';
44 return $self->_indent_lines(-1) if $name eq 'BackTab';
45 return $self->_new_line if $name eq 'Enter';
46 return $self->_closing_brace if ( $event->text // '' ) eq '}';
47 return $self->apply_edit( $self->editor->delete_word_backward ) if $name eq 'Alt+Backspace';
48 return $self->_copy if $name eq 'Ctrl+C';
49 return 0 if $name eq 'Ctrl+W'; # closes the tab
50 return $self->SUPER::handle_key($event);
51 }
52  
53 method _copy () {
54 my $editor = $self->editor;
55 return 0 unless $editor->has_selection;
56 $editor->copy;
57 return 1;
58 }
59  
60 # A new line with the indentation of the cursor's line, one level more
61 # after an opening brace.
62 method _new_line () {
63 my $editor = $self->editor;
64 my ( $row, $offset ) = $editor->cursor;
65 my $before = substr( $editor->line($row), 0, $offset );
66 my ($indent) = $before =~ /\A([ \t]*)/;
67 $indent .= $self->indent_unit if $before =~ /\{\s*\z/;
68 return $self->apply_edit( $editor->insert("\n$indent") );
69 }
70  
71 # A closing brace typed into the indentation of a line goes one level
72 # back.
73 method _closing_brace () {
74 my $editor = $self->editor;
75 my ( $row, $offset ) = $editor->cursor;
76 my $before = substr( $editor->line($row), 0, $offset );
77 return $self->apply_edit( $editor->type('}') ) if $editor->has_selection || $before !~ /\A[ \t]+\z/;
78 my $unit = $self->indent_unit;
79 my $removed = substr( $before, -length $unit ) eq $unit ? length $unit : 1;
80 $editor->set_selection( $row, $offset - $removed, $row, $offset );
81 return $self->apply_edit( $editor->insert('}') );
82 }
83  
84 # Indents (1) or outdents (-1) the lines of the selection, or the
85 # cursor's line; Tab without a selection inserts one level at the
86 # cursor.
87 method _indent_lines ($direction) {
88 my $editor = $self->editor;
89 return $self->apply_edit( $editor->insert( $self->indent_unit ) ) if $direction > 0 && !$editor->has_selection;
90  
91 my ( $first, $last ) = $editor->has_selection ? ( $editor->selection )[ 0, 2 ] : ( ( $editor->cursor )[0] ) x 2;
92 my @selection = $editor->selection;
93 $last-- if @selection && $last > $first && $selection[3] == 0; # a selection that ends at a line start leaves that line alone
94 my $unit = $self->indent_unit;
95 my @lines;
96 foreach my $row ( $first .. $last ) {
97 my $line = $editor->line($row);
98 if ( $direction > 0 ) { $line = $unit . $line }
99 elsif ( substr( $line, 0, length $unit ) eq $unit ) { substr( $line, 0, length $unit ) = '' }
100 elsif ( $line =~ /\A[ \t]/ ) { substr( $line, 0, 1 ) = '' }
101 push @lines, $line;
102 }
103 my $replacement = join "\n", @lines;
104 return 1 if $replacement eq join "\n", map { $editor->line($_) } $first .. $last;
105  
106 $editor->set_selection( $first, 0, $last, length $editor->line($last) );
107 my $changed = $editor->insert($replacement);
108 $editor->set_selection( $first, 0, $last, length $editor->line($last) );
109 return $self->apply_edit($changed);
110 }
111 }
112  
113 1;
114  
115 __END__