Friday, April 10, 2015

Android Studio code formatter for blogging

Copy on steroids is a JetBrains plugin that can be used to copy code from Android Studio (or any other flavor of IntelliJ) and paste it into a blog post.

I found it much easier to use, and better much better highlighting for cutting and pasting Android code from Android Studio to a blog post. Example:

@Override
/**
 * Receives certain Bluetooth broadcast intents.
 */
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();            // Get intent's action string
    Bundle extras = intent.getExtras();            // Get all the Intent's extras
    if (extras == null) return;                    // All intents of interest have extras.

    switch (action) {
        case "android.bluetooth.adapter.action.STATE_CHANGED": {
            bluetoothStateChanged(extras.getInt("android.bluetooth.adapter.extra.STATE", FAIL));
            break;
        }
        case "android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED": {
            a2dpStateChanged(
                    extras.getInt("android.bluetooth.adapter.extra.CONNECTION_STATE", FAIL),
                    (BluetoothDevice) extras.get("android.bluetooth.device.extra.DEVICE"));
            break;
        }
        case "android.bluetooth.device.action.BOND_STATE_CHANGED": {
            bondStateChanged(
                    extras.getInt("android.bluetooth.device.extra.BOND_STATE", FAIL),
                    (BluetoothDevice) extras.get("android.bluetooth.device.extra.DEVICE"));
            break;
        }
    }
}

The default causes long lines to wrap. If you want a scrollbar to appear instead, as above, you have to pop into HTML and add "overflow-x: auto" to the first <div> of the pasted code, and add "display: inline-block" to the first <pre>.

There is source in Github; all it needs is an open source license, and adding a setting that lets you choose scrollbars over line wrapping would be very easy.

No comments:

Post a Comment