e97f1bdbf0
This should allow doing partial migration, specifically protocol files, which are imported by other libraries, but are a small library cycle that does not import much outside of it. Change-Id: I904c05d6d5b444ee9a9dbd1f7ada12aabdcc5165 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/193583 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
67 lines
2.4 KiB
Dart
67 lines
2.4 KiB
Dart
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// @dart = 2.9
|
|
|
|
import 'package:analysis_server/lsp_protocol/protocol_generated.dart';
|
|
import 'package:analysis_server/lsp_protocol/protocol_special.dart';
|
|
import 'package:analysis_server/src/computer/computer_folding.dart';
|
|
import 'package:analysis_server/src/lsp/handlers/handlers.dart';
|
|
import 'package:analysis_server/src/lsp/lsp_analysis_server.dart';
|
|
import 'package:analysis_server/src/lsp/mapping.dart';
|
|
import 'package:analysis_server/src/protocol_server.dart';
|
|
import 'package:analyzer/dart/analysis/results.dart';
|
|
import 'package:analyzer/source/line_info.dart';
|
|
|
|
class FoldingHandler
|
|
extends MessageHandler<FoldingRangeParams, List<FoldingRange>> {
|
|
FoldingHandler(LspAnalysisServer server) : super(server);
|
|
@override
|
|
Method get handlesMessage => Method.textDocument_foldingRange;
|
|
|
|
@override
|
|
LspJsonHandler<FoldingRangeParams> get jsonHandler =>
|
|
FoldingRangeParams.jsonHandler;
|
|
|
|
@override
|
|
Future<ErrorOr<List<FoldingRange>>> handle(
|
|
FoldingRangeParams params, CancellationToken token) async {
|
|
final path = pathOfDoc(params.textDocument);
|
|
|
|
return path.mapResult((path) async {
|
|
final partialResults = <List<FoldingRegion>>[];
|
|
LineInfo lineInfo;
|
|
|
|
final unit = server.getParsedUnit(path);
|
|
if (unit?.state == ResultState.VALID) {
|
|
lineInfo = unit.lineInfo;
|
|
|
|
final regions = DartUnitFoldingComputer(lineInfo, unit.unit).compute();
|
|
partialResults.insert(0, regions);
|
|
}
|
|
|
|
// Still try to obtain line info for invalid or non-Dart files, as plugins
|
|
// could contribute to those.
|
|
lineInfo ??= server.getLineInfo(path);
|
|
|
|
if (lineInfo == null) {
|
|
// Line information would be required to translate folding results to
|
|
// LSP.
|
|
return success(const []);
|
|
}
|
|
|
|
final notificationManager = server.notificationManager;
|
|
final pluginResults = notificationManager.folding.getResults(path);
|
|
partialResults.addAll(pluginResults);
|
|
|
|
final regions =
|
|
notificationManager.merger.mergeFoldingRegions(partialResults);
|
|
|
|
return success(
|
|
regions.map((region) => toFoldingRange(lineInfo, region)).toList(),
|
|
);
|
|
});
|
|
}
|
|
}
|