Format code from terminal when Android Studio is running

I’ve found about this possibility here. However, it required some tweaks to get it running. Continue reading to know more.

Official code formatter description.

Preamble

Having a consistent code style is important. Android Studio provides a convenient shortcut for re-formatting your code (Ctrl-Alt-L). It takes care of the code formatting including rearranging of your imports. The shortcut can be used when the file to be formatted is open in the Editor’s window of the IDE. Also, a bulk formatting can be done via the side “Android” pane. However, all that pointing and clicking is a no-go for a die-hard terminal users. Fortunately, IDEA allows for code formatting from the shell using format.sh script found in the home directory of the IDE. The problem is it can’t run, while your Android Studio is running.

The problem

format.sh can’t run at the same time with Android Studio.

Why do I need to format my code from the terminal?

If you are really concerned about this question, stop reading and go live your “normies” life.

Still here? Well, I hope you are having a good laugh, cause you already know the answer – you need FULL CONTROL over code formatting operation – specifically, to which files the command is applied to. It may happen, that there is a need to apply code style to only files that are changed by the current commit. This is when having a terminal-level access to code formatting is invaluable.

Usage

Call from the root (explained later in text) of your Android Studio project:

format-code.sh

It is a helper script that applies code formatting to the files marked as modified by git. It uses modified version of format.sh that expects a space separated list of files to format.

Setup

  1. Go to <IDE_BIN_HOME> directory (this is <where Android Studio is installed>/bin directory).
  2. cp idea.properties format.properties.
  3. In format.properties, uncomment following lines (they are usually the first two lines):idea.config.path=${user.home}/.AndroidStudio/config

    idea.system.path=${user.home}/.AndroidStudio/system

  4. After uncommenting the lines, insert format/ before the last bit like so:idea.config.path=${user.home}/.AndroidStudio3.1/format/config

    idea.system.path=${user.home}/.AndroidStudio3.1/format/system

  5. Alter the above paths, so they transit through the correct .AndroidStudio folder by adding the appropriate version to it. (like .AndroidStudio3.1). Just go to your HOME and see what you got in there to know the version.
  6. Add the following to format.sh:
    export STUDIO_PROPERTIES=$IDE_BIN_HOME/format.properties

    This effectively tricks the studio.sh to believe that it is running another instance of Android Studio, thus allowing format.sh to run along the already running Android Studio (that uses idea.properties). I’m not sure if this is a correct explanation, but at least this is what I tell myself 🙂

    At this point format.sh can be run along with the running Android Studio. The rest are the tweaks to make life easier.

  7. Change format.sh so that it reads from standard input a list of space separated files. This is done by
    filesToFormat=$(cat -)

    Options are also saved to a variable:

    options="$@"

    The last line is a slight modification of the original one:

    exec "$IDE_BIN_HOME/studio.sh" format $options $filesToFormat
  8. Helper script format-code.sh is a one-liner:
    git status | grep 'modified' | awk 'ORS=" " { print $2 }' | format.sh -s .idea/codeStyles/Project.xml
    • git status | grep 'modified' – grabs all modified files.
    • awk 'ORS=" " { print $2 }' – extracts only paths (strips down modified word and all the spaces – stuff that normal git status command outputs).
    • format.sh -s .idea/codeStyles/Project.xml – modified format.sh that consumes these paths. Important bit here is the usage of the -s option that points to the code style rules to be applied. This is a lame implementation, and that’s why it is important to call format-code.sh from the root of your project – so that relative path to the code style will be resolved.
  9. Make sure that format-code.sh is in your PATH.

Here is a modified format.sh:

Setup Markdown in Emacs

Install markdown processor:

sudo apt-get install markdown

Install markdown mode for Emacs

For a full story read this. However, if you want to make it quick:

  • Create ~/.emacs file and dump the following into it:
(require 'package)
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/"))
         (package-initialize)
  • Restart Emasc. When restarting, make sure not to use -Q option, cause that effectively ignores your .emacs file from being processed.
  • Install the package
M-x package-install RET markdown-mode RET

At this point, Markdown is installed.

Install preview util

Markdown preview

M-x package-install RET  markdown-preview-mode RET
  • markdown-preview-mode – start mode and open preview window.
  • markdown-preview-open-browser – open preview window for current buffer.
  • markdown-preview-cleanup – cleanup running processes (close websocket and http servers).

That is all. Create a .md file and experiment with the above commands.