Skip to content
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

Format only modified lines #44075

Closed
kylegmaxwell opened this issue Feb 21, 2018 · 23 comments · Fixed by #104994
Closed

Format only modified lines #44075

kylegmaxwell opened this issue Feb 21, 2018 · 23 comments · Fixed by #104994
Assignees
Labels
feature-request Request for new features or functionality formatting Source formatter issues insiders-released Patch has been released in VS Code Insiders on-testplan
Milestone

Comments

@kylegmaxwell
Copy link

Issue Type

Feature Request

Description

It would be helpful if there were a way to format only the modified lines. What is modified would be determined by source control. Currently formatOnType only works with semi colon (see microsoft/vscode-cpptools#1419), and formatOnSave formats the entire file. This is undesirable when working on large codebases with legacy code. In that case I want to make a small change, and have it automatically formatted on save, but not modify the rest of the file, which would create a huge diff.
It would also be great to have similar behavior for trimming trailing white space.

VS Code Info

VS Code version: Code 1.20.1 (f88bbf9, 2018-02-13T15:31:21.019Z)
OS version: Linux x64 4.14.16-200.fc26.x86_64

Steps to Reproduce:

  1. Set preferences:
    "editor.formatOnSave": true,
    "C_Cpp.clang_format_path": "/usr/bin/clang-format",
  2. Open a C++ file with multiple formatting errors
  3. Modify the file on one line.
  4. Note that multiple modifications have been made to the file.

Does this issue occur when all extensions are disabled?:

No, formatting does not work at all in that case.

@Gruntfuggly
Copy link

Been hoping for this for a while. In the meantime, I've knocked up an extension...

https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.format-modified

@kylegmaxwell
Copy link
Author

Awesome! Just tried it out and it seems to work so far.

@alekseyt
Copy link

Great job, but this shouldn't be an extension IMO, this is a legit issue with editor and i don't personally think "feature-request" label is applicable here. I'm in the same boat and i have to use external merge tool (e.g.meld) to rollback unwanted changes.

@alexdima alexdima added formatting Source formatter issues and removed editor labels Sep 20, 2018
@guillochon
Copy link

That extension appears to be C++ only, anyone know of a Python equivalent?

@devinrhode2
Copy link

devinrhode2 commented Mar 21, 2020

Extension appears to support many languages now, but only because clang-format supports multiple languages

format-modified.languages

Use this to control which files are formatted. It is set to ["cpp"] by default, so .cpp and .h files are formatted. Use F1 -> Change Language Mode to show a list of language identifiers. Java, Javascript, Objective-C, Proto are supported by clang-format, but I have not tested them.

@ronakg
Copy link

ronakg commented Apr 20, 2020

Any update on this issue? I don't want to use the extension mentioned above because all my formatting is set up via their respective language servers. This is a very valid use-case that needs addressing.

@jrieken
Copy link
Member

jrieken commented Aug 21, 2020

This will land with the August release. This is what I planning to merge later this afternoon

  • new setting editor.formatOnSaveMode with options file and modifications
  • new command "Format Modified Lines"

Note that knowing "what is modified" requires active source control, like git. Without source control there are no modifications and nothing will be formatted.

@jrieken
Copy link
Member

jrieken commented Aug 21, 2020

Aug-21-2020 12-53-29

@kylegmaxwell
Copy link
Author

Looks awesome, thank you!

@ronakg
Copy link

ronakg commented Aug 21, 2020

This will land with the August release. This is what I planning to merge later this afternoon

  • new setting editor.formatOnSaveMode with options file and modifications
  • new command "Format Modified Lines"

Note that knowing "what is modified" requires active source control, like git. Without source control there are no modifications and nothing will be formatted.

@jrieken This looks great, thanks! When you say that without source control there are no modifications, are you saying that the setting editor.formatOnType won't have any effect also? Right now I'm relying on this setting to auto-format the newly added lines.

@jrieken
Copy link
Member

jrieken commented Aug 24, 2020

Yes - if a project is not under source control and format on save is limited to modifications then no formatting happens. Tho, when a project is under source control than all modified and added lines/files are formatted.

@ronakg
Copy link

ronakg commented Aug 24, 2020

Yes - if a project is not under source control and format on save is limited to modifications then no formatting happens. Tho, when a project is under source control than all modified and added lines/files are formatted.

I'm asking about a different setting editor.formatOnType, which is separate from editor.formatOnSave. Can you clarify how these 2 will work together now? I'm guessing editor.formatOnType will work as usual irrespective of editor.formatOnSave.

@jrieken
Copy link
Member

jrieken commented Aug 24, 2020

Oh, misread that. Yeah, format on type has nothing to do with format on save and nothing has changed for that

@icharge
Copy link

icharge commented Sep 11, 2020

This feature is great. I like it.
Please provide not only just on save.

@Nabav
Copy link

Nabav commented Sep 11, 2020

The command Format Modified Lines is not showing up in the commands list.

@jrieken
Copy link
Member

jrieken commented Sep 11, 2020

It will only show up when you have a formatter that supports formatting modified ranges, e.g one that support range formatting. The rule of thumb is that if you have "Format Selection" then you also have "Format Modified Lines"

@MikeYuanMY
Copy link

which popular formatters currently support this? I have prettier but cannot seem working with this feature

@jrieken
Copy link
Member

jrieken commented Sep 14, 2020

@icharge
Copy link

icharge commented Sep 14, 2020

Oh I see, I'm using Prettier then I have to set new shortcut key. :)
Screen Shot 2563-09-14 at 13 47 19

@kylegmaxwell
Copy link
Author

A lot of people are asking for format modified on type instead of save. I didn't ask for that since I thought it does not make sense. If you format on type then you always just format the lines as you modify them. I needed format modified since I wanted to make changes to several lines and then save and see just those lines formatted. Also I was using C++ with clang_format which does not work well with formatting single lines since it sometimes removes the line you are typing on, hence the desire to format modified lines on save.

@sheerun
Copy link

sheerun commented Sep 17, 2020

Unfortunately this often doesn't work often giving "Overlapping ranges are not allowed! error prettier/prettier-vscode#497

@valeryan
Copy link

valeryan commented Sep 17, 2020

My extension uses PHPCS/CBF to format PHP files and because many of the rules are dealing with whitespace formatting allowing format selection does not work well. PHPCBF has an option to only format modified lines. I would like to be able to implement this without also implementing format by selection and just format the document using PHPCBF's internal filter for git modified. However, it seems that as far as I can tell the Format only Modified Lines feature just piggybacks off of the already established API for format selection which I do not want to support.

Also, because I do not implement registerDocumentRangeFormattingEditProvider my extension is not activated on save when a user now sets Format on Save Mode to modified. This is causing confusion because no errors or information is shown to the user to help them understand why my extension is not functioning in this mode.

@trentbullard
Copy link

not sure if this is the right place for this, but thank you for this @kylegmaxwell it was a sad day when i had to disable formatOnSave

@github-actions github-actions bot locked and limited conversation to collaborators Oct 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality formatting Source formatter issues insiders-released Patch has been released in VS Code Insiders on-testplan
Projects
None yet
Development

Successfully merging a pull request may close this issue.

16 participants