From f99398d9d5e47e28f3749c7903df67b9030ac6e0 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 00:58:03 +0100 Subject: indent on typing dot. fixes #439 --- crates/ra_lsp_server/src/main_loop/handlers.rs | 56 ++++++++++++++++---------- 1 file changed, 34 insertions(+), 22 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 1baed73ad..2ec9073e4 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -2,15 +2,16 @@ use std::collections::HashMap; use gen_lsp_server::ErrorCode; use languageserver_types::{ - CodeActionResponse, Command, Diagnostic, - DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind, - FoldingRangeParams, Location, MarkupContent, MarkupKind, MarkedString, Position, - PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, - Range, WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, - HoverContents, DocumentFormattingParams, DocumentHighlight, + CodeActionResponse, Command, Diagnostic, DiagnosticSeverity, DocumentFormattingParams, + DocumentHighlight, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind, + FoldingRangeParams, Hover, HoverContents, Location, MarkedString, MarkupContent, MarkupKind, + ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range, RenameParams, + SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; -use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity}; -use ra_syntax::{TextUnit, text_utils::intersect}; +use ra_analysis::{ + FileId, FilePosition, FileRange, FoldKind, Query, RunnableKind, Severity, SourceChange, +}; +use ra_syntax::{text_utils::intersect, TextUnit}; use ra_text_edit::text_utils::contains_offset_nonstrict; use rustc_hash::FxHashMap; use serde_json::to_value; @@ -92,7 +93,7 @@ pub fn handle_on_type_formatting( world: ServerWorld, params: req::DocumentOnTypeFormattingParams, ) -> Result>> { - if params.ch != "=" { + if params.ch != "=" || params.ch != "." { return Ok(None); } @@ -102,19 +103,30 @@ pub fn handle_on_type_formatting( file_id, offset: params.position.conv_with(&line_index), }; - let edits = match world.analysis().on_eq_typed(position) { - None => return Ok(None), - Some(mut action) => action - .source_file_edits - .pop() - .unwrap() - .edit - .as_atoms() - .iter() - .map_conv_with(&line_index) - .collect(), - }; - Ok(Some(edits)) + + let analysis: Vec Option>> = vec![ + Box::new(|pos| world.analysis().on_eq_typed(pos)), + Box::new(|pos| world.analysis().on_dot_typed(pos)), + ]; + + // 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(), + )); + } + } + + return Ok(None); } pub fn handle_document_symbol( -- cgit v1.2.3 From 2e52b27e7139095393c7e4eebd7da0c6c26b053e Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 09:41:11 +0100 Subject: refactor --- crates/ra_lsp_server/src/main_loop.rs | 14 +++----- crates/ra_lsp_server/src/main_loop/handlers.rs | 47 ++++++++++++-------------- 2 files changed, 26 insertions(+), 35 deletions(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 60d9671de..2dc1be26a 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -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, }; @@ -15,11 +13,9 @@ use languageserver_types::NumberOrString; 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, diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 2ec9073e4..51f134e8a 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -93,36 +93,31 @@ pub fn handle_on_type_formatting( world: ServerWorld, params: req::DocumentOnTypeFormattingParams, ) -> Result>> { - 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 Option>> = 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 Option>> = 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 = action + .source_file_edits + .pop() + .unwrap() + .edit + .as_atoms() + .iter() + .map_conv_with(&line_index) + .collect(); + return Ok(Some(change)); } } -- cgit v1.2.3 From b0ffa98a005a9d37a168b589506c430692a2432f Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 09:56:00 +0100 Subject: add "." as a trigger char on type formatting --- crates/ra_lsp_server/src/caps.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_lsp_server') diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index a74f9f27b..2599a4ca6 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -37,7 +37,7 @@ pub fn server_capabilities() -> ServerCapabilities { document_range_formatting_provider: None, document_on_type_formatting_provider: Some(DocumentOnTypeFormattingOptions { first_trigger_character: "=".to_string(), - more_trigger_character: None, + more_trigger_character: Some(vec![".".to_string()]), }), folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), rename_provider: Some(RenameProviderCapability::Options(RenameOptions { -- cgit v1.2.3