forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextDocumentHandler.cs
167 lines (142 loc) · 6.13 KB
/
TextDocumentHandler.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using MediatR;
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;
namespace Microsoft.PowerShell.EditorServices.Handlers
{
class TextDocumentHandler : ITextDocumentSyncHandler
{
private readonly ILogger _logger;
private readonly AnalysisService _analysisService;
private readonly WorkspaceService _workspaceService;
private readonly RemoteFileManagerService _remoteFileManagerService;
private readonly DocumentSelector _documentSelector = new DocumentSelector(
new DocumentFilter()
{
Language = "powershell"
}
);
private SynchronizationCapability _capability;
public TextDocumentSyncKind Change => TextDocumentSyncKind.Incremental;
public TextDocumentHandler(
ILoggerFactory factory,
AnalysisService analysisService,
WorkspaceService workspaceService,
RemoteFileManagerService remoteFileManagerService)
{
_logger = factory.CreateLogger<TextDocumentHandler>();
_analysisService = analysisService;
_workspaceService = workspaceService;
_remoteFileManagerService = remoteFileManagerService;
}
public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken token)
{
ScriptFile changedFile = _workspaceService.GetFile(notification.TextDocument.Uri.ToString());
// A text change notification can batch multiple change requests
foreach (TextDocumentContentChangeEvent textChange in notification.ContentChanges)
{
changedFile.ApplyChange(
GetFileChangeDetails(
textChange.Range,
textChange.Text));
}
// TODO: Get all recently edited files in the workspace
_analysisService.RunScriptDiagnosticsAsync(new ScriptFile[] { changedFile });
return Unit.Task;
}
TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentChangeRegistrationOptions()
{
DocumentSelector = _documentSelector,
SyncKind = Change
};
}
public void SetCapability(SynchronizationCapability capability)
{
_capability = capability;
}
public Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationToken token)
{
ScriptFile openedFile =
_workspaceService.GetFileBuffer(
notification.TextDocument.Uri.ToString(),
notification.TextDocument.Text);
// TODO: Get all recently edited files in the workspace
_analysisService.RunScriptDiagnosticsAsync(new ScriptFile[] { openedFile });
_logger.LogTrace("Finished opening document.");
return Unit.Task;
}
TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector,
};
}
public Task<Unit> Handle(DidCloseTextDocumentParams notification, CancellationToken token)
{
// Find and close the file in the current session
var fileToClose = _workspaceService.GetFile(notification.TextDocument.Uri.ToString());
if (fileToClose != null)
{
_workspaceService.CloseFile(fileToClose);
_analysisService.ClearMarkers(fileToClose);
}
_logger.LogTrace("Finished closing document.");
return Unit.Task;
}
public async Task<Unit> Handle(DidSaveTextDocumentParams notification, CancellationToken token)
{
ScriptFile savedFile =
_workspaceService.GetFile(
notification.TextDocument.Uri.ToString());
if (savedFile != null)
{
if (_remoteFileManagerService.IsUnderRemoteTempPath(savedFile.FilePath))
{
await _remoteFileManagerService.SaveRemoteFileAsync(savedFile.FilePath);
}
}
return Unit.Value;
}
TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
{
return new TextDocumentSaveRegistrationOptions()
{
DocumentSelector = _documentSelector,
IncludeText = true
};
}
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
{
return new TextDocumentAttributes(uri, "powershell");
}
private static FileChange GetFileChangeDetails(Range changeRange, string insertString)
{
// The protocol's positions are zero-based so add 1 to all offsets
if (changeRange == null) return new FileChange { InsertString = insertString, IsReload = true };
return new FileChange
{
InsertString = insertString,
Line = (int)(changeRange.Start.Line + 1),
Offset = (int)(changeRange.Start.Character + 1),
EndLine = (int)(changeRange.End.Line + 1),
EndOffset = (int)(changeRange.End.Character + 1),
IsReload = false
};
}
}
}