From aad1b1ca7f3496409f48b67bb56694800029b9b0 Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Mon, 31 Jul 2017 18:22:56 +0200 Subject: [PATCH] Modify headers' update behavior This patch modify existing behavior in order to update headers. The plugin now loops through global options to see which ones are set in $MYVIMRC from which a list is built. Then, it iterates over the list to see if all headers are present from the start of the file to a certain threshold (option g:header_max_size, default is 5). If one required header is missing, it considers that headers aren't set so it adds new ones. Otherwise, it updates headers keeping positions and identations. Patch also updates the README.md and CHANGELOG.md to reflect thoses changes. --- CHANGELOG.md | 1 + README.md | 9 +++++ autoload/header.vim | 83 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 73 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92a1ba1..5c19dd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Auto add header support - Modified date field - Modified date by field +- New algorithm for updating headers allowing them to be updated within a range (g:header_max_size global option) ### Fixed - Make plugin determine file type for each call to catch new file type if changed diff --git a/README.md b/README.md index 9bc75c6..9e0b51f 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,15 @@ It sets cfg's filetype comment character as this filetype supports multiple set let g:header_cfg_comment_char = ';' ``` +This option sets the range from which vim-header will search for required +headers (ones sets in global options) to determinate if it should update +existing headers or add new ones. +```vim +let g:header_max_size = 20 +``` +This options is used to prevent text replacement if your file contains text +matching headers (for instance `File:`). Default is 5. See [#20](https://github.com/alpertuna/vim-header/issues/20#issuecomment-319127330) for more explanations. + Support ------- Supported filetypes are; diff --git a/autoload/header.vim b/autoload/header.vim index efe9d49..e54a6f7 100644 --- a/autoload/header.vim +++ b/autoload/header.vim @@ -1,8 +1,3 @@ -" File: header.vim -" Author: H.Alper Tuna -" Date: 02.08.2017 -" Last Modified Date: 02.08.2017 -" Last Modified By: H.Alper Tuna " PROPERTIES AND FUNCTIONS FOR GENERAL PURPOSES " --------------------------------------------- " Set default global values @@ -30,6 +25,9 @@ endif if !exists('g:header_cfg_comment_char') let g:header_cfg_comment_char = '#' endif +if !exists('g:header_max_size') + let g:header_max_size = 5 +endif " Path for license files directory let s:license_files_dir = expand(':p:h:h').'/licensefiles/' @@ -262,8 +260,8 @@ endfun " Update header field with the new value fun s:update_header_field(field, value) - let l:field = s:create_pattern_text(b:comment_char) . '\s*' . s:create_pattern_text(a:field) . '\s*.*' - let l:field_add = s:create_pattern_text(b:comment_char) . s:create_pattern_text(a:field) . ' ' + let l:field = s:create_pattern_text(a:field) . '\s*.*' + let l:field_add = s:create_pattern_text(a:field) . ' ' execute '%s/' . l:field . '/' . l:field_add . s:create_pattern_text(a:value) .'/' endfun @@ -431,6 +429,54 @@ fun s:add_license_header(license_name) call setpos(".", l:save_pos) endfun +" Check if required headers (ones that are set globally as required) +" are present from the start of the buffer to the header_size_threshold. +" ---------------------------------------------------------------------- +" returns 1 if all required headers are present and within the range, +" otherwise returns 0 +fun s:has_required_headers_in_range(header_size_threshold) + let l:save_pos = getpos(".") + let l:headers_fields = [] " list holding required headers + + " File header + if g:header_field_filename + call add(l:headers_fields, b:field_file) + endif + + " Author header + if g:header_field_author != '' + call add(l:headers_fields, b:field_author) + endif + + " Date header + if g:header_field_timestamp + call add(l:headers_fields, b:field_date) + endif + + " Last Modified Date header + if g:header_field_modified_timestamp + call add(l:headers_fields, b:field_modified_date) + endif + + " Last Modified By header + if g:header_field_modified_by + call add(l:headers_fields, b:field_modified_by) + endif + + " check if required headers are present and within the range + for l:header_field in l:headers_fields + let l:header_field_line_nbr = search(l:header_field) + if + \ l:header_field_line_nbr == 0 || + \ l:header_field_line_nbr > a:header_size_threshold + call setpos(".", l:save_pos) + return 0 + endif + endfor + call setpos(".", l:save_pos) + return 1 +endfun +" " MAIN FUNCTION " ------------- " Main function selects header generator to add header @@ -443,22 +489,18 @@ fun header#add_header(type, license, silent) " If filetype is available, add header else inform user if b:is_filetype_available - " If there is already header, update it - if a:type == 0 - let i = 1 - while i < 10 - let line = getline(i) - if line =~ '^' . substitute(substitute(b:comment_char . b:field_file, '\*', '\\\*', ''), '\.', '\\\.', '') - call s:update_header() - return - endif - let i = i + 1 - endwhile - endif + + let l:file_contains_headers = + \ s:has_required_headers_in_range(g:header_max_size) " Select header generator if a:type == 0 - call s:add_header() + " If there is already header, update it + if l:file_contains_headers + call s:update_header() + else + call s:add_header() + endif elseif a:type == 1 call s:add_min_header() elseif a:type == 2 @@ -466,6 +508,7 @@ fun header#add_header(type, license, silent) else echo 'There is no "' . a:type . '" type to add header.' endif + else if a:silent == 1 return