1
0
Fork 0

Support document formatting #8

pull/10/head
Michal Niewrzal 2016-09-05 18:37:39 +02:00
parent d1b9b33741
commit 1bb2b4a86c
2 changed files with 27 additions and 2 deletions

View File

@ -74,6 +74,8 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
// Support "Find all symbols"
$serverCapabilities->documentSymbolProvider = true;
// Support "Format Code"
$serverCapabilities->documentFormattingProvider = true;
return new InitializeResult($serverCapabilities);
}

View File

@ -2,7 +2,7 @@
namespace LanguageServer\Server;
use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer};
use PhpParser\{Error, Comment, Node, ParserFactory, NodeTraverser, Lexer, PrettyPrinter\Standard as PrettyPrinterStandard};
use PhpParser\NodeVisitor\NameResolver;
use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder};
use LanguageServer\Protocol\{
@ -12,7 +12,9 @@ use LanguageServer\Protocol\{
Diagnostic,
DiagnosticSeverity,
Range,
Position
Position,
FormattingOptions,
TextEdit
};
/**
@ -124,4 +126,25 @@ class TextDocument
$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 PrettyPrinterStandard();
$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];
}
}