-
Notifications
You must be signed in to change notification settings - Fork 71
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
Partial code formatting #191
Comments
It's a bit unclear to me how this should work (and from reading that Python script how it does work). A straightforward way to do this would be to generate a zero-context diff (that appears to be what they do here) and apply that to the formatted version of the original and then if that applies cleanly also format the modified file and then generate a zero-context diff between the two formatted versions and apply that diff to the original file. In commutative diagram form:
Here A is the original version of the file, B is the new version and A' and B' are the formatted versions of A and B. We want to apply the diff from A' to B' to A to get a new version of B that is partially formatted but only the code that the diff touches. But what if the diff from A' to B' doesn't apply to A cleanly? How would one handle that? |
IIUC there is no magic in
--- https://clang.llvm.org/docs/ClangFormat.html So, I guess |
I suppose this could be done in the printing phase by toggling formatting. If formatting is off the original content for those lines is printed. |
Doesn't that require that the formatter not split or combine lines? |
A potentially simpler option would be to output the results of the check by file, and compare that against the list of files modified in the PR. |
FYI, I just found https://github.com/reviewdog/action-suggester which can be used to suggest formatting for the parts of the code that the PR touches. See JuliaPreludes/Try.jl#21 for an example setup. See JuliaPreludes/Try.jl#22 for how it looks like. I've seen some edge cases for detecting what the PR touches, though (example https://github.com/tkf/DisplayAs.jl/pull/23/files#r826040308) |
AFAIK the way
But that only helps if those modified files are otherwise fully formatted, right? That doesn't sound that useful to me... |
I wonder if the code used for the Things get more complicated once there are real "skip formatting" comments, though... I don't know if current machinery dealing with |
Has there been any update on this issue yet (it doesn't look like it)? The ability to apply partial formatting would imho immediately increase the usefulness of JuliaFormatter for an automatic CI check. Why? For a large code base, it is usually unfeasible to apply formatting to the entire source: It destroys convenient However, it would awesome to be able to apply formatting to git diffs only. This allows one to ensure in a CI pipeline that a proposed PR will not change after formatting the diff, i.e., forcing people to propose only PR code that has already been formatted. Especially in packages with a non-negligible number of drive-by PRs, this could considerably reduce the burden on the maintainers. |
Any updates on this? It seems to me that this features already exists, since VS Code has both “Format Selection” and “Format Modified Lines” features, but I haven't managed to find out how they do it. |
Not natively supported yet, not sure how vscode does it. Probably something similar to where they ignore the formatting for all the lines but the selected ones. |
Unfortunately, it seems like that is a native VS Code feature. |
I added line-range formatting for Julia in the |
It is very convenient to apply formatting changes to only partial files. As an example
clang-format
can integrated with git into https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/clang-format-diff.py the actual git integration is here https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/git-clang-formatcc: @simonbyrne
The text was updated successfully, but these errors were encountered: