Skip to content

Commit

Permalink
TASK: Add options to configure PrettyPrinter (#4038)
Browse files Browse the repository at this point in the history
* TASK: Add options to configure PrettyPrinter

Resolves: #3643

* [CI] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
sabbelasichon and actions-user authored Jan 24, 2024
1 parent f853b45 commit f9d831f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 34 deletions.
15 changes: 15 additions & 0 deletions src/Configuration/Typo3Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ final class Typo3Option
* @var string
*/
public const TYPOSCRIPT_INDENT_SIZE = 'typoscript-indent-size';

/**
* @var string
*/
public const TYPOSCRIPT_INDENT_CONDITIONS = 'typoscript-indent-conditions';

/**
* @var string
*/
public const TYPOSCRIPT_WITH_CLOSING_GLOBAL_STATEMENT = 'typoscript-with-closing-global-statement';

/**
* @var string
*/
public const TYPOSCRIPT_WITH_EMPTY_LINE_BREAKS = 'typoscript-with-empty-line-breaks';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\FileProcessor\TypoScript\Factory;

use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\ValueObject\Application\File;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\ValueObject\Indent;

final class PrettyPrinterConfigurationFactory
{
/**
* @readonly
*/
private ParameterProvider $parameterProvider;

public function __construct(ParameterProvider $parameterProvider)
{
$this->parameterProvider = $parameterProvider;
}

public function createPrettyPrinterConfiguration(File $file): PrettyPrinterConfiguration
{
// keep original TypoScript format
$indent = Indent::fromFile($file);

$prettyPrinterConfiguration = PrettyPrinterConfiguration::create();

if ($indent->isSpace()) {
// default indent
$indentation = $this->parameterProvider->provideParameter(
Typo3Option::TYPOSCRIPT_INDENT_SIZE
) ?? $indent->length();
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indentation);
} else {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
}

if ($this->parameterProvider->provideParameter(Typo3Option::TYPOSCRIPT_INDENT_CONDITIONS) ?? false) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withIndentConditions();
}

if ($this->parameterProvider->provideParameter(Typo3Option::TYPOSCRIPT_WITH_CLOSING_GLOBAL_STATEMENT) ?? true) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withClosingGlobalStatement();
}

if ($this->parameterProvider->provideParameter(Typo3Option::TYPOSCRIPT_WITH_EMPTY_LINE_BREAKS) ?? true) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withEmptyLineBreaks();
}

return $prettyPrinterConfiguration;
}
}
37 changes: 11 additions & 26 deletions src/FileProcessor/TypoScript/TypoScriptFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
use Helmich\TypoScriptParser\Parser\ParseError;
use Helmich\TypoScriptParser\Parser\ParserInterface;
use Helmich\TypoScriptParser\Parser\Printer\ASTPrinterInterface;
use Helmich\TypoScriptParser\Parser\Printer\PrettyPrinterConfiguration;
use Helmich\TypoScriptParser\Parser\Traverser\Traverser;
use Helmich\TypoScriptParser\Parser\Traverser\Visitor;
use Helmich\TypoScriptParser\Tokenizer\TokenizerException;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\Console\Output\RectorOutputStyle;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -24,15 +22,14 @@
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Parallel\ValueObject\Bridge;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\ConvertToPhpFileInterface;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\TypoScriptPostRectorInterface;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\TypoScriptRectorInterface;
use Ssch\TYPO3Rector\Contract\Processor\ConfigurableProcessorInterface;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Collector\RemoveTypoScriptStatementCollector;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Factory\PrettyPrinterConfigurationFactory;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Rector\AbstractTypoScriptRector;
use Ssch\TYPO3Rector\Filesystem\FileInfoFactory;
use Ssch\TYPO3Rector\ValueObject\Indent;
use Symfony\Component\Console\Output\BufferedOutput;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -108,13 +105,16 @@ final class TypoScriptFileProcessor implements ConfigurableProcessorInterface
*/
private array $typoScriptPostRectors = [];

private ParameterProvider $parameterProvider;

/**
* @readonly
*/
private FileInfoFactory $fileInfoFactory;

/**
* @readonly
*/
private PrettyPrinterConfigurationFactory $prettyPrinterConfigurationFactory;

/**
* @param TypoScriptRectorInterface[] $typoScriptRectors
* @param TypoScriptPostRectorInterface[] $typoScriptPostRectors
Expand All @@ -128,8 +128,8 @@ public function __construct(
RectorOutputStyle $rectorOutputStyle,
FileDiffFactory $fileDiffFactory,
RemoveTypoScriptStatementCollector $removeTypoScriptStatementCollector,
ParameterProvider $parameterProvider,
FileInfoFactory $fileInfoFactory,
PrettyPrinterConfigurationFactory $prettyPrinterConfigurationFactory,
array $typoScriptRectors = [],
array $typoScriptPostRectors = []
) {
Expand All @@ -143,8 +143,8 @@ public function __construct(
$this->removeTypoScriptStatementCollector = $removeTypoScriptStatementCollector;
$this->typoScriptRectors = $typoScriptRectors;
$this->typoScriptPostRectors = $typoScriptPostRectors;
$this->parameterProvider = $parameterProvider;
$this->fileInfoFactory = $fileInfoFactory;
$this->prettyPrinterConfigurationFactory = $prettyPrinterConfigurationFactory;
}

public function supports(File $file, Configuration $configuration): bool
Expand Down Expand Up @@ -216,24 +216,9 @@ private function processFile(File $file): void
return;
}

// keep original TypoScript format
$indent = Indent::fromFile($file);

$prettyPrinterConfiguration = PrettyPrinterConfiguration::create();
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withEmptyLineBreaks();

if ($indent->isSpace()) {
// default indent
$indentation = $this->parameterProvider->provideParameter(
Typo3Option::TYPOSCRIPT_INDENT_SIZE
) ?? $indent->length();
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indentation);
} else {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
}

$prettyPrinterConfiguration = $prettyPrinterConfiguration->withClosingGlobalStatement();
$this->typoscriptPrinter->setPrettyPrinterConfiguration($prettyPrinterConfiguration);
$this->typoscriptPrinter->setPrettyPrinterConfiguration(
$this->prettyPrinterConfigurationFactory->createPrettyPrinterConfiguration($file)
);

$printStatements = $this->filterRemovedStatements($originalStatements, $file);
$this->typoscriptPrinter->printStatements($printStatements, $this->output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
-----
# Comment
[siteLanguage("languageId") == "2"]
lib.breadcrumb = TEXT
lib.breadcrumb.value = Foo
lib.breadcrumb = TEXT
lib.breadcrumb.value = Foo
[else]
lib.breadcrumb = TEXT
lib.breadcrumb.value = Baz
lib.breadcrumb = TEXT
lib.breadcrumb.value = Baz
[global]
[siteLanguage("languageId") == "3"]
lib.breadcrumb = TEXT
lib.breadcrumb.value = Bar
lib.breadcrumb = TEXT
lib.breadcrumb.value = Bar
[global]
[applicationContext matches "/^Development/" || applicationContext == "Production/Staging"]
lib.breadcrumb = TEXT
lib.breadcrumb.value = Bar
lib.breadcrumb = TEXT
lib.breadcrumb.value = Bar
[global]
3 changes: 3 additions & 0 deletions tests/FileProcessor/TypoScript/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\AdminUserConditionMatcher;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\ApplicationContextConditionMatcher;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\BrowserConditionMatcher;
Expand Down Expand Up @@ -52,6 +53,8 @@

$parameters = $rectorConfig->parameters();

$parameters->set(Typo3Option::TYPOSCRIPT_INDENT_CONDITIONS, true);

$rectorConfig->rule(AdditionalHeadersToArrayTypoScriptRector::class);
$rectorConfig->rule(LibFluidContentToLibContentElementRector::class);
$rectorConfig->rule(LibFluidContentToContentElementTypoScriptPostRector::class);
Expand Down

0 comments on commit f9d831f

Please sign in to comment.