driade / phpfmt8

PHP formatter for Sublime Text 4, with PHP 8 support.
BSD 3-Clause "New" or "Revised" License
46 stars 2 forks source link

Unindenting the break; in the switch statement #45

Closed BaiyesheaMichael closed 1 year ago

BaiyesheaMichael commented 1 year ago

I just installed the phpfmt on my latest update of the Sublime Text and I appreciate the developers for the work done thus far.

Some of my switch cases are like this:

switch ($action_to_perform) {
    case 'save_dataload':
        // Code for 'save_dataload' case
        break;
}

but I want to achieve this:

switch ($action_to_perform) {
case 'save_dataload':
    // Code for 'save_dataload' case
break;
}

I didn't find a settings to achieve this, so chat-GPT gave me a custom function to add to C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt

<?php

use Fmt\AdditionalPass;

class UnindentSwitchCaseBreak extends AdditionalPass
{
    public function candidate($source, $foundTokens)
    {
        // Check if the source code contains switch statements
        return (bool) strpos($source, 'switch');
    }

    public function format($source)
    {
        // Remove indentation from case and break statements
        $source = preg_replace('/\n(\s*)(case|break)/', "\n$1$2", $source);

        return $source;
    }
}

and I added UnindentSwitchCaseBreak to my [passes section], but it throws this error

[08-Aug-2023 11:00:31 Europe/Berlin] PHP Warning:  Undefined array key "_" in C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt\fmt.stub.php on line 3397
[08-Aug-2023 11:00:31 Europe/Berlin] PHP Notice:  fwrite(): Write of 56371 bytes failed with errno=32 Broken pipe in C:\Users\Michael\AppData\Roaming\Sublime Text\Packages\phpfmt\fmt.stub.php on line 3412

This is my settings file

{
    "excludes":
    [
        "PSR2ModifierVisibilityStaticOrder"
    ],
    "passes":
    [
        "UnindentSwitchCaseBreak",
    ],
    "php_bin": "C:/xampp/php/php.exe",

    "switch_case_space" : 1,
}

I am on a windows 11 machine running PHP 8.1.2

Pls help

driade commented 1 year ago

Hi. I'm sorry but external formatters are not currently supported.

driade commented 1 year ago

We'll have ti check if it's possible in the future.