Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework of markdown locations detection. #218

Merged
merged 19 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"phpstan/phpdoc-parser": "^2.0.0",
"phpstan/phpstan": "^2.1.1",
"phpunit/phpunit": "^10.5.0|^11.0.0",
"psr/event-dispatcher": "^1.0",
"psr/http-message": "^1.0.0|^2.0.0",
"sebastian/comparator": "^5.0|^6.0.0",
"sebastian/exporter": "^5.0|^6.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/documentator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"league/config": "^1.1.1",
"nikic/php-parser": "^5.4.0",
"phpstan/phpdoc-parser": "^2.0.0",
"psr/event-dispatcher": "^1.0",
"symfony/console": "^7.0.0",
"symfony/deprecation-contracts": "^3.0.0",
"symfony/filesystem": "^7.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/documentator/src/Editor/Locations/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function getIterator(): Traversable {
yield from [];
}

/**
* @return new<self>
*/
public function withOffset(int $offset): self {
return new self(
$this->startLine,
Expand All @@ -73,6 +76,9 @@ public function withOffset(int $offset): self {
);
}

/**
* @return new<self>
*/
public function withLength(?int $length): self {
return new self(
$this->startLine,
Expand Down
11 changes: 0 additions & 11 deletions packages/documentator/src/Markdown/Data/BlockPadding.php

This file was deleted.

13 changes: 13 additions & 0 deletions packages/documentator/src/Markdown/Data/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Data;

use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location;

/**
* @internal
* @extends Data<Location>
*/
readonly class Content extends Data {
// empty
}
5 changes: 3 additions & 2 deletions packages/documentator/src/Markdown/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Data;

use LastDragon_ru\LaraASP\Documentator\Markdown\Exceptions\DataMissed;
use LastDragon_ru\LaraASP\Documentator\Package;
use League\CommonMark\Node\Node;

use function is_a;
Expand All @@ -26,7 +27,7 @@ final public function __construct(
* @return T
*/
public static function get(Node $node): mixed {
$data = $node->data->get(static::class, null);
$data = $node->data->get(Package::Name.'.'.static::class, null);
$value = is_object($data) && is_a($data, static::class, true)
? $data->value
: static::default($node);
Expand Down Expand Up @@ -55,7 +56,7 @@ public static function optional(): Optional {
* @return T
*/
public static function set(Node $node, mixed $value): mixed {
$node->data->set(static::class, new static($value));
$node->data->set(Package::Name.'.'.static::class, new static($value));

return $value;
}
Expand Down
11 changes: 0 additions & 11 deletions packages/documentator/src/Markdown/Data/Length.php

This file was deleted.

2 changes: 1 addition & 1 deletion packages/documentator/src/Markdown/Data/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* @internal
* @extends Data<array<array-key, string>>
* @extends Data<array<int, string>>
*/
readonly class Lines extends Data {
#[Override]
Expand Down
28 changes: 1 addition & 27 deletions packages/documentator/src/Markdown/Data/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,11 @@
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Data;

use LastDragon_ru\LaraASP\Documentator\Editor\Locations\Location as LocationContract;
use LastDragon_ru\LaraASP\Documentator\Markdown\Utils;
use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\Node;
use Override;

/**
* @internal
* @extends Data<LocationContract>
*/
readonly class Location extends Data {
#[Override]
protected static function default(Node $node): mixed {
$location = null;

if ($node instanceof AbstractBlock) {
$start = $node->getStartLine();
$end = $node->getEndLine();
$offset = Offset::optional()->get($node) ?? 0;
$length = Length::optional()->get($node);
$padding = Utils::getPadding($node, null, null);

if ($padding === null && $node->parent() instanceof Document) {
$padding = 0;
}

if ($start !== null && $end !== null && $padding !== null) {
$location = new LocationContract($start, $end, $offset, $length, $padding);
}
}

return $location;
}
// empty
}
11 changes: 0 additions & 11 deletions packages/documentator/src/Markdown/Data/Offset.php

This file was deleted.

11 changes: 0 additions & 11 deletions packages/documentator/src/Markdown/Data/Padding.php

This file was deleted.

2 changes: 1 addition & 1 deletion packages/documentator/src/Markdown/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function mutate(Mutation ...$mutations): self {
}

/**
* @return array<array-key, string>
* @return array<int, string>
*/
protected function getLines(): array {
return Lines::get($this->node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace LastDragon_ru\LaraASP\Documentator\Markdown\Extensions;
namespace LastDragon_ru\LaraASP\Documentator\Markdown\Environment;

use League\CommonMark\Environment\EnvironmentAwareInterface;
use League\CommonMark\Environment\EnvironmentInterface;
Expand All @@ -17,7 +17,7 @@
trait Aware {
#[Override]
public function setEnvironment(EnvironmentInterface $environment): void {
$object = $this->getObject();
$object = $this->getParser();

if ($object instanceof EnvironmentAwareInterface) {
$object->setEnvironment($environment);
Expand All @@ -26,12 +26,12 @@ public function setEnvironment(EnvironmentInterface $environment): void {

#[Override]
public function setConfiguration(ConfigurationInterface $configuration): void {
$object = $this->getObject();
$object = $this->getParser();

if ($object instanceof ConfigurationAwareInterface) {
$object->setConfiguration($configuration);
}
}

abstract protected function getObject(): object;
abstract protected function getParser(): object;
}
Loading
Loading