forked from felixfbecker/php-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageServer.php
103 lines (93 loc) Β· 3.63 KB
/
LanguageServer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace LanguageServer;
use LanguageServer\Server\TextDocument;
use LanguageServer\Protocol\{ServerCapabilities, ClientCapabilities, TextDocumentSyncKind, Message};
use LanguageServer\Protocol\InitializeResult;
use AdvancedJsonRpc\{Dispatcher, ResponseError, Response as ResponseBody, Request as RequestBody};
class LanguageServer extends \AdvancedJsonRpc\Dispatcher
{
/**
* Handles textDocument/* method calls
*
* @var Server\TextDocument
*/
public $textDocument;
public $telemetry;
public $window;
public $workspace;
public $completionItem;
public $codeLens;
private $protocolReader;
private $protocolWriter;
private $client;
public function __construct(ProtocolReader $reader, ProtocolWriter $writer)
{
parent::__construct($this, '/');
$this->protocolReader = $reader;
$this->protocolReader->onMessage(function (Message $msg) {
$err = null;
$result = null;
try {
// Invoke the method handler to get a result
$result = $this->dispatch($msg->body);
} catch (ResponseError $e) {
// If a ResponseError is thrown, send it back in the Response (result will be null)
$err = $e;
} catch (Throwable $e) {
// If an unexpected error occured, send back an INTERNAL_ERROR error response (result will be null)
$err = new ResponseError(
$e->getMessage(),
$e->getCode() === 0 ? ErrorCode::INTERNAL_ERROR : $e->getCode(),
null,
$e
);
}
// Only send a Response for a Request
// Notifications do not send Responses
if (RequestBody::isRequest($msg->body)) {
$this->protocolWriter->write(new Message(new ResponseBody($msg->body->id, $result, $err)));
}
});
$this->protocolWriter = $writer;
$this->client = new LanguageClient($writer);
$this->textDocument = new Server\TextDocument($this->client);
}
/**
* The initialize request is sent as the first request from the client to the server.
*
* @param string $rootPath The rootPath of the workspace. Is null if no folder is open.
* @param int $processId The process Id of the parent process that started the server.
* @param ClientCapabilities $capabilities The capabilities provided by the client (editor)
* @return InitializeResult
*/
public function initialize(string $rootPath, int $processId, ClientCapabilities $capabilities): InitializeResult
{
$serverCapabilities = new ServerCapabilities();
// Ask the client to return always full documents (because we need to rebuild the AST from scratch)
$serverCapabilities->textDocumentSync = TextDocumentSyncKind::FULL;
// Support "Find all symbols"
$serverCapabilities->documentSymbolProvider = true;
// Support "Format Code"
$serverCapabilities->documentFormattingProvider = true;
return new InitializeResult($serverCapabilities);
}
/**
* The shutdown request is sent from the client to the server. It asks the server to shut down, but to not exit
* (otherwise the response might not be delivered correctly to the client). There is a separate exit notification that
* asks the server to exit.
*
* @return void
*/
public function shutdown()
{
}
/**
* A notification to ask the server to exit its process.
*
* @return void
*/
public function exit()
{
exit(0);
}
}