From fd33c89207f6dc621ef6e8c26ea288089448d811 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 18:15:19 +0300 Subject: [PATCH] switch to FileRange --- crates/ra_analysis/src/extend_selection.rs | 11 ++++++++++ crates/ra_analysis/src/lib.rs | 8 +++++--- crates/ra_lsp_server/src/conv.rs | 13 +++++++++++- .../ra_lsp_server/src/main_loop/handlers.rs | 20 +++++++++---------- 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 crates/ra_analysis/src/extend_selection.rs diff --git a/crates/ra_analysis/src/extend_selection.rs b/crates/ra_analysis/src/extend_selection.rs new file mode 100644 index 00000000000..5e1fbee18f0 --- /dev/null +++ b/crates/ra_analysis/src/extend_selection.rs @@ -0,0 +1,11 @@ +use ra_db::SyntaxDatabase; + +use crate::{ + TextRange, FileRange, + db::RootDatabase, +}; + +pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { + let file = db.source_file(frange.file_id); + ra_editor::extend_selection(&file, frange.range).unwrap_or(frange.range) +} diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 3fa4189ce2b..98abe85233f 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -14,9 +14,11 @@ macro_rules! ctry { mod imp; mod completion; mod symbol_index; -mod syntax_highlighting; pub mod mock_analysis; +mod extend_selection; +mod syntax_highlighting; + use std::{fmt, sync::Arc}; use rustc_hash::FxHashMap; @@ -277,8 +279,8 @@ pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { pub fn file_line_index(&self, file_id: FileId) -> Arc { self.imp.file_line_index(file_id) } - pub fn extend_selection(&self, file: &SourceFileNode, range: TextRange) -> TextRange { - ra_editor::extend_selection(file, range).unwrap_or(range) + pub fn extend_selection(&self, frange: FileRange) -> TextRange { + extend_selection::extend_selection(&self.imp.db, frange) } pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option { ra_editor::matching_brace(file, offset) diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index d3670104e45..3d56ccd9717 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -2,7 +2,7 @@ self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; -use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; +use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition,FileRange, CompletionItem, CompletionItemKind, InsertText}; use ra_editor::{LineCol, LineIndex, translate_offset_with_edit}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -218,6 +218,17 @@ fn try_conv_with(self, world: &ServerWorld) -> Result { } } +impl<'a> TryConvWith for (&'a TextDocumentIdentifier, Range) { + type Ctx = ServerWorld; + type Output = FileRange; + fn try_conv_with(self, world: &ServerWorld) -> Result { + let file_id = self.0.try_conv_with(world)?; + let line_index = world.analysis().file_line_index(file_id); + let range = self.1.conv_with(&line_index); + Ok(FileRange { file_id, range }) + } +} + impl TryConvWith for Vec { type Ctx = ::Ctx; type Output = Vec<::Output>; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 0e9a66a8a9b..d6f3dbe28da 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -8,7 +8,7 @@ PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents, }; -use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition, Severity}; +use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FileRange, FilePosition, Severity}; use ra_syntax::{TextUnit, text_utils::intersect}; use ra_text_edit::text_utils::contains_offset_nonstrict; use rustc_hash::FxHashMap; @@ -33,13 +33,13 @@ pub fn handle_extend_selection( params: req::ExtendSelectionParams, ) -> Result { let file_id = params.text_document.try_conv_with(&world)?; - let file = world.analysis().file_syntax(file_id); let line_index = world.analysis().file_line_index(file_id); let selections = params .selections .into_iter() .map_conv_with(&line_index) - .map(|r| world.analysis().extend_selection(&file, r)) + .map(|range| FileRange { file_id, range }) + .map(|frange| world.analysis().extend_selection(frange)) .map_conv_with(&line_index) .collect(); Ok(req::ExtendSelectionResult { selections }) @@ -71,13 +71,8 @@ pub fn handle_join_lines( world: ServerWorld, params: req::JoinLinesParams, ) -> Result { - let file_id = params.text_document.try_conv_with(&world)?; - let line_index = world.analysis().file_line_index(file_id); - let range = params.range.conv_with(&line_index); - world - .analysis() - .join_lines(file_id, range) - .try_conv_with(&world) + let frange = (¶ms.text_document, params.range).try_conv_with(&world)?; + world.analysis().join_lines(frange).try_conv_with(&world) } pub fn handle_on_enter( @@ -614,7 +609,10 @@ pub fn handle_code_action( let line_index = world.analysis().file_line_index(file_id); let range = params.range.conv_with(&line_index); - let assists = world.analysis().assists(file_id, range)?.into_iter(); + let assists = world + .analysis() + .assists(FileRange { file_id, range })? + .into_iter(); let fixes = world .analysis() .diagnostics(file_id)?