-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathErrorRequest.cs
141 lines (125 loc) · 4.99 KB
/
ErrorRequest.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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Session;
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Event;
using Microsoft.PowerShell.EditorServices.Transport.Stdio.Message;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.PowerShell.EditorServices.Transport.Stdio.Request
{
[MessageTypeName("geterr")]
public class ErrorRequest : RequestBase<ErrorRequestArguments>
{
private static CancellationTokenSource existingRequestCancellation;
public static ErrorRequest Create(params string[] filePaths)
{
return new ErrorRequest
{
Arguments = new ErrorRequestArguments
{
Files = filePaths
}
};
}
public override void ProcessMessage(
EditorSession editorSession,
MessageWriter messageWriter)
{
List<ScriptFile> fileList = new List<ScriptFile>();
// If there's an existing task, attempt to cancel it
try
{
if (existingRequestCancellation != null)
{
// Try to cancel the request
existingRequestCancellation.Cancel();
// If cancellation didn't throw an exception,
// clean up the existing token
existingRequestCancellation.Dispose();
existingRequestCancellation = null;
}
}
catch (Exception e)
{
// TODO: Catch a more specific exception!
Logger.Write(
LogLevel.Error,
string.Format(
"Exception while cancelling analysis task:\n\n{0}",
e.ToString()));
return;
}
// Create a fresh cancellation token and then start the task.
// We create this on a different TaskScheduler so that we
// don't block the main message loop thread.
// TODO: Is there a better way to do this?
existingRequestCancellation = new CancellationTokenSource();
Task.Factory.StartNew(
() =>
DelayThenInvokeDiagnostics(
this.Arguments.Delay,
this.Arguments.Files,
editorSession,
messageWriter,
existingRequestCancellation.Token),
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.Default);
}
private static async Task DelayThenInvokeDiagnostics(
int delayMilliseconds,
string[] filesToAnalyze,
EditorSession editorSession,
MessageWriter messageWriter,
CancellationToken cancellationToken)
{
// First of all, wait for the desired delay period before
// analyzing the provided list of files
try
{
await Task.Delay(delayMilliseconds, cancellationToken);
}
catch (TaskCanceledException)
{
// If the task is cancelled, exit directly
return;
}
// If we've made it past the delay period then we don't care
// about the cancellation token anymore. This could happen
// when the user stops typing for long enough that the delay
// period ends but then starts typing while analysis is going
// on. It makes sense to send back the results from the first
// delay period while the second one is ticking away.
// Get the requested files
foreach (string filePath in filesToAnalyze)
{
ScriptFile scriptFile =
editorSession.Workspace.GetFile(
filePath);
var semanticMarkers =
editorSession.AnalysisService.GetSemanticMarkers(
scriptFile);
// Always send syntax and semantic errors. We want to
// make sure no out-of-date markers are being displayed.
messageWriter.WriteMessage(
SyntaxDiagnosticEvent.Create(
scriptFile.FilePath,
scriptFile.SyntaxMarkers));
messageWriter.WriteMessage(
SemanticDiagnosticEvent.Create(
scriptFile.FilePath,
semanticMarkers));
}
}
}
public class ErrorRequestArguments
{
public string[] Files { get; set; }
public int Delay { get; set; }
}
}