Skip to content

Commit

Permalink
Don't show hovercards for primitive and null values (#8528)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette authored Nov 26, 2024
1 parent fae2844 commit 0d0b96c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ class _LineItemState extends State<LineItem>

final word = wordForHover(event.localPosition.dx, widget.lineContents);

if (word != '') {
if (word != '' && !isPrimitiveValueOrNull(word)) {
try {
final response = await evalService.evalAtCurrentFrame(word);
final isolateRef =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ class DiagnosticsNodeDescription extends StatelessWidget {
enabled:
() =>
preferences.inspector.hoverEvalModeEnabled.value &&
diagnosticLocal.objectGroupApi != null,
diagnosticLocal.objectGroupApi != null &&
!isPrimitiveValueOrNull(description),
asyncGenerateHoverCardData: ({
required event,
required isHoverStale,
Expand Down
19 changes: 19 additions & 0 deletions packages/devtools_app/lib/src/shared/ui/hover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ String wordForHover(double dx, TextSpan line) {
return word;
}

bool isPrimitiveValueOrNull(String valueAsString) {
if (valueAsString.isEmpty) return false;
final isNull = valueAsString == 'null';
final isBool = valueAsString == 'true' || valueAsString == 'false';
final isInt = int.tryParse(valueAsString) != null;
final isDouble = double.tryParse(valueAsString) != null;

bool isString = false;
if (valueAsString.length > 2) {
final firstChar = valueAsString[0];
final lastChar = valueAsString[valueAsString.length - 1];
isString =
[firstChar, lastChar].every((char) => char == '"') ||
[firstChar, lastChar].every((char) => char == "'");
}

return isNull || isBool || isInt || isDouble || isString;
}

/// Returns the index in the Textspan's plainText for which the hover offset is
/// located.
int _hoverIndexFor(double dx, TextSpan line) {
Expand Down
34 changes: 34 additions & 0 deletions packages/devtools_app/test/shared/hover_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,38 @@ void main() {
expect(wordForHover(250, _textSpan), 'foo.bar');
expect(wordForHover(300, _textSpan), 'foo.bar.baz');
});

group('isPrimitiveValueOrNull', () {
test('returns false for non-primitives values', () {
expect(isPrimitiveValueOrNull('myVariable'), isFalse);
expect(isPrimitiveValueOrNull('MyWidget'), isFalse);
expect(isPrimitiveValueOrNull('MyClass'), isFalse);
});

test('returns true for null', () {
expect(isPrimitiveValueOrNull('null'), isTrue);
});

test('returns true for ints', () {
expect(isPrimitiveValueOrNull('10'), isTrue);
expect(isPrimitiveValueOrNull('3'), isTrue);
expect(isPrimitiveValueOrNull('255'), isTrue);
});

test('returns true for doubles', () {
expect(isPrimitiveValueOrNull('.3'), isTrue);
expect(isPrimitiveValueOrNull('1.7'), isTrue);
expect(isPrimitiveValueOrNull('123.389'), isTrue);
});

test('returns true for bools', () {
expect(isPrimitiveValueOrNull('true'), isTrue);
expect(isPrimitiveValueOrNull('false'), isTrue);
});

test('returns true for strings', () {
expect(isPrimitiveValueOrNull('"Hello World!"'), isTrue);
expect(isPrimitiveValueOrNull("'Hello World!'"), isTrue);
});
});
}

0 comments on commit 0d0b96c

Please sign in to comment.