run-clang-format icon indicating copy to clipboard operation
run-clang-format copied to clipboard

Add in-place reformat

Open okainov opened this issue 6 years ago • 2 comments

Clang-format allows to do in-place reformatting with -i option. Would be useful to have the same in the script as well.

Example - using in git pre-commit hook to auto-format all the files. Probably it should still exit with non-zero exit code if at least one file was reformatted though.

okainov avatar Apr 01 '20 08:04 okainov

I would recommend git clang-format for this kind of things, it already supports that.

/tmp/wxWidgets$ git clang-format --diff
diff --git a/src/stc/stc.cpp b/src/stc/stc.cpp
index b936dd2f4d..561379d7d3 100644
--- a/src/stc/stc.cpp
+++ b/src/stc/stc.cpp
@@ -207,7 +207,7 @@ bool wxStyledTextCtrl::Create(wxWindow *parent,
 #ifdef LINK_LEXERS
     Scintilla_LinkLexers();
 #endif
-     m_swx = new ScintillaWX(this);
+    m_swx = new ScintillaWX(this);
     m_stopWatch.Start();
     m_lastKeyDownConsumed = false;
     m_vScrollBar = NULL;

/tmp/wxWidgets$ git clang-format
The following files would be modified but have unstaged changes:
M	src/stc/stc.cpp
Please commit, stage, or stash them first.
# command exited with status 2

/tmp/wxWidgets$ git add src/stc/stc.cpp

/tmp/wxWidgets$ git clang-format
changed files:
    src/stc/stc.cpp

/tmp/wxWidgets$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   src/stc/stc.cpp

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   src/stc/stc.cpp

Sarcasm avatar Apr 02 '20 08:04 Sarcasm

I agree -i could be nice to have. git clang-format is good for formatting single files or commits, but I want to reformat the whole source tree of my project in one go. I've simply added -i in the correct place in the code, this did the job, but could be generally useful.

matangover avatar Jun 10 '20 21:06 matangover

I believe this issue can be closed, as the -i flag has been added in:

  • https://github.com/Sarcasm/run-clang-format/pull/30

carlosperate avatar Nov 03 '22 14:11 carlosperate

Indeed, closing, thank you.

Sarcasm avatar Nov 03 '22 15:11 Sarcasm