diff --git a/pkg/analysis_server/lib/src/domains/execution/completion.dart b/pkg/analysis_server/lib/src/domains/execution/completion.dart deleted file mode 100644 index 3918a8339d7d..000000000000 --- a/pkg/analysis_server/lib/src/domains/execution/completion.dart +++ /dev/null @@ -1,140 +0,0 @@ -// 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/src/protocol_server.dart' - show CompletionSuggestion, RuntimeCompletionExpression, SourceEdit; -import 'package:analysis_server/src/services/completion/dart/completion_manager.dart'; -import 'package:analyzer/dart/analysis/results.dart'; -import 'package:analyzer/file_system/overlay_file_system.dart'; -import 'package:analyzer/src/dart/analysis/driver.dart'; -import 'package:analyzer/src/util/performance/operation_performance.dart'; -import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart'; - -class RuntimeCompletionComputer { - final OverlayResourceProvider resourceProvider; - final AnalysisDriver analysisDriver; - - final String code; - final int offset; - - final String contextPath; - final int contextOffset; - - RuntimeCompletionComputer(this.resourceProvider, this.analysisDriver, - this.code, this.offset, this.contextPath, this.contextOffset); - - Future compute() async { - var contextResult = await analysisDriver.getResolvedUnit(contextPath); - if (contextResult is! ResolvedUnitResult) { - return RuntimeCompletionResult([], []); - } - - var session = contextResult.session; - - const codeMarker = '__code__'; - - // Insert the code being completed at the context offset. - var changeBuilder = ChangeBuilder(session: session); - var nextImportPrefixIndex = 0; - await changeBuilder.addDartFileEdit(contextPath, (builder) { - builder.addInsertion(contextOffset, (builder) { - builder.writeln('{'); - - // TODO(scheglov): Use variables. - - builder.write(codeMarker); - builder.writeln(';'); - - builder.writeln('}'); - }); - }, importPrefixGenerator: (uri) => '__prefix${nextImportPrefixIndex++}'); - - // Compute the patched context file content. - var targetCode = SourceEdit.applySequence( - contextResult.content, - changeBuilder.sourceChange.edits[0].edits, - ); - - // Insert the code being completed. - var targetOffset = targetCode.indexOf(codeMarker) + offset; - targetCode = targetCode.replaceAll(codeMarker, code); - - // Update the context file content to include the code being completed. - // Then resolve it, and restore the file to its initial state. - var targetResult = await _withContextFileContent(targetCode, () async { - return await analysisDriver.getResolvedUnit(contextPath); - }); - if (targetResult is! ResolvedUnitResult) { - return RuntimeCompletionResult([], []); - } - - var dartRequest = DartCompletionRequest.forResolvedUnit( - resolvedUnit: targetResult, - offset: targetOffset, - ); - - var serverSuggestions = await DartCompletionManager( - budget: CompletionBudget(CompletionBudget.defaultDuration), - ).computeSuggestions( - dartRequest, - OperationPerformanceImpl(''), - maxSuggestions: -1, - useFilter: false, - ); - var suggestions = serverSuggestions.map((e) => e.build()).toList(); - - // Remove completions with synthetic import prefixes. - suggestions.removeWhere((s) => s.completion.startsWith('__prefix')); - - // TODO(scheglov): Add support for expressions. - var expressions = []; - return RuntimeCompletionResult(expressions, suggestions); - } - - Future _withContextFileContent( - String newContent, Future Function() f) async { - if (resourceProvider.hasOverlay(contextPath)) { - var contextFile = resourceProvider.getFile(contextPath); - var prevOverlayContent = contextFile.readAsStringSync(); - var prevOverlayStamp = contextFile.modificationStamp; - try { - resourceProvider.setOverlay( - contextPath, - content: newContent, - modificationStamp: 0, - ); - analysisDriver.changeFile(contextPath); - return await f(); - } finally { - resourceProvider.setOverlay( - contextPath, - content: prevOverlayContent, - modificationStamp: prevOverlayStamp, - ); - analysisDriver.changeFile(contextPath); - } - } else { - try { - resourceProvider.setOverlay( - contextPath, - content: newContent, - modificationStamp: 0, - ); - analysisDriver.changeFile(contextPath); - return await f(); - } finally { - resourceProvider.removeOverlay(contextPath); - analysisDriver.changeFile(contextPath); - } - } - } -} - -/// The result of performing runtime completion. -class RuntimeCompletionResult { - final List expressions; - final List suggestions; - - RuntimeCompletionResult(this.expressions, this.suggestions); -} diff --git a/pkg/analysis_server/test/src/domains/execution/completion_test.dart b/pkg/analysis_server/test/src/domains/execution/completion_test.dart deleted file mode 100644 index a6ff36418cf0..000000000000 --- a/pkg/analysis_server/test/src/domains/execution/completion_test.dart +++ /dev/null @@ -1,390 +0,0 @@ -// 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/src/domains/execution/completion.dart'; -import 'package:analysis_server/src/protocol_server.dart'; -import 'package:analyzer/file_system/overlay_file_system.dart'; -import 'package:test/test.dart'; -import 'package:test_reflective_loader/test_reflective_loader.dart'; - -import '../../../abstract_context.dart'; - -void main() { - defineReflectiveSuite(() { - defineReflectiveTests(RuntimeCompletionComputerTest); - }); -} - -@reflectiveTest -class RuntimeCompletionComputerTest extends AbstractContextTest { - late OverlayResourceProvider overlayResourceProvider; - late String contextFilePath; - late int contextOffset; - - late RuntimeCompletionResult result; - - void addContextFile(String content) { - contextFilePath = convertPath('$testPackageLibPath/context.dart'); - newFile(contextFilePath, content); - - contextOffset = content.indexOf('// context line'); - expect(contextOffset, isNonNegative, - reason: "Not found '// context line'."); - } - - void assertNotSuggested(String completion) { - var suggestion = getSuggest(completion); - if (suggestion != null) { - failedCompletion('unexpected $completion'); - } - } - - void assertSuggested(String completion, {String? returnType}) { - var suggestion = getSuggest(completion); - if (suggestion == null) { - failedCompletion('expected $completion'); - } - if (returnType != null) { - expect(suggestion.returnType, returnType); - } - } - - Future computeCompletion(String code) async { - var codeOffset = code.indexOf('^'); - expect(codeOffset, isNonNegative); - code = code.replaceAll('^', ''); - - var contextFile = getFile(contextFilePath); - var computer = RuntimeCompletionComputer( - overlayResourceProvider, - driverFor(contextFile), - code, - codeOffset, - contextFilePath, - contextOffset, - ); - result = await computer.compute(); - } - - Never failedCompletion(String message) { - var sb = StringBuffer(message); - sb.write('\n found'); - result.suggestions.toList() - ..sort((a, b) => a.completion.compareTo(b.completion)) - ..forEach((suggestion) { - sb.write('\n ${suggestion.completion} -> $suggestion'); - }); - fail(sb.toString()); - } - - CompletionSuggestion? getSuggest(String completion) { - expect(result.suggestions, isNotNull); - for (var suggestion in result.suggestions) { - if (suggestion.completion == completion) { - return suggestion; - } - } - return null; - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_class_fields() async { - addContextFile(r''' -class A { - int a; -} -class B extends A { - double b, c; - void foo() { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - assertSuggested('c', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_class_methods() async { - addContextFile(r''' -class A { - int a() => null; -} -class B extends A { - double b() => null; - void foo() { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_inPart() async { - newFile('$testPackageLibPath/a.dart', r''' -part 'b.dart'; -part 'context.dart'; - -int a; -'''); - newFile('$testPackageLibPath/b.dart', r''' -part of 'a.dart'; - -double b; -'''); - addContextFile(r''' -part of 'a.dart'; - -String c; - -void f() { - // context line -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - assertSuggested('c', returnType: 'String'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_locals_block() async { - addContextFile(r''' -class A { - int foo; -} - -void contextFunction() { - var a = new A(); - // context line -} -'''); - await computeCompletion('a.^'); - assertSuggested('foo'); - - // There was an issue with cleaning up - // Check that the second time it works too. - await computeCompletion('a.^'); - assertSuggested('foo'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_locals_block_codeWithClosure() async { - addContextFile(r''' -void f() { - var items = []; - // context line -} -'''); - await computeCompletion('items.forEach((e) => e.^)'); - assertSuggested('toUpperCase'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_locals_block_nested() async { - addContextFile(r''' -void f() { - var a = 0; - var b = 0.0; - { - var a = ''; - // context line - } - var c = 0; -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'String'); - assertSuggested('b', returnType: 'double'); - - // "c" is defined after the context offset, so is not visible. - assertNotSuggested('c'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_locals_for() async { - addContextFile(r''' -void f(List intItems, List doubleItems) { - for (var a = 0, b = 0.0; a < 5; a++) { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_locals_forEach() async { - addContextFile(r''' -void f(List intItems, List doubleItems) { - for (var a in intItems) { - for (var b in doubleItems) { - // context line - } - } -}sosol -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_constructor() async { - addContextFile(r''' -class C { - C(int a, double b) { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_function() async { - addContextFile(r''' -void f(int a, double b) { - // context line -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_function_locals() async { - addContextFile(r''' -void f(int a, int b) { - String a; - double c; - // context line -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'String'); - assertSuggested('b', returnType: 'int'); - assertSuggested('c', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_function_nested() async { - addContextFile(r''' -void foo(int a, double b) { - void bar(String a, bool c) { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'String'); - assertSuggested('b', returnType: 'double'); - assertSuggested('c', returnType: 'bool'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_functionExpression() async { - addContextFile(r''' -void f(List intItems, List doubleItems) { - intItems.forEach((a) { - doubleItems.forEach((b) { - // context line - }); - }); -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_method() async { - addContextFile(r''' -class C { - void f(int a, double b) { - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_parameters_method_locals() async { - addContextFile(r''' -class C { - void f(int a, int b) { - String a; - double c; - // context line - } -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'String'); - assertSuggested('b', returnType: 'int'); - assertSuggested('c', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_syntheticImportPrefix() async { - newFile('/test/lib/a.dart', 'class A {}'); - newFile('/test/lib/b.dart', 'class B {}'); - addContextFile(r''' -import 'a.dart'; -impoty 'b.dart'; -void f() { - var a = new A(); - var b = new B(); - // context line -} -'''); - await computeCompletion('^'); - for (var suggestion in result.suggestions) { - expect(suggestion.completion, isNot(startsWith('__prefix'))); - } - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_topLevelFunctions() async { - addContextFile(r''' -int a() => null; -double b() => null; -void f() { - // context line -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } - - @FailingTest(reason: 'No support for OverlayResourceProvider') - Future test_topLevelVariables() async { - addContextFile(r''' -int a; -double b; - -void f() { - // context line -} -'''); - await computeCompletion('^'); - assertSuggested('a', returnType: 'int'); - assertSuggested('b', returnType: 'double'); - } -} diff --git a/pkg/analysis_server/test/src/domains/execution/test_all.dart b/pkg/analysis_server/test/src/domains/execution/test_all.dart deleted file mode 100644 index 1cbd9d36a070..000000000000 --- a/pkg/analysis_server/test/src/domains/execution/test_all.dart +++ /dev/null @@ -1,13 +0,0 @@ -// 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:test_reflective_loader/test_reflective_loader.dart'; - -import 'completion_test.dart' as completion_test; - -void main() { - defineReflectiveSuite(() { - completion_test.main(); - }); -} diff --git a/pkg/analysis_server/test/src/domains/test_all.dart b/pkg/analysis_server/test/src/domains/test_all.dart index d931cccf86cb..ccca833ce628 100644 --- a/pkg/analysis_server/test/src/domains/test_all.dart +++ b/pkg/analysis_server/test/src/domains/test_all.dart @@ -4,12 +4,10 @@ import 'package:test_reflective_loader/test_reflective_loader.dart'; -import 'execution/test_all.dart' as execution; import 'flutter/test_all.dart' as flutter; void main() { defineReflectiveSuite(() { - execution.main(); flutter.main(); }); }