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

feat: new hover format #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
77 changes: 75 additions & 2 deletions src/DefinitionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use LanguageServer\Index\ReadableIndex;
use LanguageServer\Factory\SymbolInformationFactory;
use LanguageServerProtocol\SymbolInformation;
use LanguageServerProtocol\SymbolKind;
use Microsoft\PhpParser;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\FunctionLike;
Expand Down Expand Up @@ -58,10 +59,45 @@ public function __construct(ReadableIndex $index)
* Builds the declaration line for a given node. Declarations with multiple lines are trimmed.
*
* @param Node $node
* @param SymbolInformation|null $symbolInformation
* @param Type|null $type
* @return string
*/
public function getDeclarationLineFromNode($node): string
public function getDeclarationLineFromNode($node, $symbolInformation, $type): string
{
if ($symbolInformation !== null) {
if ($symbolInformation->kind === SymbolKind::VARIABLE) {
$defLine = 'var ';
if ($type !== null) {
$defLine .= (string)$type . ' ';
}
$defLine .= '$' . $symbolInformation->name;
return $defLine;
} elseif ($symbolInformation->kind === SymbolKind::CONSTANT) {
$defLine = 'const ';
if ($type !== null) {
$defLine .= (string)$type . ' ';
}
$defLine .= $symbolInformation->name;
return $defLine;
} elseif ($symbolInformation->kind === SymbolKind::INTERFACE) {
$defLine = 'interface ';
$defLine .= $symbolInformation->name;

$namespaceNode = $node->getNamespaceDefinition();
if ($namespaceNode !== null) {
$defLine = $namespaceNode->getText() . "\n\n" . $defLine;
}
return $defLine;
} elseif ($symbolInformation->kind === SymbolKind::PROPERTY) {
$defLine = 'property ';
if ($type !== null) {
$defLine .= (string)$type . ' ';
}
$defLine .= '$' . $symbolInformation->name;
return $defLine;
}
}
// If node is part of a declaration list, build a declaration line that discludes other elements in the list
// - [PropertyDeclaration] // public $a, [$b = 3], $c; => public $b = 3;
// - [ConstDeclaration|ClassConstDeclaration] // "const A = 3, [B = 4];" => "const B = 4;"
Expand All @@ -86,6 +122,22 @@ public function getDeclarationLineFromNode($node): string
// cut at body start and replace all newlines
$defLine = \str_replace(["\n", "\r"], " ", \strtok($defLine, "{"));
$defLine = \preg_replace('!\s+!', " ", \trim($defLine));

if ($node instanceof Node\MethodDeclaration) {
$classNode = $node->getFirstAncestor(Node\Statement\ClassDeclaration::class, Node\Statement\InterfaceDeclaration::class, Node\Statement\TraitDeclaration::class);
if ($classNode !== null) {
$fqn = (string)$classNode->getNamespacedName();
if ($fqn) {
$defLine = str_replace('function ', 'function ' . $fqn . '::', $defLine);
}
}
}
} elseif ($node instanceof Node\Statement\ClassDeclaration) {
$defLine = \rtrim(\strtok($defLine, "\n"), "\r");
$namespaceNode = $node->getNamespaceDefinition();
if ($namespaceNode !== null) {
$defLine = $namespaceNode->getText() . "\n\n" . $defLine;
}
} else {
// Trim string to only include first line
$defLine = \rtrim(\strtok($defLine, "\n"), "\r");
Expand Down Expand Up @@ -250,11 +302,13 @@ public function createDefinitionFromNode(Node $node, string $fqn = null): Defini
}
}

// TODO add also @method doc entries

$def->symbolInformation = SymbolInformationFactory::fromNode($node, $fqn);

if ($def->symbolInformation !== null) {
$def->type = $this->getTypeFromNode($node);
$def->declarationLine = $this->getDeclarationLineFromNode($node);
$def->declarationLine = $this->getDeclarationLineFromNode($node, $def->symbolInformation, $def->type);
$def->documentation = $this->getDocumentationFromNode($node);
}

Expand Down Expand Up @@ -719,6 +773,15 @@ private static function isVariableDeclaration(Node $n, string $name)
return true;
}

if (
$n instanceof Node\Expression\Variable &&
$n->getName() === $name &&
$n->parent != null && $n->parent->parent != null &&
$n->parent->parent->parent instanceof Node\Expression\ListIntrinsicExpression
) {
return true;
}

return false;
}

Expand Down Expand Up @@ -1329,6 +1392,16 @@ public function getTypeFromNode($node)
return new Types\Mixed_();
}

// LIST DECLARATION
if (
$node instanceof Node\Expression\Variable &&
$node->parent != null && $node->parent->parent != null &&
$node->parent->parent->parent instanceof Node\Expression\ListIntrinsicExpression
) {
// TODO implement actual list type from right side assignment
return new Types\Mixed_();
}

// PROPERTIES, CONSTS, CLASS CONSTS, ASSIGNMENT EXPRESSIONS
// Get the documented type the assignment resolves to.
if (
Expand Down
1 change: 1 addition & 0 deletions src/Factory/SymbolInformationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static function fromNode($node, string $fqn = null)
)
|| $node instanceof Node\UseVariableName
|| $node instanceof Node\Parameter
|| $node->parent->parent->parent instanceof Node\Expression\ListIntrinsicExpression
) {
$symbol->kind = SymbolKind::VARIABLE;
} else {
Expand Down
11 changes: 11 additions & 0 deletions src/Server/TextDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
Hover,
Location,
MarkedString,
MarkupContent,
Position,
Range,
ReferenceContext,
SymbolDescriptor,
PackageDescriptor,
SymbolLocationInformation,
SymbolKind,
TextDocumentIdentifier,
TextDocumentItem,
VersionedTextDocumentIdentifier,
Expand Down Expand Up @@ -348,6 +350,15 @@ public function hover(TextDocumentIdentifier $textDocument, Position $position):
if ($def->documentation) {
$contents[] = $def->documentation;
}
if ($def->signatureInformation && $def->signatureInformation->parameters) {
$contents[] = new MarkedString('markdown', implode("\n", array_map(function (\LanguageServerProtocol\ParameterInformation $p) {
return "@param {$p->label}" . ($p->documentation ? " {$p->documentation}" : '');
}, $def->signatureInformation->parameters)));
}
$type_kind = $def->symbolInformation !== null && in_array($def->symbolInformation->kind, [SymbolKind::METHOD, SymbolKind::FUNCTION]) ? "@return" : "@type";
if ($def->type) {
$contents[] = new MarkedString('markdown', $type_kind . " " . (string)$def->type);
}
return new Hover($contents, $range);
});
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Validation/cases/WithReturnTypehints.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"containerName": "Fixtures\\Prophecy"
},
"type": null,
"declarationLine": "class WithReturnTypehints extends EmptyClass",
"declarationLine": "namespace Fixtures\\Prophecy;\n\nclass WithReturnTypehints extends EmptyClass",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -73,7 +73,7 @@
},
"type__tostring": "\\Fixtures\\Prophecy\\WithReturnTypehints",
"type": {},
"declarationLine": "public function getSelf(): self",
"declarationLine": "public function Fixtures\\Prophecy\\WithReturnTypehints::getSelf(): self",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand All @@ -97,7 +97,7 @@
},
"type__tostring": "string",
"type": {},
"declarationLine": "public function getName(): string",
"declarationLine": "public function Fixtures\\Prophecy\\WithReturnTypehints::getName(): string",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand All @@ -121,7 +121,7 @@
},
"type__tostring": "\\Fixtures\\Prophecy\\EmptyClass",
"type": {},
"declarationLine": "public function getParent(): parent",
"declarationLine": "public function Fixtures\\Prophecy\\WithReturnTypehints::getParent(): parent",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand All @@ -145,7 +145,7 @@
},
"type__tostring": "static",
"type": {},
"declarationLine": "public function getStatic(): static",
"declarationLine": "public function Fixtures\\Prophecy\\WithReturnTypehints::getStatic(): static",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"type__tostring": "array<string,bool>",
"type": {},
"declarationLine": "protected $foo;",
"declarationLine": "property array<string,bool> $foo",
"documentation": null,
"signatureInformation": null
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/classDefinition1.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "TestNamespace"
},
"type": null,
"declarationLine": "class A {",
"declarationLine": "namespace TestNamespace;\n\nclass A {",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "int",
"type": {},
"declarationLine": "public $a;",
"declarationLine": "property int $a",
"documentation": null,
"signatureInformation": null
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Validation/cases/classProperty1.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "TestNamespace"
},
"type": null,
"declarationLine": "class TestClass",
"declarationLine": "namespace TestNamespace;\n\nclass TestClass",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public $testProperty;",
"declarationLine": "property mixed $testProperty",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -86,7 +86,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function testMethod($testParameter)",
"declarationLine": "public function TestNamespace\\TestClass::testMethod($testParameter)",
"documentation": null,
"signatureInformation": {
"label": "(mixed $testParameter)",
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/constants.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "MyNamespace"
},
"type": null,
"declarationLine": "class A",
"declarationLine": "namespace MyNamespace;\n\nclass A",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"declarationLine": "public static function MyNamespace\\A::suite()",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/constants2.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "MyNamespace"
},
"type": null,
"declarationLine": "class A",
"declarationLine": "namespace MyNamespace;\n\nclass A",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"declarationLine": "public static function MyNamespace\\A::suite()",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/constants3.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "MyNamespace"
},
"type": null,
"declarationLine": "class A",
"declarationLine": "namespace MyNamespace;\n\nclass A",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public static function suite()",
"declarationLine": "public static function MyNamespace\\A::suite()",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/constants4.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"containerName": "MyNamespace"
},
"type": null,
"declarationLine": "class A",
"declarationLine": "namespace MyNamespace;\n\nclass A",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -65,7 +65,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "public function suite()",
"declarationLine": "public function MyNamespace\\A::suite()",
"documentation": null,
"signatureInformation": {
"label": "()",
Expand Down
4 changes: 2 additions & 2 deletions tests/Validation/cases/constants5.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"containerName": "MyNamespace"
},
"type": null,
"declarationLine": "class Mbstring",
"declarationLine": "namespace MyNamespace;\n\nclass Mbstring",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -62,7 +62,7 @@
},
"type__tostring": "\\MyNamespace\\PHP_INT_MAX",
"type": {},
"declarationLine": "const MB_CASE_FOLD = PHP_INT_MAX;",
"declarationLine": "const \\MyNamespace\\PHP_INT_MAX MB_CASE_FOLD",
"documentation": null,
"signatureInformation": null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"containerName": ""
},
"type": null,
"declarationLine": "interface A {",
"declarationLine": "interface A",
"documentation": null,
"signatureInformation": null
},
Expand All @@ -42,7 +42,7 @@
},
"type__tostring": "mixed",
"type": {},
"declarationLine": "function b ($a = MY_CONSTANT);",
"declarationLine": "function A::b ($a = MY_CONSTANT);",
"documentation": null,
"signatureInformation": {
"label": "(\\MY_CONSTANT $a = MY_CONSTANT)",
Expand Down
2 changes: 1 addition & 1 deletion tests/Validation/cases/interfaceProperty.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"containerName": ""
},
"type": null,
"declarationLine": "interface A {",
"declarationLine": "interface A",
"documentation": null,
"signatureInformation": null
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Validation/cases/magicConsts.php.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"type__tostring": "array<\\__CLASS__,bool>",
"type": {},
"declarationLine": "private static $deprecationsTriggered;",
"declarationLine": "property array<\\__CLASS__,bool> $deprecationsTriggered",
"documentation": null,
"signatureInformation": null
}
Expand Down
Loading