Repository: phpactor/phpactor (php)

Issue:
Option to show full class name in completion items
Currently when completing it shows only some parts.

It would (for me) be useful to show the full class.

<img width="241" alt="Screenshot 2023-05-07 at 09 41 47" src="https://user-images.githubusercontent.com/866743/236664563-2d7f265d-7a85-4503-b6e3-455426dd4ad7.png">


Pull request:
Making the label formatter configurable
Closes #2260 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b93420e5..c1bee017 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ Features:
   - Diagnostic for undefined variables #2209
   - Code action to suggest fixes for undefined variables (in case of typos) #2209
   - PHPUnit: code action for adding `setUp` / `tearDown` #2180 @mamazu
+  - Making the completion label formatter configurable #2277 @mamazu
 
 Improvements:
 
@@ -31,7 +32,7 @@ Bug fixes:
   - Do not make fully qualified name usage relative in class-mover #2208 @mamazu
   - Fix resolution of `self` type (esp. in relation to traits) #2116
   - Fix different virtual member types with the same name replacing eachother #2108
-  - Specify maximimum size (255 chars) for string literal types #2144
+  - Specify maximum size (255 chars) for string literal types #2144
   - Fix docblock parser with `$this` when used as generic argument #2092
 
 ## 2023.04.10
diff --git a/doc/reference/configuration.rst b/doc/reference/configuration.rst
index d6a2e579..49154496 100644
--- a/doc/reference/configuration.rst
+++ b/doc/reference/configuration.rst
@@ -680,6 +680,22 @@ Sets a limit on the number of completion suggestions for any request
 **Default**: ``null``
 
 
+.. _param_completion.label_formatter:
+
+
+``completion.label_formatter``
+""""""""""""""""""""""""""""""
+
+
+Definition of how to format entries in the completion list
+
+
+**Default**: ``"helpful"``
+
+
+**Allowed values**: "helpful", "fqn"
+
+
 .. _NavigationExtension:
 
 
@@ -874,7 +890,7 @@ FilePathResolverExtension
 """""""""""""""""""""""""""""""""""
 
 
-**Default**: ``"\/home\/daniel\/www\/phpactor\/phpactor"``
+**Default**: ``"\/home\/mamazu\/packages\/phpactor\/phpactor"``
 
 
 .. _param_file_path_resolver.app_name:
diff --git a/lib/Completion/Core/LabelFormatter.php b/lib/Completion/Core/LabelFormatter.php
index d5354c8d..2e7f57bd 100644
--- a/lib/Completion/Core/LabelFormatter.php
+++ b/lib/Completion/Core/LabelFormatter.php
@@ -10,6 +10,9 @@ namespace Phpactor\Completion\Core;
  */
 interface LabelFormatter
 {
+    public const HELPFUL = 'helpful';
+    public const FQN = 'fqn';
+
     /**
      * @param array<string,bool> $seen
      */
diff --git a/lib/Extension/Completion/CompletionExtension.php b/lib/Extension/Completion/CompletionExtension.php
index beea2541..ad350b9f 100644
--- a/lib/Extension/Completion/CompletionExtension.php
+++ b/lib/Extension/Completion/CompletionExtension.php
@@ -2,6 +2,7 @@
 
 namespace Phpactor\Extension\Completion;
 
+use InvalidArgumentException;
 use Phpactor\Completion\Core\ChainCompletor;
 use Phpactor\Completion\Core\ChainSignatureHelper;
 use Phpactor\Completion\Core\Completor\DedupeCompletor;
@@ -11,6 +12,7 @@ use Phpactor\Completion\Core\Completor\LimitingCompletor;
 use Phpactor\Completion\Core\Formatter\ObjectFormatter;
 use Phpactor\Completion\Core\LabelFormatter;
 use Phpactor\Completion\Core\LabelFormatter\HelpfulLabelFormatter;
+use Phpactor\Completion\Core\LabelFormatter\PassthruLabelFormatter;
 use Phpactor\Completion\Core\SuggestionDocumentor;
 use Phpactor\Completion\Core\TypedCompletorRegistry;
 use Phpactor\Container\Extension;
@@ -33,7 +35,7 @@ class CompletionExtension implements Extension
     public const PARAM_DEDUPE = 'completion.dedupe';
     public const PARAM_DEDUPE_MATCH_FQN = 'completion.dedupe_match_fqn';
     public const PARAM_LIMIT = 'completion.limit';
-
+    public const PARAM_LABEL_FORMATTER = 'completion.label_formatter';
 
     public function configure(Resolver $schema): void
     {
@@ -41,11 +43,19 @@ class CompletionExtension implements Extension
             self::PARAM_DEDUPE => true,
             self::PARAM_DEDUPE_MATCH_FQN => true,
             self::PARAM_LIMIT => null,
+            self::PARAM_LABEL_FORMATTER => LabelFormatter::HELPFUL,
         ]);
         $schema->setDescriptions([
             self::PARAM_DEDUPE => 'If results should be de-duplicated',
             self::PARAM_DEDUPE_MATCH_FQN => 'If ``' . self::PARAM_DEDUPE . '``, consider the class FQN in addition to the completion suggestion',
             self::PARAM_LIMIT => 'Sets a limit on the number of completion suggestions for any request',
+            self::PARAM_LABEL_FORMATTER => 'Definition of how to format entries in the completion list',
+        ]);
+        $schema->setEnums([
+            self::PARAM_LABEL_FORMATTER => [
+                LabelFormatter::HELPFUL,
+                LabelFormatter::FQN,
+            ]
         ]);
     }
 
@@ -76,14 +86,16 @@ class CompletionExtension implements Extension
             $mapped = [];
             foreach ($completors as $type => $completors) {
                 $completors = new ChainCompletor($completors);
-                if ($container->getParameter(self::PARAM_DEDUPE)) {
+                if ($container->parameter(self::PARAM_DEDUPE)->bool()) {
                     $completors = new DedupeCompletor(
                         $completors,
-                        $container->getParameter(self::PARAM_DEDUPE_MATCH_FQN)
+                        $container->parameter(self::PARAM_DEDUPE_MATCH_FQN)->bool()
                     );
                 }
 
-                if ($limit = $container->getParameter(self::PARAM_LIMIT)) {
+                /** @var int|null $limit */
+                $limit = $container->getParameter(self::PARAM_LIMIT);
+                if (is_int($limit)) {
                     $completors = new LimitingCompletor($completors, $limit);
                 }
 
@@ -99,7 +111,11 @@ class CompletionExtension implements Extension
         });
 
         $container->register(LabelFormatter::class, function (Container $container) {
-            return new HelpfulLabelFormatter();
+            return match ($formatter = $container->parameter(self::PARAM_LABEL_FORMATTER)->string()) {
+                LabelFormatter::HELPFUL => new HelpfulLabelFormatter(),
+                LabelFormatter::FQN => new PassthruLabelFormatter(),
+                default => throw new InvalidArgumentException('Unknown formatter type: ' . $formatter),
+            };
         });
 
         $container->register(self::SERVICE_SHORT_DESC_FORMATTER, function (Container $container) {
@@ -133,7 +149,6 @@ class CompletionExtension implements Extension
         $container->register(self::SERVICE_SIGNATURE_HELPER, function (Container $container) {
             $helpers = [];
 
-            $helper = null;
             foreach (array_keys($container->getServiceIdsForTag(self::TAG_SIGNATURE_HELPER)) as $serviceId) {
                 $helpers[] = $container->get($serviceId);
             }


Test patch:
diff --git a/lib/Extension/Completion/Tests/Unit/CompletionExtensionTest.php b/lib/Extension/Completion/Tests/Unit/CompletionExtensionTest.php
index eec1a717..71c83e47 100644
--- a/lib/Extension/Completion/Tests/Unit/CompletionExtensionTest.php
+++ b/lib/Extension/Completion/Tests/Unit/CompletionExtensionTest.php
@@ -5,6 +5,7 @@ namespace Phpactor\Extension\Completion\Tests\Unit;
 use PHPUnit\Framework\TestCase;
 use Phpactor\Completion\Core\Completor;
 use Phpactor\Completion\Core\Formatter\Formatter;
+use Phpactor\Completion\Core\LabelFormatter;
 use Phpactor\Completion\Core\SignatureHelp;
 use Phpactor\Completion\Core\SignatureHelper;
 use Phpactor\Completion\Core\Suggestion;
@@ -130,6 +131,7 @@ class CompletionExtensionTest extends TestCase
             CompletionExtension::PARAM_DEDUPE => false,
             CompletionExtension::PARAM_DEDUPE_MATCH_FQN => false,
             CompletionExtension::PARAM_LIMIT => 10,
+            CompletionExtension::PARAM_LABEL_FORMATTER => LabelFormatter::HELPFUL,
         ]);
     }
 }