Skip to content

Commit

Permalink
Version 3.7.0-90.0.dev
Browse files Browse the repository at this point in the history
Merge 507d40f into dev
  • Loading branch information
Dart CI committed Nov 1, 2024
2 parents 7bd4841 + 507d40f commit 32bebf2
Show file tree
Hide file tree
Showing 66 changed files with 1,035 additions and 310 deletions.
6 changes: 6 additions & 0 deletions pkg/analysis_server/lib/src/g3/utilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analysis_server/src/services/correction/organize_imports.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/line_info.dart';
Expand Down Expand Up @@ -61,12 +62,17 @@ ParseStringResult sortDirectives(String contents, {String? fileName}) {
);
var token = scanner.tokenize(reportScannerErrors: false);
var lineInfo = LineInfo(scanner.lineStarts);
var languageVersion = LibraryLanguageVersion(
package: ExperimentStatus.currentVersion,
override: scanner.overrideVersion,
);

var parser = p.Parser(
source,
errorListener,
featureSet: scanner.featureSet,
lineInfo: lineInfo,
languageVersion: languageVersion,
);

var unit = parser.parseCompilationUnit(token);
Expand Down
30 changes: 8 additions & 22 deletions pkg/analysis_server/lib/src/handler/legacy/edit_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:dart_style/src/dart_formatter.dart';
import 'package:dart_style/src/exceptions.dart';
import 'package:dart_style/src/source_code.dart';
import 'package:pub_semver/pub_semver.dart';

/// The handler for the `edit.format` request.
class EditFormatHandler extends LegacyHandler {
Expand All @@ -27,11 +26,9 @@ class EditFormatHandler extends LegacyHandler {
clientUriConverter: server.uriConverter);
var file = params.file;

String unformattedCode;
try {
var resource = server.resourceProvider.getFile(file);
unformattedCode = resource.readAsStringSync();
} catch (e) {
var driver = server.getAnalysisDriver(file);
var unit = driver?.parseFileSync(file);
if (unit is! ParsedUnitResult) {
sendResponse(Response.formatInvalidFile(request));
return;
}
Expand All @@ -45,29 +42,18 @@ class EditFormatHandler extends LegacyHandler {
length = null;
}

var unformattedCode = unit.content;
var code = SourceCode(
unformattedCode,
selectionStart: start,
selectionLength: length,
);

var driver = server.getAnalysisDriver(file);
var unit = await driver?.getResolvedUnit(file);

int? pageWidth;
Version effectiveLanguageVersion;
if (unit is ResolvedUnitResult) {
pageWidth = unit.analysisOptions.formatterOptions.pageWidth;
effectiveLanguageVersion = unit.libraryElement2.effectiveLanguageVersion;
} else {
// If the unit doesn't resolve, don't try to format it since we don't
// know what language version (and thus what formatting style) to apply.
sendResponse(Response.formatWithErrors(request));
return;
}

var effectivePageWidth =
unit.analysisOptions.formatterOptions.pageWidth ?? params.lineLength;
var effectiveLanguageVersion = unit.unit.languageVersion.effective;
var formatter = DartFormatter(
pageWidth: pageWidth ?? params.lineLength,
pageWidth: effectivePageWidth,
languageVersion: effectiveLanguageVersion);
SourceCode formattedResult;
try {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analysis_server/lib/src/lsp/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ abstract final class CustomMethods {
Method('dart/textDocumentContentDidChange');

// TODO(dantup): Remove custom AnalyzerStatus status method soon as no clients
// should be relying on it as we now support proper $/progress events.
// should be relying on it as we now support proper $/progress events.
static const analyzerStatus = Method(r'$/analyzerStatus');

/// Semantic tokens are dynamically registered using a single string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import 'package:analysis_server/src/lsp/temporary_overlay_operation.dart';
import 'package:analysis_server/src/services/correction/bulk_fix_processor.dart';
import 'package:analysis_server/src/utilities/source_change_merger.dart';

class FixAllCommandHandler extends SimpleEditCommandHandler
with LspHandlerHelperMixin {
class FixAllCommandHandler extends SimpleEditCommandHandler {
FixAllCommandHandler(super.server);

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import 'package:analysis_server/src/lsp/progress.dart';
import 'package:analysis_server/src/protocol_server.dart';
import 'package:meta/meta.dart';

class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler
with LspHandlerHelperMixin {
class PerformRefactorCommandHandler extends AbstractRefactorCommandHandler {
/// A [Future] used by tests to allow inserting a delay between resolving
/// the initial unit and the refactor running.
@visibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef _ScoredCompletionItem = ({CompletionItem item, double score});

class CompletionHandler
extends LspMessageHandler<CompletionParams, CompletionList>
with LspPluginRequestHandlerMixin, LspHandlerHelperMixin {
with LspPluginRequestHandlerMixin {
/// A [Future] used by tests to allow inserting a delay between resolving
/// the initial unit and the completion code running.
@visibleForTesting
Expand Down Expand Up @@ -528,7 +528,7 @@ class CompletionHandler
for (var suggestion in candidateSuggestions.suggestions) {
var item = await candidateSuggestionToCompletionItem(suggestion);
if (item != null) {
completionItems.add((item: item, score: suggestion.matcherScore));
completionItems.add((item: item, score: suggestion.matcherScore));
}
}
return completionItems;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ typedef StaticOptions
= Either3<bool, InlayHintOptions, InlayHintRegistrationOptions>;

class InlayHintHandler
extends LspMessageHandler<InlayHintParams, List<InlayHint>>
with LspHandlerHelperMixin {
extends LspMessageHandler<InlayHintParams, List<InlayHint>> {
InlayHintHandler(super.server);
@override
Method get handlesMessage => Method.textDocument_inlayHint;
Expand Down
3 changes: 1 addition & 2 deletions pkg/analysis_server/lib/src/lsp/handlers/handler_rename.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ class PrepareRenameHandler extends LspMessageHandler<TextDocumentPositionParams,
}
}

class RenameHandler extends LspMessageHandler<RenameParams, WorkspaceEdit?>
with LspHandlerHelperMixin {
class RenameHandler extends LspMessageHandler<RenameParams, WorkspaceEdit?> {
RenameHandler(super.server);

LspGlobalClientConfiguration get config =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analysis_server/src/lsp/error_or.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_augmentation.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_augmented.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_connect_to_dtd.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_dart_text_document_content_provider.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_diagnostic_server.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_reanalyze.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_super.dart';
Expand All @@ -20,7 +21,6 @@ import 'package:analysis_server/src/lsp/handlers/handler_code_actions.dart';
import 'package:analysis_server/src/lsp/handlers/handler_code_lens.dart';
import 'package:analysis_server/src/lsp/handlers/handler_completion.dart';
import 'package:analysis_server/src/lsp/handlers/handler_completion_resolve.dart';
import 'package:analysis_server/src/lsp/handlers/handler_dart_text_document_content_provider.dart';
import 'package:analysis_server/src/lsp/handlers/handler_definition.dart';
import 'package:analysis_server/src/lsp/handlers/handler_document_color.dart';
import 'package:analysis_server/src/lsp/handlers/handler_document_color_presentation.dart';
Expand Down
70 changes: 32 additions & 38 deletions pkg/analysis_server/lib/src/lsp/handlers/handlers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,41 @@ mixin HandlerHelperMixin<S extends AnalysisServer> {
ErrorOr<T> analysisFailedError<T>(String path) => error<T>(
ServerErrorCodes.FileAnalysisFailed, 'Analysis failed for file', path);

/// Extracts the current document version from [textDocument] if available,
/// or uses the version that the server has via
/// [LspAnalysisServer.getVersionedDocumentIdentifier].
OptionalVersionedTextDocumentIdentifier extractDocumentVersion(
TextDocumentIdentifier textDocument,
String path,
) {
return switch (textDocument) {
OptionalVersionedTextDocumentIdentifier() => textDocument,
VersionedTextDocumentIdentifier() =>
OptionalVersionedTextDocumentIdentifier(
uri: textDocument.uri, version: textDocument.version),
_ => server.getVersionedDocumentIdentifier(path),
};
}

bool fileHasBeenModified(String path, int? clientVersion) {
var serverDocumentVersion = server.getDocumentVersion(path);
return clientVersion != null && clientVersion != serverDocumentVersion;
}

ErrorOr<T> fileNotAnalyzedError<T>(String path) => error<T>(
ServerErrorCodes.FileNotAnalyzed, 'File is not being analyzed', path);

ErrorOr<LineInfo> getLineInfo(String path) {
var lineInfo = server.getLineInfo(path);

if (lineInfo == null) {
return error(ServerErrorCodes.InvalidFilePath,
'Unable to obtain line information for file', path);
} else {
return success(lineInfo);
}
}

/// Returns whether [doc] is a user-editable document or not.
///
/// Only editable documents have overlays and can be modified by the client.
Expand Down Expand Up @@ -254,44 +286,6 @@ mixin HandlerHelperMixin<S extends AnalysisServer> {
}
}

/// Provides some helpers for request handlers to produce common errors or
/// obtain resolved results after waiting for in-progress analysis.
mixin LspHandlerHelperMixin {
LspAnalysisServer get server;

/// Extracts the current document version from [textDocument] if available,
/// or uses the version that the server has via
/// [LspAnalysisServer.getVersionedDocumentIdentifier].
OptionalVersionedTextDocumentIdentifier extractDocumentVersion(
TextDocumentIdentifier textDocument,
String path,
) {
return switch (textDocument) {
OptionalVersionedTextDocumentIdentifier() => textDocument,
VersionedTextDocumentIdentifier() =>
OptionalVersionedTextDocumentIdentifier(
uri: textDocument.uri, version: textDocument.version),
_ => server.getVersionedDocumentIdentifier(path),
};
}

bool fileHasBeenModified(String path, int? clientVersion) {
var serverDocumentVersion = server.getDocumentVersion(path);
return clientVersion != null && clientVersion != serverDocumentVersion;
}

ErrorOr<LineInfo> getLineInfo(String path) {
var lineInfo = server.getLineInfo(path);

if (lineInfo == null) {
return error(ServerErrorCodes.InvalidFilePath,
'Unable to obtain line information for file', path);
} else {
return success(lineInfo);
}
}
}

/// A base class for LSP handlers that require an LSP analysis server and are
/// not supported over the legacy protocol.
abstract class LspMessageHandler<P, R>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import 'package:analysis_server/lsp_protocol/protocol.dart';
import 'package:analysis_server/src/lsp/client_capabilities.dart';
import 'package:analysis_server/src/lsp/client_configuration.dart';
import 'package:analysis_server/src/lsp/handlers/custom/handler_dart_text_document_content_provider.dart';
import 'package:analysis_server/src/lsp/handlers/handler_call_hierarchy.dart';
import 'package:analysis_server/src/lsp/handlers/handler_change_workspace_folders.dart';
import 'package:analysis_server/src/lsp/handlers/handler_code_actions.dart';
import 'package:analysis_server/src/lsp/handlers/handler_code_lens.dart';
import 'package:analysis_server/src/lsp/handlers/handler_completion.dart';
import 'package:analysis_server/src/lsp/handlers/handler_dart_text_document_content_provider.dart';
import 'package:analysis_server/src/lsp/handlers/handler_definition.dart';
import 'package:analysis_server/src/lsp/handlers/handler_document_color.dart';
import 'package:analysis_server/src/lsp/handlers/handler_document_highlights.dart';
Expand Down
8 changes: 1 addition & 7 deletions pkg/analysis_server/lib/src/lsp/source_edits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/source/source.dart';
import 'package:analyzer/src/dart/analysis/results.dart';
import 'package:analyzer/src/dart/scanner/reader.dart';
import 'package:analyzer/src/dart/scanner/scanner.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';

/// Checks whether a string contains only characters that are allowed to differ
/// between unformattedformatted code (such as whitespace, commas, semicolons).
Expand Down Expand Up @@ -98,11 +96,7 @@ ErrorOr<List<TextEdit>?> generateEditsForFormatting(
// the LSP configuration.
var effectivePageWidth =
result.analysisOptions.formatterOptions.pageWidth ?? defaultPageWidth;

var effectiveLanguageVersion = switch (result.unit.languageVersionToken) {
var token? => Version(token.major, token.minor, 0),
_ => (result as ParsedUnitResultImpl).fileState.packageLanguageVersion,
};
var effectiveLanguageVersion = result.unit.languageVersion.effective;

var code = SourceCode(unformattedSource);
SourceCode formattedResult;
Expand Down
43 changes: 6 additions & 37 deletions pkg/analysis_server/lib/src/plugin/plugin_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,21 @@ class PluginLocator {
/// [resourceProvider] to access the file system.
PluginLocator(this.resourceProvider);

/// Given the root directory of a package (the [packageRoot]), return the path
/// to the plugin associated with the package, or `null` if there is no plugin
/// associated with the package.
/// Given the root directory of a package (the [packageRoot]), returns the
/// path to the plugin associated with the package, or `null` if there is no
/// plugin associated with the package.
///
/// This will look first in the `pubspec.yaml` file in the package root for a
/// top-level key (`analysis_plugin`) indicating where the plugin is located.
/// The value associated with the key is expected to be the path of the plugin
/// relative to the package root. If the directory exists, the it is returned.
/// Looks for the directory `tools/analysis_plugin` relative to
/// the package root.
///
/// If the key is not defined in the `pubspec.yaml` file, or if the directory
/// given does not exist, then this method will look for the directory
/// `tools/analysis_plugin` relative to the package root. If the directory
/// exists, then it is returned.
///
/// This method does not validate the content of the plugin directory before
/// returning it.
/// The content of the plugin directory is not validated.
String? findPlugin(String packageRoot) {
return pluginMap.putIfAbsent(packageRoot, () => _findPlugin(packageRoot));
}

/// The implementation of [findPlugin].
String? _findPlugin(String packageRoot) {
var packageFolder = resourceProvider.getFolder(packageRoot);
// TODO(brianwilkerson): Re-enable this after deciding how we want to deal
// with discovery of plugins.
// import 'package:yaml/yaml.dart';
// File pubspecFile = packageFolder.getChildAssumingFile(pubspecFileName);
// if (pubspecFile.exists) {
// try {
// YamlDocument document = loadYamlDocument(pubspecFile.readAsStringSync(),
// sourceUrl: pubspecFile.toUri());
// YamlNode contents = document.contents;
// if (contents is YamlMap) {
// String pluginPath = contents[analyzerPluginKey];
// if (pluginPath != null) {
// Folder pluginFolder =
// packageFolder.getChildAssumingFolder(pluginPath);
// if (pluginFolder.exists) {
// return pluginFolder.path;
// }
// }
// }
// } catch (exception) {
// // If we can't read the file, or if it isn't valid YAML, then ignore it.
// }
// }
var pluginFolder = packageFolder
.getChildAssumingFolder(toolsFolderName)
.getChildAssumingFolder(defaultPluginFolderName);
Expand Down
Loading

0 comments on commit 32bebf2

Please sign in to comment.