diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 5fa52ec1b45..d78f7792558 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -6,8 +6,8 @@ }; use ra_ide_api::{ translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition, - FileRange, FileSystemEdit, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo, - Severity, SourceChange, SourceFileEdit, + FileRange, FileSystemEdit, Fold, FoldKind, InsertTextFormat, LineCol, LineIndex, + NavigationTarget, RangeInfo, Severity, SourceChange, SourceFileEdit, }; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; use ra_text_edit::{AtomTextEdit, TextEdit}; @@ -225,6 +225,26 @@ fn conv_with( } } +impl ConvWith<&LineIndex> for Fold { + type Output = lsp_types::FoldingRange; + + fn conv_with(self, line_index: &LineIndex) -> lsp_types::FoldingRange { + let range = self.range.conv_with(&line_index); + lsp_types::FoldingRange { + start_line: range.start.line, + start_character: Some(range.start.character), + end_line: range.end.line, + end_character: Some(range.end.character), + kind: match self.kind { + FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment), + FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports), + FoldKind::Mods => None, + FoldKind::Block => None, + }, + } + } +} + impl, CTX> ConvWith for Option { type Output = Option; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index f18a6871efb..ae57e57e95e 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -3,14 +3,11 @@ use lsp_server::ErrorCode; use lsp_types::{ CodeAction, CodeActionResponse, CodeLens, Command, CompletionItem, Diagnostic, - DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeKind, - FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, - PrepareRenameResponse, Range, RenameParams, SymbolInformation, TextDocumentIdentifier, - TextEdit, WorkspaceEdit, -}; -use ra_ide_api::{ - AssistId, FileId, FilePosition, FileRange, FoldKind, Query, Runnable, RunnableKind, + DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams, + Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, PrepareRenameResponse, + Range, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, }; +use ra_ide_api::{AssistId, FileId, FilePosition, FileRange, Query, Runnable, RunnableKind}; use ra_prof::profile; use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit}; use rustc_hash::FxHashMap; @@ -383,32 +380,9 @@ pub fn handle_folding_range( params: FoldingRangeParams, ) -> Result>> { let file_id = params.text_document.try_conv_with(&world)?; + let folds = world.analysis().folding_ranges(file_id)?; let line_index = world.analysis().file_line_index(file_id)?; - - let res = Some( - world - .analysis() - .folding_ranges(file_id)? - .into_iter() - .map(|fold| { - let kind = match fold.kind { - FoldKind::Comment => Some(FoldingRangeKind::Comment), - FoldKind::Imports => Some(FoldingRangeKind::Imports), - FoldKind::Mods => None, - FoldKind::Block => None, - }; - let range = fold.range.conv_with(&line_index); - FoldingRange { - start_line: range.start.line, - start_character: Some(range.start.character), - end_line: range.end.line, - end_character: Some(range.end.character), - kind, - } - }) - .collect(), - ); - + let res = Some(folds.into_iter().map_conv_with(&*line_index).collect()); Ok(res) }