aboutsummaryrefslogtreecommitdiff
path: root/crates/flycheck/src/lib.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-25 08:28:27 +0100
committerGitHub <[email protected]>2020-06-25 08:28:27 +0100
commit0d2e695ffcd1e96cd3a6603a1d5f387f5bcfbf2e (patch)
tree6653b04e51bd538773db8f4bcb63dd29f8ca0625 /crates/flycheck/src/lib.rs
parentedf8f419b532b93102222b9deb2c8bb5080911d9 (diff)
parent941d4bfa53ebf4dd683889c2151544bcc3095073 (diff)
Merge #5050
5050: Simplify r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/flycheck/src/lib.rs')
-rw-r--r--crates/flycheck/src/lib.rs76
1 files changed, 34 insertions, 42 deletions
diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs
index af75adbe2..3e73cf6ff 100644
--- a/crates/flycheck/src/lib.rs
+++ b/crates/flycheck/src/lib.rs
@@ -10,7 +10,6 @@ use std::{
10 time::Instant, 10 time::Instant,
11}; 11};
12 12
13use cargo_metadata::Message;
14use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender}; 13use crossbeam_channel::{never, select, unbounded, Receiver, RecvError, Sender};
15 14
16pub use cargo_metadata::diagnostic::{ 15pub use cargo_metadata::diagnostic::{
@@ -50,17 +49,17 @@ impl fmt::Display for FlycheckConfig {
50#[derive(Debug)] 49#[derive(Debug)]
51pub struct FlycheckHandle { 50pub struct FlycheckHandle {
52 // XXX: drop order is significant 51 // XXX: drop order is significant
53 cmd_send: Sender<CheckCommand>, 52 cmd_send: Sender<Restart>,
54 handle: jod_thread::JoinHandle<()>, 53 handle: jod_thread::JoinHandle,
55} 54}
56 55
57impl FlycheckHandle { 56impl FlycheckHandle {
58 pub fn spawn( 57 pub fn spawn(
59 sender: Box<dyn Fn(CheckTask) + Send>, 58 sender: Box<dyn Fn(Message) + Send>,
60 config: FlycheckConfig, 59 config: FlycheckConfig,
61 workspace_root: PathBuf, 60 workspace_root: PathBuf,
62 ) -> FlycheckHandle { 61 ) -> FlycheckHandle {
63 let (cmd_send, cmd_recv) = unbounded::<CheckCommand>(); 62 let (cmd_send, cmd_recv) = unbounded::<Restart>();
64 let handle = jod_thread::spawn(move || { 63 let handle = jod_thread::spawn(move || {
65 FlycheckActor::new(sender, config, workspace_root).run(&cmd_recv); 64 FlycheckActor::new(sender, config, workspace_root).run(&cmd_recv);
66 }); 65 });
@@ -69,12 +68,12 @@ impl FlycheckHandle {
69 68
70 /// Schedule a re-start of the cargo check worker. 69 /// Schedule a re-start of the cargo check worker.
71 pub fn update(&self) { 70 pub fn update(&self) {
72 self.cmd_send.send(CheckCommand::Update).unwrap(); 71 self.cmd_send.send(Restart).unwrap();
73 } 72 }
74} 73}
75 74
76#[derive(Debug)] 75#[derive(Debug)]
77pub enum CheckTask { 76pub enum Message {
78 /// Request a clearing of all cached diagnostics from the check watcher 77 /// Request a clearing of all cached diagnostics from the check watcher
79 ClearDiagnostics, 78 ClearDiagnostics,
80 79
@@ -82,23 +81,20 @@ pub enum CheckTask {
82 AddDiagnostic { workspace_root: PathBuf, diagnostic: Diagnostic }, 81 AddDiagnostic { workspace_root: PathBuf, diagnostic: Diagnostic },
83 82
84 /// Request check progress notification to client 83 /// Request check progress notification to client
85 Status(Status), 84 Progress(Progress),
86} 85}
87 86
88#[derive(Debug)] 87#[derive(Debug)]
89pub enum Status { 88pub enum Progress {
90 Being, 89 Being,
91 Progress(String), 90 DidCheckCrate(String),
92 End, 91 End,
93} 92}
94 93
95pub enum CheckCommand { 94struct Restart;
96 /// Request re-start of check thread
97 Update,
98}
99 95
100struct FlycheckActor { 96struct FlycheckActor {
101 sender: Box<dyn Fn(CheckTask) + Send>, 97 sender: Box<dyn Fn(Message) + Send>,
102 config: FlycheckConfig, 98 config: FlycheckConfig,
103 workspace_root: PathBuf, 99 workspace_root: PathBuf,
104 last_update_req: Option<Instant>, 100 last_update_req: Option<Instant>,
@@ -109,12 +105,12 @@ struct FlycheckActor {
109 /// doesn't provide a way to read sub-process output without blocking, so we 105 /// doesn't provide a way to read sub-process output without blocking, so we
110 /// have to wrap sub-processes output handling in a thread and pass messages 106 /// have to wrap sub-processes output handling in a thread and pass messages
111 /// back over a channel. 107 /// back over a channel.
112 check_process: Option<jod_thread::JoinHandle<()>>, 108 check_process: Option<jod_thread::JoinHandle>,
113} 109}
114 110
115impl FlycheckActor { 111impl FlycheckActor {
116 fn new( 112 fn new(
117 sender: Box<dyn Fn(CheckTask) + Send>, 113 sender: Box<dyn Fn(Message) + Send>,
118 config: FlycheckConfig, 114 config: FlycheckConfig,
119 workspace_root: PathBuf, 115 workspace_root: PathBuf,
120 ) -> FlycheckActor { 116 ) -> FlycheckActor {
@@ -128,14 +124,14 @@ impl FlycheckActor {
128 } 124 }
129 } 125 }
130 126
131 fn run(&mut self, cmd_recv: &Receiver<CheckCommand>) { 127 fn run(&mut self, cmd_recv: &Receiver<Restart>) {
132 // If we rerun the thread, we need to discard the previous check results first 128 // If we rerun the thread, we need to discard the previous check results first
133 self.clean_previous_results(); 129 self.clean_previous_results();
134 130
135 loop { 131 loop {
136 select! { 132 select! {
137 recv(&cmd_recv) -> cmd => match cmd { 133 recv(&cmd_recv) -> cmd => match cmd {
138 Ok(cmd) => self.handle_command(cmd), 134 Ok(Restart) => self.last_update_req = Some(Instant::now()),
139 Err(RecvError) => { 135 Err(RecvError) => {
140 // Command channel has closed, so shut down 136 // Command channel has closed, so shut down
141 break; 137 break;
@@ -154,15 +150,15 @@ impl FlycheckActor {
154 150
155 if self.should_recheck() { 151 if self.should_recheck() {
156 self.last_update_req = None; 152 self.last_update_req = None;
157 self.send(CheckTask::ClearDiagnostics); 153 self.send(Message::ClearDiagnostics);
158 self.restart_check_process(); 154 self.restart_check_process();
159 } 155 }
160 } 156 }
161 } 157 }
162 158
163 fn clean_previous_results(&self) { 159 fn clean_previous_results(&self) {
164 self.send(CheckTask::ClearDiagnostics); 160 self.send(Message::ClearDiagnostics);
165 self.send(CheckTask::Status(Status::End)); 161 self.send(Message::Progress(Progress::End));
166 } 162 }
167 163
168 fn should_recheck(&mut self) -> bool { 164 fn should_recheck(&mut self) -> bool {
@@ -175,37 +171,31 @@ impl FlycheckActor {
175 false 171 false
176 } 172 }
177 173
178 fn handle_command(&mut self, cmd: CheckCommand) {
179 match cmd {
180 CheckCommand::Update => self.last_update_req = Some(Instant::now()),
181 }
182 }
183
184 fn handle_message(&self, msg: CheckEvent) { 174 fn handle_message(&self, msg: CheckEvent) {
185 match msg { 175 match msg {
186 CheckEvent::Begin => { 176 CheckEvent::Begin => {
187 self.send(CheckTask::Status(Status::Being)); 177 self.send(Message::Progress(Progress::Being));
188 } 178 }
189 179
190 CheckEvent::End => { 180 CheckEvent::End => {
191 self.send(CheckTask::Status(Status::End)); 181 self.send(Message::Progress(Progress::End));
192 } 182 }
193 183
194 CheckEvent::Msg(Message::CompilerArtifact(msg)) => { 184 CheckEvent::Msg(cargo_metadata::Message::CompilerArtifact(msg)) => {
195 self.send(CheckTask::Status(Status::Progress(msg.target.name))); 185 self.send(Message::Progress(Progress::DidCheckCrate(msg.target.name)));
196 } 186 }
197 187
198 CheckEvent::Msg(Message::CompilerMessage(msg)) => { 188 CheckEvent::Msg(cargo_metadata::Message::CompilerMessage(msg)) => {
199 self.send(CheckTask::AddDiagnostic { 189 self.send(Message::AddDiagnostic {
200 workspace_root: self.workspace_root.clone(), 190 workspace_root: self.workspace_root.clone(),
201 diagnostic: msg.message, 191 diagnostic: msg.message,
202 }); 192 });
203 } 193 }
204 194
205 CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} 195 CheckEvent::Msg(cargo_metadata::Message::BuildScriptExecuted(_))
206 CheckEvent::Msg(Message::BuildFinished(_)) => {} 196 | CheckEvent::Msg(cargo_metadata::Message::BuildFinished(_))
207 CheckEvent::Msg(Message::TextLine(_)) => {} 197 | CheckEvent::Msg(cargo_metadata::Message::TextLine(_))
208 CheckEvent::Msg(Message::Unknown) => {} 198 | CheckEvent::Msg(cargo_metadata::Message::Unknown) => {}
209 } 199 }
210 } 200 }
211 201
@@ -256,9 +246,11 @@ impl FlycheckActor {
256 let res = run_cargo(cmd, &mut |message| { 246 let res = run_cargo(cmd, &mut |message| {
257 // Skip certain kinds of messages to only spend time on what's useful 247 // Skip certain kinds of messages to only spend time on what's useful
258 match &message { 248 match &message {
259 Message::CompilerArtifact(artifact) if artifact.fresh => return true, 249 cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => {
260 Message::BuildScriptExecuted(_) => return true, 250 return true
261 Message::Unknown => return true, 251 }
252 cargo_metadata::Message::BuildScriptExecuted(_)
253 | cargo_metadata::Message::Unknown => return true,
262 _ => {} 254 _ => {}
263 } 255 }
264 256
@@ -278,7 +270,7 @@ impl FlycheckActor {
278 })) 270 }))
279 } 271 }
280 272
281 fn send(&self, check_task: CheckTask) { 273 fn send(&self, check_task: Message) {
282 (self.sender)(check_task) 274 (self.sender)(check_task)
283 } 275 }
284} 276}