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