Skip to content

Commit

Permalink
Version 3.4.0-204.0.dev
Browse files Browse the repository at this point in the history
Merge 888941c into dev
  • Loading branch information
Dart CI committed Mar 6, 2024
2 parents 64f3104 + 888941c commit 274b73f
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,36 @@ class FeatureComputer {
return range.upper;
}

/// Return the distance between the [reference] and the referenced local
/// [variable], where the distance is defined to be the number of variable
/// declarations between the local variable and the reference.
int localVariableDistance(AstNode reference, LocalVariableElement variable) {
/// Return the distance between the [reference] and [variable].
///
/// For [LocalVariableElement] the distance is the number of variable
/// declarations between [variable] and the reference.
///
/// For [ParameterElement] the first one is considered to be "closer".
/// Plus we add any local variables declared on the path to [reference].
int localVariableDistance(AstNode reference, VariableElement variable) {
var distance = 0;
AstNode? node = reference;
while (node != null) {
if (node is ForStatement || node is ForElement) {
if (node is FunctionExpression) {
if (node.parameters case var parameterList?) {
for (var parameter in parameterList.parameters) {
if (parameter.declaredElement == variable) {
return distance;
}
distance++;
}
}
} else if (node is MethodDeclaration) {
if (node.parameters case var parameterList?) {
for (var parameter in parameterList.parameters) {
if (parameter.declaredElement == variable) {
return distance;
}
distance++;
}
}
} else if (node is ForStatement || node is ForElement) {
var loopParts = node is ForStatement
? node.forLoopParts
: (node as ForElement).forLoopParts;
Expand Down Expand Up @@ -448,7 +470,7 @@ class FeatureComputer {
/// variable whose declaration is separated from the completion location by
/// [distance] other variable declarations.
double localVariableDistanceFeature(
AstNode reference, LocalVariableElement variable) {
AstNode reference, VariableElement variable) {
var distance = localVariableDistance(reference, variable);
return _distanceToPercent(distance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,13 @@ class SuggestionBuilder {
/// Add a suggestion for a [parameter].
void suggestParameter(ParameterElement parameter) {
var variableType = parameter.type;
// TODO(brianwilkerson): Use the distance to the declaring function as
// another feature.
var target = request.target;
var entity = target.entity;
var node = entity is AstNode ? entity : target.containingNode;
var contextType = request.featureComputer
.contextTypeFeature(request.contextType, variableType);
var localVariableDistance =
request.featureComputer.localVariableDistanceFeature(node, parameter);
var elementKind = _computeElementKind(parameter);
var isConstant = _preferConstants
? request.featureComputer.isConstantFeature(parameter)
Expand All @@ -999,6 +1002,7 @@ class SuggestionBuilder {
contextType: contextType,
elementKind: elementKind,
isConstant: isConstant,
localVariableDistance: localVariableDistance,
);
_addBuilder(
_createCompletionSuggestionBuilder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,42 +95,9 @@ CompletionDefaultArgumentList computeCompletionDefaultArgumentList(
}
offset = sb.length;

var parameterType = param.type;
if (parameterType is FunctionType) {
var rangeStart = offset;
int rangeLength;

// TODO(pq): consider adding ranges for params
// pending: https://github.com/dart-lang/sdk/issues/40207
// (types in closure param completions make this UX awkward)
final parametersString = buildClosureParameters(parameterType);
final blockBuffer = StringBuffer(parametersString);

blockBuffer.write(' ');

// TODO(pq): consider refactoring to share common logic w/
// ArgListContributor.buildClosureSuggestions
final returnType = parameterType.returnType;
if (returnType is VoidType) {
blockBuffer.write('{');
rangeStart = sb.length + blockBuffer.length;
blockBuffer.write(' }');
rangeLength = 1;
} else {
final returnValue = returnType.isDartCoreBool ? 'false' : 'null';
blockBuffer.write('=> ');
rangeStart = sb.length + blockBuffer.length;
blockBuffer.write(returnValue);
rangeLength = returnValue.length;
}

sb.write(blockBuffer);
ranges.addAll([rangeStart, rangeLength]);
} else {
var name = param.name;
sb.write(name);
ranges.addAll([offset, name.length]);
}
var name = param.name;
sb.write(name);
ranges.addAll([offset, name.length]);
}

for (var param in namedParams) {
Expand Down
5 changes: 3 additions & 2 deletions pkg/analysis_server/test/client/completion_driver_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ abstract class AbstractCompletionDriverTest
The actual suggestions do not match the expected suggestions$where.
To accept the current state change the expectation to
$actual
\r${'-' * 64}
\r${actual.trimRight().split('\n').join('\n\r')}
\r${'-' * 64}
''');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3400,8 +3400,8 @@ void f() {
suggestions
f0
kind: methodInvocation
defaultArgumentList: x, (a, b) { }, y
defaultArgumentListRanges: [0, 1, 11, 1, 15, 1]
defaultArgumentList: x, closure, y
defaultArgumentListRanges: [0, 1, 3, 7, 12, 1]
''');
}

Expand All @@ -3420,8 +3420,8 @@ void f() {
suggestions
f0
kind: methodInvocation
defaultArgumentList: (a, b) => false
defaultArgumentListRanges: [10, 5]
defaultArgumentList: closure
defaultArgumentListRanges: [0, 7]
''');
}

Expand All @@ -3440,8 +3440,8 @@ void f() {
suggestions
f0
kind: methodInvocation
defaultArgumentList: (a, b) => null
defaultArgumentListRanges: [10, 4]
defaultArgumentList: closure
defaultArgumentListRanges: [0, 7]
''');
}

Expand All @@ -3460,8 +3460,8 @@ void f() {
suggestions
f0
kind: methodInvocation
defaultArgumentList: (a, b) { }
defaultArgumentListRanges: [8, 1]
defaultArgumentList: closure
defaultArgumentListRanges: [0, 7]
''');
}

Expand Down

This file was deleted.

Loading

0 comments on commit 274b73f

Please sign in to comment.