Skip to content

Commit

Permalink
[analysis_server] Improve performance of LSP textDocument/codeActions…
Browse files Browse the repository at this point in the history
… with no range

Fixes Dart-Code/Dart-Code#3952.

Change-Id: Ie523dc628ab72705259810baccafa949af96280e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/244101
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
DanTup authored and Commit Bot committed May 9, 2022
1 parent 072603d commit 3c0289d
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart
Expand Up @@ -87,6 +87,11 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,

final unit = await path.mapResult(requireResolvedUnit);

/// Whether a fix of kind [kind] should be included in the results.
///
/// Unlike [shouldIncludeAnyOfKind], this function is called with a more
/// specific action kind and answers the question "Should we include this
/// specific fix kind?".
bool shouldIncludeKind(CodeActionKind? kind) {
/// Checks whether the kind matches the [wanted] kind.
///
Expand All @@ -95,7 +100,7 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
/// - refactor.foobar - not included
/// - refactor.foo.bar - included
bool isMatch(CodeActionKind wanted) =>
kind == wanted || kind.toString().startsWith('${wanted.toString()}.');
kind == wanted || kind.toString().startsWith('$wanted.');

// If the client wants only a specific set, use only that filter.
final only = params.context.only;
Expand All @@ -112,6 +117,30 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
return true;
}

/// Whether any fixes of kind [kind] should be included in the results.
///
/// Unlike [shouldIncludeKind], this function is called with a more general
/// action kind and answers the question "Should we include any actions of
/// kind CodeActionKind.Source?".
bool shouldIncludeAnyOfKind(CodeActionKind? kind) {
/// Checks whether the kind matches the [wanted] kind.
///
/// If `kind` is `refactor.foo` then for these `wanted` values:
/// - wanted=refactor.foo - true
/// - wanted=refactor.foo.bar - true
/// - wanted=refactor - false
/// - wanted=refactor.bar - false
bool isMatch(CodeActionKind wanted) =>
kind == wanted || wanted.toString().startsWith('$kind.');

final only = params.context.only;
if (only != null) {
return only.any(isMatch);
}

return true;
}

return unit.mapResult((unit) {
final startOffset = toOffset(unit.lineInfo, params.range.start);
final endOffset = toOffset(unit.lineInfo, params.range.end);
Expand All @@ -124,6 +153,7 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
(performance) => _getCodeActions(
performance,
shouldIncludeKind,
shouldIncludeAnyOfKind,
supportsLiteralCodeActions,
supportsApplyEdit,
supportedDiagnosticTags,
Expand Down Expand Up @@ -306,6 +336,7 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
Future<ErrorOr<List<Either2<Command, CodeAction>>>> _getCodeActions(
OperationPerformanceImpl performance,
bool Function(CodeActionKind?) shouldIncludeKind,
bool Function(CodeActionKind?) shouldIncludeAnyOfKind,
bool supportsLiterals,
bool supportsWorkspaceApplyEdit,
Set<DiagnosticTag> supportedDiagnosticTags,
Expand All @@ -316,26 +347,31 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
ResolvedUnitResult unit,
) async {
final results = await Future.wait([
performance.runAsync(
'_getSourceActions',
(_) => _getSourceActions(shouldIncludeKind, supportsLiterals,
supportsWorkspaceApplyEdit, path),
),
performance.runAsync(
'_getAssistActions',
(_) => _getAssistActions(shouldIncludeKind, supportsLiterals, path,
range, offset, length, unit),
),
performance.runAsync(
'_getRefactorActions',
(_) => _getRefactorActions(
shouldIncludeKind, supportsLiterals, path, offset, length, unit),
),
performance.runAsync(
'_getFixActions',
(_) => _getFixActions(shouldIncludeKind, supportsLiterals, path, offset,
supportedDiagnosticTags, range, unit),
),
if (shouldIncludeAnyOfKind(CodeActionKind.Source))
performance.runAsync(
'_getSourceActions',
(_) => _getSourceActions(shouldIncludeKind, supportsLiterals,
supportsWorkspaceApplyEdit, path),
),
// Assists go under the Refactor CodeActionKind so check that here.
if (shouldIncludeAnyOfKind(CodeActionKind.Refactor))
performance.runAsync(
'_getAssistActions',
(_) => _getAssistActions(shouldIncludeKind, supportsLiterals, path,
range, offset, length, unit),
),
if (shouldIncludeAnyOfKind(CodeActionKind.Refactor))
performance.runAsync(
'_getRefactorActions',
(_) => _getRefactorActions(
shouldIncludeKind, supportsLiterals, path, offset, length, unit),
),
if (shouldIncludeAnyOfKind(CodeActionKind.QuickFix))
performance.runAsync(
'_getFixActions',
(_) => _getFixActions(shouldIncludeKind, supportsLiterals, path,
offset, supportedDiagnosticTags, range, unit),
),
]);
final flatResults = results.expand((x) => x).toList();

Expand Down

0 comments on commit 3c0289d

Please sign in to comment.