22
33namespace Phpactor \LanguageServer \Tests \Unit \Handler \TextDocument ;
44
5+ use Phpactor \LanguageServerProtocol \Position ;
6+ use Phpactor \LanguageServerProtocol \Range ;
7+ use Phpactor \LanguageServerProtocol \ServerCapabilities ;
8+ use Phpactor \LanguageServerProtocol \TextDocumentContentChangeIncrementalEvent ;
59use Phpactor \LanguageServerProtocol \TextDocumentIdentifier ;
610use Phpactor \LanguageServerProtocol \TextDocumentItem ;
11+ use Phpactor \LanguageServerProtocol \TextDocumentSyncKind ;
12+ use Phpactor \LanguageServer \Core \Handler \CanRegisterCapabilities ;
713use Phpactor \LanguageServer \Core \Handler \Handler ;
814use Phpactor \LanguageServer \Core \Rpc \ResponseMessage ;
915use Phpactor \LanguageServer \Event \TextDocumentClosed ;
16+ use Phpactor \LanguageServer \Event \TextDocumentIncrementallyUpdated ;
1017use Phpactor \LanguageServer \Event \TextDocumentOpened ;
1118use Phpactor \LanguageServer \Event \TextDocumentSaved ;
1219use Phpactor \LanguageServer \Event \TextDocumentUpdated ;
@@ -26,6 +33,11 @@ class TextDocumentHandlerTest extends HandlerTestCase
2633 */
2734 private ObjectProphecy $ dispatcher ;
2835
36+ /**
37+ * @var TextDocumentSyncKind::*
38+ */
39+ private int $ syncKind = TextDocumentSyncKind::FULL ;
40+
2941 protected function setUp (): void
3042 {
3143 $ this ->dispatcher = $ this ->prophesize (EventDispatcherInterface::class);
@@ -34,10 +46,31 @@ protected function setUp(): void
3446 public function handler (): Handler
3547 {
3648 return new TextDocumentHandler (
37- $ this ->dispatcher ->reveal ()
49+ $ this ->dispatcher ->reveal (),
50+ $ this ->syncKind ,
3851 );
3952 }
4053
54+ public function testSyncKindFull (): void
55+ {
56+ $ handler = $ this ->handler ();
57+ self ::assertInstanceOf (CanRegisterCapabilities::class, $ handler );
58+ $ capabiltiies = new ServerCapabilities ();
59+ $ handler ->registerCapabiltiies ($ capabiltiies );
60+ self ::assertEquals (TextDocumentSyncKind::FULL , $ capabiltiies ->textDocumentSync );
61+ }
62+
63+ public function testSyncKindIncremental (): void
64+ {
65+ $ this ->syncKind = TextDocumentSyncKind::INCREMENTAL ;
66+ $ handler = $ this ->handler ();
67+ self ::assertInstanceOf (CanRegisterCapabilities::class, $ handler );
68+ $ capabiltiies = new ServerCapabilities ();
69+ $ handler ->registerCapabiltiies ($ capabiltiies );
70+ self ::assertEquals (TextDocumentSyncKind::INCREMENTAL , $ capabiltiies ->textDocumentSync );
71+
72+ }
73+
4174 public function testOpensDocument (): void
4275 {
4376 $ textDocument = ProtocolFactory::textDocumentItem ('foobar ' , 'foo ' );
@@ -65,6 +98,48 @@ public function testUpdatesDocument(): void
6598 $ this ->dispatcher ->dispatch (new TextDocumentUpdated ($ identifier , 'asd ' ))->shouldHaveBeenCalled ();
6699 }
67100
101+ public function testUpdatesDocumentIncrementally (): void
102+ {
103+ $ textDocument = ProtocolFactory::textDocumentItem ('foobar ' , 'foo ' );
104+ $ identifier = ProtocolFactory::versionedTextDocumentIdentifier ('foobar ' , 1 );
105+
106+ $ this ->dispatch ('textDocument/didChange ' , [
107+ 'textDocument ' => $ identifier ,
108+ 'contentChanges ' => [
109+ [
110+ 'range ' => [
111+ 'start ' => [
112+ 'character ' => 0 ,
113+ 'line ' => 75 ,
114+ ],
115+ 'end ' => [
116+ 'character ' => 0 ,
117+ 'line ' => 75 ,
118+ ],
119+ ],
120+ 'rangeLength ' => 1 ,
121+ 'text ' => 'hello ' ,
122+ ],
123+ ],
124+ ]);
125+
126+ $ this ->dispatcher ->dispatch (
127+ new TextDocumentIncrementallyUpdated (
128+ $ identifier ,
129+ [
130+ new TextDocumentContentChangeIncrementalEvent (
131+ new Range (
132+ new Position (75 , 0 ),
133+ new Position (75 , 0 ),
134+ ),
135+ rangeLength: 1 ,
136+ text: 'hello ' ,
137+ ),
138+ ],
139+ )
140+ )->shouldHaveBeenCalled ();
141+ }
142+
68143 public function testWillSave (): void
69144 {
70145 $ response = $ this ->dispatch ('textDocument/willSave ' , [
0 commit comments