Giter Site home page Giter Site logo

nofmt's Introduction

nofmt

A gofmt alternative that gets out of your way.

Nofmt attempts to achieve the following goals:

  • Correct indentation and spacing without introducing unnecessary newlines.
    For example, the following is a formatted result:

    results := []string{}
    for msg := range rx1chan { results = append(results, msg) }
    for msg := range rx2chan { results = append(results, msg) }
    for msg := range rx3chan { results = append(results, msg) }
    data, err := Process(results); if err != nil { return err }
    for !acknowledged { acknowledged = tx.Send(data) }
  • Code already formatted with gofmt should not be modified by nofmt.
    For example, the following code should be valid and remain unchanged:

    for !acknowledged {
        acknowledged = tx.Send(data)
    }
  • Idempotence. Formatting a file twice should produce the same results as formatting it once.

  • Full goimports functionality. Imports can be added and removed automatically.

Installation

Install go

#--- Installation ---
VER=1.23.1
cd $HOME
rm -rf $HOME/goroot
mkdir -p $HOME/goroot $HOME/go/bin
wget "https://dl.google.com/go/go${VER}.linux-amd64.tar.gz"
tar -xf "go${VER}.linux-amd64.tar.gz" -C $HOME/goroot --strip-components 1 go
hash -r

echo 'export PATH="$HOME/bin:$HOME/go/bin:$HOME/goroot/bin:$PATH"' >> $HOME/.bashrc

Log out and back in again to ensure the PATH is updated.

Build modified gofmt, goimports, and gopls:

cd $HOME/src
git clone https://github.com/yqdv/nofmt.git
git clone https://github.com/golang/tools.git

# Backup the go/printer/nodes.go file to go/printer/nodes.go.orig
md5sum $HOME/goroot/src/go/printer/nodes.go
cp $HOME/goroot/src/go/printer/{nodes.go,nodes.go.orig}

# Patch go/printer/nodes.go to update linebreak()
cd $HOME/src/nofmt
patch $HOME/goroot/src/go/printer/nodes.go < inject/go/printer/nodes.go.patch

# Build "gofmt", "goimports", and "gopls" using the modified source
cd $HOME/goroot/src/cmd/gofmt
go build
cp gofmt $HOME/go/bin/gofmt-nofmt

# Build "goimports" using the modified source
cd $HOME/src/tools/cmd/goimports
go build
mv goimports $HOME/go/bin/goimports-nofmt

# Build "gopls" using the modified source
cd $HOME/src/tools/gopls
go build
mv gopls $HOME/go/bin/gopls-nofmt

# Backup existing binaries and create links to the new ones
if [[ -f "$HOME/go/bin/gofmt"         && ! -L "$HOME/go/bin/gofmt"         ]]; then cp $HOME/go/bin/{gofmt,gofmt.orig};             fi
if [[ -f "$HOME/go/bin/goimports"     && ! -L "$HOME/go/bin/goimports"     ]]; then cp $HOME/go/bin/{goimports,goimports.orig};     fi
if [[ -f "$HOME/go/bin/gopls"         && ! -L "$HOME/go/bin/gopls"         ]]; then cp $HOME/go/bin/{gopls,gopls.orig};             fi
if [[ -f "$HOME/goroot/bin/gofmt"     && ! -L "$HOME/goroot/bin/gofmt"     ]]; then cp $HOME/goroot/bin/{gofmt,gofmt.orig};         fi
if [[ -f "$HOME/goroot/bin/goimports" && ! -L "$HOME/goroot/bin/goimports" ]]; then cp $HOME/goroot/bin/{goimports,goimports.orig}; fi
if [[ -f "$HOME/goroot/bin/gopls"     && ! -L "$HOME/goroot/bin/gopls"     ]]; then cp $HOME/goroot/bin/{gopls,gopls.orig};         fi

ln -sf $HOME/go/bin/gofmt-nofmt     $HOME/go/bin/gofmt
ln -sf $HOME/go/bin/goimports-nofmt $HOME/go/bin/goimports
ln -sf $HOME/go/bin/gopls-nofmt     $HOME/go/bin/gopls
ln -sf $HOME/go/bin/gofmt-nofmt     $HOME/goroot/bin/gofmt
ln -sf $HOME/go/bin/goimports-nofmt $HOME/goroot/bin/goimports
ln -sf $HOME/go/bin/gopls-nofmt     $HOME/goroot/bin/gopls

hash -r

# Revert the source file patch
cp $HOME/goroot/src/go/printer/{nodes.go.orig,nodes.go}

nofmt's People

Contributors

yqdv avatar

Watchers

 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.