This commit is contained in:
Simon Vandel Sillesen 2019-01-06 09:41:11 +01:00
parent bb8624dff6
commit 2e52b27e71
2 changed files with 26 additions and 35 deletions

View File

@ -1,13 +1,11 @@
mod handlers; mod handlers;
mod subscriptions; mod subscriptions;
use std::{ use std::{fmt, path::PathBuf, sync::Arc};
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::{ use gen_lsp_server::{
handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse, handle_shutdown, ErrorCode, RawMessage, RawNotification, RawRequest, RawResponse,
}; };
@ -15,11 +13,9 @@ use languageserver_types::NumberOrString;
use ra_analysis::{Canceled, FileId, LibraryData}; use ra_analysis::{Canceled, FileId, LibraryData};
use ra_vfs::VfsTask; use ra_vfs::VfsTask;
use rayon; use rayon;
use threadpool::ThreadPool;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use failure::{format_err, bail}; use threadpool::ThreadPool;
use failure_derive::Fail;
use crate::{ use crate::{
main_loop::subscriptions::Subscriptions, main_loop::subscriptions::Subscriptions,

View File

@ -93,36 +93,31 @@ pub fn handle_on_type_formatting(
world: ServerWorld, world: ServerWorld,
params: req::DocumentOnTypeFormattingParams, params: req::DocumentOnTypeFormattingParams,
) -> Result<Option<Vec<TextEdit>>> { ) -> Result<Option<Vec<TextEdit>>> {
if params.ch != "=" || params.ch != "." { let analysis: Option<Box<Fn(FilePosition) -> Option<SourceChange>>> = match params.ch.as_str() {
return Ok(None); "=" => Some(Box::new(|pos| world.analysis().on_eq_typed(pos))),
} "." => Some(Box::new(|pos| world.analysis().on_dot_typed(pos))),
_ => 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: Vec<Box<Fn(FilePosition) -> Option<SourceChange>>> = vec![ if let Some(ana) = analysis {
Box::new(|pos| world.analysis().on_eq_typed(pos)), let file_id = params.text_document.try_conv_with(&world)?;
Box::new(|pos| world.analysis().on_dot_typed(pos)), 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) { if let Some(mut action) = ana(position) {
return Ok(Some( let change: Vec<TextEdit> = action
action .source_file_edits
.source_file_edits .pop()
.pop() .unwrap()
.unwrap() .edit
.edit .as_atoms()
.as_atoms() .iter()
.iter() .map_conv_with(&line_index)
.map_conv_with(&line_index) .collect();
.collect(), return Ok(Some(change));
));
} }
} }