New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a customisable formatter (custom indent settings, maybe .editorconfig?) #914

Open
Arcrueid opened this Issue May 18, 2018 · 29 comments

Comments

10 participants
@Arcrueid
Copy link

Arcrueid commented May 18, 2018

by my resolution I need to increase the tabsize from 2 to 4 spaces but it does not work

User settings:
"[dart]":{ "editor.tabSize": 4, "editor.insertSpaces": true, "editor.detectIndentation": false, }

when formatting the document, always use 2 spaces.

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented May 18, 2018

I'm afraid this is by design. The formatter that Dart Code uses is the official one (https://github.com/dart-lang/dart_style) and it does not support changing indenting; it always uses 2 spaces.

I'd love to have an alternative formatter, as I'm not a big fan of dart_style's opinions, but it's way down the list of priorities at this time.

I'm gonna keep this issue open and move to the backlog, as I think having a flexible formatter (such as based on editorconfig) is a reasonable request.

@DanTup DanTup modified the milestones: On Deck, Backlog May 18, 2018

@DanTup DanTup changed the title Format Document Support a customisable formatter (custom indent settings, maybe .editorconfig?) May 18, 2018

@AshSimmonds

This comment has been minimized.

Copy link

AshSimmonds commented Jun 16, 2018

For anyone OCD'd by this in VSCode and needs 4 or whatever spaces in the mean time, install the Macros extension, and map a key (or remap ctrl-S, whatever) to something like this:

"macros": {
    "saveAndFormatCode": [
        "cursorEnd",
        {
            "command": "type",
            "args": {
                "text": "  "
            }
        },
        "cursorLeftSelect",
        "cursorLeftSelect",
        "editor.action.selectHighlights",
        {
            "command": "type",
            "args": {
                "text": "    "
            }
        },
        "editor.action.trimTrailingWhitespace",
        "workbench.action.files.save",
    ]
}
@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Jun 16, 2018

@AshSimmonds How does that work? I presume it's effectively replacing 2-spaces with a tab, but I don't understand how it's working - what is the cursorEnd bit doing?

Also; if you are just replacing two spaces with a tab, this might mess up when stuff is indented with 4 spaces (wrapped lines) (though that might not be a big deal).

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Jun 16, 2018

Oh, I see - it's typing two spaces at the end, selecting them, then selecting all of the highlighted items (eg. the other double-spaces) and replacing them. Neat trick - though may have the issue mentioned above (and may mess with them insides strings, etc.)

@bwilkerson out of curiosity, does the plugin framework allow replacing the formatter in the server?

@AshSimmonds

This comment has been minimized.

Copy link

AshSimmonds commented Jun 16, 2018

I don't know that it's a great solution, but it works for me, for now.

Basically yeah - it puts the cursor at the end of the line (because often I save just willy nilly) then plugs in two spaces, then selects them, then selects ALL two spaces throughout the file, then replaces them with four spaces, then removes them by trimming - hopefully leaving the user where they were. (often it leaves you in the same place but still multi-selected, boo hoo but whatever)

@AshSimmonds

This comment has been minimized.

Copy link

AshSimmonds commented Jun 16, 2018

Oh and yeah - this will obviously mess up anything with two+ spaces in a string or whatever in your code - not my problem basically, just make your own sanity checks if you do stuff like that for some reason.

All this does is replaces all double spaces with quadruple spaces, nothing more, nothing less.

@bwilkerson

This comment has been minimized.

Copy link

bwilkerson commented Jun 16, 2018

... does the plugin framework allow replacing the formatter in the server?

No. The plugin framework allows most operations to be extended, but not replaced. Replacement is hard. What if two plugins want to replace the same operation? Which one wins?

Extending is much easier. What if two plugins want to add navigation information? No problem, we just union all of the results.

However, not all operations are amenable to being extended. Formatting seems like one of those.

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Jun 16, 2018

Makes sense, thanks!

@pablote

This comment has been minimized.

Copy link

pablote commented Jul 9, 2018

Seeing how opinionated the dart_style guys are about this, would it make sense to fork dart_style? make a more configurable version of it, and use that instead on dart-code? I don't really think that being able to chose between tabs/spaces and/or the tab size hurts the lang at all.

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Jul 10, 2018

Whether it's worthwhile depends on a few things:

  1. How much work is it to make a flexible/customisable version of dart_style
  2. How much work would it be to replace the built-in formatter (dart_style is included in the server, it's not just as simple as changing a dependency)
  3. How will it effect code generated by the server (the user wouldn't want all quick-fixes etc. to provide space-indented code if they'd used tabs)
  4. What is the demand for this from users

Right now, I don't have a good answer for any of these.

It's something I'd personally like to see - I really dislike the 2-space indenting, and with an ultrawide monitor wrapping at 80 characters just makes me scroll up/down a lot three quarters of my screen is just the background of my editor - but the benefit needs to outweigh the cost and right now I don't know if it does. I am keeping an eye on the 👍's on this issue though, and so far there are only five and one of them is mine!

@pablote

This comment has been minimized.

Copy link

pablote commented Jul 10, 2018

Ok, that makes sense. I've only recently got into Dart, because of Flutter, and I don't personally feel very confident to do these changes myself. I don't like the 2 spaces either, line widths of around 120 make more sense nowadays, and formatting doesn't seem to handle widget hierarchies great:

output2

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Jul 10, 2018

@pablote That formatting looks really bad - I don't think it should do that. Could you paste that code into a new issue for me to look at? Also - did you increase the max length from 80? I found that makes things much worse (it tries to unwrap everything), though I would still expect the trailing commas to keep things on multiple lines.

@pablote

This comment has been minimized.

Copy link

pablote commented Jul 10, 2018

I did, but reverting back to 80 causes the same issue. Creating a new issue thanks.

@tabinnorway

This comment has been minimized.

Copy link

tabinnorway commented Sep 21, 2018

Any idea if this is going to be worked on? I mean, it's not 1989. I have two big-arse monitors, and it's a little weird to have some Dart plugin developer tell me that all my Dart code needs to fit within the leftmost 1/6th of my editor space.

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Sep 22, 2018

it's a little weird to have some Dart plugin developer tell me

It's also a little weird for someone choosing to use a completely free open source plugin that I started in my spare time to suggest that I'm telling him how to write code 😕

As is stated in my comments above, I very much support this idea. I'm not a big fan of the default formatting either (I sometimes work on flutter_tools where I have to disable the formatter and format code manually). I have an Ultawide monitor and also think 80 character is silly (and although this is customisable, it causes other things to unwrap that I don't want).

The blocker for this is demand - see my comments in #914 (comment). If there is not enough demand then the analysis server is certainly not going to support it (and doing this without server support would not be efficient). The best thing you can for now do is put a 👍 on it and encourage others that want it to do the same. Supporting comments are welcome too, but please try to show a little respect for people that have put considerable spare time into delivering the free open source software you've chosen to use.

@AshSimmonds

This comment has been minimized.

Copy link

AshSimmonds commented Sep 22, 2018

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Sep 22, 2018

Re-indenting is a slightly simpler problem than changing the formatting entirely. Part of the problem I think many would like to see fixed though is the line-wrapping at 80 characters.

If fixing the indenting without changing wrapping etc. is valuable to many, then maybe there's value in doing something like your macro internally immediately after any format and on any new code inserted by code actions.

Put a 👍 on this comment if you think the indenting may be worthwhile on its own.

@AshSimmonds

This comment has been minimized.

Copy link

AshSimmonds commented Sep 22, 2018

Huh, ok. I've never noticed the line-wrap issue, always thought that was an IDE determined thing. Maybe I'm dumb and thinking of something else?

screenshot_20180922-185621
screenshot_20180922-185541

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Sep 22, 2018

Sorry, I didn't mean the line-wrap in the editor, but rather where the formatter breaks lines of code. For example if you write:

if (thing)
  return;

The formatter will unwrap this onto one line:

if (thing) return;

Unless the condition is really long, then it'll leave it on the next line. Same goes for code like:

myThing
    .filter(a => x(a))
    .first;

It'll be unwrapped onto a single line depending on the lengths.

There are also other things people may want to control, like brace placement. This case somewhat lumps them all together, but it's possible people have different priorities, maybe indenting is the biggest issue and may be worth trying to solve without being flexible with all of the other things.

@tabinnorway

This comment has been minimized.

Copy link

tabinnorway commented Sep 24, 2018

It's also a little weird for someone choosing to use a completely free open source plugin that I started in my spare time to suggest that I'm telling him how to write code

Sorry, bad on my part. Going for the thumbs up somewhere else :-)

@gggustafson

This comment has been minimized.

Copy link

gggustafson commented Nov 20, 2018

@DanTup Over more than 53 years of programming, the last 40 of which were in a structured environment, I have come to appreciate coding guidelines. Where the dart folks and I disagree is formatting. I do not understand why dart formatting is so rigorous, trampling on the desires of the developers upon who dart depends for its success. I wish to propose a method whereby everyone can use their own styles. It is already imbedded in Visual Studio.

The concept is relatively simple. Provide a list of formatting options akin to the current Settings. The following is a partial list from Visual Studio 2008:

Line Numbers
WrappingPreserveSingleLine
FormatOnPaste
Add New Line On Enter After Fully Typed Word
WrappingKeepStatementsOnSingleLine
SpaceWithinOtherParentheses
Auto Formatting On Close Brace
OpenCloseBracesIndent
SpaceWithinMethodCallParentheses
SpaceAfterCast
SpacesIgnoreAroundVariableDeclaration
SpaceWithinExpressionParentheses
SpaceBetweenEmptyMethodDeclarationParentheses
PlaceSystemNamespaceFirst
SpaceWithinSquareBrackets
SpaceBetweenEmptySquareBrackets
SpaceBeforeOpenSquareBracket
SpacingAfterMethodDeclarationName
SpaceAfterMethodCallName
SpaceWithinCastParentheses
SpaceWithinMethodDeclarationParenthesis
SpaceBetweenEmptyMethodCallParentheses

Each programmer sets their own settings, reflecting their own preferences. During editing, reformatting modifies the source so that it meets the programmer's desires.

Here's the trick. Although the programmer writes the code using a favored style, the company, client, reader, etc. has a coding style of their own. Thus when a piece of code is being reviewed, it can be reformatted into the style most comfortable to each reader. When code is being delivered, the style imposed is that defined by the customer. And when code is returned to a programmer, that programmer can apply a different style.

The VS 2008 settings are at Tools->Options->Text Editor. The interface is intuitive and instructive.

@DanTup

This comment has been minimized.

Copy link
Member

DanTup commented Nov 21, 2018

@gggustafson I think something like EditorConfig would be the best way to achieve something like this (which VS does support too).

See the comments above for why there's been no movement on this - it's somewhat complicated to implement and the demand for it is low (even though personally,I really support it - I think the existing formatter trades readability for consistency, and that isn't always the right choice). The best thing to do is add a 👍 and encourage anyone else that feels strongly about it to do the same. It'd be easier to justify spending effort on this if there's a good demand for it.

Thanks!

@gggustafson

This comment has been minimized.

Copy link

gggustafson commented Nov 21, 2018

@DanTup I have no doubt that implementing a formatter of the kind that I suggest may be somewhat complicated. But as usual, I'd be glad to provide assistance if desired.

In so far as low demand, that may be caused by a number of factors: low use of dart and flutter in the wild, abysmal coding styles taught in "schools of higher learning", failure on the part of programmers to realize that they are not the only persons affected by their coding style.

When I was teaching the core computer science courses at Chapman University, I was amazed at the views my students had of programming. They couldn't care less whether or not their code was maintainable! It was only after the class was split into small three or four person groups, each group given unique-to-the-group requirements for a group project, coded the project, exchanged the code between groups, and then given changed requirements, that they began to understand how much coding style matters. Trading readability for consistency is immature! (It would also have resulted in a lower grade!)

Today, I am just "playing" with dart and flutter. I have not yet decided its utility. But I have time to work on a formatting project. Let me know if I may be of assistance.

@seanvikoren

This comment has been minimized.

Copy link

seanvikoren commented Nov 29, 2018

@DanTup

It is my dream to use tabs (not spaces) set for a width of 4 wile editing Dart files. Just for fun, I checked my max visible characters, and with VS Code in full-screen mode, using a 9 point font (yes, still quite readable), I have room for 475 characters visible charters per line .

If there is anything I can do to get involved in expanding the flexibility of the formatter, I am at your service.

It is my suspicion that the adoption of Dart would be positively affected if the formatter was more flexible and I want to help with this if possible.

@gggustafson Have you got any traction on this?

@gggustafson

This comment has been minimized.

Copy link

gggustafson commented Dec 1, 2018

@seanvikoren See @DanTup reply regarding EditorConfig, above. I am considering writing a dart formatter that more closely follows that of Visual Studio (VS). FYI the VS formatter honors TAB vs space.

As Dan asked, please add an up-vote and encourage anyone else that feels strongly about it to do the same.

@petermichaux

This comment has been minimized.

Copy link

petermichaux commented Jan 15, 2019

Indenting by a personal preference of some number of spaces or tabs is a detail programmers are definitely passionate about. Having it configurable would be very well appreciated by those not in the 2 spaces camp. Fingers crossed this can become configuration option.

@seanvikoren

This comment has been minimized.

Copy link

seanvikoren commented Jan 15, 2019

@tedhenry100

This comment has been minimized.

Copy link

tedhenry100 commented Jan 16, 2019

4 spaces please! 🙏

@gggustafson

This comment has been minimized.

Copy link

gggustafson commented Jan 17, 2019

I am currently developing a command-line formatter. I am using the options from Visual Studio for C#. I will post when it's available for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment