dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.27k stars 1.58k forks source link

Add Closing Tag token #32202

Open Rockvole opened 6 years ago

Rockvole commented 6 years ago

Add a closing tag token to Dart to permit the addition of xml-like closing tags. These "closing tags" could be added automatically by dartfmt or an editor for example.

I suggest # - or _ but the Dart language creators should decide what is best.

Here is an example of how it would work :

e.g. if we use # for example :

# My comment

^ This is invalid code currently, and also after the new "closing tags" feature

new Text("Hello World",
         softWrap: true
)#Text

^ The #Text is not intended for user edits, it is only valid when combined with an end bracket. This is so that it is not confused as a comment for general use. The text after the )# would be ignored by the compiler as per a comment.

More detailed explanation can be seen in : Idea: Auto generated closing tags

Rockvole commented 6 years ago

@lrhn - any comments on this ?

bwilkerson commented 6 years ago

The latest versions of IntelliJ and VS Code both make visible support added to the analysis server to provide IDE level decorations exactly like what you're describing when the nesting structure of constructor invocations is deep enough. This was initially provided for Flutter build methods where the nesting level can get quite deep. The decorations show up in the edit pane, but are neither selectable nor editable. If you can try out one or the other, we'd love to hear your feedback.

Rockvole commented 6 years ago

Do you mean these ghost comments ?

https://user-images.githubusercontent.com/3357543/36224109-685b88b0-117b-11e8-9b55-65fc79ea73b5.png

They are nice to look at, but not as useful as physical closing tags, because when you are inserting code fragments and your code is temporarily broken, the ghost tags vanish and become unhelpful - exactly when you really need to refer to them.

bwilkerson commented 6 years ago

Yes, those.

It isn't clear to me that having it explicitly in the source code would simplify editing, but then I've never had such a feature available, so perhaps I'd be surprised.

That said, it seems like using the existing comment syntax would be as good as introducing a new syntax:

new Text("Hello World",
         softWrap: true
)//Text

and it would be easy for someone to write a tool to automatically add them when they're missing.

Rockvole commented 6 years ago

The purpose of creating a new type of comment specifically for "closing tags" is so the user does not start editing them or think of them as comments. If the user adds their own comment - then an automatic tool could mistake that for an incorrect closing tag and overwrite it.

I have edited a lot of code with closing tags in the past in the form of JSTL. A more recent example would be JSX or editing the Android XML layouts. It is quite a bit easier to see where code fragments begin and end in these examples. I think not having the closing tags in flutter is worse, because a lot of the time the mistake which you are trying to fix is small e.g. a missing / misplaced comma or curly bracket.

zoechi commented 6 years ago

Setting the cursor on a closing brace highlights the opening brace (for example in IntelliJ). If find that much better because it doesn't add so much noise.

Rockvole commented 6 years ago

I admit that after developing with Flutter for a couple of months I am better at seeing the patterns in the code than I was so it is not such a big deal. Plus you develop the habits such as finding the bracket insertion positions before pasting the code in place.

But I would still argue that its a useful option to have particularly for beginners. If a feature like this makes a transition less jarring for beginners then I think it is worth some effort.

Depending on what language you came from the confusion will vary, like your previous language might not have had anonymous functions - and those brackets seem wrong until you have seen them for a while. If you are coming from JSX then having the "ghost tags" move around while you are trying to fix a piece of code would seem like a step-down I am sure.

Also, I dont agree that there is added noise because I am talking about replacing the "ghost closing tags" with "physical closing tags" so the "noise" is identical. If you dont like that you would turn the feature off in both cases.