-
Notifications
You must be signed in to change notification settings - Fork 49
Description
Consider this Perl code. (And please ignore the poor quality and nonsense logic, it's not meant to ever be executed.):
#!/usr/bin/perl
use warnings;
use strict;
sub some_func {
my $varA = 12;
my $prefix = "a list of words";
my @words = ["apple", "banana", "carrot", "date", "eggplant", "fig"];
my %methods = {
make_string => sub {
my $out_text;
if ($varA == 12) {
$out_text = "No.";
} else {
$out_text =
sprintf
'%s: first word: "%s", second word: "%s", third word: "%s", fourth word: "%s", fifth word: "%s", sixth word: "%s"',
$prefix, $words[0], $words[1], $words[2],
$words[3], $words[4], $words[5];
}
return $out_text;
}
};
my $out = %methods->make_string();
}
sub other_func {
# Do nothing
return;
}
sub problem_func {
my $prefix = "a list of words";
my @words = ["apple", "banana", "carrot", "date", "eggplant"];
my %methods = {
make_string => sub {
# This will be a problem
my $out_text;
$out_text =
sprintf
'%s: first word: %s, second word: %s, third word: %s, fourth word: %s, fifth word: %s, sixth word: %s',
$prefix, $words[0], $words[1], $words[2],
$words[3], $words[4], $words[5];
return $out_text;
}
}
}When loaded into VS Code, even with Perl Navigator active, the outdented elements (the long strings, the comment inside sub problem_func) will interrupt fold regions in effect at the time they're encountered, i.e.:
- The fold region for
sub some_funcruns from the start of the definition until the'%s:...'format string - The fold region for
my %methods =also ends at the long string - The long string becomes a fold region that ends at the end of the function definition
- The outdented comment in
sub problem_funchas the same effect on its fold region(s) - The outdented string in
sub probem_funcinterrupts the$out_text =region, but not the one for the comment (which cut off the regions forsub problem_funcandmy %methods =).
Python comparison
For purposes of comparison, consider this equivalent Python code:
#!/usr/bin/python3
def some_func():
"""Returns a string."""
varA = 12
prefix = "a list of words"
words = ["apple", "banana", "carrot", "date", "eggplant", "fig"]
def make_string():
out_text: str
if varA == 12:
out_text = "No."
else:
out_text = (
'%s: first word: %s, second word: %s, third word: %s, fourth word: %s, fifth word: %s, sixth word: %s'
% (prefix, *words)
)
return out_text
out = make_string()
return out
def other_func():
"""Does nothing."""
pass
def problem_func():
"""Does nothign, in a complicated way."""
prefix = "a list of words"
words = ["apple", "banana", "carrot", "date", "eggplant", "fig"]
def make_string():
# This won't be a problem
out_text: str
out_text = (
'%s: first word: %s, second word: %s, third word: %s, fourth word: %s, fifth word: %s, sixth word: %s'
% (prefix, *words)
)
return out_text
out = make_string()With extensions disabled, using only the default VS Code parsing, the Python code will be affected similar to the Perl code. (Folding regions broken by outdents.)
However, if VSCode's Python extensions are loaded, one of them (most likely Pylance) will update the folding regions to correctly follow the semantic structure of the program. (Meaning, each block's region goes to the end of the block, skipping over outdented lines.)
Assuming it's possible, it would be awesome if Perl Navigator did the same.