aboutsummaryrefslogtreecommitdiff
path: root/crates/server/src/main_loop
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-30 14:27:09 +0100
committerAleksey Kladov <[email protected]>2018-08-30 14:27:09 +0100
commit9fcebbc51284408203c05219a0ee92519f51ea74 (patch)
tree5fd3f53769e3fe5b8f6a0d2a5eaa86167b7a3b10 /crates/server/src/main_loop
parent7570d85869da7e2d35958047f8d1a90e3b6e2212 (diff)
subscriptions
Diffstat (limited to 'crates/server/src/main_loop')
-rw-r--r--crates/server/src/main_loop/handlers.rs10
-rw-r--r--crates/server/src/main_loop/mod.rs80
-rw-r--r--crates/server/src/main_loop/subscriptions.rs21
3 files changed, 67 insertions, 44 deletions
diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs
index 45083b084..6b70399b0 100644
--- a/crates/server/src/main_loop/handlers.rs
+++ b/crates/server/src/main_loop/handlers.rs
@@ -1,7 +1,7 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2 2
3use languageserver_types::{ 3use languageserver_types::{
4 Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, 4 Diagnostic, DiagnosticSeverity, DocumentSymbol,
5 Command, TextDocumentIdentifier, 5 Command, TextDocumentIdentifier,
6 SymbolInformation, Position, Location, TextEdit, 6 SymbolInformation, Position, Location, TextEdit,
7 CompletionItem, InsertTextFormat, CompletionItemKind, 7 CompletionItem, InsertTextFormat, CompletionItemKind,
@@ -325,9 +325,9 @@ pub fn handle_code_action(
325 325
326pub fn publish_diagnostics( 326pub fn publish_diagnostics(
327 world: ServerWorld, 327 world: ServerWorld,
328 uri: Url 328 file_id: FileId,
329) -> Result<req::PublishDiagnosticsParams> { 329) -> Result<req::PublishDiagnosticsParams> {
330 let file_id = world.uri_to_file_id(&uri)?; 330 let uri = world.file_id_to_uri(file_id)?;
331 let line_index = world.analysis().file_line_index(file_id); 331 let line_index = world.analysis().file_line_index(file_id);
332 let diagnostics = world.analysis().diagnostics(file_id) 332 let diagnostics = world.analysis().diagnostics(file_id)
333 .into_iter() 333 .into_iter()
@@ -344,9 +344,9 @@ pub fn publish_diagnostics(
344 344
345pub fn publish_decorations( 345pub fn publish_decorations(
346 world: ServerWorld, 346 world: ServerWorld,
347 uri: Url 347 file_id: FileId,
348) -> Result<req::PublishDecorationsParams> { 348) -> Result<req::PublishDecorationsParams> {
349 let file_id = world.uri_to_file_id(&uri)?; 349 let uri = world.file_id_to_uri(file_id)?;
350 Ok(req::PublishDecorationsParams { 350 Ok(req::PublishDecorationsParams {
351 uri, 351 uri,
352 decorations: highlight(&world, file_id), 352 decorations: highlight(&world, file_id),
diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs
index 0f66248a5..cd17cab56 100644
--- a/crates/server/src/main_loop/mod.rs
+++ b/crates/server/src/main_loop/mod.rs
@@ -1,4 +1,5 @@
1mod handlers; 1mod handlers;
2mod subscriptions;
2 3
3use std::{ 4use std::{
4 collections::{HashSet}, 5 collections::{HashSet},
@@ -6,7 +7,7 @@ use std::{
6 7
7use threadpool::ThreadPool; 8use threadpool::ThreadPool;
8use crossbeam_channel::{Sender, Receiver}; 9use crossbeam_channel::{Sender, Receiver};
9use languageserver_types::Url; 10use libanalysis::FileId;
10 11
11use { 12use {
12 req, dispatch, 13 req, dispatch,
@@ -14,6 +15,7 @@ use {
14 io::{Io, RawMsg, RawRequest, RawNotification}, 15 io::{Io, RawMsg, RawRequest, RawNotification},
15 vfs::FileEvent, 16 vfs::FileEvent,
16 server_world::{ServerWorldState, ServerWorld}, 17 server_world::{ServerWorldState, ServerWorld},
18 main_loop::subscriptions::{Subscriptions},
17}; 19};
18 20
19pub(super) fn main_loop( 21pub(super) fn main_loop(
@@ -28,6 +30,7 @@ pub(super) fn main_loop(
28 30
29 let mut pending_requests: HashSet<u64> = HashSet::new(); 31 let mut pending_requests: HashSet<u64> = HashSet::new();
30 let mut fs_events_receiver = Some(&fs_events_receiver); 32 let mut fs_events_receiver = Some(&fs_events_receiver);
33 let mut subs = Subscriptions::new();
31 loop { 34 loop {
32 enum Event { 35 enum Event {
33 Msg(RawMsg), 36 Msg(RawMsg),
@@ -47,7 +50,7 @@ pub(super) fn main_loop(
47 None => Event::FsWatcherDead, 50 None => Event::FsWatcherDead,
48 } 51 }
49 }; 52 };
50 53 let mut state_changed = false;
51 match event { 54 match event {
52 Event::ReceiverDead => { 55 Event::ReceiverDead => {
53 io.cleanup_receiver()?; 56 io.cleanup_receiver()?;
@@ -70,6 +73,7 @@ pub(super) fn main_loop(
70 Event::Fs(events) => { 73 Event::Fs(events) => {
71 trace!("fs change, {} events", events.len()); 74 trace!("fs change, {} events", events.len());
72 state.apply_fs_changes(events); 75 state.apply_fs_changes(events);
76 state_changed = true;
73 } 77 }
74 Event::Msg(msg) => { 78 Event::Msg(msg) => {
75 match msg { 79 match msg {
@@ -79,7 +83,8 @@ pub(super) fn main_loop(
79 } 83 }
80 } 84 }
81 RawMsg::Notification(not) => { 85 RawMsg::Notification(not) => {
82 on_notification(io, &mut state, pool, &task_sender, not)? 86 on_notification(io, &mut state, &mut subs, not)?;
87 state_changed = true;
83 } 88 }
84 RawMsg::Response(resp) => { 89 RawMsg::Response(resp) => {
85 if !pending_requests.remove(&resp.id) { 90 if !pending_requests.remove(&resp.id) {
@@ -89,6 +94,15 @@ pub(super) fn main_loop(
89 } 94 }
90 } 95 }
91 }; 96 };
97
98 if state_changed {
99 update_file_notifications_on_threadpool(
100 pool,
101 state.snapshot(),
102 task_sender.clone(),
103 subs.subscriptions(),
104 )
105 }
92 } 106 }
93} 107}
94 108
@@ -140,8 +154,7 @@ fn on_request(
140fn on_notification( 154fn on_notification(
141 io: &mut Io, 155 io: &mut Io,
142 state: &mut ServerWorldState, 156 state: &mut ServerWorldState,
143 pool: &ThreadPool, 157 subs: &mut Subscriptions,
144 sender: &Sender<Task>,
145 not: RawNotification, 158 not: RawNotification,
146) -> Result<()> { 159) -> Result<()> {
147 let mut not = Some(not); 160 let mut not = Some(not);
@@ -149,13 +162,8 @@ fn on_notification(
149 let uri = params.text_document.uri; 162 let uri = params.text_document.uri;
150 let path = uri.to_file_path() 163 let path = uri.to_file_path()
151 .map_err(|()| format_err!("invalid uri: {}", uri))?; 164 .map_err(|()| format_err!("invalid uri: {}", uri))?;
152 state.add_mem_file(path, params.text_document.text); 165 let file_id = state.add_mem_file(path, params.text_document.text);
153 update_file_notifications_on_threadpool( 166 subs.add_sub(file_id);
154 pool,
155 state.snapshot(),
156 sender.clone(),
157 uri,
158 );
159 Ok(()) 167 Ok(())
160 })?; 168 })?;
161 dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| { 169 dispatch::handle_notification::<req::DidChangeTextDocument, _>(&mut not, |mut params| {
@@ -166,23 +174,15 @@ fn on_notification(
166 .ok_or_else(|| format_err!("empty changes"))? 174 .ok_or_else(|| format_err!("empty changes"))?
167 .text; 175 .text;
168 state.change_mem_file(path.as_path(), text)?; 176 state.change_mem_file(path.as_path(), text)?;
169 update_file_notifications_on_threadpool(
170 pool,
171 state.snapshot(),
172 sender.clone(),
173 uri,
174 );
175 Ok(()) 177 Ok(())
176 })?; 178 })?;
177 dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| { 179 dispatch::handle_notification::<req::DidCloseTextDocument, _>(&mut not, |params| {
178 let uri = params.text_document.uri; 180 let uri = params.text_document.uri;
179 let path = uri.to_file_path() 181 let path = uri.to_file_path()
180 .map_err(|()| format_err!("invalid uri: {}", uri))?; 182 .map_err(|()| format_err!("invalid uri: {}", uri))?;
181 state.remove_mem_file(path.as_path())?; 183 let file_id = state.remove_mem_file(path.as_path())?;
182 let not = req::PublishDiagnosticsParams { 184 subs.remove_sub(file_id);
183 uri, 185 let not = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() };
184 diagnostics: Vec::new(),
185 };
186 let not = dispatch::send_notification::<req::PublishDiagnostics>(not); 186 let not = dispatch::send_notification::<req::PublishDiagnostics>(not);
187 io.send(RawMsg::Notification(not)); 187 io.send(RawMsg::Notification(not));
188 Ok(()) 188 Ok(())
@@ -227,25 +227,27 @@ fn update_file_notifications_on_threadpool(
227 pool: &ThreadPool, 227 pool: &ThreadPool,
228 world: ServerWorld, 228 world: ServerWorld,
229 sender: Sender<Task>, 229 sender: Sender<Task>,
230 uri: Url, 230 subscriptions: Vec<FileId>,
231) { 231) {
232 pool.execute(move || { 232 pool.execute(move || {
233 match handlers::publish_diagnostics(world.clone(), uri.clone()) { 233 for file_id in subscriptions {
234 Err(e) => { 234 match handlers::publish_diagnostics(world.clone(), file_id) {
235 error!("failed to compute diagnostics: {:?}", e) 235 Err(e) => {
236 } 236 error!("failed to compute diagnostics: {:?}", e)
237 Ok(params) => { 237 }
238 let not = dispatch::send_notification::<req::PublishDiagnostics>(params); 238 Ok(params) => {
239 sender.send(Task::Notify(not)); 239 let not = dispatch::send_notification::<req::PublishDiagnostics>(params);
240 } 240 sender.send(Task::Notify(not));
241 } 241 }
242 match handlers::publish_decorations(world, uri) {
243 Err(e) => {
244 error!("failed to compute decorations: {:?}", e)
245 } 242 }
246 Ok(params) => { 243 match handlers::publish_decorations(world.clone(), file_id) {
247 let not = dispatch::send_notification::<req::PublishDecorations>(params); 244 Err(e) => {
248 sender.send(Task::Notify(not)) 245 error!("failed to compute decorations: {:?}", e)
246 }
247 Ok(params) => {
248 let not = dispatch::send_notification::<req::PublishDecorations>(params);
249 sender.send(Task::Notify(not))
250 }
249 } 251 }
250 } 252 }
251 }); 253 });
diff --git a/crates/server/src/main_loop/subscriptions.rs b/crates/server/src/main_loop/subscriptions.rs
new file mode 100644
index 000000000..963096aef
--- /dev/null
+++ b/crates/server/src/main_loop/subscriptions.rs
@@ -0,0 +1,21 @@
1use std::collections::HashSet;
2use libanalysis::FileId;
3
4pub struct Subscriptions {
5 subs: HashSet<FileId>,
6}
7
8impl Subscriptions {
9 pub fn new() -> Subscriptions {
10 Subscriptions { subs: HashSet::new() }
11 }
12 pub fn add_sub(&mut self, file_id: FileId) {
13 self.subs.insert(file_id);
14 }
15 pub fn remove_sub(&mut self, file_id: FileId) {
16 self.subs.remove(&file_id);
17 }
18 pub fn subscriptions(&self) -> Vec<FileId> {
19 self.subs.iter().cloned().collect()
20 }
21}