• Stars
    star
    1,325
  • Rank 35,141 (Top 0.7 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Formatter.nvim

A format runner for Neovim.

splash

We want to thank the neoformat contributors. They developed a lot of formatter configurations that we used as a reference to create our own opt-in default formatter configurations.

Features

  • Written in Lua
  • Asynchronous execution
  • Buffer locking
  • Opt-in default formatter configurations
  • Conditional formatting
  • Before/after format hooks

Install

With packer.nvim:

require('packer').use { 'mhartington/formatter.nvim' }

With paq-nvim:

require("paq") { 'mhartington/formatter.nvim' }

With vim-plug:

Plug 'mhartington/formatter.nvim'

With Vundle.vim:

Plugin 'mhartington/formatter.nvim'

With vim-pathogen:

cd ~/.vim/bundle && \
git clone https://github.com/mhartington/formatter.nvim

With dein.vim:

call dein#add('mhartington/formatter.nvim')

Configure

Setup:

-- Utilities for creating configurations
local util = require "formatter.util"

-- Provides the Format, FormatWrite, FormatLock, and FormatWriteLock commands
require("formatter").setup {
  -- Enable or disable logging
  logging = true,
  -- Set the log level
  log_level = vim.log.levels.WARN,
  -- All formatter configurations are opt-in
  filetype = {
    -- Formatter configurations for filetype "lua" go here
    -- and will be executed in order
    lua = {
      -- "formatter.filetypes.lua" defines default configurations for the
      -- "lua" filetype
      require("formatter.filetypes.lua").stylua,

      -- You can also define your own configuration
      function()
        -- Supports conditional formatting
        if util.get_current_buffer_file_name() == "special.lua" then
          return nil
        end

        -- Full specification of configurations is down below and in Vim help
        -- files
        return {
          exe = "stylua",
          args = {
            "--search-parent-directories",
            "--stdin-filepath",
            util.escape_path(util.get_current_buffer_file_path()),
            "--",
            "-",
          },
          stdin = true,
        }
      end
    },

    -- Use the special "*" filetype for defining formatter configurations on
    -- any filetype
    ["*"] = {
      -- "formatter.filetypes.any" defines default configurations for any
      -- filetype
      require("formatter.filetypes.any").remove_trailing_whitespace
    }
  }
}

Opt-in formatters

By default, there are no preconfigured formatters. You can opt-into default configurations per formatter, default configurations per filetype, and default configurations for any filetype or write your own. It is hard to predict what everyone wants, but at the same time we realize that most formatter configurations are the same. See the discussion in #97 for more information.

You can use the default configurations per formatter, default configurations per filetype, and default configurations for any filetype as a starting point for creating your configurations. Feel free to contribute to this repository by creating or improving default configurations that everyone can use! The guide for contributing to default configurations is below.

You can use the util module which has various functions that help with creating default configurations as shown above.

Map keys

nnoremap <silent> <leader>f :Format<CR>
nnoremap <silent> <leader>F :FormatWrite<CR>

Format after save

augroup FormatAutogroup
  autocmd!
  autocmd BufWritePost * FormatWrite
augroup END

Before/after format hooks

You can execute code before and after formatting like so:

augroup FormatAutogroup
  autocmd!
  autocmd User FormatterPre lua print "This will print before formatting"
  autocmd User FormatterPost lua print "This will print after formatting"
augroup END

Note that these commands are executed using silent when the log_level is higher than vim.log.levels.DEBUG.

Buffer locking

Use the FormatLock and FormatWriteLock commands instead of Format and FormatWrite to lock the buffer (set buffer option modifiable to false) while formatting.

Configuration specification

Each formatter configuration is a function that returns a table. Because each entry is a function, the tables for each filetype act as an ordered list (or array). This means things run in the order you list them, keep this in mind. You can also return nil from these functions to conditionally apply formatters.

Each formatter configuration should return a table that consist of:

  • exe: the program you wish to run.
  • args: a table of arguments to pass (optional)
  • stdin: if it should use the standard input (optional)
  • cwd : the path to run the program from (optional)
  • try_node_modules: tries to run a formatter from locally install npm packages (optional) (to be implemented)
  • no_append : don't append the path of the file to the formatter command (optional)
  • ignore_exitcode : set to true if the program expects non-zero success exit code (optional)
  • transform : pass a function that takes in the formatted text and returns the text to be applied to the buffer (optional) (see ruby rubocop default formatter configuration as an example)
  • tempfile_dir: directory for temp file when not using stdin (optional)
  • tempfile_prefix: prefix for temp file when not using stdin (optional)
  • tempfile_postfix: postfix for temp file when not using stdin (optional)

cwd

The cwd argument can be used for in example monolithic projects which contain sources with different styles. Setting cwd to the path of the file being formatted causes, for example, clang-format to search for the nearest .clang-format file in the file's parent directories.

try_node_modules

The try_node_modules argument is not yet implemented, but feel free to use this argument in your configurations. When we add support for it, you get the node_modules package scanning functionality automatically!

no_append

The no_append argument is important for formatters that don't take the path to the formatted file as the last argument. A small minority of formatters take the path to the formatted file as a named argument. For an example, check the default javascript prettydiff configuration.

Contribute

Refer to the CONTRIBUTING.md file for more information.

More Repositories

1

oceanic-next

Oceanic Next theme for neovim
Vim Script
1,090
star
2

nvim-typescript

Typescript tooling for Neovim
TypeScript
616
star
3

dotfiles

Shell
556
star
4

oceanic-next-iterm

iterm port of https://github.com/voronianski/oceanic-next-theme/
201
star
5

Ionic-Chat

simple ionic chat example
JavaScript
88
star
6

star-track-ionic

TypeScript
88
star
7

StarTrack-ng

TypeScript
67
star
8

ion-md-input

A material design inspired text input for Ionic Framework | NO LONGER MAINTAINED
JavaScript
67
star
9

StarTrack-react

Star Track built with Ionic and React
TypeScript
46
star
10

Star-Track-vue

Vue
43
star
11

ionic-firebase-auth

Testing Firebase's various authentication methods
JavaScript
41
star
12

lazyLoad2-components

TypeScript
37
star
13

cordova-config-utils

JavaScript
32
star
14

ion-hide-tabs

CSS
27
star
15

lazyLoadTabs

TypeScript
27
star
16

vim-angular2-snippets

Snippets for Angular2 and Vim!
26
star
17

vim-typings

search for typings in vim!
Vim Script
25
star
18

vscode-oceanicnext

Oceanic Next for vscode
18
star
19

ion-ngrx

TypeScript
15
star
20

lazy-load-image

TypeScript
13
star
21

present-it

JavaScript
12
star
22

PinchToZoom

JavaScript
12
star
23

ionic-vue-vite

Vue
11
star
24

supabase-ionic-vue

Vue
10
star
25

ionic-v2-and-beyond

JavaScript
10
star
26

pwa-across-frameworks

TypeScript
9
star
27

oceanic-next-shell

256 color space for oceanic next
Shell
9
star
28

KeyboardTest

CSS
8
star
29

lang-bolt

CoffeeScript
8
star
30

quick-shell

Shell
7
star
31

cdk-virtual-scroll-ionic

TypeScript
7
star
32

ionic-firebase

JavaScript
6
star
33

fade-transition-demo

TypeScript
6
star
34

ionic-gestures

An example of using ionic's built-in gestures
JavaScript
6
star
35

ionic-vue-demo

Vue
6
star
36

builders-deep-dive

An example of various angular builders
TypeScript
6
star
37

v5-animations

TypeScript
6
star
38

vuex-todo

CSS
5
star
39

ionicv2-c3js

example of ionicvs and c3js
JavaScript
5
star
40

v2-convert

JavaScript
5
star
41

errorBar

JavaScript
5
star
42

ionic-video

JavaScript
5
star
43

ionicv2-Geolocation

JavaScript
4
star
44

ngspain-workshop

TypeScript
4
star
45

twitch-geo

TypeScript
4
star
46

V2Proxy

CSS
4
star
47

angular-14-test

TypeScript
4
star
48

frameworks-are-dead

HTML
4
star
49

ng-atl-conf

TypeScript
4
star
50

vimconf-live

JavaScript
4
star
51

Nested-Side-Menu

JavaScript
4
star
52

Ionic-Todo

A sample todo app with ionic
CSS
4
star
53

ionic-react-virtuoso

TypeScript
4
star
54

infiniteScrollTest

JavaScript
4
star
55

lazyLoadingBlank

Lazy Loading a blank sample app
CSS
4
star
56

supabase-ionic-react

TypeScript
4
star
57

supabase-ionic-angular

TypeScript
3
star
58

forum-react-css-issues

TypeScript
3
star
59

ionicV2PouchDB

JavaScript
3
star
60

react-notes

TypeScript
3
star
61

StarTrack-vue

Vue
3
star
62

oceanic-next-hyperterm

JavaScript
3
star
63

ionic-presents

JavaScript
3
star
64

ionic-snippets

Vim Snippet
3
star
65

ionic-io-demo

Examples of ionic.io's services, https://apps.ionic.io
JavaScript
3
star
66

ion4-popover-data

TypeScript
3
star
67

inline-video

JavaScript
3
star
68

ngx-honk

HTML
3
star
69

random-user

An example app with the random user API
TypeScript
3
star
70

ionic-friends-slides

TypeScript
3
star
71

Devnexus2016

TypeScript
3
star
72

v4-forms-test

TypeScript
3
star
73

bug-ion-router-outlet

TypeScript
3
star
74

svg-perf-test

JavaScript
3
star
75

cool-angular2-add-ons

A list of interesting and cool add-ons for angular 2 projects
3
star
76

angular-responsive-img

Angular directive to easily place responsive <img>'s using <picture> element
JavaScript
3
star
77

phillyete2016

Sample App for PhillyETE 2016
CSS
2
star
78

firebase-push-gen

JavaScript
2
star
79

quick-vim

quick and easy vim
Vim Script
2
star
80

defx-devicons

Python
2
star
81

zeit-pwa

TypeScript
2
star
82

angular-router-error

TypeScript
2
star
83

ionic4-custom-animation

TypeScript
2
star
84

echo-plex

TypeScript
2
star
85

Devnexus

JavaScript
2
star
86

Connect2017WorkShop

TypeScript
2
star
87

v4-routing-example

TypeScript
2
star
88

lazy-loaded-components-v4

TypeScript
2
star
89

denite-neomake

Denite source for neomake
Python
2
star
90

gulp-cordova-config

JavaScript
2
star
91

sentry-demo

TypeScript
2
star
92

capacitor-talk

JavaScript
2
star
93

ionic-realm-demo

TypeScript
2
star
94

ionic4-css-variable-example

TypeScript
2
star
95

vue-monorepo

CSS
2
star
96

ng-pwa-test

TypeScript
2
star
97

tkyt-demo

CSS
2
star
98

mhartington.github.io

Mike Hartingtons
HTML
2
star
99

stencil-inspector

A Chrome plugin that adds a new inspector pane to the Elements panel, providing Stencil-specific information about selected elements.
TypeScript
2
star
100

Angular-summit

JavaScript
1
star