Skip to content

Perl Navigator doesn't supply semantic structure for code folding, in VS Code #175

@ferdnyc

Description

@ferdnyc

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.:

  1. The fold region for sub some_func runs from the start of the definition until the '%s:...' format string
  2. The fold region for my %methods = also ends at the long string
  3. The long string becomes a fold region that ends at the end of the function definition
  4. The outdented comment in sub problem_func has the same effect on its fold region(s)
  5. The outdented string in sub probem_func interrupts the $out_text = region, but not the one for the comment (which cut off the regions for sub problem_func and my %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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions