Refactor TestCode to prepare to move it to analyzer

This is the first phase of preparing it to be moved. The plan is for the
extensions to remain in this library along with an export of the new
library in the analyzer package.

The second phase will be to rewrite it to not use string_scanner so that
we don't have to add a dependency to the analyzer package.

Change-Id: I0d4e9c47bdcb7b825a9d3bfc76331a4e575ac664
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/263443
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Brian Wilkerson 2022-10-18 18:31:53 +00:00 committed by Commit Queue
parent 138c956bcd
commit c42ab25787
4 changed files with 54 additions and 47 deletions

View file

@ -50,7 +50,7 @@ class FoldingTest extends AbstractLspAnalysisServerTest {
void expectRanges(Map<int, FoldingRangeKind?> expected,
{bool requireAll = true}) {
final expectedRanges = expected.entries.map((entry) {
final range = code.ranges[entry.key].lsp;
final range = code.ranges[entry.key].range;
return FoldingRange(
startLine: range.start.line,
startCharacter: lineFoldingOnly ? null : range.start.character,

View file

@ -91,7 +91,7 @@ abstract class AbstractTypeHierarchyTest extends AbstractLspAnalysisServerTest {
await initialize();
final result = await prepareTypeHierarchy(
mainFileUri,
code.position.lsp,
code.position.position,
);
prepareResult = result?.singleOrNull;
}
@ -109,8 +109,8 @@ class PrepareTypeHierarchyTest extends AbstractTypeHierarchyTest {
_isItem(
'MyClass1',
mainFileUri,
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
);
}
@ -155,8 +155,8 @@ import 'main.dart';
'MyClass2',
otherFileUri,
detail: 'extends',
range: otherCode.ranges[0].lsp,
selectionRange: otherCode.ranges[1].lsp,
range: otherCode.ranges[0].range,
selectionRange: otherCode.ranges[1].range,
),
]));
}
@ -174,8 +174,8 @@ class MyCla^ss1 {}
'MyClass2',
mainFileUri,
detail: 'extends',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -193,8 +193,8 @@ class MyCla^ss1 {}
'MyClass2',
mainFileUri,
detail: 'implements',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -212,8 +212,8 @@ class MyCla^ss1 {}
'MyMixin1',
mainFileUri,
detail: 'constrained to',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -231,8 +231,8 @@ mixin MyMi^xin1 {}
'MyClass1',
mainFileUri,
detail: 'mixes in',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -266,8 +266,8 @@ class MyCla^ss2 extends MyClass1 {}
'MyClass1',
otherFileUri,
detail: 'extends',
range: otherCode.ranges[0].lsp,
selectionRange: otherCode.ranges[1].lsp,
range: otherCode.ranges[0].range,
selectionRange: otherCode.ranges[1].range,
),
]));
}
@ -285,8 +285,8 @@ class MyCla^ss2 extends MyClass1 {}
'MyClass1',
mainFileUri,
detail: 'extends',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -305,8 +305,8 @@ class MyCla^ss2 implements MyClass1 {}
'MyClass1',
mainFileUri,
detail: 'implements',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -324,8 +324,8 @@ mixin MyMix^in1 on MyClass1 {}
'MyClass1',
mainFileUri,
detail: 'constrained to',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}
@ -344,8 +344,8 @@ class MyCla^ss1 with MyMixin1 {}
'MyMixin1',
mainFileUri,
detail: 'mixes in',
range: code.ranges[0].lsp,
selectionRange: code.ranges[1].lsp,
range: code.ranges[0].range,
selectionRange: code.ranges[1].range,
),
]));
}

View file

@ -31,8 +31,8 @@ int a = 1
expect(code.positions, hasLength(1));
expect(code.position.offset, 4);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.lsp, lsp.Position(line: 0, character: 4));
expect(code.position.lsp, code.positions[0].lsp);
expect(code.position.position, lsp.Position(line: 0, character: 4));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
@ -62,8 +62,8 @@ String a = '^^^';
expect(code.positions, hasLength(1));
expect(code.position.offset, 7);
expect(code.position.offset, code.positions[0].offset);
expect(code.position.lsp, lsp.Position(line: 0, character: 7));
expect(code.position.lsp, code.positions[0].lsp);
expect(code.position.position, lsp.Position(line: 0, character: 7));
expect(code.position.position, code.positions[0].position);
expect(code.ranges, isEmpty);
}
@ -86,9 +86,9 @@ int b = 2;
expect(code.positions[1].offset, 10);
expect(code.positions[2].offset, 16);
expect(code.positions[0].lsp, lsp.Position(line: 0, character: 4));
expect(code.positions[1].lsp, lsp.Position(line: 0, character: 10));
expect(code.positions[2].lsp, lsp.Position(line: 1, character: 5));
expect(code.positions[0].position, lsp.Position(line: 0, character: 4));
expect(code.positions[1].position, lsp.Position(line: 0, character: 10));
expect(code.positions[2].position, lsp.Position(line: 1, character: 5));
}
void test_positions_reused() {
@ -130,12 +130,12 @@ int b = 2;
expect(code.ranges[0].sourceRange, SourceRange(4, 6));
expect(code.ranges[1].sourceRange, SourceRange(11, 10));
expect(
code.ranges[0].lsp,
code.ranges[0].range,
lsp.Range(
start: lsp.Position(line: 0, character: 4),
end: lsp.Position(line: 0, character: 10)));
expect(
code.ranges[1].lsp,
code.ranges[1].range,
lsp.Range(
start: lsp.Position(line: 1, character: 0),
end: lsp.Position(line: 1, character: 10)));

View file

@ -121,10 +121,7 @@ class TestCode {
final positions = positionOffsets.map(
(number, offset) => MapEntry(
number,
TestCodePosition(
offset,
toPosition(lineInfo.getLocation(offset)),
),
TestCodePosition(lineInfo, offset),
),
);
@ -132,9 +129,9 @@ class TestCode {
(number, offset) => MapEntry(
number,
TestCodeRange(
lineInfo,
code.substring(offset, rangeEndOffsets[number]!),
SourceRange(offset, rangeEndOffsets[number]! - offset),
toRange(lineInfo, offset, rangeEndOffsets[number]! - offset),
),
),
);
@ -150,20 +147,21 @@ class TestCode {
/// A marked position in the test code.
class TestCodePosition {
/// Line break information for the test code.
final LineInfo lineInfo;
/// The 0-based offset of the marker.
///
/// Offsets are based on [TestCode.code], with all parsed markers removed.
final int offset;
/// The LSP [Position] of the marker.
///
/// Positions are based on [TestCode.code], with all parsed markers removed.
final Position lsp;
TestCodePosition(this.offset, this.lsp);
TestCodePosition(this.lineInfo, this.offset);
}
class TestCodeRange {
/// Line break information for the test code.
final LineInfo lineInfo;
/// The text from [TestCode.code] covered by this range.
final String text;
@ -173,10 +171,19 @@ class TestCodeRange {
/// removed.
final SourceRange sourceRange;
TestCodeRange(this.lineInfo, this.text, this.sourceRange);
}
extension TestCodePositionExtension on TestCodePosition {
/// Return the LSP [Position] of the marker.
///
/// Positions are based on [TestCode.code], with all parsed markers removed.
Position get position => toPosition(lineInfo.getLocation(offset));
}
extension TestCodeRangeExtension on TestCodeRange {
/// The LSP [Range] indicated by the markers.
///
/// Ranges are based on [TestCode.code], with all parsed markers removed.
final Range lsp;
TestCodeRange(this.text, this.sourceRange, this.lsp);
Range get range => toRange(lineInfo, sourceRange.offset, sourceRange.length);
}