1
0
Fork 0

Added constructors

pull/250/head
Ivan Bozhanov 2017-01-21 22:25:44 +02:00
parent 9e822fbb61
commit df0c9d405d
8 changed files with 153 additions and 112 deletions

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\PrettyPrinter\Standard as PrettyPrinter; use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
use phpDocumentor\Reflection\{Types, Type, Fqsen, TypeResolver}; use phpDocumentor\Reflection\{Types, Type, Fqsen, TypeResolver};
use LanguageServer\Protocol\SymbolInformation; use LanguageServer\Protocol\SymbolInformation;
use LanguageServer\Protocol\ParameterInformation;
use LanguageServer\Index\ReadableIndex; use LanguageServer\Index\ReadableIndex;
class DefinitionResolver class DefinitionResolver
@ -134,7 +135,13 @@ class DefinitionResolver
$def->parameters = []; $def->parameters = [];
if ($node instanceof Node\FunctionLike) { if ($node instanceof Node\FunctionLike) {
foreach ($node->getParams() as $param) { foreach ($node->getParams() as $param) {
$def->parameters[] = $this->prettyPrinter->prettyPrint([$param]); if (!$param->getAttribute('parentNode')) {
$param->setAttribute('parentNode', $node);
}
$def->parameters[] = new ParameterInformation(
$this->prettyPrinter->prettyPrint([$param]),
$this->getDocumentationFromNode($param)
);
} }
} }
return $def; return $def;

View File

@ -23,4 +23,14 @@ class ParameterInformation
* @var string|null * @var string|null
*/ */
public $documentation; public $documentation;
/**
* @param string $label The label of this signature. Will be shown in the UI.
* @param string|null $documentation The human-readable doc-comment of this signature.
*/
public function __construct(string $label = null, string $documentation = null)
{
$this->label = $label;
$this->documentation = $documentation;
}
} }

View File

@ -29,4 +29,16 @@ class SignatureHelp
* @var int|null * @var int|null
*/ */
public $activeParameter; public $activeParameter;
/**
* @param SignatureInformation[] $signatures The signatures.
* @param bool $activeSignature The active signature.
* @param bool $activeParameter The active parameter of the active signature.
*/
public function __construct(array $signatures = [], int $activeSignature = 0, int $activeParameter = 0)
{
$this->signatures = $signatures;
$this->activeSignature = $activeSignature;
$this->activeParameter = $activeParameter;
}
} }

View File

@ -31,4 +31,16 @@ class SignatureInformation
* @var ParameterInformation[]|null * @var ParameterInformation[]|null
*/ */
public $parameters; public $parameters;
/**
* @param string $label The label of this signature. Will be shown in the UI.
* @param string|null $documentation The human-readable doc-comment of this signature.
* @param ParameterInformation[] $parameters The parameters of this signature.
*/
public function __construct(string $label = null, string $documentation = null, array $parameters = [])
{
$this->label = $label;
$this->documentation = $documentation;
$this->parameters = $parameters;
}
} }

View File

@ -45,9 +45,6 @@ class SignatureHelpProvider
*/ */
public function provideSignature(PhpDocument $doc, Position $pos) : SignatureHelp public function provideSignature(PhpDocument $doc, Position $pos) : SignatureHelp
{ {
$help = new SignatureHelp;
$help->signatures = [];
$handle = fopen('php://temp', 'r+'); $handle = fopen('php://temp', 'r+');
fwrite($handle, $doc->getContent()); fwrite($handle, $doc->getContent());
fseek($handle, 0); fseek($handle, 0);
@ -89,7 +86,7 @@ class SignatureHelpProvider
if ($node === null) { if ($node === null) {
fclose($handle); fclose($handle);
return $help; return new SignatureHelp;
} }
$params = ''; $params = '';
@ -138,7 +135,7 @@ class SignatureHelpProvider
} else { } else {
if (!preg_match('(([a-zA-Z_\x7f-\xff][:a-zA-Z0-9_\x7f-\xff]*)\s*\((.*)$)', $line, $method)) { if (!preg_match('(([a-zA-Z_\x7f-\xff][:a-zA-Z0-9_\x7f-\xff]*)\s*\((.*)$)', $line, $method)) {
fclose($handle); fclose($handle);
return $help; return new SignatureHelp;
} }
$def = $this->index->getDefinition($method[1] . '()'); $def = $this->index->getDefinition($method[1] . '()');
$params = $method[2]; $params = $method[2];
@ -148,18 +145,8 @@ class SignatureHelpProvider
if ($def) { if ($def) {
$method = preg_split('(::|->)', str_replace('()', '', $def->fqn), 2); $method = preg_split('(::|->)', str_replace('()', '', $def->fqn), 2);
$method = $method[1] ?? $method[0]; $method = $method[1] ?? $method[0];
$signature = new SignatureInformation;
$signature->label = $method . '('.implode(', ', $def->parameters).')';
$signature->documentation = $def->documentation;
$signature->parameters = [];
foreach ($def->parameters as $param) {
$p = new ParameterInformation;
$p->label = $param;
$signature->parameters[] = $p;
}
$help->activeSignature = 0;
$help->activeParameter = 0;
$params = ltrim($params, "( "); $params = ltrim($params, "( ");
$activeParameter = 0;
if (strlen(trim($params))) { if (strlen(trim($params))) {
try { try {
$lex = new \PhpParser\Lexer(); $lex = new \PhpParser\Lexer();
@ -173,10 +160,10 @@ class SignatureHelpProvider
while ($value !== "\0") { while ($value !== "\0") {
$lex->getNextToken($value); $lex->getNextToken($value);
if (($value === ")" || $value === ";") && !count($stack)) { if (($value === ")" || $value === ";") && !count($stack)) {
return $help; return new SignatureHelp;
} }
if ($value === ',' && !count($stack)) { if ($value === ',' && !count($stack)) {
$help->activeParameter++; $activeParameter++;
} }
if ($value === '(') { if ($value === '(') {
$stack[] = ')'; $stack[] = ')';
@ -189,11 +176,24 @@ class SignatureHelpProvider
} catch (\Exception $ignore) { } catch (\Exception $ignore) {
} }
} }
if ($help->activeParameter < count($signature->parameters)) { if ($activeParameter < count($def->parameters)) {
$help->signatures[] = $signature; $params = array_map(function ($v) {
return $v->label;
}, $def->parameters);
return new SignatureHelp(
[
new SignatureInformation(
$method . '('.implode(', ', $params).')',
$def->documentation,
$def->parameters
)
],
0,
$activeParameter
);
} }
} }
return $help; return new SignatureHelp;
} }
} }

View File

@ -43,7 +43,7 @@ class LanguageServerTest extends TestCase
$serverCapabilities->completionProvider->resolveProvider = false; $serverCapabilities->completionProvider->resolveProvider = false;
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>']; $serverCapabilities->completionProvider->triggerCharacters = ['$', '>'];
$serverCapabilities->signatureHelpProvider = new SignatureHelpOptions; $serverCapabilities->signatureHelpProvider = new SignatureHelpOptions;
$serverCapabilities->signatureHelpProvider->triggerCharacters = ['(']; $serverCapabilities->signatureHelpProvider->triggerCharacters = ['(',','];
$serverCapabilities->xworkspaceReferencesProvider = true; $serverCapabilities->xworkspaceReferencesProvider = true;
$serverCapabilities->xdefinitionProvider = true; $serverCapabilities->xdefinitionProvider = true;
$serverCapabilities->xdependenciesProvider = true; $serverCapabilities->xdependenciesProvider = true;

View File

@ -53,17 +53,17 @@ class SignatureHelpTest extends TestCase
new Position(9, 22) new Position(9, 22)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testMethodClosedReference() public function testMethodClosedReference()
@ -75,17 +75,17 @@ class SignatureHelpTest extends TestCase
new Position(14, 11) new Position(14, 11)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testMethodNotClosed() public function testMethodNotClosed()
@ -97,17 +97,17 @@ class SignatureHelpTest extends TestCase
new Position(9, 22) new Position(9, 22)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testMethodNotClosedReference() public function testMethodNotClosedReference()
@ -119,17 +119,17 @@ class SignatureHelpTest extends TestCase
new Position(14, 14) new Position(14, 14)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testFuncClosed() public function testFuncClosed()
@ -141,17 +141,17 @@ class SignatureHelpTest extends TestCase
new Position(6, 10) new Position(6, 10)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'helpFunc1(int $count = 0)',
$info->label = 'helpFunc1(int $count = 0)'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('int $count = 0')
$info->parameters[] = $param; ]
$param->label = 'int $count = 0'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testFuncNotClosed() public function testFuncNotClosed()
@ -163,17 +163,17 @@ class SignatureHelpTest extends TestCase
new Position(6, 10) new Position(6, 10)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'helpFunc2(int $count = 0)',
$info->label = 'helpFunc2(int $count = 0)'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('int $count = 0')
$info->parameters[] = $param; ]
$param->label = 'int $count = 0'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testStaticClosed() public function testStaticClosed()
@ -185,17 +185,17 @@ class SignatureHelpTest extends TestCase
new Position(9, 19) new Position(9, 19)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
public function testStaticNotClosed() public function testStaticNotClosed()
@ -207,16 +207,16 @@ class SignatureHelpTest extends TestCase
new Position(9, 19) new Position(9, 19)
)->wait(); )->wait();
$help = new SignatureHelp; $this->assertEquals(new SignatureHelp(
$help->signatures = []; [
$info = new SignatureInformation; new SignatureInformation(
$help->signatures[] = $info; 'method(string $param = "")',
$info->label = 'method(string $param = "")'; null,
$info->parameters = []; [
$param = new ParameterInformation; new ParameterInformation('string $param = ""')
$info->parameters[] = $param; ]
$param->label = 'string $param = ""'; )
]
$this->assertEquals($help, $result); ), $result);
} }
} }