Giter Site home page Giter Site logo

cformat's Introduction

cformat

Purpose

cformat is a program for conveniently formatting changes made to C or C++ files using clang-format. Contrary to most formatters, the program allows for incremental reformatting of files. That is, when lines in a file are changed they and only they will be reformatted (optionally with some context), while the remainder of the file will be preserved as-is. Such behavior can be tremendously useful in incrementally migrating over a code base to being formatted in an automated fashion.

Given that the program is merely a wrapper around clang-format, it automatically picks up the corresponding configuration.

Usage

cformat interprets a patch in unified diff format and infers files and changed lines based on it. This patch is read from standard input.

While not at all tied to git, we use it here for illustration purposes (and because it is likely the context in which the program will be used). To simplify usage, we introduce a git alias (git cfp -- "c format patch") that retrieves any currently staged changes and pipes them to the program. An alias for annotating the currently staged changes could look like this:

[alias]
  cfp = "!cfp() { git diff --no-prefix --relative --staged --unified=0 \"$@\" | cformat; }; cfp"

Once you have staged changes, you can run git cfp and affected lines will get reformatted according to clang-format's rules.

Three aspects of the above definition are of particular relevance here:

  • --no-prefix is important to make sure that paths contained in emitted diffs do not contain any a/ prefixes but map right to files in the file system
  • --relative is necessary because without it git emits file names relative to the repository root, whereas cformat may be invoked from a sub-directory, in which case paths would not necessarily map to files
  • --unified=0 ensures that only lines that were actually touched are reformatted, as opposed to lines contained in the context that is used for fuzzy matching; increase the context to format additional lines surrounding changes

Installation

cformat is written in Rust and requires the Cargo package manager to be built. It can be installed using cargo install cformat. It relies on clang-format being installed and runnable.

cformat's People

Contributors

d-e-s-o avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.