Support document formatting (#10)
parent
d1b9b33741
commit
6169998b92
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TestNamespace;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class TestClass
|
||||||
|
{
|
||||||
|
public $testProperty;
|
||||||
|
|
||||||
|
public function testMethod($testParameter)
|
||||||
|
{
|
||||||
|
$testVariable = 123;
|
||||||
|
|
||||||
|
if ( empty($testParameter)){
|
||||||
|
echo 'Empty';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace TestNamespace;
|
||||||
|
|
||||||
|
class TestClass
|
||||||
|
{
|
||||||
|
public $testProperty;
|
||||||
|
public function testMethod($testParameter)
|
||||||
|
{
|
||||||
|
$testVariable = 123;
|
||||||
|
if (empty($testParameter)) {
|
||||||
|
echo 'Empty';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -74,6 +74,8 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
|
||||||
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
|
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
|
||||||
// Support "Find all symbols"
|
// Support "Find all symbols"
|
||||||
$serverCapabilities->documentSymbolProvider = true;
|
$serverCapabilities->documentSymbolProvider = true;
|
||||||
|
// Support "Format Code"
|
||||||
|
$serverCapabilities->documentFormattingProvider = true;
|
||||||
return new InitializeResult($serverCapabilities);
|
return new InitializeResult($serverCapabilities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace LanguageServer\Server;
|
namespace LanguageServer\Server;
|
||||||
|
|
||||||
use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer};
|
use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer};
|
||||||
|
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
|
||||||
use PhpParser\NodeVisitor\NameResolver;
|
use PhpParser\NodeVisitor\NameResolver;
|
||||||
use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder};
|
use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder};
|
||||||
use LanguageServer\Protocol\{
|
use LanguageServer\Protocol\{
|
||||||
|
@ -12,7 +13,9 @@ use LanguageServer\Protocol\{
|
||||||
Diagnostic,
|
Diagnostic,
|
||||||
DiagnosticSeverity,
|
DiagnosticSeverity,
|
||||||
Range,
|
Range,
|
||||||
Position
|
Position,
|
||||||
|
FormattingOptions,
|
||||||
|
TextEdit
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,4 +127,25 @@ class TextDocument
|
||||||
$this->asts[$uri] = $stmts;
|
$this->asts[$uri] = $stmts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The document formatting request is sent from the server to the client to format a whole document.
|
||||||
|
*
|
||||||
|
* @param TextDocumentIdentifier $textDocument The document to format
|
||||||
|
* @param FormattingOptions $options The format options
|
||||||
|
* @return TextEdit[]
|
||||||
|
*/
|
||||||
|
public function formatting(TextDocumentIdentifier $textDocument, FormattingOptions $options)
|
||||||
|
{
|
||||||
|
$nodes = $this->asts[$textDocument->uri];
|
||||||
|
if (empty($nodes)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$prettyPrinter = new PrettyPrinter();
|
||||||
|
$edit = new TextEdit();
|
||||||
|
$edit->range = new Range(new Position(0, 0), new Position(PHP_INT_MAX, PHP_INT_MAX));
|
||||||
|
$edit->newText = $prettyPrinter->prettyPrintFile($nodes);
|
||||||
|
return [$edit];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ class LanguageServerTest extends TestCase
|
||||||
'workspaceSymbolProvider' => null,
|
'workspaceSymbolProvider' => null,
|
||||||
'codeActionProvider' => null,
|
'codeActionProvider' => null,
|
||||||
'codeLensProvider' => null,
|
'codeLensProvider' => null,
|
||||||
'documentFormattingProvider' => null,
|
'documentFormattingProvider' => true,
|
||||||
'documentRangeFormattingProvider' => null,
|
'documentRangeFormattingProvider' => null,
|
||||||
'documentOnTypeFormattingProvider' => null,
|
'documentOnTypeFormattingProvider' => null,
|
||||||
'renameProvider' => null
|
'renameProvider' => null
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace LanguageServer\Tests\Server;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use LanguageServer\Tests\MockProtocolStream;
|
use LanguageServer\Tests\MockProtocolStream;
|
||||||
use LanguageServer\{Server, Client, LanguageClient};
|
use LanguageServer\{Server, Client, LanguageClient};
|
||||||
use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity};
|
use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions};
|
||||||
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody};
|
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody};
|
||||||
|
|
||||||
class TextDocumentTest extends TestCase
|
class TextDocumentTest extends TestCase
|
||||||
|
@ -197,4 +197,34 @@ class TextDocumentTest extends TestCase
|
||||||
]]
|
]]
|
||||||
], json_decode(json_encode($args), true));
|
], json_decode(json_encode($args), true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFormatting()
|
||||||
|
{
|
||||||
|
$textDocument = new Server\TextDocument(new LanguageClient(new MockProtocolStream()));
|
||||||
|
// Trigger parsing of source
|
||||||
|
$textDocumentItem = new TextDocumentItem();
|
||||||
|
$textDocumentItem->uri = 'whatever';
|
||||||
|
$textDocumentItem->languageId = 'php';
|
||||||
|
$textDocumentItem->version = 1;
|
||||||
|
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/format.php');
|
||||||
|
$textDocument->didOpen($textDocumentItem);
|
||||||
|
|
||||||
|
// how code should look after formatting
|
||||||
|
$expected = file_get_contents(__DIR__ . '/../../fixtures/format_expected.php');
|
||||||
|
// Request formatting
|
||||||
|
$result = $textDocument->formatting(new TextDocumentIdentifier('whatever'), new FormattingOptions());
|
||||||
|
$this->assertEquals([0 => [
|
||||||
|
'range' => [
|
||||||
|
'start' => [
|
||||||
|
'line' => 0,
|
||||||
|
'character' => 0
|
||||||
|
],
|
||||||
|
'end' => [
|
||||||
|
'line' => PHP_INT_MAX,
|
||||||
|
'character' => PHP_INT_MAX
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'newText' => $expected
|
||||||
|
]], json_decode(json_encode($result), true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue