blob: d832d0678d75ac38c8efb02d7d16d73c97425b99 [file] [log] [blame]
// Copyright (c) 2018, 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.
import 'package:analysis_server/lsp_protocol/protocol_generated.dart';
import 'package:analysis_server/lsp_protocol/protocol_special.dart';
import 'package:analysis_server/src/lsp/constants.dart';
import 'package:analysis_server/src/lsp/handlers/commands/simple_edit_handler.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/progress.dart';
import 'package:analysis_server/src/services/correction/sort_members.dart';
import 'package:analyzer/dart/analysis/results.dart';
class SortMembersCommandHandler extends SimpleEditCommandHandler {
SortMembersCommandHandler(LspAnalysisServer server) : super(server);
String get commandName => 'Sort Members';
Future<ErrorOr<void>> handle(List<Object?>? arguments,
ProgressReporter reporter, CancellationToken cancellationToken) async {
if (arguments == null || arguments.length != 1 || arguments[0] is! String) {
return ErrorOr.error(ResponseError(
code: ServerErrorCodes.InvalidCommandArguments,
'$commandName requires a single String parameter containing the path of a Dart file',
// Get the version of the doc before we calculate edits so we can send it back
// to the client so that they can discard this edit if the document has been
// modified since.
final path = arguments.single as String;
final docIdentifier = server.getVersionedDocumentIdentifier(path);
var driver = server.getAnalysisDriver(path);
final result = driver?.parseFileSync(path);
if (cancellationToken.isCancellationRequested) {
return error(ErrorCodes.RequestCancelled, 'Request was cancelled');
if (result is! ParsedUnitResult) {
return ErrorOr.error(ResponseError(
code: ServerErrorCodes.FileNotAnalyzed,
message: '$commandName is only available for analyzed files',
final code = result.content;
final unit = result.unit;
if (hasScanParseErrors(result.errors)) {
return ErrorOr.error(ResponseError(
code: ServerErrorCodes.FileHasErrors,
'Unable to $commandName because the file contains parse errors',
data: path,
final sorter = MemberSorter(code, unit);
final edits = sorter.sort();
if (edits.isEmpty) {
return success(null);
return await sendSourceEditsToClient(docIdentifier, unit, edits);