emacsorphanage / dart-mode

An Emacs mode for the Dart language
GNU General Public License v3.0
15 stars 2 forks source link

Refactoring, templates, etc #102

Open bbuccianti opened 5 years ago

bbuccianti commented 5 years ago

Hi! I'm thinking of making a function similar of one in VsCode that put a widget inside another under the child property.

Do you think we can do it? I think i can help with your guide

bradyt commented 5 years ago

I assume you mean "guidance", not "guide".

The question as phrased might be a better fit elsewhere, for example, freenode's #emacs.

bbuccianti commented 5 years ago

I think that a function like this make sense in dart-mode package for emacs. I just want to help on this project. I know where is freenode's #emacs.

But you're the maintainer. Thank for the reply

bradyt commented 5 years ago

I was certainly on the fence about closing this. I'll reopen with some additional input.

Where does this go, how should this be done?

(Note: Some types of refactoring may eventually be available via dart's language server.)

It's not a given that this belongs in dart-mode.

Let's look for idioms in Emacs. My intention here is to provide a lightweight language mode to support syntax highlighting and smart indentation. I look forward to dart team providing dartfmt via the language server, as then we can remove support for dartfmt from dart-mode. Tools like eglot can then handle dartfmt.

Perhaps when syntax highlighting and indentation are to a better situation, I wouldn't be so averse to adding functionality to this project.

One approach to improving the refactoring and templating issue, is to simply contribute to the yasnippet project. I took a quick look, it even has some tools for working with current selection.

This all could potentially go in something like an independent dart-refactor project. Consider I also think a flutter-minor-mode would be great too, for example, to call hot-reload from your editing buffer.

And consider, if something new is added to dart-mode, I might be really wanting new contributions to have tests added with them. I could certainly use some help with adding tests to dart-mode, including some notes on setting up tests. I'd love help with getting tests going for syntax highlighting and indentation. I've tried a couple times but have not been happy enough with my drafts.

A summary of related features I've thought about

Wrapping constructor

Without a more verbose description from you, I've had to assume that this is what you originally meant.

build(ctx) {
  return WidgetA(
    child: WidgetB(),
  );
}

becomes

build(ctx) {
  return WidgetA(
    child: WidgetC(
      child: WidgetB(),
    ),
  );
}

Inserting or switching to stateful widget

We might want to insert a stateful widget, or a little more interesting, quickly convert a stateless widget to a stateful widget.

class Blah extends StatelessWidget {
  var someStuff;
}

becomes

class Blah extends StatefulWidget {
  BlahState createState() => BlahState();
}

class BlahState extends State<Blah> {
  var someStuff;
  Widget build(BuildContext context) {
    return Container();
  }
}

Toggling function bodies

Sometimes I'd like to quickly toggle between the two forms,

void main() => x;

and

void main() {
  return x;
}
bradyt commented 5 years ago

Hmm, so C-M-h (mark-sexp) could help with selecting a region, before applying yasnippet to selection. But I'm not sure that dart-mode is smart everywhere about detecting statements. And that is something I'd very much like dart-mode to do correctly. But I wasn't able to quickly make a minimal broken example, so just leaving a note here.

In other words, I completely feel that an idiomatic lightweight major-mode for editing programming languages, should have working motion commands.

But hacking together with backwards-word and forward-list could be enough to get an okay-ish automated selection, before wrapping a widget. User can always stop and consider the selection, to make sure it makes sense.

bbuccianti commented 5 years ago

That's what I'm talking about. Sorry for my little explanation. I'm trying really hard to being capablr of comunicate my intentions but english isn't my native language.

I think that we can use something like expand region in order to select the widget that are going to become a child for the new widget. But I don't really know how to do that.

Currently dart-mode can't detect a widget, right? Maybe we can work in thid capability?

Something like if I'm with my cursor in widget B and I invoke a dart-mode function expand then wifget B is selected. Then I think is trivial to implement the kill of the region, insert of the new widget and put the killed region inside the child.

Any way I can help you on this?

bradyt commented 5 years ago

I've given a few ideas, not sure what's stopping now. Perhaps IRC would be a good place to find more guidance.

You're also welcome to just wait on discussion here, not sure when I will make time to add more to your ideas.

Quick note, M-m C-u 3 C-M-SPC seems to select a reasonable region. And again, perhaps yasnippet is a good way to go here.

Or C-e C-M-u C-M-SPC M-b.

bradyt commented 5 years ago

There is also reddit and stack exchange for help with custom editing commands. Not sure it's really in scope of this project.