-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathMainWindowViewModel.cs
More file actions
197 lines (170 loc) · 5.87 KB
/
MainWindowViewModel.cs
File metadata and controls
197 lines (170 loc) · 5.87 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.VisualStudio.LanguageServer.Protocol;
namespace LanguageServerWithUI
{
public class MainWindowViewModel : INotifyPropertyChanged
{
private string logMessage;
private ObservableCollection<DiagnosticTag> tags = new ObservableCollection<DiagnosticTag>();
private readonly LanguageServer.LanguageServer languageServer;
private string responseText;
private string currentSettings;
private MessageType messageType;
public MainWindowViewModel()
{
var stdInPipeName = @"input";
var stdOutPipeName = @"output";
var pipeAccessRule = new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(pipeAccessRule);
var readerPipe = new NamedPipeClientStream(stdInPipeName);
var writerPipe = new NamedPipeClientStream(stdOutPipeName);
readerPipe.Connect();
writerPipe.Connect();
this.languageServer = new LanguageServer.LanguageServer(writerPipe, readerPipe);
this.languageServer.Disconnected += OnDisconnected;
this.languageServer.PropertyChanged += OnLanguageServerPropertyChanged;
Tags.Add(new DiagnosticTag());
this.LogMessage = string.Empty;
this.ResponseText = string.Empty;
}
private void OnLanguageServerPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("CurrentSettings"))
{
this.CurrentSettings = this.languageServer.CurrentSettings;
}
}
internal void SendLogMessage()
{
this.languageServer.LogMessage(arg: null, message: this.LogMessage, messageType: this.MessageType);
}
internal void SendMessage()
{
this.languageServer.ShowMessage(message: this.LogMessage, messageType: this.MessageType);
}
internal void SendMessageRequest()
{
Task.Run(async () =>
{
MessageActionItem selectedAction = await this.languageServer.ShowMessageRequestAsync(message: this.LogMessage, messageType: this.MessageType, actionItems: new string[] { "option 1", "option 2", "option 3" });
this.ResponseText = $"The user selected: {selectedAction?.Title ?? "cancelled"}";
});
}
private void OnDisconnected(object sender, System.EventArgs e)
{
Application.Current.Dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
}
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<DiagnosticTag> Tags
{
get { return this.tags; }
}
public string LogMessage
{
get
{
return this.logMessage;
}
set
{
this.logMessage = value;
this.NotifyPropertyChanged(nameof(LogMessage));
}
}
public string ResponseText
{
get
{
return this.responseText;
}
set
{
this.responseText = value;
this.NotifyPropertyChanged(nameof(ResponseText));
}
}
public string CustomText
{
get
{
return this.languageServer.CustomText;
}
set
{
this.languageServer.CustomText = value;
this.NotifyPropertyChanged(nameof(CustomText));
}
}
public MessageType MessageType
{
get
{
return this.messageType;
}
set
{
this.messageType = value;
this.NotifyPropertyChanged(nameof(MessageType));
}
}
public string CurrentSettings
{
get
{
return this.currentSettings;
}
set
{
this.currentSettings = value;
this.NotifyPropertyChanged(nameof(CurrentSettings));
}
}
public void SendDiagnostics()
{
var diagnosticTags = Tags.ToDictionary((d) => d.Text, (d) => d.Severity);
this.languageServer.SetDiagnostics(diagnosticTags);
this.languageServer.SendDiagnostics();
}
private void NotifyPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DiagnosticTag : INotifyPropertyChanged
{
private string text;
private DiagnosticSeverity severity;
public event PropertyChangedEventHandler PropertyChanged;
public string Text
{
get { return this.text; }
set
{
this.text = value;
OnPropertyChanged(nameof(Text));
}
}
public DiagnosticSeverity Severity
{
get { return this.severity; }
set
{
this.severity = value;
OnPropertyChanged(nameof(Severity));
}
}
private void OnPropertyChanged(string property)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}