purcell / emacs-reformatter

Define commands which run reformatters on the current Emacs buffer
266 stars 21 forks source link

Support formatters that output patch #20

Open lafrenierejm opened 4 years ago

lafrenierejm commented 4 years ago

ShellCheck is an example of a tool that doesn't support writing changes in-place, but does support diff-style output. Example of running shellcheck --format=diff against edx/devstack/course-generator/create-courses.sh:

--- a/create-courses.sh
+++ b/create-courses.sh
@@ -7,21 +7,21 @@
 echo "Parsing options"
 container_error=false
 for arg in "$@"; do
-    if [ $arg == "--studio" ]; then
+    if [ "$arg" == "--studio" ]; then
         if [ ! "$(docker-compose exec lms bash -c 'echo "Course will be created for studio"; exit $?')" ]; then
             echo "Issue with studio container"
             container_error=true
         else
             studio=true
         fi
-    elif [ $arg == "--ecommerce" ]; then
+    elif [ "$arg" == "--ecommerce" ]; then
         if [ ! "$(docker exec -t edx.devstack.ecommerce bash -c 'echo "Course will be created for ecommerce"; exit $?')" ]; then
             echo "Issue with ecommerce container"
             container_error=true
         else
             ecommerce=true
         fi
-    elif [ $arg == "--marketing" ]; then
+    elif [ "$arg" == "--marketing" ]; then
         if [ ! "$(docker exec -t edx.devstack.marketing bash -c 'echo "Course will be created for marketing"; exit $?')" ]; then
             echo "Issue with marketing container. Course creation will proceed without marketing container."
         else
purcell commented 4 years ago

Wait, I use ShellCheck all the time, and thought it was only a linter. And its README even says:

ShellCheck does not attempt to enforce any kind of formatting or indenting style, so also check out shfmt!

What am I missing? :-)

lafrenierejm commented 4 years ago

Wait, I use ShellCheck all the time, and thought it was only a linter. And its README even says:

ShellCheck does not attempt to enforce any kind of formatting or indenting style, so also check out shfmt!

What am I missing? :-)

It is true that its authors don't intend for ShellCheck to be used as a formatter. For some of the problems it can detect, though, ShellCheck is capable of recommending fixes. And one of the output formats ShellCheck supports is diff. That output would be sufficient to be consumed in an automated way, I I find that I typically do want to apply most or all of the fixes ShellCheck is able to recommend, and I I really like the ergonomics of defining formatters using this reformatter, so my first instinct was to check reformatter to see if it supported using diffs.

The feature could feasibly be useful beyond just ShellCheck, so I thought to start a conversation about including it. :) I've started working on a branch to add the feature; if you would rather hold off judgement on the value of the feature until you can demo, I'd be glad to let you know once I have an MVP.

purcell commented 4 years ago

The feature could feasibly be useful beyond just ShellCheck, so I thought to start a conversation about including it. :)

It's definitely something I'd consider.

lafrenierejm commented 4 years ago

@purcell I wasn't able to figure out how to get Emacs to apply a patch in an entirely automated fashion. All of my attempts resulted in Emacs starting an interactive diff session. Do you have suggestions for how to avoid that?

purcell commented 4 years ago

There's code floating around for programatically applying RCS-style diffs to the current buffer. That's ultimately what's needed, and ideally not just for RCS-style diffs.

purcell commented 4 years ago

ie. the whole point is to walk the diff and then make very minimal edits in the buffer according to what the diff says.

wyuenho commented 4 years ago

py-isort, py-autopep8 apply RCS patches too. Would be nice if this package could support it so I don't have to install dozens of packages to format code.