1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #1888 - add go-to-variable

This commit is contained in:
Brown 2019-07-01 18:48:33 -04:00
parent 2e1493be73
commit 5c76b3c82d
4 changed files with 57 additions and 28 deletions

View File

@ -943,8 +943,8 @@ class Codebase
*/
public function getSymbolInformation(string $file_path, string $symbol)
{
if (substr($symbol, 0, 6) === 'type: ') {
return substr($symbol, 6);
if (\is_numeric($symbol[0])) {
return \preg_replace('/[^:]*:/', '', $symbol);
}
try {
@ -1013,6 +1013,15 @@ class Codebase
*/
public function getSymbolLocation(string $file_path, string $symbol)
{
if (\is_numeric($symbol[0])) {
$symbol = \preg_replace('/:.*/', '', $symbol);
$symbol_parts = explode('-', $symbol);
$file_contents = $this->getFileContents($file_path);
return new CodeLocation\Raw($file_contents, $file_path, (int) $symbol_parts[0], (int) $symbol_parts[1]);
}
try {
if (strpos($symbol, '::')) {
if (strpos($symbol, '()')) {
@ -1115,23 +1124,7 @@ class Codebase
}
if ($reference === null || $start_pos === null || $end_pos === null) {
ksort($type_map);
foreach ($type_map as $start_pos => list($end_pos, $possible_type)) {
if ($offset < $start_pos) {
break;
}
if ($offset > $end_pos) {
continue;
}
$reference = 'type: ' . $possible_type;
}
if ($reference === null || $start_pos === null || $end_pos === null) {
return null;
}
return null;
}
$range = new Range(

View File

@ -256,6 +256,17 @@ class VariableFetchAnalyzer
}
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt,
$first_appearance->raw_file_start . '-' . $first_appearance->raw_file_end . ':mixed'
);
}
$statements_analyzer->registerVariableUses([$first_appearance->getHash() => $first_appearance]);
}
} else {
@ -264,7 +275,6 @@ class VariableFetchAnalyzer
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
&& isset($stmt->inferredType)
) {
$codebase->analyzer->addNodeType(
$statements_analyzer->getFilePath(),
@ -272,6 +282,23 @@ class VariableFetchAnalyzer
(string) $stmt->inferredType
);
}
if ($codebase->store_node_types
&& !$context->collect_initializations
&& !$context->collect_mutations
) {
$first_appearance = $statements_analyzer->getFirstAppearance($var_name);
if ($first_appearance) {
$codebase->analyzer->addNodeReference(
$statements_analyzer->getFilePath(),
$stmt,
$first_appearance->raw_file_start
. '-' . $first_appearance->raw_file_end
. ':' . $stmt->inferredType->getId()
);
}
}
}
return null;

View File

@ -242,7 +242,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
// Support "Completion"
if ($this->project_analyzer->provide_completion) {
$serverCapabilities->completionProvider = new CompletionOptions;
$serverCapabilities->completionProvider = new CompletionOptions();
$serverCapabilities->completionProvider->resolveProvider = false;
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>', ':'];
}

View File

@ -104,9 +104,12 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
/** @var int|null */
protected $a;
const BANANA = "🍌";
const BANANA = "nana";
public function foo() : void {}
public function foo() : void {
$a = 1;
echo $a;
}
}
function bar() : int {
@ -141,8 +144,14 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
$function_symbol_location = $codebase->getSymbolLocation('somefile.php', 'B\bar()');
$this->assertNotNull($function_symbol_location);
$this->assertSame(13, $function_symbol_location->getLineNumber());
$this->assertSame(16, $function_symbol_location->getLineNumber());
$this->assertSame(26, $function_symbol_location->getColumn());
$function_symbol_location = $codebase->getSymbolLocation('somefile.php', '257-259');
$this->assertNotNull($function_symbol_location);
$this->assertSame(11, $function_symbol_location->getLineNumber());
$this->assertSame(25, $function_symbol_location->getColumn());
}
/**
@ -214,19 +223,19 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
$this->assertNotNull($symbol_at_position);
$this->assertSame('type: int|null', $symbol_at_position[0]);
$this->assertSame('245-246:int|null', $symbol_at_position[0]);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(12, 30));
$this->assertNotNull($symbol_at_position);
$this->assertSame('type: int', $symbol_at_position[0]);
$this->assertSame('213-214:int(1)', $symbol_at_position[0]);
$symbol_at_position = $codebase->getReferenceAtPosition('somefile.php', new Position(17, 30));
$this->assertNotNull($symbol_at_position);
$this->assertSame('type: int', $symbol_at_position[0]);
$this->assertSame('425-426:int(2)', $symbol_at_position[0]);
}
/**
@ -362,7 +371,7 @@ class SymbolLookupTest extends \Psalm\Tests\TestCase
public static function staticFoo(string $a) {}
public function __construct() {}
public function __construct() {}
}
function foo(string $a) {