Skip to content

Commit

Permalink
Merge pull request #81 from weirongxu/fix/ruby-without-parentheses
Browse files Browse the repository at this point in the history
fix(ruby): Fix ruby keyword arguments and without parentheses
  • Loading branch information
kkoomen authored Mar 7, 2020
2 parents da8b5c5 + 8de191a commit c4a5670
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 13 deletions.
4 changes: 3 additions & 1 deletion autoload/doge/pattern.vim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ 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, ' ')), '\')
let l:glue = has_key(a:pattern, 'normalize')
\ && a:pattern['normalize'] == v:false ? "\n" : ' '
let l:curr_line_raw = escape(doge#helpers#trim(join(l:lines, l:glue)), '\')
if l:curr_line_raw !~# a:pattern['match']
return 0
endif
Expand Down
10 changes: 7 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,16 @@ 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:function_and_class_method_pattern = doge#helpers#deepextend(s:pattern_base, {
\ 'match': '\m^def\s\+\%([^=(!]\+\)[=!]\?\s*(\(.\{-}\))',
\ 'normalize': 0,
\ 'match': '\m^def\s\+\%([^=(!\n ]\+\)[=!]\?\s*\((\_.\{-})\|[^\n]\+\)\?',
\ 'tokens': ['parameters'],
\})

Expand Down
26 changes: 25 additions & 1 deletion playground/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,34 @@ 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*)
# @param p4 [[TODO:type]] [TODO:description]
def parameters (p1,p2=4, *p3,**p4)
end

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

# [TODO:description]
Expand Down Expand Up @@ -55,3 +78,4 @@ def [](attribute)
# @param block [[TODO:type]] [TODO:description]
def each(&block)
end
end
160 changes: 152 additions & 8 deletions test/filetypes/ruby/functions.vader
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# ==============================================================================
# Functions without parameters.
# ==============================================================================
Given ruby (function without parameters):
Given ruby (function without parameters and with parentheses):
def myFunc() # inline comment
val = 1
end

Do (trigger doge):
Expand All @@ -11,29 +12,116 @@ Do (trigger doge):
Expect ruby (generated comment with nothing but the text 'TODO'):
# [TODO:description]
def myFunc() # inline comment
val = 1
end

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

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

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

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

# ==============================================================================
# Functions with parameters.
Given ruby (function with parameters):
# ==============================================================================
def myFunc(p1, p_2 = some_default_value)
Given ruby (function with parameters):
def myFunc(p1, p_2 = some_default_value) # inline comment
val = 1
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) # inline comment
val = 1
end

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

Given ruby (function with parameters without parentheses):
def myFunc p1, p_2 = some_default_value # inline comment
val = 1
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 # inline comment
val = 1
end

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

Given ruby (function with keyword parameters):
def myFunc(p1, p_2: some_default_value) # inline comment
val = 1
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) # inline comment
val = 1
end

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

Given ruby (function with keyword parameters but without parentheses):
def myFunc p1, p_2: some_default_value # inline comment
val = 1
end

def myFunc p1, p_2: # inline comment
val = 1
end

Do (trigger doge):
\<C-d>
:8\<CR>
\<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)
def myFunc p1, p_2: some_default_value # inline comment
val = 1
end

# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p_2 [[TODO:type]] [TODO:description]
def myFunc p1, p_2: # inline comment
val = 1
end

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

Given ruby (function with parameters with a '&' in its name):
def each(&block)
def each(&block) # inline comment
val = 1
end

Do (trigger doge):
Expand All @@ -42,13 +130,65 @@ Do (trigger doge):
Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param block [[TODO:type]] [TODO:description]
def each(&block)
def each(&block) # inline comment
val = 1
end

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

Given ruby (function with paramters containing '*' in its name):
def parameters (p1,p2=4, p3*)
def parameters (p1,p2=4, *p3, **p4)
val = 1
end

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

Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p2 [[TODO:type]] [TODO:description]
# @param p3 [[TODO:type]] [TODO:description]
# @param p4 [[TODO:type]] [TODO:description]
def parameters (p1,p2=4, *p3, **p4)
val = 1
end

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

Given ruby (function with multiline parameters):
def parameters ( #inline comment
p1,
p2=4,
*p3,
**p4
)
val = 1
end

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

Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param p1 [[TODO:type]] [TODO:description]
# @param p2 [[TODO:type]] [TODO:description]
# @param p3 [[TODO:type]] [TODO:description]
# @param p4 [[TODO:type]] [TODO:description]
def parameters ( #inline comment
p1,
p2=4,
*p3,
**p4
)
val = 1
end

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

Given ruby (function with paramters containing '*' in its name and without parentheses):
def parameters p1,p2=4, *p3, **p4
val = 1
end

Do (trigger doge):
Expand All @@ -59,13 +199,16 @@ Expect ruby (generated comment with @param tags):
# @param p1 [[TODO:type]] [TODO:description]
# @param p2 [[TODO:type]] [TODO:description]
# @param p3 [[TODO:type]] [TODO:description]
def parameters (p1,p2=4, p3*)
# @param p4 [[TODO:type]] [TODO:description]
def parameters p1,p2=4, *p3, **p4
val = 1
end

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

Given ruby (function with paramters containing ':' in its name):
def myFunc (format = :html)
val = 1
end

Do (trigger doge):
Expand All @@ -75,6 +218,7 @@ Expect ruby (generated comment with @param tags):
# [TODO:description]
# @param format [[TODO:type]] [TODO:description]
def myFunc (format = :html)
val = 1
end

# ==============================================================================
Expand Down

0 comments on commit c4a5670

Please sign in to comment.