📜 ⬆️ ⬇️

Vim FileStyle: check open file for compliance with Vim settings

Why do you need it


When team development is important to comply with a specific coding standard. It is not always about naming variables, functions, etc. The above is largely a matter of experience and the ability to formulate thoughts in the text. Coding standard often begins with the definition of the following things:


Non-compliance with the above style parameters by different people within the same file leads to the following problems:


Often, such problems arise because the new person entering the project did not set up the editor correctly, and this goes unnoticed until the moment it becomes necessary to compare different versions of the same file.
')


In order to immediately see such problems when opening a file, I wrote a small plugin.

Why plugin


There were several reasons for this:

  1. I have been using Vim for editing code for almost 2 years on an ongoing basis, and I have long wanted to write some plugin
  2. Using standard settings like listchars does not highlight when spaces are used for indentation with the noexpandtab option noexpandtab
  3. Set depending on the settings in ftplugin

Functional



Plug inside


The plugin is based on the matchadd() function. This function searches the open buffer for a match with the specified pattern and highlights it in accordance with the specified backlight circuit.

For example, we want to highlight all spaces in the buffer with yellow:

 :highligh Spaces ctermbg=Yellow guibg=Yellow :call matchadd('Spaces', '\s\+') 

The overall backlight function is as follows:

 function FileStyleHighlightPattern(highlight) call matchadd(a:highlight['highlight'], a:highlight['pattern']) endfunction 

The dictionary with the name of the backlight circuit and the pattern for comparison is accepted as input.

In order for the plugin to automatically start checking patterns, it was necessary to add automatic commands when initializing the plugin:

 augroup filestyle_auto_commands autocmd! autocmd BufReadPost,BufNewFile * call FileStyleActivate() autocmd FileType * call FileStyleCheckFiletype() autocmd WinEnter * call FileStyleCheck() augroup end 

It is necessary to stipulate separately what the FileType event handler is for. It had to be added to ensure that the plugin does not work on help files, since their contents can be arbitrary and in no way be consistent with the current settings, and also cannot be edited from the help window.

The WinEnter handler WinEnter needed so that when dividing a window ( :split ) in an open window, there is also a backlight.

This is the final result:



Download can be on the links: vim.org | Github

UPD: Fixed error undefined variable filestyle_active . Changes are uploaded to GitHub and Vim.org

Source: https://habr.com/ru/post/245691/


All Articles