Correct variable insertion
parent
f6a7ce1a8b
commit
6fb21817e4
|
@ -110,13 +110,13 @@ class CompletionProvider
|
|||
/**
|
||||
* Returns suggestions for a specific cursor position in a document
|
||||
*
|
||||
* @param PhpDocument $document The opened document
|
||||
* @param PhpDocument $doc The opened document
|
||||
* @param Position $pos The cursor position
|
||||
* @return CompletionItem[]
|
||||
*/
|
||||
public function provideCompletion(PhpDocument $document, Position $pos): array
|
||||
public function provideCompletion(PhpDocument $doc, Position $pos): array
|
||||
{
|
||||
$node = $document->getNodeAtPosition($pos);
|
||||
$node = $doc->getNodeAtPosition($pos);
|
||||
|
||||
if ($node instanceof Node\Expr\Error) {
|
||||
$node = $node->getAttribute('parentNode');
|
||||
|
@ -262,13 +262,17 @@ class CompletionProvider
|
|||
$item->label = '$' . ($var instanceof Node\Expr\ClosureUse ? $var->var : $var->name);
|
||||
$item->documentation = $this->definitionResolver->getDocumentationFromNode($var);
|
||||
$item->detail = (string)$this->definitionResolver->getTypeFromNode($var);
|
||||
$item->textEdit = new TextEdit(
|
||||
new Range($pos, $pos),
|
||||
stripStringOverlap($doc->getRange(new Range(new Position(0, 0), $pos)), $item->label)
|
||||
);
|
||||
$items[] = $item;
|
||||
}
|
||||
} else if ($node instanceof Node\Stmt\InlineHTML || $pos == new Position(0, 0)) {
|
||||
$item = new CompletionItem('<?php', CompletionItemKind::KEYWORD);
|
||||
$item->textEdit = new TextEdit(
|
||||
new Range($pos, $pos),
|
||||
stripStringOverlap($document->getRange(new Range(new Position(0, 0), $pos)), '<?php')
|
||||
stripStringOverlap($doc->getRange(new Range(new Position(0, 0), $pos)), '<?php')
|
||||
);
|
||||
$items[] = $item;
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ class Position
|
|||
public function toOffset(string $content): int
|
||||
{
|
||||
$lines = explode("\n", $content);
|
||||
return array_sum(array_map('strlen', array_slice($lines, 0, $this->line))) + $this->character;
|
||||
$slice = array_slice($lines, 0, $this->line);
|
||||
return array_sum(array_map('strlen', $slice)) + count($slice) + $this->character;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,8 +95,26 @@ class CompletionTest extends TestCase
|
|||
new Position(8, 5)
|
||||
)->wait();
|
||||
$this->assertEquals([
|
||||
new CompletionItem('$var', CompletionItemKind::VARIABLE, 'int'),
|
||||
new CompletionItem('$param', CompletionItemKind::VARIABLE, 'string|null', 'A parameter')
|
||||
new CompletionItem(
|
||||
'$var',
|
||||
CompletionItemKind::VARIABLE,
|
||||
'int',
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
new TextEdit(new Range(new Position(8, 5), new Position(8, 5)), 'var')
|
||||
),
|
||||
new CompletionItem(
|
||||
'$param',
|
||||
CompletionItemKind::VARIABLE,
|
||||
'string|null',
|
||||
'A parameter',
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
new TextEdit(new Range(new Position(8, 5), new Position(8, 5)), 'param')
|
||||
)
|
||||
], $items);
|
||||
}
|
||||
|
||||
|
@ -106,10 +124,19 @@ class CompletionTest extends TestCase
|
|||
$this->project->openDocument($completionUri, file_get_contents($completionUri));
|
||||
$items = $this->textDocument->completion(
|
||||
new TextDocumentIdentifier($completionUri),
|
||||
new Position(8, 5)
|
||||
new Position(8, 6)
|
||||
)->wait();
|
||||
$this->assertEquals([
|
||||
new CompletionItem('$param', CompletionItemKind::VARIABLE, 'string|null', 'A parameter')
|
||||
new CompletionItem(
|
||||
'$param',
|
||||
CompletionItemKind::VARIABLE,
|
||||
'string|null',
|
||||
'A parameter',
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
new TextEdit(new Range(new Position(8, 6), new Position(8, 6)), 'aram')
|
||||
)
|
||||
], $items);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue