-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
206 lines (163 loc) · 6.35 KB
/
main.rs
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
198
199
200
201
202
203
204
205
206
use ::futures::future::try_join;
use anyhow::{Context, Result};
use clap::Parser;
use rust_search::SearchBuilder;
use serde_json::{json, Value};
use tokio::io::{self, AsyncReadExt, AsyncWriteExt, BufReader};
use roslyn_language_server::{
notification::{
add_content_length_header, Notification, Params, ProjectParams, SolutionParams,
},
roslyn::start_roslyn,
server_version::SERVER_VERSION,
};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Remove old versions of Microsoft.CodeAnalysis.LanguageServer
#[arg(short, long, default_value_t = true)]
remove_old_server_versions: bool,
}
#[tokio::main]
async fn main() {
let args = Args::parse();
let version = SERVER_VERSION;
let pipe = start_roslyn(version, args.remove_old_server_versions).await;
let (reader, mut writer) = tokio::io::split(pipe);
let stdin = io::stdin();
let mut stdout = io::stdout();
let stream_to_stdout = async {
let mut reader = BufReader::new(reader);
loop {
let mut buffer = vec![0; 3048];
let bytes_read = reader
.read(&mut buffer)
.await
.expect("Unable to read incoming server notification");
if bytes_read == 0 {
break; // EOF reached
}
let notification = String::from_utf8(buffer[..bytes_read].to_vec())
.expect("Unable to convert buffer to string");
if notification.contains("capabilities") {
let patched_result_notification = force_pull_diagnostics_hack(¬ification)?;
stdout
.write_all(patched_result_notification.as_bytes())
.await?;
break;
}
stdout
.write_all(&buffer[..bytes_read])
.await
.expect("Unable to forward client notification to server");
}
io::copy(&mut reader, &mut stdout).await
};
let stdin_to_stream = async {
let mut stdin = BufReader::new(stdin);
loop {
let mut buffer = vec![0; 6000];
let bytes_read = stdin
.read(&mut buffer)
.await
.expect("Unable to read incoming client notification");
if bytes_read == 0 {
break; // EOF reached
}
writer
.write_all(&buffer[..bytes_read])
.await
.expect("Unable to forward client notification to server");
let notification = String::from_utf8(buffer[..bytes_read].to_vec())
.expect("Unable to convert buffer to string");
if notification.contains("initialize") {
let root_path = parse_root_path(¬ification)
.expect("Root path not part of initialize notification");
let solution_files = find_extension(&root_path, "sln");
let solution_to_open = solution_files.first().map(|found| found.to_owned());
if let Some(solution_to_open) = solution_to_open {
let open_solution_notification =
create_open_solution_notification(&solution_to_open);
writer
.write_all(open_solution_notification.as_bytes())
.await
.expect("Unable to send open solution notification to server");
break;
}
let project_files = find_extension(&root_path, "csproj");
let open_projects_notification = create_open_projects_notification(project_files);
writer
.write_all(open_projects_notification.as_bytes())
.await
.expect("Unable to send open projects notification to server");
break;
}
}
io::copy(&mut stdin, &mut writer).await
};
try_join(stdin_to_stream, stream_to_stdout)
.await
.expect("Will never finish");
}
fn parse_root_path(notification: &str) -> Result<String> {
let json_start = notification
.find('{')
.context("Notification was not json")?;
let parsed_notification: Value = serde_json::from_str(¬ification[json_start..])?;
let root_path = (parsed_notification["params"]["rootUri"]
.as_str()
.map(uri_to_path))
.or_else(|| parsed_notification["params"]["rootPath"].as_str())
.context("Root URI/path was not given by the client")?;
Ok(root_path.to_string())
}
fn find_extension(root_path: &str, extension: &str) -> Vec<String> {
SearchBuilder::default()
.location(root_path)
.ext(extension)
.build()
.collect()
}
fn create_open_solution_notification(file_path: &str) -> String {
let notification = Notification {
jsonrpc: "2.0".to_string(),
method: "solution/open".to_string(),
params: Params::Solution(SolutionParams {
solution: path_to_uri(file_path),
}),
};
notification.serialize()
}
fn path_to_uri(file_path: &str) -> String {
format!("file://{file_path}")
}
fn uri_to_path(uri: &str) -> &str {
uri.strip_prefix("file://")
.expect("URI should start with \"file://\"")
}
fn create_open_projects_notification(file_paths: Vec<String>) -> String {
let uris: Vec<String> = file_paths
.iter()
.map(|file_path| path_to_uri(file_path))
.collect();
let notification = Notification {
jsonrpc: "2.0".to_string(),
method: "project/open".to_string(),
params: Params::Project(ProjectParams { projects: uris }),
};
notification.serialize()
}
fn force_pull_diagnostics_hack(notification: &str) -> Result<String, std::io::Error> {
let json_start = notification.find('{').ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No JSON start found",
))?;
let mut parsed_notification: Value = serde_json::from_str(¬ification[json_start..])?;
let diagnostic_provider = json!({
"interFileDependencies": true,
"workDoneProgress": true,
"workspaceDiagnostics": true
});
parsed_notification["result"]["capabilities"]["diagnosticProvider"] = diagnostic_provider;
Ok(add_content_length_header(&parsed_notification.to_string()))
}