Skip to content

Commit 4eb0bb4

Browse files
bors[bot]matklad
andauthored
Merge #1874
1874: move fold conversino to conv.rs r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents eeac224 + 36732a4 commit 4eb0bb4

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

crates/ra_lsp_server/src/conv.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use lsp_types::{
66
};
77
use ra_ide_api::{
88
translate_offset_with_edit, CompletionItem, CompletionItemKind, FileId, FilePosition,
9-
FileRange, FileSystemEdit, InsertTextFormat, LineCol, LineIndex, NavigationTarget, RangeInfo,
10-
Severity, SourceChange, SourceFileEdit,
9+
FileRange, FileSystemEdit, Fold, FoldKind, InsertTextFormat, LineCol, LineIndex,
10+
NavigationTarget, RangeInfo, Severity, SourceChange, SourceFileEdit,
1111
};
1212
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
1313
use ra_text_edit::{AtomTextEdit, TextEdit};
@@ -225,6 +225,26 @@ impl ConvWith<(&LineIndex, LineEndings)> for &AtomTextEdit {
225225
}
226226
}
227227

228+
impl ConvWith<&LineIndex> for Fold {
229+
type Output = lsp_types::FoldingRange;
230+
231+
fn conv_with(self, line_index: &LineIndex) -> lsp_types::FoldingRange {
232+
let range = self.range.conv_with(&line_index);
233+
lsp_types::FoldingRange {
234+
start_line: range.start.line,
235+
start_character: Some(range.start.character),
236+
end_line: range.end.line,
237+
end_character: Some(range.end.character),
238+
kind: match self.kind {
239+
FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
240+
FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
241+
FoldKind::Mods => None,
242+
FoldKind::Block => None,
243+
},
244+
}
245+
}
246+
}
247+
228248
impl<T: ConvWith<CTX>, CTX> ConvWith<CTX> for Option<T> {
229249
type Output = Option<T::Output>;
230250

crates/ra_lsp_server/src/main_loop/handlers.rs

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ use std::{fmt::Write as _, io::Write as _};
33
use lsp_server::ErrorCode;
44
use lsp_types::{
55
CodeAction, CodeActionResponse, CodeLens, Command, CompletionItem, Diagnostic,
6-
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeKind,
7-
FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, Position,
8-
PrepareRenameResponse, Range, RenameParams, SymbolInformation, TextDocumentIdentifier,
9-
TextEdit, WorkspaceEdit,
10-
};
11-
use ra_ide_api::{
12-
AssistId, FileId, FilePosition, FileRange, FoldKind, Query, Runnable, RunnableKind,
6+
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams,
7+
Hover, HoverContents, Location, MarkupContent, MarkupKind, Position, PrepareRenameResponse,
8+
Range, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit,
139
};
10+
use ra_ide_api::{AssistId, FileId, FilePosition, FileRange, Query, Runnable, RunnableKind};
1411
use ra_prof::profile;
1512
use ra_syntax::{AstNode, SyntaxKind, TextRange, TextUnit};
1613
use rustc_hash::FxHashMap;
@@ -383,32 +380,9 @@ pub fn handle_folding_range(
383380
params: FoldingRangeParams,
384381
) -> Result<Option<Vec<FoldingRange>>> {
385382
let file_id = params.text_document.try_conv_with(&world)?;
383+
let folds = world.analysis().folding_ranges(file_id)?;
386384
let line_index = world.analysis().file_line_index(file_id)?;
387-
388-
let res = Some(
389-
world
390-
.analysis()
391-
.folding_ranges(file_id)?
392-
.into_iter()
393-
.map(|fold| {
394-
let kind = match fold.kind {
395-
FoldKind::Comment => Some(FoldingRangeKind::Comment),
396-
FoldKind::Imports => Some(FoldingRangeKind::Imports),
397-
FoldKind::Mods => None,
398-
FoldKind::Block => None,
399-
};
400-
let range = fold.range.conv_with(&line_index);
401-
FoldingRange {
402-
start_line: range.start.line,
403-
start_character: Some(range.start.character),
404-
end_line: range.end.line,
405-
end_character: Some(range.start.character),
406-
kind,
407-
}
408-
})
409-
.collect(),
410-
);
411-
385+
let res = Some(folds.into_iter().map_conv_with(&*line_index).collect());
412386
Ok(res)
413387
}
414388

0 commit comments

Comments
 (0)