Skip to content

Commit

Permalink
feat: Strip comments before generating to ensure patterns do not break
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed Jun 26, 2019
1 parent 580273b commit a7135bb
Show file tree
Hide file tree
Showing 23 changed files with 88 additions and 68 deletions.
16 changes: 12 additions & 4 deletions autoload/doge/generate.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ set cpoptions&vim
" Generates a comment based on a given pattern.
function! doge#generate#pattern(pattern) abort
" Assuming multiline function expressions won't be longer than 15 lines.
let l:lines = getline('.', line('.') + 15)
let l:lines_raw = getline('.', line('.') + 15)
let l:lines = map(l:lines_raw, {key, line ->
\ substitute(line, b:doge_pattern_single_line_comment, '' ,'g')})

" Skip if the cursor doesn't start with text.
if empty(trim(l:lines[0]))
return 0
endif

" Skip immediately if the current line does not match.
let l:curr_line = escape(trim(join(l:lines, ' ')), '\')
if l:curr_line !~# a:pattern['match']
" Skip if the current line does not match the main pattern.
let l:curr_line_raw = escape(trim(join(l:lines, ' ')), '\')
if l:curr_line_raw !~# a:pattern['match']
return 0
endif

let l:curr_line = substitute(
\ l:curr_line_raw,
\ b:doge_pattern_multi_line_comment,
\ '',
\ 'g')

" Extract the primary tokens.
let l:tokens = doge#token#extract(
\ l:curr_line,
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/coffee.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m#\@<!##\@!.\+$'
let b:doge_pattern_multi_line_comment = '\m###.\{-}###'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/groovy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/java.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/javascript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" Matches the following pattern:
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/kotlin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" Matches the following pattern:
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/lua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m--\+\([[\)\@!.\{-}$'
let b:doge_pattern_multi_line_comment = '\m--\+[[.\{-}]]--\+'
let b:doge_patterns = []

let s:parameters_match_pattern = '\m\([^,]\+\)'
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/php.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/python.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m#.\{-}$'
let b:doge_pattern_multi_line_comment = '\m\(""".\{-}"""\|' . "'''.\\{-}'''" . '\)'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/r.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m#.\{-}$'
let b:doge_pattern_multi_line_comment = '\m#.\{-}$'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/ruby.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m#.\{-}$'
let b:doge_pattern_multi_line_comment = '\m\(=begin.\{-}=end\|<<-DOC.\{-}DOC\)'
let b:doge_patterns = []

" ==============================================================================
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/scala.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" Matches the following pattern:
Expand Down
2 changes: 2 additions & 0 deletions ftplugin/typescript.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
let s:save_cpo = &cpoptions
set cpoptions&vim

let b:doge_pattern_single_line_comment = '\m\(\/\*.\{-}\*\/\|\/\/.\{-}$\)'
let b:doge_pattern_multi_line_comment = '\m\/\*.\{-}\*\/'
let b:doge_patterns = []

" Matches the following pattern:
Expand Down
4 changes: 2 additions & 2 deletions test/filetypes/coffee/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Functions without parameters.
# ==============================================================================
Given coffee (function without parameters):
myFunc = () -> x * x
myFunc = () -> x * x # inline comment

Do (trigger doge):
\<C-d>
Expand All @@ -12,7 +12,7 @@ Expect coffee (generated comment with @description and @function tags):
@description TODO
@function myFunc
###
myFunc = () -> x * x
myFunc = () -> x * x # inline comment

# ==============================================================================
# Functions with parameters.
Expand Down
8 changes: 4 additions & 4 deletions test/filetypes/javascript/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Functions without parameters.
# ==============================================================================
Given javascript (function without parameters):
function myFunc() {}
function myFunc(/* inline comment */) {}

Do (trigger doge):
\<C-d>
Expand All @@ -11,13 +11,13 @@ Expect javascript (generated comment with @description tag):
/**
* @description TODO
*/
function myFunc() {}
function myFunc(/* inline comment */) {}

# ==============================================================================
# Functions with parameters without type hints.
# ==============================================================================
Given javascript (function with parameters without type hints):
function myFunc($p1 = 'value', p2 = [], p3, p4) {}
function myFunc($p1 = 'value', p2 = [], p3, p4 /* inline comment */) {}

Do (trigger doge):
\<C-d>
Expand All @@ -30,7 +30,7 @@ Expect javascript (generated comment with @description and @param tags):
* @param {*} p3 TODO
* @param {*} p4 TODO
*/
function myFunc($p1 = 'value', p2 = [], p3, p4) {}
function myFunc($p1 = 'value', p2 = [], p3, p4 /* inline comment */) {}

# ==============================================================================
# Functions with parameters with type hints.
Expand Down
32 changes: 0 additions & 32 deletions test/filetypes/kotlin/classes.vader
Original file line number Diff line number Diff line change
Expand Up @@ -208,35 +208,3 @@ Expect kotlin (generated comments with @param tags):
*/
constructor(parent: Person, query: Query<*, <T>>, users: List<User>) {}
}

# ==============================================================================
# Classes without parameters that should be ignored.
# ==============================================================================
Given kotlin (classes without parameters that should be ignored):
class Delegate {}

class ResourceDelegate<T> : ReadOnlyProperty<MyUI, T> {}

class MyView : View {}

class Bar2 : Foo() {}

Do (trigger doge):
\<C-d>
:3\<CR>
\<C-d>
:3\<CR>
\<C-d>
:5\<CR>
\<C-d>
:7\<CR>
\<C-d>

Expect kotlin (no changes):
class Delegate {}

class ResourceDelegate<T> : ReadOnlyProperty<MyUI, T> {}

class MyView : View {}

class Bar2 : Foo() {}
8 changes: 4 additions & 4 deletions test/filetypes/kotlin/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Functions without type parameters without parameters without return type.
# ==============================================================================
Given kotlin (function without type parameters without parameters without return type):
fun myFunc() {}
fun myFunc(/* inline comment */) {}

Do (trigger doge):
\<C-d>
Expand All @@ -12,7 +12,7 @@ Expect kotlin (generated comment with nothing but the text 'TODO' and @return ta
* TODO
* @return TODO
*/
fun myFunc() {}
fun myFunc(/* inline comment */) {}

# ==============================================================================
# Functions with type parameters without parameters without return type.
Expand Down Expand Up @@ -88,7 +88,7 @@ Expect kotlin (generated comment with nothing but the text 'TODO' and @return ta
Given kotlin (function without type parameters with parameters with return type):
fun myFunc(i: Int = 0, lst: List<*, <T>>, str: String.() -> Unit, map: MutableMap<String, Any?>, str: List<String, <T>>): Int? {}

abstract fun signal(t: Int, u: Int) = apply(t, u)
abstract fun signal(t: Int/* inline comment */, u: Int) = apply(t, u)

Do (trigger doge):
\<C-d>
Expand All @@ -113,7 +113,7 @@ Expect kotlin (generated comment with @param and @return tags):
* @param u TODO
* @return TODO
*/
abstract fun signal(t: Int, u: Int) = apply(t, u)
abstract fun signal(t: Int/* inline comment */, u: Int) = apply(t, u)

# ==============================================================================
# Different function modifiers.
Expand Down
8 changes: 4 additions & 4 deletions test/filetypes/lua/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# Functions without parameters.
# ==============================================================================
Given lua (function without parameters):
function myFunc()
function myFunc() -- inline comment
end

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

Expect lua (generated comment with nothing but the text 'TODO'):
-- TODO
function myFunc()
function myFunc() -- inline comment
end

# ==============================================================================
# Functions with parameters.
# ==============================================================================
Given lua (function with parameters):
function myFunc(p1, p2, p3, p4)
function myFunc(p1, p2, p3, p4) -- inline comment
end

Do (trigger doge):
Expand All @@ -29,7 +29,7 @@ Expect lua (generated comment with @param tags):
-- @param p2 TODO
-- @param p3 TODO
-- @param p4 TODO
function myFunc(p1, p2, p3, p4)
function myFunc(p1, p2, p3, p4) -- inline comment
end

# ==============================================================================
Expand Down
22 changes: 18 additions & 4 deletions test/filetypes/php/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Functions without parameters.
# ==============================================================================
Given php (function without parameters):
function myFunc() {}
function myFunc(/* inline comment */) {}

Do (trigger doge):
\<C-d>
Expand All @@ -12,13 +12,20 @@ Expect php (generated comment with nothing but the text 'TODO'):
* TODO
*
*/
function myFunc() {}
function myFunc(/* inline comment */) {}

# ==============================================================================
# Functions with parameters.
# ==============================================================================
Given php (function with parameters):
function myFunction(array &$p1, string $p2, &$p3 = NULL, \Drupal\core\Entity\Node $p4) {}
function myFunction(
array &$p1 /* inline comment */,
string $p2,
&$p3 = NULL,
\Drupal\core\Entity\Node $p4 // inline comment
) {
//
}

Do (trigger doge):
\<C-d>
Expand All @@ -32,4 +39,11 @@ Expect php (generated comment with @param tags):
* @param mixed $p3 TODO
* @param \Drupal\core\Entity\Node $p4 TODO
*/
function myFunction(array &$p1, string $p2, &$p3 = NULL, \Drupal\core\Entity\Node $p4) {}
function myFunction(
array &$p1 /* inline comment */,
string $p2,
&$p3 = NULL,
\Drupal\core\Entity\Node $p4 // inline comment
) {
//
}
4 changes: 2 additions & 2 deletions test/filetypes/python/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Functions without parameters.
# ==============================================================================
Given python (function without parameters):
def myFunc():
def myFunc(): # inline comment
pass

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

Expect python (no changes):
def myFunc():
def myFunc(): # inline comment
"""
TODO

Expand Down
8 changes: 4 additions & 4 deletions test/filetypes/r/functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Functions without parameters.
# ==============================================================================
Given r (functions without parameters):
myFunc = function() {
myFunc = function() { # inline comment
# ...
}

myFunc <- function() {
myFunc <- function() { # inline comment
# ...
}

Expand All @@ -21,7 +21,7 @@ Expect r (generated comments with nothing but the text 'TODO' and @export tag):
#'
#' @export
#'
myFunc = function() {
myFunc = function() { # inline comment
# ...
}

Expand All @@ -30,7 +30,7 @@ Expect r (generated comments with nothing but the text 'TODO' and @export tag):
#'
#' @export
#'
myFunc <- function() {
myFunc <- function() { # inline comment
# ...
}

Expand Down
Loading

0 comments on commit a7135bb

Please sign in to comment.