rearch-dart icon indicating copy to clipboard operation
rearch-dart copied to clipboard

feat: add flutter and widget related macros

Open GregoryConrad opened this issue 1 year ago • 3 comments
trafficstars

Partially fixes #14

GregoryConrad avatar Jan 30 '24 23:01 GregoryConrad

Macro execution doesn't appear to work in Flutter yet, but this is working in Dart-only for basic cases as far as I can tell.

Only thing that is broken is default values for named parameters, but those appear to be unimplemented by the macro system at the moment anyways.

Initial comments are that the macro API is a bit of a pain to work with; why is there no pattern matching on TypeAnnotation/why do I manually need to (typeAnnotation as NamedTypeAnnotation)? Also, I can't see any clearly defined way to check equality between two types (where one is defined externally), so I opted instead for String equality right now (which is obviously "wrong," but will work for 99.9% of cases).

They should really incorporate an api like: builder.resolve(myType) == Widget.staticType or similar, where Widget is imported from package:flutter/widgets.dart

GregoryConrad avatar Feb 02 '24 17:02 GregoryConrad

The widget macros, which were the scope of this PR, are essentially done, at least to the best of what I can do right now with the current macros implementation. Just need to wait until macros are implemented further before I can clean this up more.

GregoryConrad avatar Feb 05 '24 01:02 GregoryConrad

Looks like quite a bit changed in the macros implementation since the last actual commit--need to bring this up to speed again.

GregoryConrad avatar May 19 '24 17:05 GregoryConrad

Updated this yet once again; added in some cool quasi-quasiquoting (get it?) functionality to make writing the macros suck less. See the extension _Substitute on String if you're making your own macros.

This time I had to test in packages/rearch since Flutter support isn't there yet. For future reference, needed this type boilerplate:

abstract class BuildContext {
  T dependOnInheritedWidgetOfExactType<T>();
}

abstract class Widget {
  const Widget({Object? key,});
}

abstract class WidgetHandle {}

abstract class RearchConsumer extends Widget {
  const RearchConsumer({super.key,});
  Widget build(BuildContext context, WidgetHandle use);
}
abstract class InheritedWidget extends Widget {
  const InheritedWidget({required Widget child,super.key,});
}
extension A on WidgetHandle {
  (T, void Function(T)) state<T>(T init) => throw '';
}

GregoryConrad avatar Dec 22 '24 04:12 GregoryConrad

https://github.com/dart-lang/language/issues/1482#issuecomment-2622895490

GregoryConrad avatar Jan 29 '25 21:01 GregoryConrad