Support document formatting #8
parent
d1b9b33741
commit
1bb2b4a86c
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace LanguageServer\Server;
|
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 PhpParser\NodeVisitor\NameResolver;
|
||||||
use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder};
|
use LanguageServer\{LanguageClient, ColumnCalculator, SymbolFinder};
|
||||||
use LanguageServer\Protocol\{
|
use LanguageServer\Protocol\{
|
||||||
|
@ -12,7 +12,9 @@ use LanguageServer\Protocol\{
|
||||||
Diagnostic,
|
Diagnostic,
|
||||||
DiagnosticSeverity,
|
DiagnosticSeverity,
|
||||||
Range,
|
Range,
|
||||||
Position
|
Position,
|
||||||
|
FormattingOptions,
|
||||||
|
TextEdit
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,4 +126,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 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];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue