Skip to content

Commit

Permalink
fix(ruby): Fix ruby without parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
weirongxu committed Mar 6, 2020
1 parent da8b5c5 commit 7f2adf0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
19 changes: 16 additions & 3 deletions autoload/doge/pattern.vim
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@ function! doge#pattern#generate(pattern) abort
endif
else
" Skip if the current line does not match the main pattern.
let l:curr_line_raw = escape(doge#helpers#trim(join(l:lines, ' ')), '\')
if l:curr_line_raw !~# a:pattern['match']
let l:curr_line_raw = escape(doge#helpers#trim(join(l:lines, "\n")), '\')
let l:match = v:null
if type(a:pattern['match']) == type('')
if l:curr_line_raw =~# a:pattern['match']
let l:match = a:pattern['match']
endif
elseif type(a:pattern['match']) == type([])
for l:m in a:pattern['match']
if l:curr_line_raw =~# l:m
let l:match = l:m
break
endif
endfor
endif
if l:match is v:null
return 0
endif

Expand All @@ -43,7 +56,7 @@ function! doge#pattern#generate(pattern) abort
" Extract the primary tokens.
let l:tokens = get(doge#token#extract(
\ l:curr_line,
\ a:pattern['match'],
\ l:match,
\ a:pattern['tokens']
\ ), 0, {})
endif
Expand Down
5 changes: 2 additions & 3 deletions autoload/doge/token.vim
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ endfunction
" value of that group. The regex groups in the 'regex' parameter should be
" symmetrical in length to the 'regex_group_names' parameter.
function! doge#token#extract(text, regex, regex_group_names) abort
let l:submatches = []
call substitute(a:text, a:regex, '\=add(l:submatches, submatch(0))', 'g')
let l:matches = []
call substitute(a:text, a:regex, '\=add(l:matches, submatch(0))', 'g')

let l:matches = map(l:submatches, { k, v -> doge#helpers#trim(v) })
let l:tokens = []

" We can expect a list of matches like:
Expand Down
14 changes: 11 additions & 3 deletions ftplugin/ruby.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let b:doge_patterns = doge#buffer#get_patterns()
" ==============================================================================
let s:pattern_base = {
\ 'parameters': {
\ 'match': '\m\([[:alnum:]_]\+\)\%(\s*=\s*[^,]\+\)\?',
\ 'match': '\m\([[:alnum:]_]\+\)\%(\s*[=:]\s*[^,]\+\)\?',
\ 'tokens': ['name'],
\ 'format': '@param {name} [!type] !description',
\ },
Expand All @@ -37,12 +37,20 @@ let s:pattern_base = {
" Matches regular function expressions and class methods.
" ------------------------------------------------------------------------------
" def myFunc(p1, p_2 = some_default_value)
" def def parameters (p1,p2=4, p3*)
" def parameters (p1,p2=4, *p3)
" def parameters p1, p2 = 4, *p3
" def where(attribute, type = nil, **options)
" def each(&block)
" def self.class_method(attribute)
" def self.class_parameters p1,p2=4, *p3
" ------------------------------------------------------------------------------
let s:parameters_matches = [
\ '(\(.\{-}\))',
\ '\s\+\([^\n]*\)',
\ '\n',
\ ]
let s:function_and_class_method_pattern = doge#helpers#deepextend(s:pattern_base, {
\ 'match': '\m^def\s\+\%([^=(!]\+\)[=!]\?\s*(\(.\{-}\))',
\ 'match': map(s:parameters_matches, { i, m -> '\m^def\s\+\%([^=(!\n ]\+\)[=!]\?\s*' . m}),
\ 'tokens': ['parameters'],
\})

Expand Down
24 changes: 23 additions & 1 deletion playground/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,32 @@ def myFunc(p1, p_2 = some_default_value)
def myFunc (format = :html)
end

# [TODO:description]
def myFunc
end

# [TODO:description]
# @param format [[TODO:type]] [TODO:description]
def myFunc format = :html
end

# [TODO:description]
# @param format [[TODO:type]] [TODO:description]
def myFunc(format: :html)
end

# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p2 [[TODO:type]] [TODO:description]
# @param p3 [[TODO:type]] [TODO:description]
def parameters (p1,p2=4, p3*)
def parameters (p1,p2=4, *p3)
end

# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p2 [[TODO:type]] [TODO:description]
# @param p3 [[TODO:type]] [TODO:description]
def parameters p1,p2=4, *p3
end

# [TODO:description]
Expand Down Expand Up @@ -55,3 +76,4 @@ def [](attribute)
# @param block [[TODO:type]] [TODO:description]
def each(&block)
end
end
64 changes: 63 additions & 1 deletion test/filetypes/ruby/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ Expect ruby (generated comment with nothing but the text 'TODO'):
def myFunc() # inline comment
end

# ------------------------------------------------------------------------------

Given ruby (function without parameters and parentheses):
def myFunc # inline comment
end

Do (trigger doge):
\<C-d>

Expect ruby (generated comment with nothing but the text 'TODO'):
# [TODO:description]
def myFunc # inline comment
end

# ==============================================================================
# Functions with parameters.
Given ruby (function with parameters):
# ==============================================================================
Given ruby (function with parameters):
def myFunc(p1, p_2 = some_default_value)
end

Expand All @@ -32,6 +46,54 @@ Expect ruby (generated comment with @param tags):

# ------------------------------------------------------------------------------

Given ruby (function with parameters but without parentheses):
def myFunc p1, p_2 = some_default_value
end

Do (trigger doge):
\<C-d>

Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p_2 [[TODO:type]] [TODO:description]
def myFunc p1, p_2 = some_default_value
end

# ------------------------------------------------------------------------------

Given ruby (function with keyword parameters):
def myFunc(p1, p_2: some_default_value)
end

Do (trigger doge):
\<C-d>

Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p_2 [[TODO:type]] [TODO:description]
def myFunc(p1, p_2: some_default_value)
end

# ------------------------------------------------------------------------------

Given ruby (function with keyword parameters but without parentheses):
def myFunc p1, p_2: some_default_value
end

Do (trigger doge):
\<C-d>

Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p_2 [[TODO:type]] [TODO:description]
def myFunc p1, p_2: some_default_value
end

# ------------------------------------------------------------------------------

Given ruby (function with parameters with a '&' in its name):
def each(&block)
end
Expand Down

0 comments on commit 7f2adf0

Please sign in to comment.