sponsor Vim development Vim logo Vim Book Ad

IndentConsistencyCop : Is the buffer's indentation consistent and does it conform to tab settings?

 script karma  Rating 38/15, Downloaded by 2092    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
 
script type
utility
 
description
DESCRIPTION
In order to achieve consistent indentation, you need to agree on the
indentation width (e.g. 2, 4 or 8 spaces), and the indentation method (only
tabs, only spaces, or a mix of tabs and spaces that minimizes the number of
spaces and is called 'softtabstop' in Vim). Unfortunately, different people
use different editors and cannot agree on "the right" width and method.
Consistency is important, though, to make the text look the same in different
editors and on printouts. If any editor inadvertently converts tabs and
spaces, version control and diff'ing will be much harder to do.

The IndentConsistencyCop examines the indent of the buffer and analyzes the
used indent widths and methods. If there are conflicting ones or if bad
combinations of tabs and spaces are found, it alerts you and offers help in
locating the offenders - just like a friendly policeman:

    :IndentConsistencyCop
   Found inconsistent indentation in this buffer; generated from these
    conflicting settings:
    - tabstop (1838 of 3711 lines) <- buffer setting
    - 4 spaces (33 of 3711 lines)
    - bad mix of spaces and tabs (4 of 3711 lines)
        [I]gnore, (H)ighlight wrong indents...: h
    What kind of inconsistent indents do you want to highlight?
        Not [b]uffer settings (sts4), Not best (g)uess (tab), Not (c)hosen
        setting..., (I)llegal indents only: g
    Marked 180 incorrect lines.

If the buffer contents are okay, the IndentConsistencyCop can evaluate whether
Vim's buffer settings are compatible with the indent used in the buffer. The
friendly cop offers to correct your buffer settings if you run the risk of
screwing up the indent consistency with your wrong buffer settings:

    :IndentConsistencyCop
   The buffer's indent settings are inconsistent with the used indent '8
    spaces'; these settings must be changed:
    - expandtab from 0 to 1
    How do you want to deal with the inconsistency?
        [I]gnore, (C)hange: c
    The buffer settings have been changed: tabstop=8 softtabstop=0 shiftwidth=8
    expandtab

The IndentConsistencyCop is only concerned with the amount of whitespace from
column 1 to the first visible character; it does not check the alignment of
tables, equals signs in variable assignments, etc. Neither does it know any
specifics about programming languages, or your personal preferred indentation
style.

RELATED WORKS
- IndentConsistencyCopAutoCmds (vimscript #1691) complements this plugin. It
  automatically triggers the IndentConsistencyCop for certain filetypes when
  loading the buffer and optionally also on each write.
- Indent Finder (vimscript #513) is a Python script and Vim plugin that scans
  any loaded buffer and configures the appropriate indent settings.
- yaifa.vim (vimscript #3096) is a port to vimscript of the above.
- detectindent.vim (https://github.com/ciaranm/detectindent) by Ciaran
  McCreesh tries to auto-detect the indentation settings.
- GuessIndent (vimscript #4251) is based on detectindent.vim
- indentdetect.vim
  (https://github.com/ervandew/vimfiles/blob/master/vim/plugin/indentdetect.vim)
  by Eric Van Dewoestine performs a simple detection and can set defaults
  based on the filespec.
- matchindent.vim (vimscript #4066) detects tabs, 2 and 4-space indents and
  adapts the indent settings accordingly.
- sleuth.vim (vimscript #4375) by Tim Pope automatically adjusts 'shiftwidth'
  and 'tabstop' heuristically (via a simplistic sampling that does not check
  for bad or inconsistent indents) or by looking at other files of the same
  type.

USAGE
Start the examination of the current buffer or range via:
    :[range]IndentConsistencyCop
The triggering can be done automatically for configurable filetypes with the
autocmds defined in IndentConsistencyCopAutoCmds.vim (vimscript #1691).

If you chose to highlight incorrect indents, either re-execute the
IndentConsistencyCop to update the highlighting, or execute
    :IndentConsistencyCopOff
to remove the highlightings.

If you just want to check a read-only file, or do not intend to modify the
file, you don't care if Vim's buffer settings are compatible with the used
indent. In this case, you can use
    :[range]IndentRangeConsistencyCop
instead of :IndentConsistencyCop.
 
install details
INSTALLATION
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim IndentConsistencyCop*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

DEPENDENCIES
- Requires Vim 7.0 or higher.

CONFIGURATION
For a permanent configuration, put the following commands into your vimrc:

You can select method(s) of highlighting incorrect lines via
g:indentconsistencycop_highlighting; the default fills the search pattern,
jumps to the first error, sets 'list', uses the 'Error' highlighting and folds
away the correct lines.
The variable defines the highlighting methods of incorrect lines, when this is
requested by the user. Multiple methods can be combined by concatenating the
values. The changes done for highlighting are undone when highlighting is
removed via :IndentConsistencyCopOff.
    s - Fill search pattern with all incorrect lines, so that you navigate
        through all incorrect lines with n/N.
    g - Jump to the first error.
    l - As a visualization aid, execute ':setlocal list' to see difference
        between tabs and spaces.
    m - Use error highlighting to highlight the wrong indent via the
        'IndentConsistencyCop' highlight group. This is especially useful if
        you don't use the search pattern in combination with 'set hlsearch'
        to locate the incorrect lines.
        You can customize this by defining / linking the
        'IndentConsistencyCop' highlight group before this script is
        sourced:
        highlight link IndentConsistencyCop Error
    f:{n} (n = 0..9) - Fold correct lines with a context of {n} lines (like
        in Vim diff mode).
    TODO:
    q - Populate quickfix list with all incorrect lines. Idea: Use :cgetexpr.

    let g:indentconsistencycop_highlighting = 'sglmf:3'

Some comment styles use additional whitespace characters inside the comment
block to neatly left-align the comment block, e.g. this is often used in Java
and C/C++ programs:
    /* This is a comment that spans multiple
     * lines; neatly left-aligned with asterisks.
     */
The IndentConsistencyCop would be confused by these special indents, so the
non-indent pattern defined in g:indentconsistencycop_non_indent_pattern
removes these additional whitespaces from the indent when evaluating lines.
    let g:indentconsistencycop_non_indent_pattern = ' \*\%([*/ \t]\|$\)'
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
IndentConsistencyCop-1.44.vmb.gz 1.44 2014-01-11 7.0 Ingo Karkat - BUG: The version 1.43 workaround for the Vim 7.4 new regexp engine was ineffective, because the \%#=1 atom needs to be prepended to the entire regular expression, but that's not possible with the configuration value alone. (Also, the workaround mistakenly specified auto-select (0) instead of old engine (1).) Move the workaround to s:GetBeginningWhitespace() instead.
- ENH: Close all consistent parts of the buffer when highlighting the inconsistencies via folding, and restore the original 'foldlevel' setting (and therefore the global fold state set by zM / zR) on :IndentConsistencyCopOff. Thanks to Marcelo Montú for the idea.
- ENH: Enable folding to highlight the inconsistencies when it was previously :set nofoldenable'd.
IndentConsistencyCop-1.43.vmb.gz 1.43 2013-12-19 7.0 Ingo Karkat - Minor: Make matchstr() robust against 'ignorecase'.
- Improve g:indentconsistencycop_non_indent_pattern to also handle empty comment lines with a sole ' *' prefix. Thanks to Marcelo Montu for reporting this.
IndentConsistencyCop-1.42.vmb.gz 1.42 2012-12-11 7.0 Ingo Karkat When a perfect or authoritative rating didn't pass the majority rule, try to turn around the verdict by checking consistency with buffer settings, as is done for small indents only. For example, this avoids a wrong verdict of inconsistent spc8 when there are more spc8 than spc4. Thanks to Marcelo Montú for reporting this issue.
IndentConsistencyCop-1.41.vmb.gz 1.41 2012-12-07 7.0 Ingo Karkat Change the behavior of :IndentRangeConsistencyCop to consider the buffer settings to turn around the verdict of "inconsistent indent" (but still not report inconsistent buffer settings alone). Otherwise, together with the IndentConsistencyCopAutoCmds triggers, it can happen that on opening (i.e. :IndentConsistencyCop), the file is judged okay (considering the buffer settings), but on writing the buffer (:IndentRangeConsistencyCop), a potential inconsistency due to too small indent is reported. Thanks to Marcelo Montú for reporting this issue.
IndentConsistencyCop-1.40.vmb.gz 1.40 2012-10-11 7.0 Ingo Karkat - The cop can often do a solid assessment when the maximum indent is 8. Only when there are no smaller indents, a higher indent is needed to unequivocally recognize soft tabstops.
- ENH: Better handle integer overflow when rating and normalizing: Limit to MAX_INT instead of carrying on with negative ratings, or just use Float values when Vim has support for it.
- When we have only a few, widely indented lines, there may be more than one way to interpret them as a perfect setting. Choose one over the other via some simple heuristics instead of the previous assertion error.
- FIX: Fall back to the old :2match when matchadd() is not available.
IndentConsistencyCop.vba.gz 1.31 2012-04-03 7.0 Ingo Karkat - Use matchadd() instead of :2match to avoid clashes with user highlightings (or other plugins like html_matchtag.vim).
- ENH: Clear highlighting when another buffer is loaded into the window to avoid that the highlightings persist in a now wrong context.
IndentConsistencyCop.vba.gz 1.30 2011-11-23 7.0 Ingo Karkat ENH: Avoid the spurious "potential inconsistency with buffer settings" warning when there are only small consistent indents detected as space-indents, but the equivalent softtabstop-indent is consistent with the buffer settings. As many files only have small indents, this warning popped up regularly and has been the most annoying for me, also because to rectify it, one has to answer three questions: "[W]rong", "[s]ofttabstop", [N]).
IndentConsistencyCop.vba.gz 1.21 2010-12-31 7.0 Ingo Karkat - Added b:indentconsistencycop_result.isIgnore to allow the IndentConsistencyCopAutoCmds integration to suspend further invocations of the cop in the buffer.
- BUG: :IndentRangeConsistencyCop didn't report inconsistencies at all because of a bad conditional statement introduced in 1.20.014.
- Using separate autoload script to help speed up Vim startup.
- Added separate help file and packaging the plugin as a vimball.
IndentConsistencyCop.vim 1.20.017 2008-07-22 7.0 Ingo Karkat BF: Undefined variable l:isEntireBuffer in IndentBufferConsistencyCop().  
IndentConsistencyCop.vim 1.20.016 2008-07-21 7.0 Ingo Karkat ENH: Added b:indentconsistencycop_result buffer-scoped dictionary containing the results of the check, which can be used by other integrations.
Also check consistency of buffer settings if the buffer/range does not contain indented text. Inconsistent indent settings can then be corrected with a queried setting.
BF: Clear previous highlighting if buffer/range now does not contain indented text.
IndentConsistencyCop.vim 1.10.012 2008-06-23 7.0 Ingo Karkat Minor change: Added -bar to all commands that do not take any arguments, so that these can be chained together.
IndentConsistencyCop.vim 1.10.011 2008-02-28 7.0 Ingo Karkat Improved the algorithm so that 'softtabstop' is recognized even when a file only has small indents with either (up to 7) spaces or tabs, but no tab + space combination.
IndentConsistencyCop.vim 1.00.010 2007-11-08 7.0 Ingo Karkat BF: In an inconsistent and large buffer/range that has only one or a few small inconsistencies and one dominant (i.e. 99%) setting, the text "Some minor / inconclusive potential settings have been omitted." is not printed.
ENH: Print "noexpandtab/expandtab" instead of " expandtab to 0/1", as the user would :setlocal the setting.
IndentConsistencyCop.vim 1.00.009 2007-06-04 7.0 Ingo Karkat ENH: Improved detection accuracy for soft tabstops when the maximum indent is too small for a solid assessment. When the maximum indent of the buffer is not enough to be sure of the indent settings (i.e. differentiating between soft tabstops and spaces), an inconsistent indent was reported, even though it is much more likely that the indent is consistent with "soft tabstop n", but that wasn't recognized because of the small indents used in the file. If allowed, the cop now examines the buffer settings to possibly turn around the verdict of "inconsistent indent".
IndentConsistencyCop.vim 1.00.008 2007-04-02 7.0 Ingo Karkat Allowing user to override wrongly found consistent setting (e.g. 'sts1' instead of 'tab') by choosing 'Wrong, choose correct setting...' in the IndentBufferConsistencyCop.
IndentConsistencyCop.vim 0.07 2006-11-02 7.0 Ingo Karkat Corrected unreasonable assumption of a consistent small indent setting (of 1 or 2 spaces) when actually only some wrong spaces spoil the consistency. Now, a perfect consistent rating is only accepted if its absolute rating number is also the maximum rating.
BF: Avoiding runtime error in IndentBufferInconsistencyCop() if s:ratings is empty.
BF: Suppressing 'Not buffer setting' option if the buffer setting is inconsistent ('badset'), which threw an exception when selected.
IndentConsistencyCop.vim 0.05 2006-10-30 7.0 Ingo Karkat Improved g:indentconsistencycop_non_indent_pattern to also allow ' *\t' and ' *****' comments.
IndentConsistencyCop.vim 0.04 2006-10-24 7.0 Ingo Karkat Initial upload
ip used for rating: 50.16.130.188

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo