File Coverage

lib/Kephra/Edit/Format.pm
Criterion Covered Total %
statement 6 129 4.6
branch 0 24 0.0
condition 0 14 0.0
subroutine 2 22 9.0
pod 0 17 0.0
total 8 206 3.8


line stmt bran cond sub pod time code
1             package Kephra::Edit::Format;
2             our $VERSION = '0.26';
3            
4 1     1   1262 use strict;
  1         2  
  1         35  
5 1     1   6 use warnings;
  1         2  
  1         7884  
6            
7 0     0     sub _ep_ref { Kephra::App::EditPanel::_ref() }
8 0     0     sub _config { Kephra::API::settings()->{editpanel} }
9            
10             # change indention width of selected text
11             sub _indent_selection {
12 0   0 0     my $width = shift || 0;
13 0           my $ep = _ep_ref();
14 0           $ep->BeginUndoAction;
15 0           for ( $ep->LineFromPosition($ep->GetSelectionStart)
16             .. $ep->LineFromPosition($ep->GetSelectionEnd) ) {
17 0 0         $ep->SetLineIndentation( $_, $ep->GetLineIndentation($_) + $width )
18             unless $ep->PositionFromLine($_) == $ep->GetLineEndPosition($_);
19             }
20 0           $ep->EndUndoAction;
21             }
22            
23             sub autoindent {
24 0     0 0   my $ep = _ep_ref();
25 0           my $line = $ep->GetCurrentLine;
26            
27 0           $ep->BeginUndoAction;
28 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_NEWLINE);
29 0           my $indent = $ep->GetLineIndentation( $line );
30 0           $ep->SetLineIndentation( $line + 1, $indent);
31 0           $ep->GotoPos( $ep->GetLineIndentPosition( $line + 1 ) );
32 0           $ep->EndUndoAction;
33             }
34            
35             sub blockindent_open {
36 0     0 0   my $ep = _ep_ref();
37 0           my $tabsize = Kephra::Document::Data::attr('tab_size');
38 0           my $line = $ep->GetCurrentLine;
39 0           my $first_cpos = $ep->PositionFromLine($line)
40             + $ep->GetLineIndentation($line); # position of first char in line
41 0           my $matchfirst = $ep->BraceMatch($first_cpos);
42            
43 0           $ep->BeginUndoAction;
44            
45             # dedent a "} else {" correct
46 0 0 0       if ($ep->GetCharAt($first_cpos) == 125 and $matchfirst > -1) {
47 0           $ep->SetLineIndentation( $line, $ep->GetLineIndentation(
48             $ep->LineFromPosition($matchfirst) ) );
49             }
50             # grabbing
51 0           my $bracepos = $ep->GetCurrentPos - 1;
52 0           my $leadindent = $ep->GetLineIndentation($line);
53 0           my $matchbrace = $ep->BraceMatch( $bracepos );
54 0           my $matchindent= $ep->GetLineIndentation($ep->LineFromPosition($matchbrace));
55            
56             # make newl line
57 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_NEWLINE);
58            
59             # make new brace if there is missing one
60 0 0 0       if (_config()->{auto}{brace}{make} and
      0        
61             ($matchbrace == -1 or $ep->GetLineIndentation($line) != $matchindent )){
62 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_NEWLINE);
63 0           $ep->AddText('}');
64 0           $ep->SetLineIndentation( $line + 2, $leadindent );
65             }
66 0           $ep->SetLineIndentation( $line + 1, $leadindent + $tabsize );
67 0           $ep->GotoPos( $ep->GetLineIndentPosition( $line + 1 ) );
68            
69 0           $ep->EndUndoAction;
70             }
71            
72             sub blockindent_close {
73 0     0 0   my $ep = _ep_ref();
74 0           my $bracepos = shift;
75 0 0         unless ($bracepos) {
76 0           $bracepos = $ep->GetCurrentPos - 1;
77 0           $bracepos-- while $ep->GetCharAt($bracepos) == 32;
78             }
79            
80 0           $ep->BeginUndoAction;
81            
82             # 1 if it not textend, goto next line
83 0           my $match = $ep->BraceMatch($bracepos);
84 0           my $line = $ep->GetCurrentLine;
85 0 0 0       unless ($ep->GetLineIndentPosition($line)+1 == $ep->GetLineEndPosition($line)
86             or $ep->LineFromPosition($match) == $line ) {
87 0           $ep->GotoPos($bracepos);
88 0           $ep->CmdKeyExecute(&Wx::wxSTC_CMD_NEWLINE);
89 0           $ep->GotoPos( $ep->GetCurrentPos + 1 );
90 0           $line++;
91             }
92            
93             # 2 wenn match dann korrigiere einrückung ansonst letzte - tabsize
94 0 0         if ( $match > -1 ) {
95 0           $ep->SetLineIndentation( $line,
96             $ep->GetLineIndentation( $ep->LineFromPosition($match) ) );
97             } else {
98 0           $ep->SetLineIndentation( $line,
99             $ep->GetLineIndentation( $line - 1 )
100             - Kephra::Document::Data::attr('tab_size') );
101             }
102            
103             # make new line
104 0 0         _config()->{auto}{indent}
105             ? autoindent()
106             : $ep->CmdKeyExecute(&Wx::wxSTC_CMD_NEWLINE);
107            
108             # 3 lösche dubs wenn in nächster zeile nur spaces bis dup
109             #if ( _config()->{auto}{brace}{join} ) {
110             #my $delbrace = $ep->PositionFromLine( $line + 2 )
111             #+ $ep->GetLineIndentation( $line + 1 );
112             #if ( $ep->GetCharAt($delbrace) == 125 ) {
113             #$ep->SetTargetStart( $ep->GetCurrentPos );
114             #$ep->SetTargetEnd( $delbrace + 1 );
115             #$ep->ReplaceTarget('');
116             #}
117             #}
118            
119 0           $ep->EndUndoAction;
120             }
121            
122 0     0 0   sub indent_space { _indent_selection( 1) }
123 0     0 0   sub dedent_space { _indent_selection(-1) }
124 0     0 0   sub indent_tab { _indent_selection( Kephra::Document::Data::attr('tab_size') ) }
125 0     0 0   sub dedent_tab { _indent_selection(-Kephra::Document::Data::attr('tab_size') ) }
126            
127             #
128             sub align_indent {
129 0     0 0   my $ep = _ep_ref();
130 0           my $firstline = $ep->LineFromPosition( $ep->GetSelectionStart );
131 0           my $align = $ep->GetLineIndentation($firstline);
132 0           $ep->BeginUndoAction();
133             $ep->SetLineIndentation($_ ,$align)
134 0           for $firstline + 1 .. $ep->LineFromPosition($ep->GetSelectionEnd);
135 0           $ep->EndUndoAction();
136             }
137            
138             # deleting trailing spaces on line ends
139             sub del_trailing_spaces {
140 0     0 0   &Kephra::Edit::_save_positions;
141 0           my $ep = _ep_ref();
142 0           my $text = Kephra::Edit::_select_all_if_none();
143 0           $text =~ s/[ \t]+(\r|\n|\Z)/$1/g;
144 0           $ep->BeginUndoAction;
145 0           $ep->ReplaceSelection($text);
146 0           $ep->EndUndoAction;
147 0           Kephra::Edit::_restore_positions();
148             }
149            
150             #
151             sub join_lines {
152 0     0 0   my $ep = _ep_ref();
153 0           my $text = $ep->GetSelectedText();
154 0           $text =~ s/[\r|\n]+/ /g; # delete end of line marker
155 0           $ep->BeginUndoAction;
156 0           $ep->ReplaceSelection($text);
157 0           $ep->EndUndoAction;
158             }
159            
160             sub blockformat{
161 0 0   0 0   return unless Scalar::Util::looks_like_number($_[0]);
162 0           my $width = (int shift) + 1;
163 0           my $ep = _ep_ref();
164 0           my ($begin, $end) = $ep->GetSelection;
165 0           my $bline = $ep->LineFromPosition($begin);
166 0           my $tmp_begin = $ep->PositionFromLine($bline);
167 0           my $bspace = ' ' x $ep->GetLineIndentation($bline);
168 0 0         my $space = _config()->{auto}{indention} ? $bspace : '';
169 0           chop $bspace;
170            
171 0           $ep->SetSelection($tmp_begin, $end);
172 0           require Text::Wrap;
173 0           $Text::Wrap::columns = $width;
174 0           $Text::Wrap::unexpand = Kephra::Document::Data::attr('tab_use');
175 0           $Text::Wrap::tabstop = Kephra::Document::Data::attr('tab_size');
176            
177 0           my $text = $ep->GetSelectedText;
178 0           $text =~ s/[\r|\n]+/ /g;
179 0           $ep->BeginUndoAction();
180 0           $ep->ReplaceSelection( Text::Wrap::fill($bspace, $space, $text) );
181 0           $ep->EndUndoAction();
182             }
183            
184             sub blockformat_LLI{
185 0     0 0   blockformat( _config()->{indicator}{right_margin}{position} );
186             }
187            
188             sub blockformat_custom {
189 0     0 0   my $l18n = Kephra::Config::Localisation::strings()->{dialog}{edit};
190 0           my $width = Kephra::Dialog::get_text(
191             $l18n->{wrap_width_input},
192             $l18n->{wrap_custom_headline}
193             );
194 0 0         blockformat( $width ) if defined $width;
195             }
196            
197            
198            
199             # breaking too long lines into smaller one
200             sub line_break {
201 0 0   0 0   return unless Scalar::Util::looks_like_number($_[0]);
202 0           my $width = (int shift) + 1;
203 0           my $ep = _ep_ref();
204 0           my ($begin, $end)= $ep->GetSelection;
205 0           my $tmp_begin = $ep->LineFromPosition( $ep->PositionFromLine($begin) );
206            
207 0           $ep->SetSelection($tmp_begin, $end);
208 0           require Text::Wrap;
209 0           $Text::Wrap::columns = $width;
210 0           $Text::Wrap::unexpand = Kephra::Document::Data::attr('tab_use');
211 0           $Text::Wrap::tabstop = Kephra::Document::Data::attr('tab_size');
212            
213 0           $ep->BeginUndoAction();
214 0           $ep->ReplaceSelection( Text::Wrap::wrap('', '', $ep->GetSelectedText) );
215 0           $ep->EndUndoAction();
216             }
217            
218             sub linebreak_custom {
219 0     0 0   my $l10n = Kephra::Config::Localisation::strings()->{dialog}{edit};
220 0           my $width = Kephra::Dialog::get_text
221             ($l10n->{wrap_width_input}, $l10n->{wrap_custom_headline} );
222 0 0         line_break( $width ) if defined $width;
223             }
224            
225             sub linebreak_LLI {
226 0     0 0   line_break( _config()->{indicator}{right_margin}{position} );
227             }
228            
229             sub linebreak_window {
230 0     0 0   my $app = Kephra::App::Window::_ref();
231 0           my $ep = _ep_ref();
232 0           my ($width) = $app->GetSizeWH();
233 0           my $pos = $ep->GetColumn( $ep->PositionFromPointClose(100, 67) );
234 0           Kephra::Dialog::msg_box( $pos, '' );
235             #line_break($width);
236             }
237            
238             1;
239             __END__