Git Product home page Git Product logo

guess-indent.nvim's Introduction

:GuessIndent

MIT License Tests

Blazing fast indentation style detection for Neovim written in Lua. The goal of this plugin is to automatically detect the indentation style used in a buffer and updating the buffer options accordingly. This mimics the "Guess Indentation Settings From Buffer" function built into Sublime Text.

How it works

Whenever you open a new buffer, guess-indent looks at the first few hundred lines and uses them to determine how the buffer should be indented. It then automatically updates the buffer options so that they match the opened file.

Installation

Install using your favorite package manager and then call the following setup function somewhere in your config:

require('guess-indent').setup {}

If you are using packer.nvim, you can install and set up guess-indent simultaneously:

-- using packer.nvim
use {
  'nmac427/guess-indent.nvim',
  config = function() require('guess-indent').setup {} end,
}

Usage

By default, guess-indent automatically runs whenever you open a new buffer. You can also run it manually using the :GuessIndent command.

Configuration

The plugin provides the following configuration options:

-- This is the default configuration
require('guess-indent').setup {
  auto_cmd = true,  -- Set to false to disable automatic execution
  override_editorconfig = false, -- Set to true to override settings set by .editorconfig
  filetype_exclude = {  -- A list of filetypes for which the auto command gets disabled
    "netrw",
    "tutor",
  },
  buftype_exclude = {  -- A list of buffer types for which the auto command gets disabled
    "help",
    "nofile",
    "terminal",
    "prompt",
  },
}

Normally it should not be necessary to disable the automatic execution of guess-indent, because it usually takes less than a millisecond to run, even for large files.

Alternatives

Licence

This project is licensed under the terms of the MIT license. For more detail check out the LICENSE file.

guess-indent.nvim's People

Contributors

heygarrett avatar mehalter avatar nmac427 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

guess-indent.nvim's Issues

Google C++ style

Google C++ style place class member access specifier in 3 spaces indent, which make guess-indent set indent to 1 space. In practice this should be 4 spaces.
A simple example:

class Example {
   public:
    Example() {}
};

can we support this as a special case?

Feature Suggestion: also set `listchars` depending on tabs/spaces

I find it useful to only show tab via listchars when using spaces and vice versa, since that allows me to quickly identify the use of the wrong type of indention in a file.

Currently, I run a simple autocommand like this to adjust listchars depending whether the buffer uses tabs or spaces:

autocmd("BufReadPost", {
	callback = function()
		-- defered, to ensure it runs after `:GuessIndent`
		vim.defer_fn(function()
			opt_local.listchars = vim.opt_global.listchars:get() -- copy the global
			if bo.expandtab then
				opt_local.listchars:append { tab = "? " }
				opt_local.listchars:append { lead = " " }
			else
				opt_local.listchars:append { tab = "  " }
				opt_local.listchars:append { lead = "" }
			end
		end, 5)
	end,
})

I was thinking that adjusting listchars would actually make a lot of sense for in the context of this plugin (and would also save one from using hacky solutions like deferring that autocmd)

Turn off Notifications?

I noticed that the plugin seems to creates a prints a notification every time it sets the indentation.

While that may be useful for when you manually run :GuessIndent or when the indentation values have been changed, it is a bit annoying when the notification appears every time you enter a new buffer, even if the indentation was already set correctly?

Will not automatically start the plugin.

Hi,

 use({
  "nmac427/guess-indent.nvim",
  config = function()
    require("guess-indent").setup({})
  end,
})

Trying out this plugin with the config from the readme.

Sadly the autostart is not working. Even with auto_cmd=true.

I need to manually call GuessIndent to get it work. :(

Any idea? Do you need more info? Do i need to setup the plugin in an specific order?

How to use GuessIndent plugin and ftplugin files ?

Hello, thanks for your work on GuessIndent plugin : it's a great tool for my neovim usage.

I'm using ftplugin files for C, Go and Python to automatically configure my indent settings (filetype plugin indent on in configuration). For example, ftplugin/python.vim :

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal smarttab
setlocal expandtab

How to use your plugin to set automatically indentation with ftplugin files ? If your plugin detects an indentation different from my configuration, what configuration will be used ?
I want to use my default configuration for C, Go and Python when I work on my projects but I would like to use automatic indentation detected by GuessIndent when I work on external project (common usage : C project indented with TAB of 8, 4 spaces for indent in my personal config for C).

Silence :GuessIndent command output?

I disabled the automatic execution of guess-indent and started calling vim.api.nvim_command("GuessIndent") in one of my own autocmds so I can dynamically adjust listchars based on the file's indentation.

But now I'm getting messages about the indentation being set (eg, Did set indentation to tabs.) Is there a way to silence those messages?

non-issue: readme.md: add lazy and retab keymaps

Hi,
thank you so much for making this plugin!!

Below is my lua for lazy, you could add that to the readme.

I've also added the following keybindings:

<Tab>g to guess the indetation
<Tab>t to convert to tabs (normal mode)
<Tab>t to convert to tabs (visual line mode)
<Tab>s to convert to spaces (normal mode)
<Tab>s to convert to spaces (visual line mode)

While the last two (four) bindings don't have anything to do with your plugin, you may still choose to add them to the readme as they provide a great workflow enhancement when converting styles.

return {
	'nmac427/guess-indent.nvim',
	lazy = false,
	config = function()
		require('guess-indent').setup {}
		vim.keymap.set('n', '<Tab>g', vim.cmd.GuessIndent, { desc = "GuessIndent (manual)" })
		-- to tabs
		vim.keymap.set('n', '<Tab>t', '<Cmd>set noexpandtab<CR><Cmd>%retab!<CR><Cmd>set tabstop=8<CR>', { noremap = true, desc = "retab to tabs" })
		vim.keymap.set('v', '<Tab>t', "<Cmd>set noexpandtab<CR><Cmd>'<,'>retab!<CR><Cmd>set tabstop=8<CR>", { noremap = true, desc = "retab to tabs" })
		-- to spaces (for the average emacs enjoyer)
		vim.keymap.set('n', '<Tab>s', '<Cmd>set expandtab<CR><Cmd>set tabstop=8<CR><Cmd>%retab!<CR>', { noremap = true, desc = "retab to spaces" })
		vim.keymap.set('v', '<Tab>s', "<Cmd>set expandtab<CR><Cmd>set tabstop=8<CR><Cmd>'<,'>retab!<CR>", { noremap = true, desc = "retab to spaces" })
	end,
}

For beginners like me, these would be quite helpful.

User config is ignored if `auto_cmd` is set to false

The file types and buffer types to exclude are ignored when auto_cmd is set to false. However, I only disabled the autocmd so I could call guess-indent from my own autocmd, to allow for more control over when it's run relative to other indentation-related configuration.

Currently I'm manually calling require("guess-indent").set_from_buffer(true) from my own autocmd, but it would be great if there was a command, or perhaps an argument that could be passed to :GuessIndent, that would respect the user config regardless of what auto_cmd is set to.

Not detect tabs with size 4

Thanks for this plugin.

guess-indent not detect tabs with size 4.

Example using lua file.

2022-03-04_15-33_54.mp4

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.