forked from microsoft/python-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
140 lines (119 loc) · 5.8 KB
/
Program.cs
File metadata and controls
140 lines (119 loc) · 5.8 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
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// #define WAIT_FOR_DEBUGGER
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Python.Core.IO;
using Microsoft.Python.Core.OS;
using Microsoft.Python.Core.Services;
using Microsoft.Python.Core.Threading;
using Microsoft.Python.LanguageServer.Services;
using Newtonsoft.Json;
using StreamJsonRpc;
using StreamJsonRpc.Protocol;
namespace Microsoft.Python.LanguageServer.Server {
internal static class Program {
public static void Main(string[] args) {
CheckDebugMode();
using (CoreShell.Create()) {
var services = CoreShell.Current.ServiceManager;
var messageFormatter = new JsonMessageFormatter();
// StreamJsonRpc v1.4 serializer defaults
messageFormatter.JsonSerializer.NullValueHandling = NullValueHandling.Ignore;
messageFormatter.JsonSerializer.ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor;
messageFormatter.JsonSerializer.Converters.Add(new UriConverter());
using (var cin = Console.OpenStandardInput())
using (var cout = Console.OpenStandardOutput())
using (var server = new Implementation.LanguageServer())
using (var rpc = new LanguageServerJsonRpc(cout, cin, messageFormatter, server)) {
rpc.TraceSource.Switch.Level = SourceLevels.Error;
rpc.SynchronizationContext = new SingleThreadSynchronizationContext();
var clientApp = new ClientApplication(rpc);
var osp = new OSPlatform();
services
.AddService(clientApp)
.AddService(new Logger(clientApp))
.AddService(new UIService(clientApp))
.AddService(new ProgressService(clientApp))
.AddService(new TelemetryService(clientApp))
.AddService(new IdleTimeService())
.AddService(osp)
.AddService(new ProcessServices())
.AddService(new FileSystem(osp));
services.AddService(messageFormatter.JsonSerializer);
var token = server.Start(services, rpc);
rpc.StartListening();
// Wait for the "exit" request, it will terminate the process.
token.WaitHandle.WaitOne();
}
}
}
private static void CheckDebugMode() {
#if WAIT_FOR_DEBUGGER
var start = DateTime.Now;
while (!System.Diagnostics.Debugger.IsAttached) {
System.Threading.Thread.Sleep(1000);
if ((DateTime.Now - start).TotalMilliseconds > 15000) {
break;
}
}
#endif
}
private class LanguageServerJsonRpc : JsonRpc {
public LanguageServerJsonRpc(Stream sendingStream, Stream receivingStream, IJsonRpcMessageFormatter formatter, object target)
: base(new HeaderDelimitedMessageHandler(sendingStream, receivingStream, formatter), target) { }
protected override JsonRpcError.ErrorDetail CreateErrorDetails(JsonRpcRequest request, Exception exception) {
var localRpcEx = exception as LocalRpcException;
return new JsonRpcError.ErrorDetail {
Code = (JsonRpcErrorCode?)localRpcEx?.ErrorCode ?? JsonRpcErrorCode.InvocationError,
Message = exception.Message,
Data = exception.StackTrace,
};
}
}
}
sealed class UriConverter : JsonConverter {
public override bool CanConvert(Type objectType) => objectType == typeof(Uri);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
if (reader.TokenType == JsonToken.String) {
var str = (string)reader.Value;
return new Uri(str.Replace("%3A", ":"));
}
if (reader.TokenType == JsonToken.Null) {
return null;
}
throw new InvalidOperationException($"UriConverter: unsupported token type {reader.TokenType}");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
if (null == value) {
writer.WriteNull();
return;
}
if (value is Uri uri) {
var scheme = uri.Scheme;
var str = uri.ToString();
// If URI does not have :// it is most probably untitled: scheme in which
// there is no path so we don't have to escape or deal with slashes.
if (str.Contains("://")) {
str = uri.Scheme + "://" + str.Substring(scheme.Length + 3).Replace(":", "%3A").Replace('\\', '/');
}
writer.WriteValue(str);
return;
}
throw new InvalidOperationException($"UriConverter: unsupported value type {value.GetType()}");
}
}
}