]> git.lizzy.rs Git - rust.git/commitdiff
refactor
authorSimon Vandel Sillesen <simon.vandel@gmail.com>
Sun, 6 Jan 2019 08:41:11 +0000 (09:41 +0100)
committerSimon Vandel Sillesen <simon.vandel@gmail.com>
Sun, 6 Jan 2019 08:41:11 +0000 (09:41 +0100)
crates/ra_lsp_server/src/main_loop.rs
crates/ra_lsp_server/src/main_loop/handlers.rs

index 60d9671dee7b139049d7c67c5b5c554802a371ea..2dc1be26aac1f4dafbc4a104172bb44f7bfc3886 100644 (file)
@@ -1,13 +1,11 @@
 mod handlers;
 mod subscriptions;
 
-use std::{
-    fmt,
-    path::PathBuf,
-    sync::Arc,
-};
+use std::{fmt, path::PathBuf, sync::Arc};
 
-use crossbeam_channel::{unbounded, select, Receiver, Sender, RecvError};
+use crossbeam_channel::{select, unbounded, Receiver, RecvError, Sender};
+use failure::{bail, format_err};
+use failure_derive::Fail;
 use gen_lsp_server::{
     handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
 };
 use ra_analysis::{Canceled, FileId, LibraryData};
 use ra_vfs::VfsTask;
 use rayon;
-use threadpool::ThreadPool;
 use rustc_hash::FxHashSet;
 use serde::{de::DeserializeOwned, Serialize};
-use failure::{format_err, bail};
-use failure_derive::Fail;
+use threadpool::ThreadPool;
 
 use crate::{
     main_loop::subscriptions::Subscriptions,
index 2ec9073e402e3e0d9600e983e19ee2a67eea891f..51f134e8a4088bad9baf437a6a77061a4570a22c 100644 (file)
@@ -93,36 +93,31 @@ pub fn handle_on_type_formatting(
     world: ServerWorld,
     params: req::DocumentOnTypeFormattingParams,
 ) -> Result<Option<Vec<TextEdit>>> {
-    if params.ch != "=" || params.ch != "." {
-        return Ok(None);
-    }
-
-    let file_id = params.text_document.try_conv_with(&world)?;
-    let line_index = world.analysis().file_line_index(file_id);
-    let position = FilePosition {
-        file_id,
-        offset: params.position.conv_with(&line_index),
+    let analysis: Option<Box<Fn(FilePosition) -> Option<SourceChange>>> = match params.ch.as_str() {
+        "=" => Some(Box::new(|pos| world.analysis().on_eq_typed(pos))),
+        "." => Some(Box::new(|pos| world.analysis().on_dot_typed(pos))),
+        _ => None,
     };
 
-    let analysis: Vec<Box<Fn(FilePosition) -> Option<SourceChange>>> = vec![
-        Box::new(|pos| world.analysis().on_eq_typed(pos)),
-        Box::new(|pos| world.analysis().on_dot_typed(pos)),
-    ];
+    if let Some(ana) = analysis {
+        let file_id = params.text_document.try_conv_with(&world)?;
+        let line_index = world.analysis().file_line_index(file_id);
+        let position = FilePosition {
+            file_id,
+            offset: params.position.conv_with(&line_index),
+        };
 
-    // try all analysis until one succeeds
-    for ana in analysis {
         if let Some(mut action) = ana(position) {
-            return Ok(Some(
-                action
-                    .source_file_edits
-                    .pop()
-                    .unwrap()
-                    .edit
-                    .as_atoms()
-                    .iter()
-                    .map_conv_with(&line_index)
-                    .collect(),
-            ));
+            let change: Vec<TextEdit> = action
+                .source_file_edits
+                .pop()
+                .unwrap()
+                .edit
+                .as_atoms()
+                .iter()
+                .map_conv_with(&line_index)
+                .collect();
+            return Ok(Some(change));
         }
     }