Skip to content

Commit

Permalink
Implement signature help - closes vimeo#1841
Browse files Browse the repository at this point in the history
  • Loading branch information
iluuu1994 committed Jun 28, 2019
1 parent 2a1ed6c commit 0e989cd
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 11 deletions.
54 changes: 54 additions & 0 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,60 @@ public function getReferenceAtPosition(string $file_path, Position $position)
return [$reference, $range];
}

/**
* @return array{0: string, 1: int, 2: Range}|null
*/
public function getFunctionArgumentAtPosition(string $file_path, Position $position)
{
$is_open = $this->file_provider->isOpen($file_path);

if (!$is_open) {
throw new \Psalm\Exception\UnanalyzedFileException($file_path . ' is not open');
}

$file_contents = $this->getFileContents($file_path);

$offset = $position->toOffset($file_contents);

list(,,, $argument_map) = $this->analyzer->getMapsForFile($file_path);

$reference = null;
$argument_number = null;

if (!$argument_map) {
return null;
}

$start_pos = null;
$end_pos = null;

ksort($argument_map);

foreach ($argument_map as $start_pos => list($end_pos, $possible_reference, $possible_argument_number)) {
if ($offset < $start_pos) {
break;
}

if ($offset > $end_pos) {
continue;
}

$reference = $possible_reference;
$argument_number = $possible_argument_number;
}

if ($reference === null || $argument_number === null) {
return null;
}

$range = new Range(
self::getPositionFromOffset($start_pos, $file_contents),
self::getPositionFromOffset($end_pos, $file_contents)
);

return [$reference, $argument_number, $range];
}

/**
* @return array{0: string, 1: '->'|'::'|'symbol', 2: array<string, string>}|null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,13 @@ function (PhpParser\Node\Arg $arg) {
}
}

self::recordArgumentPositions(
$statements_analyzer,
$stmt,
$codebase,
$method_id
);

if (self::checkMethodArgs(
$method_id,
$args,
Expand Down Expand Up @@ -1643,4 +1650,107 @@ private static function checkMagicGetterOrSetterProperty(

return true;
}

private static function recordArgumentPositions(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\MethodCall $stmt,
Codebase $codebase,
string $method_id
): void {
$file_content = $codebase->file_provider->getContents($statements_analyzer->getFilePath());

// Find opening paren
$first_argument = $stmt->args[0] ?? null;
$first_argument_character = $first_argument !== null
? $first_argument->getStartFilePos()
: ($stmt->getEndFilePos() - 1);
$method_name_and_first_parent_source_code = substr(
$file_content,
$stmt->getStartFilePos(),
$first_argument_character - $stmt->getStartFilePos()
);
$method_name_and_first_parent_tokens = token_get_all('<?php ' . $method_name_and_first_parent_source_code);
$opening_paren_position = $first_argument_character;
foreach (array_reverse($method_name_and_first_parent_tokens) as $token) {
$token = is_string($token) ? $token : $token[1];
$opening_paren_position -= strlen($token);

if ($token === '(') {
break;
}
}

// Record ranges of the source code that need to be tokenized to find commas
/** @var array{0: int, 1: int}[] $ranges */
$ranges = [];

// Add range between opening paren and first argument
$first_argument = $stmt->args[0] ?? null;
$first_argument_starting_position = $first_argument !== null
? $first_argument->getStartFilePos()
: $stmt->getEndFilePos() - 1;
$first_range_starting_position = $opening_paren_position + 1;
if ($first_range_starting_position !== $first_argument_starting_position) {
$ranges[] = [$first_range_starting_position, $first_argument_starting_position];
}

// Add range between arguments
foreach ($stmt->args as $i => $argument) {
$range_start = $argument->getEndFilePos() + 1;
$next_argument = $stmt->args[$i + 1] ?? null;
$range_end = $next_argument !== null
? $next_argument->getStartFilePos()
: $stmt->getEndFilePos();

if ($range_start !== $range_end) {
$ranges[] = [$range_start, $range_end];
}
}

$commas = [];
foreach ($ranges as $range) {
$position = $range[0];
$length = $range[1] - $position;
$range_source_code = substr($file_content, $position, $length);
$range_tokens = token_get_all('<?php ' . $range_source_code);
array_shift($range_tokens);

$current_position = $position;
foreach ($range_tokens as $token) {
$token = is_string($token) ? $token : $token[1];

if ($token === ',') {
$commas[] = $current_position;
}

$current_position += strlen($token);
}
}

$argument_start_position = $opening_paren_position + 1;
$argument_number = 0;
while (!empty($commas)) {
$comma = reset($commas);
array_shift($commas);

$codebase->analyzer->addNodeArgument(
$statements_analyzer->getFilePath(),
$argument_start_position,
$comma,
$method_id,
$argument_number
);

++$argument_number;
$argument_start_position = $comma + 1;
}

$codebase->analyzer->addNodeArgument(
$statements_analyzer->getFilePath(),
$argument_start_position,
$stmt->getEndFilePos(),
$method_id,
$argument_number
);
}
}
93 changes: 86 additions & 7 deletions src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ class Analyzer
*/
private $alias_map = [];

/**
* @var array<string, array<int, array{0: int, 1: string, 2: int}>>
*/
private $argument_map = [];

/**
* @var array<string, array<int, \Psalm\Type\Union>>
*/
Expand Down Expand Up @@ -425,10 +430,12 @@ function () {
FileManipulationBuffer::add($file_path, $manipulations);
}

foreach ($pool_data['file_maps'] as $file_path => list($reference_map, $type_map, $alias_map)) {
foreach ($pool_data['file_maps'] as $file_path => list($reference_map, $type_map, $alias_map, $argument_map)
) {
$this->reference_map[$file_path] = $reference_map;
$this->type_map[$file_path] = $type_map;
$this->alias_map[$file_path] = $alias_map;
$this->argument_map[$file_path] = $argument_map;
}
}

Expand Down Expand Up @@ -512,10 +519,11 @@ public function loadCachedResults(ProjectAnalyzer $project_analyzer)
$this->existing_issues = $codebase->file_reference_provider->getExistingIssues();
$file_maps = $codebase->file_reference_provider->getFileMaps();

foreach ($file_maps as $file_path => list($reference_map, $type_map, $alias_map)) {
foreach ($file_maps as $file_path => list($reference_map, $type_map, $alias_map, $argument_map)) {
$this->reference_map[$file_path] = $reference_map;
$this->type_map[$file_path] = $type_map;
$this->alias_map[$file_path] = $alias_map;
$this->argument_map[$file_path] = $argument_map;
}
}

Expand Down Expand Up @@ -905,6 +913,40 @@ public function shiftFileOffsets(array $diff_map)
}
}
}

foreach ($this->argument_map as $file_path => &$argument_map) {
if (!isset($this->analyzed_methods[$file_path])) {
unset($this->argument_map[$file_path]);
continue;
}

$file_diff_map = $diff_map[$file_path] ?? [];

if (!$file_diff_map) {
continue;
}

$first_diff_offset = $file_diff_map[0][0];
$last_diff_offset = $file_diff_map[count($file_diff_map) - 1][1];

foreach ($argument_map as $argument_from => list($argument_to, $method_id, $argument_number)) {
if ($argument_to < $first_diff_offset || $argument_from > $last_diff_offset) {
continue;
}


foreach ($file_diff_map as list($from, $to, $file_offset)) {
if ($argument_from >= $from && $argument_from <= $to) {
unset($argument_map[$argument_from]);
$argument_map[$argument_from += $file_offset] = [
$argument_to += $file_offset,
$method_id,
$argument_number,
];
}
}
}
}
}

/**
Expand Down Expand Up @@ -1055,6 +1097,20 @@ public function addNodeReference(string $file_path, PhpParser\Node $node, string
];
}

public function addNodeArgument(
string $file_path,
int $start_position,
int $end_position,
string $reference,
int $argument_number
): void {
$this->argument_map[$file_path][$start_position] = [
$end_position,
$reference,
$argument_number
];
}

/**
* @return void
*/
Expand Down Expand Up @@ -1297,6 +1353,14 @@ public function removeExistingDataForFile($file_path, $start, $end)
}
}
}

if (isset($this->argument_map[$file_path])) {
foreach ($this->argument_map[$file_path] as $map_start => $_) {
if ($map_start >= $start && $map_start <= $end) {
unset($this->argument_map[$file_path][$map_start]);
}
}
}
}

/**
Expand All @@ -1313,7 +1377,8 @@ public function getAnalyzedMethods()
* array{
* 0: TaggedCodeType,
* 1: TaggedCodeType,
* 2: array<int, array{0: int, 1: array<string, string>}>
* 2: array<int, array{0: int, 1: array<string, string>}>,
* 3: array<int, array{0: int, 1: string, 2: int}>
* }
* >
*/
Expand All @@ -1322,37 +1387,51 @@ public function getFileMaps()
$file_maps = [];

foreach ($this->reference_map as $file_path => $reference_map) {
$file_maps[$file_path] = [$reference_map, [], []];
$file_maps[$file_path] = [$reference_map, [], [], []];
}

foreach ($this->type_map as $file_path => $type_map) {
if (isset($file_maps[$file_path])) {
$file_maps[$file_path][1] = $type_map;
} else {
$file_maps[$file_path] = [[], $type_map, []];
$file_maps[$file_path] = [[], $type_map, [], []];
}
}

foreach ($this->alias_map as $file_path => $alias_map) {
if (isset($file_maps[$file_path])) {
$file_maps[$file_path][2] = $alias_map;
} else {
$file_maps[$file_path] = [[], [], $alias_map];
$file_maps[$file_path] = [[], [], $alias_map, []];
}
}

foreach ($this->argument_map as $file_path => $argument_map) {
if (isset($file_maps[$file_path])) {
$file_maps[$file_path][3] = $argument_map;
} else {
$file_maps[$file_path] = [[], [], [], $argument_map];
}
}

return $file_maps;
}

/**
* @return array{0: TaggedCodeType, 1: TaggedCodeType, 2: array<int, array{0: int, 1: array<string, string>}>}
* @return array{
* 0: TaggedCodeType,
* 1: TaggedCodeType,
* 2: array<int, array{0: int, 1: array<string, string>}>
* 3: array<int, array{0: int, 1: string, 2: int}>
* }
*/
public function getMapsForFile(string $file_path)
{
return [
$this->reference_map[$file_path] ?? [],
$this->type_map[$file_path] ?? [],
$this->alias_map[$file_path] ?? [],
$this->argument_map[$file_path] ?? [],
];
}

Expand Down
5 changes: 1 addition & 4 deletions src/Psalm/Internal/LanguageServer/LanguageServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ function () use ($capabilities, $rootPath, $processId) {
$serverCapabilities->completionProvider->triggerCharacters = ['$', '>', ':'];
}

/*
$serverCapabilities->signatureHelpProvider = new SignatureHelpOptions();
$serverCapabilities->signatureHelpProvider->triggerCharacters = ['(', ','];
*/
$serverCapabilities->signatureHelpProvider = new SignatureHelpOptions(['(', ',']);

// Support global references
$serverCapabilities->xworkspaceReferencesProvider = false;
Expand Down
Loading

0 comments on commit 0e989cd

Please sign in to comment.