fetchLSPFoldRanges method
Fetches fold ranges from the LSP server.
If successful, these fold ranges will be used instead of the built-in fold range detection algorithm.
Example:
await controller.fetchLSPFoldRanges();
Implementation
Future<void> fetchLSPFoldRanges() async {
if (lspConfig == null || openedFile == null) return;
try {
final response = await lspConfig!.getLSPFoldRanges(openedFile!);
final result = response['result'];
if (result is List && result.isNotEmpty) {
final Map<int, FoldRange> foldMap = {};
final oldRanges = _lspFoldRanges;
for (final item in result) {
if (item is Map<String, dynamic>) {
final startLine = item['startLine'] as int?;
final endLine = item['endLine'] as int?;
if (startLine != null && endLine != null && endLine > startLine) {
final newFold = FoldRange(startLine, endLine);
FoldRange? existing =
oldRanges?[startLine] ?? foldings[startLine];
if (existing == null) {
for (
int offset = 1;
offset <= 3 && existing == null;
offset++
) {
existing =
oldRanges?[startLine - offset] ??
oldRanges?[startLine + offset] ??
foldings[startLine - offset] ??
foldings[startLine + offset];
if (existing != null) {
final oldSpan = existing.endIndex - existing.startIndex;
final newSpan = endLine - startLine;
if ((oldSpan - newSpan).abs() > (oldSpan * 0.3)) {
existing = null;
}
}
}
}
if (existing != null) {
newFold.isFolded = existing.isFolded;
newFold.originallyFoldedChildren =
existing.originallyFoldedChildren;
}
foldMap[startLine] = newFold;
}
}
}
_lspFoldRanges = foldMap.isEmpty ? null : foldMap;
} else {
_lspFoldRanges = null;
}
_lspFoldRangesAdjustedNotFetched = false;
notifyListeners();
} catch (e) {
debugPrint('Error fetching LSP fold ranges: $e');
_lspFoldRanges = null;
}
}