• Stars
    star
    262
  • Rank 151,065 (Top 4 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

Prettier plugin for Neovim's built-in LSP client.

prettier.nvim

Prettier plugin for Neovim's built-in LSP client.

Requirements

Installation

Install the plugins with your preferred plugin manager. For example:

With vim-plug

Plug 'neovim/nvim-lspconfig'
Plug 'jose-elias-alvarez/null-ls.nvim'
Plug 'MunifTanjim/prettier.nvim'

With packer.nvim

use('neovim/nvim-lspconfig')
use('jose-elias-alvarez/null-ls.nvim')
use('MunifTanjim/prettier.nvim')

Setup

Warning

Prettier won't be automatically installed by this plugin. You need to have it installed either globally or locally for the project.

Setting up null-ls

For Latest Neovim:

local null_ls = require("null-ls")

local group = vim.api.nvim_create_augroup("lsp_format_on_save", { clear = false })
local event = "BufWritePre" -- or "BufWritePost"
local async = event == "BufWritePost"

null_ls.setup({
  on_attach = function(client, bufnr)
    if client.supports_method("textDocument/formatting") then
      vim.keymap.set("n", "<Leader>f", function()
        vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
      end, { buffer = bufnr, desc = "[lsp] format" })

      -- format on save
      vim.api.nvim_clear_autocmds({ buffer = bufnr, group = group })
      vim.api.nvim_create_autocmd(event, {
        buffer = bufnr,
        group = group,
        callback = function()
          vim.lsp.buf.format({ bufnr = bufnr, async = async })
        end,
        desc = "[lsp] format on save",
      })
    end

    if client.supports_method("textDocument/rangeFormatting") then
      vim.keymap.set("x", "<Leader>f", function()
        vim.lsp.buf.format({ bufnr = vim.api.nvim_get_current_buf() })
      end, { buffer = bufnr, desc = "[lsp] format" })
    end
  end,
})
For Older Neovim:
local null_ls = require("null-ls")

null_ls.setup({
  on_attach = function(client, bufnr)
    if client.resolved_capabilities.document_formatting then
      vim.cmd("nnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.formatting()<CR>")

      -- format on save
      vim.cmd("autocmd BufWritePost <buffer> lua vim.lsp.buf.formatting()")
    end

    if client.resolved_capabilities.document_range_formatting then
      vim.cmd("xnoremap <silent><buffer> <Leader>f :lua vim.lsp.buf.range_formatting({})<CR>")
    end
  end,
})

Setting Up prettier.nvim

prettier.nvim needs to be initialized with the require("prettier").setup() function. All the settings are optional.

local prettier = require("prettier")

prettier.setup({
  bin = 'prettier', -- or `'prettierd'` (v0.23.3+)
  filetypes = {
    "css",
    "graphql",
    "html",
    "javascript",
    "javascriptreact",
    "json",
    "less",
    "markdown",
    "scss",
    "typescript",
    "typescriptreact",
    "yaml",
  },
})

Note

It's highly recommended that you use this plugin with prettierd.

You can also supply some options to null-ls:

prettier.setup({
  ["null-ls"] = {
    condition = function()
      return prettier.config_exists({
        -- if `false`, skips checking `package.json` for `"prettier"` key
        check_package_json = true,
      })
    end,
    runtime_condition = function(params)
      -- return false to skip running prettier
      return true
    end,
    timeout = 5000,
  }
})

You can set Prettier's options too. They are passed to the prettier CLI.

prettier.setup({
  cli_options = {
    arrow_parens = "always",
    bracket_spacing = true,
    bracket_same_line = false,
    embedded_language_formatting = "auto",
    end_of_line = "lf",
    html_whitespace_sensitivity = "css",
    -- jsx_bracket_same_line = false,
    jsx_single_quote = false,
    print_width = 80,
    prose_wrap = "preserve",
    quote_props = "as-needed",
    semi = true,
    single_attribute_per_line = false,
    single_quote = false,
    tab_width = 2,
    trailing_comma = "es5",
    use_tabs = false,
    vue_indent_script_and_style = false,
  },
})

By default these options are only used if prettier config file is not found. If you want to change that behavior, you can use the following option:

prettier.setup({
  cli_options = {
    -- https://prettier.io/docs/en/cli.html#--config-precedence
    config_precedence = "prefer-file", -- or "cli-override" or "file-override"
  },
})

Note:

  • You can only use prettier.nvim with vim.lsp.* methods if prettier config file is present in your project directory.
  • Supported version of prettierd is 0.23.3+.

Setup without LSP

If you don't want to do LSP setup, and just use Prettier:

Keybindings

" formatting in normal mode
nmap <Leader>f <Plug>(prettier-format)

" range_formatting in visual mode
xmap <Leader>f <Plug>(prettier-format)

Commands

:Prettier command will format the current buffer.

License

Licensed under the MIT License. Check the LICENSE file for details.

More Repositories

1

nui.nvim

UI Component Library for Neovim.
Lua
1,382
star
2

minimo

Minimo - Minimalist theme for Hugo
HTML
531
star
3

nougat.nvim

๐Ÿซ Hyperextensible Statusline / Tabline / Winbar for Neovim ๐Ÿš€
Lua
185
star
4

tmux-suspend

Plugin that lets you suspend local tmux session, so that you can work with nested remote tmux session painlessly.
Shell
114
star
5

tmux-mode-indicator

Plugin that displays prompt indicating currently active Tmux mode.
Shell
108
star
6

node-bitbucket

Bitbucket API client for Browser and Node.js
JavaScript
95
star
7

exrc.nvim

Secure Project Local Config for Neovim
Lua
56
star
8

tree-sitter-lua

Lua grammar for tree-sitter.
C
40
star
9

eslint.nvim

ESLint plugin for Neovim's built-in LSP client.
Lua
35
star
10

gatsby-theme-dox

Documentation made easy with Gatsby. ๐ŸŽ‰
JavaScript
31
star
11

scripts.sh

Handy Shell Scripts
Shell
21
star
12

luver

Version manager for Lua, built with โค๏ธ
Shell
21
star
13

zed

ZSH Plugin Manager
Shell
20
star
14

node-bkash

bKash API client for Browser & Node.js
JavaScript
17
star
15

nvim-treesitter-lua

Tree-sitter Lua parser integration for nvim-treesitter.
Scheme
12
star
16

oclif-plugin-completion

oclif plugin for generating shell completions
TypeScript
10
star
17

stapsher

API service for handling user-generated contents on static sites
JavaScript
9
star
18

gmail-oauth2-script

Script to get OAuth2 Access Token for Gmail.
Shell
6
star
19

express-zod-openapi

Express + Zod + OpenAPI
TypeScript
6
star
20

express-joi-openapi

Express + Joi + OpenAPI
TypeScript
6
star
21

regio

University Department Information Management Software
JavaScript
4
star
22

anontius

Personal Asking Portal (like ASK.fm)
JavaScript
4
star
23

draft-js-modules

Draft.js Modules
TypeScript
4
star
24

octoherd-script-set-pr-merge-config

An octoherd script to set pull request merge config
JavaScript
4
star
25

setup-neovim-action

๐Ÿš€ Setup Neovim on Github Actions.
Shell
4
star
26

luver-action

๐ŸŒ› Set up your GitHub Actions workflow with specific versions of Lua, LuaJIT, LuaRocks using Luver โค๏ธ
Shell
3
star
27

llhttp.lua

Lua interface for https://github.com/nodejs/llhttp
C
3
star
28

is-mobile-phone-number-bd

Mobile phone number validator for Bangladesh
TypeScript
3
star
29

lua-evdev

LuaJIT FFI Bindings for libevdev.
Lua
3
star
30

rofunicode

Unicode Character Picker for Rofi
JavaScript
2
star
31

gist-embedder

Library for Embedding GitHub Gist
JavaScript
2
star
32

lua-udev

LuaJIT FFI Bindings for libudev.
Lua
2
star
33

kee

๐Ÿ–ฑ โŒจ Do amazing things with Keys...
Shell
1
star
34

middleware-pipeline

Simple Middleware Pipeline
TypeScript
1
star
35

js-set-time

Sets time to Date instance.
TypeScript
1
star
36

hashicorp-vault

HashiCorp Vault API client for Node.js
JavaScript
1
star
37

unoti

Unified Notification
TypeScript
1
star
38

npm-package-template

NPM Package Template
JavaScript
1
star
39

luarocks-publish-action

๐ŸŒ› Publish to LuaRocks using GitHub Actions
JavaScript
1
star
40

Exercism

๐Ÿ‘จโ€๐Ÿ’ป Exercism Solutions
Rust
1
star
41

freeCodeCamp-FrontEnd-Projects

freeCodeCamp Front End Development Projects
JavaScript
1
star
42

x-git-hooks

Git Hooks Handler
Rust
1
star
43

bitbucket-api-routes

Machine-readable, Bitbucket API routes specifications
JavaScript
1
star
44

freeCodeCamp-React-Projects

(freeCodeCamp) React Projects
JavaScript
1
star