Skip to content

Instantly share code, notes, and snippets.

@johnnymo87
Last active April 22, 2023 18:06
Show Gist options
  • Save johnnymo87/48963e42d9de217fc17a9b8cd27d573d to your computer and use it in GitHub Desktop.
Save johnnymo87/48963e42d9de217fc17a9b8cd27d573d to your computer and use it in GitHub Desktop.
Vim script that I used to flip YARDoc from Java style to Ruby style
" Fix YARD documentation comments in one file.
function! FixYardComments()
" Search for incorrect @param syntax.
let search_pattern = '@param \[\(.*\)]\s\+\(\w\+\)'
" Replace incorrect syntax with correct one.
let replace_pattern = '@param \2 \[\1\]'
" Apply the replacement to the entire file.
execute '%s/' . search_pattern . '/' . replace_pattern . '/g'
endfunction
" Map the function to a key combination
nnoremap <leader>fy :call FixYardComments()<CR>
" Fix YARD documentation comments in all ruby files.
function! FixAllYardComments()
" Get the list of Ruby files with incorrect @param syntax.
let ruby_files = systemlist('rg --files-with-matches "@param \\[.*\\]\\s+\\w"')
" Join the list elements into a single string with space as the separator.
let ruby_files_string = join(ruby_files, ' ')
" Populate the argument list with the Ruby files.
execute 'args' ruby_files_string
" Execute FixYardComments() on all files in the argument list.
argdo call FixYardComments()
" Write all changes to disk.
argdo update
endfunction
" Map the function to a key combination
nnoremap <leader>fa :call FixAllYardComments()<CR>

Me

In YARDoc documentation, I've noticed that usually we use the @param tag as @param variable_name [VariableType] some plain English description. However, in some parts of the documentation, I see that the variable name and variable type are switched, i.e. @param [VariableType] variable_name some plain English description. Is this intentional? Are both formats allowed? Is there some debate on this? Which format is more popular?

GPT

In YARDoc documentation, both formats for the @param tag are allowed and considered valid. The format @param variable_name [VariableType] some plain English description is called the "Ruby Style" and is the default format used by YARDoc. The format @param [VariableType] variable_name some plain English description is called the "Java Style" and is also supported by YARDoc.

There is some debate on which format is better, but ultimately it comes down to personal preference and the conventions of the codebase being documented. Some developers prefer the Ruby Style because it follows the natural order of the parameter declaration in Ruby, while others prefer the Java Style because it puts the type information up front and can be easier to read.

In general, the Ruby Style is more popular in the Ruby community and is the default format used by YARDoc, so you'll see it more often. However, both formats are valid and supported by YARDoc, so you should choose the one that works best for you and your project.

Prompt:

  • You are Tim Pope, a world-renowned expert in the use of Vim, the highly configurable text editor that is beloved by programmers and power users alike. As a Vim expert, you have spent countless hours honing your skills and developing plugins that help users be more productive. Today, you are here to share your knowledge and expertise with the world.
  • I am an experienced vim user and software engineer working on a rails 6.1 ruby 2.7 application.
  • The application uses YARD documentation comments, but there's a mistake in how we've been doing it.
  • Rather than documenting the parameter with @param variable_name [VariableType], we've been documenting it with @param [VariableType] variable_name.
  • I've written a vim script that will fix the documentation comments. I've defined a function to fix one file at a time, and to fix all files at once.
  • When I run my function to fix one file at a time, it works as expected.
  • But when I run my function to fix all files at once, the function to fix just one file crashes with the following output.
    Error detected while processing function FixAllYardComments:
    line    5:
    E730: using List as a String
    [`rg --files-with-matches]
    "`rg --files-with-matches" [New File] --No lines in buffer--
    Error detected while processing function FixAllYardComments[8]..FixYardComments:
    line    8:
    E486: Pattern not found: @param \[\(.*\)] \(\w\+\)
    
  • So tell me, Tim, how should I update the vim script below so as to fix this problem? Please respond with an updated version of the vim script, but also please explain what is the reasoning behind the change, and an explanation of any vim features that you employ.

Below will follow the vim script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment