-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathTextDocumentSyncHandler.cs
179 lines (160 loc) · 6.91 KB
/
TextDocumentSyncHandler.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OmniSharp.Extensions.Embedded.MediatR;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
using OmniSharp.Models;
using OmniSharp.Models.FileClose;
using OmniSharp.Models.FileOpen;
using OmniSharp.Models.UpdateBuffer;
namespace OmniSharp.LanguageServerProtocol.Handlers
{
class TextDocumentSyncHandler : ITextDocumentSyncHandler
{
public static IEnumerable<IJsonRpcHandler> Enumerate(
RequestHandlers handlers,
OmniSharpWorkspace workspace)
{
foreach (var (selector, openHandler, closeHandler, bufferHandler) in handlers
.OfType<
Mef.IRequestHandler<FileOpenRequest, FileOpenResponse>,
Mef.IRequestHandler<FileCloseRequest, FileCloseResponse>,
Mef.IRequestHandler<UpdateBufferRequest, object>>())
{
// TODO: Fix once cake has working support for incremental
var documentSyncKind = TextDocumentSyncKind.Incremental;
if (selector.ToString().IndexOf(".cake") > -1) documentSyncKind = TextDocumentSyncKind.Full;
yield return new TextDocumentSyncHandler(openHandler, closeHandler, bufferHandler, selector, documentSyncKind, workspace);
}
}
// TODO Make this configurable?
private readonly DocumentSelector _documentSelector;
private SynchronizationCapability _capability;
private readonly Mef.IRequestHandler<FileOpenRequest, FileOpenResponse> _openHandler;
private readonly Mef.IRequestHandler<FileCloseRequest, FileCloseResponse> _closeHandler;
private readonly Mef.IRequestHandler<UpdateBufferRequest, object> _bufferHandler;
private readonly OmniSharpWorkspace _workspace;
public TextDocumentSyncHandler(
Mef.IRequestHandler<FileOpenRequest, FileOpenResponse> openHandler,
Mef.IRequestHandler<FileCloseRequest, FileCloseResponse> closeHandler,
Mef.IRequestHandler<UpdateBufferRequest, object> bufferHandler,
DocumentSelector documentSelector,
TextDocumentSyncKind documentSyncKind,
OmniSharpWorkspace workspace)
{
_openHandler = openHandler;
_closeHandler = closeHandler;
_bufferHandler = bufferHandler;
_workspace = workspace;
_documentSelector = documentSelector;
Change = documentSyncKind;
}
public TextDocumentSyncKind Change { get; }
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
{
var document = _workspace.GetDocument(Helpers.FromUri(uri));
if (document == null) return new TextDocumentAttributes(uri, "");
return new TextDocumentAttributes(uri, "");
}
public async Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken cancellationToken)
{
var contentChanges = notification.ContentChanges.ToArray();
if (contentChanges.Length == 1 && contentChanges[0].Range == null)
{
var change = contentChanges[0];
await _bufferHandler.Handle(new UpdateBufferRequest()
{
FileName = Helpers.FromUri(notification.TextDocument.Uri),
Buffer = change.Text
});
return Unit.Value;
}
var changes = contentChanges
.Select(change => new LinePositionSpanTextChange()
{
NewText = change.Text,
StartColumn = Convert.ToInt32(change.Range.Start.Character),
StartLine = Convert.ToInt32(change.Range.Start.Line),
EndColumn = Convert.ToInt32(change.Range.End.Character),
EndLine = Convert.ToInt32(change.Range.End.Line),
})
.ToArray();
await _bufferHandler.Handle(new UpdateBufferRequest()
{
FileName = Helpers.FromUri(notification.TextDocument.Uri),
Changes = changes
});
return Unit.Value;
}
public async Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationToken cancellationToken)
{
if (_openHandler != null)
{
await _openHandler.Handle(new FileOpenRequest()
{
Buffer = notification.TextDocument.Text,
FileName = Helpers.FromUri(notification.TextDocument.Uri)
});
}
return Unit.Value;
}
public async Task<Unit> Handle(DidCloseTextDocumentParams notification, CancellationToken cancellationToken)
{
if (_closeHandler != null)
{
await _closeHandler.Handle(new FileCloseRequest()
{
FileName = Helpers.FromUri(notification.TextDocument.Uri)
});
}
return Unit.Value;
}
public async Task<Unit> Handle(DidSaveTextDocumentParams notification, CancellationToken cancellationToken)
{
if (_capability?.DidSave == true)
{
await _bufferHandler.Handle(new UpdateBufferRequest()
{
FileName = Helpers.FromUri(notification.TextDocument.Uri),
Buffer = notification.Text
});
}
return Unit.Value;
}
TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions()
{
DocumentSelector = _documentSelector,
SyncKind = Change
};
}
TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector,
};
}
TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions()
{
DocumentSelector = _documentSelector,
IncludeText = true
};
}
public void SetCapability(SynchronizationCapability capability)
{
_capability = capability;
}
}
}