|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:_fe_analyzer_shared/src/scanner/token.dart'; |
| 6 | +import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart'; |
| 7 | +import 'package:analysis_server/src/services/correction/fix.dart'; |
| 8 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 9 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 10 | +import 'package:analyzer/source/line_info.dart'; |
| 11 | +import 'package:analyzer/source/source_range.dart'; |
| 12 | +import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; |
| 13 | +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; |
| 14 | +import 'package:analyzer_plugin/utilities/range_factory.dart'; |
| 15 | +import 'package:collection/collection.dart'; |
| 16 | + |
| 17 | +class MoveDocCommentToLibraryDirective extends CorrectionProducer { |
| 18 | + @override |
| 19 | + FixKind get fixKind => DartFixKind.MOVE_DOC_COMMENT_TO_LIBRARY_DIRECTIVE; |
| 20 | + |
| 21 | + @override |
| 22 | + Future<void> compute(ChangeBuilder builder) async { |
| 23 | + var comment = node.thisOrAncestorOfType<Comment>(); |
| 24 | + if (comment == null) { |
| 25 | + return; |
| 26 | + } |
| 27 | + var compilationUnit = comment.root; |
| 28 | + if (compilationUnit is! CompilationUnit) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + var firstDirective = compilationUnit.directives.firstOrNull; |
| 33 | + if (firstDirective is LibraryDirective) { |
| 34 | + await _moveToExistingLibraryDirective(builder, comment, firstDirective); |
| 35 | + } else if (libraryElement.featureSet.isEnabled(Feature.unnamedLibraries)) { |
| 36 | + await _moveToNewLibraryDirective(builder, comment, compilationUnit); |
| 37 | + } |
| 38 | + |
| 39 | + // If the library doesn't support unnamed libraries, then we cannot add |
| 40 | + // a new library directive; we don't know what to name it. |
| 41 | + } |
| 42 | + |
| 43 | + Future<void> _moveToExistingLibraryDirective(ChangeBuilder builder, |
| 44 | + Comment comment, LibraryDirective libraryDirective) async { |
| 45 | + // Just move the annotation to the existing library directive. |
| 46 | + var commentRange = utils.getLinesRange(range.node(comment)); |
| 47 | + await builder.addDartFileEdit(file, (builder) { |
| 48 | + builder.addDeletion(commentRange); |
| 49 | + var commentText = utils.getRangeText(commentRange); |
| 50 | + builder.addSimpleInsertion( |
| 51 | + libraryDirective.firstTokenAfterCommentAndMetadata.offset, |
| 52 | + commentText); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + Future<void> _moveToNewLibraryDirective(ChangeBuilder builder, |
| 57 | + Comment comment, CompilationUnit compilationUnit) async { |
| 58 | + var commentRange = _rangeOfFirstBlock(comment, compilationUnit.lineInfo); |
| 59 | + |
| 60 | + // Create a new, unnamed library directive, and move the comment to just |
| 61 | + // above the directive. |
| 62 | + var token = compilationUnit.beginToken; |
| 63 | + |
| 64 | + if (token.type == TokenType.SCRIPT_TAG) { |
| 65 | + // TODO(srawlins): Handle this case. |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (token.precedingComments == comment.beginToken) { |
| 70 | + // Do not "move" the comment. Just slip a library directive below it. |
| 71 | + await builder.addDartFileEdit(file, (builder) { |
| 72 | + builder.addSimpleInsertion(commentRange.end, 'library;$eol'); |
| 73 | + }); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + int insertionOffset; |
| 78 | + String prefix; |
| 79 | + Token? commentOnFirstToken = token.precedingComments; |
| 80 | + if (commentOnFirstToken != null) { |
| 81 | + while (commentOnFirstToken!.next != null) { |
| 82 | + commentOnFirstToken = commentOnFirstToken.next!; |
| 83 | + |
| 84 | + if (commentOnFirstToken == comment.beginToken) { |
| 85 | + // Do not "move" the comment. Just slip a library directive below it. |
| 86 | + await builder.addDartFileEdit(file, (builder) { |
| 87 | + builder.addSimpleInsertion(commentRange.end, 'library;$eol$eol'); |
| 88 | + }); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + // `token` is now the last of the leading comments (perhaps a Copyight |
| 93 | + // notice, a Dart language version, etc.) |
| 94 | + insertionOffset = commentOnFirstToken.end; |
| 95 | + prefix = '$eol$eol'; |
| 96 | + } else { |
| 97 | + insertionOffset = 0; |
| 98 | + prefix = ''; |
| 99 | + } |
| 100 | + |
| 101 | + await builder.addDartFileEdit(file, (builder) { |
| 102 | + builder.addDeletion(commentRange); |
| 103 | + var commentText = utils.getRangeText(commentRange); |
| 104 | + builder.addSimpleInsertion( |
| 105 | + insertionOffset, '$prefix${commentText}library;$eol$eol'); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + /// The range of the first "block" in [comment]. |
| 110 | + /// |
| 111 | + /// A [Comment] can contain blank lines (even an end-of-line comment, and an |
| 112 | + /// end-of-line doc comment). But for the purpose of this fix, we interpret |
| 113 | + /// only the first "block" or "paragraph" of text as what was intented to be |
| 114 | + /// the library comment. |
| 115 | + SourceRange _rangeOfFirstBlock(Comment comment, LineInfo lineInfo) { |
| 116 | + for (var token in comment.tokens) { |
| 117 | + var next = token.next; |
| 118 | + if (next != null && |
| 119 | + lineInfo.getLocation(next.offset).lineNumber > |
| 120 | + lineInfo.getLocation(token.end).lineNumber + 1) { |
| 121 | + // There is a blank line. Interpret this as two separate doc comments. |
| 122 | + return utils.getLinesRange(range.startEnd(comment.tokens.first, token)); |
| 123 | + } |
| 124 | + } |
| 125 | + return utils.getLinesRange(range.node(comment)); |
| 126 | + } |
| 127 | +} |
0 commit comments