From 8abad34d0415db29bbc69dd7b32833d261c1c3db Mon Sep 17 00:00:00 2001 From: Kim K Date: Mon, 18 Nov 2019 23:52:16 +0800 Subject: [PATCH] feat(#48): Support Shell --- README.md | 5 ++- autoload/doge/generate.vim | 4 +-- autoload/doge/helpers.vim | 2 +- autoload/doge/token.vim | 2 +- ftplugin/sh.vim | 55 +++++++++++++++++++++++++++++++ playground/test.sh | 27 +++++++++++++++ test/filetypes/sh/functions.vader | 43 ++++++++++++++++++++++++ 7 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 ftplugin/sh.vim create mode 100644 playground/test.sh create mode 100644 test/filetypes/sh/functions.vader diff --git a/README.md b/README.md index 1d5dd263..2225c2e2 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Is your favorite doc standard not supported? | :white_check_mark: | R | [Roxygen2][roxygen2] | | :white_check_mark: | C++ | [Doxygen][doxygen] | | :white_check_mark: | C | [Doxygen][doxygen], [KernelDoc][kerneldoc] | +| :white_check_mark: | Shell | [Google][sh-google] | # Getting started @@ -123,7 +124,7 @@ If you want to change the doc standard specifically for a buffer you can do: Here is the full list of available doc standards per filetype: | Variable | Default | Supported | -| :--------------------------------- | :----------- | :------------------------------------------------------------------------------------------------------------------------------------------- | +| :--------------------------------- | :------------------ | :------------------------------------------------------------------------------------------------------------------------------------------- | | `g:doge_doc_standard_python` | `'reST'` | `'reST'`, `'numpy'`, `'google'`, `'sphinx'` | | `g:doge_doc_standard_php` | `'phpdoc'` | `'phpdoc'` | | `g:doge_doc_standard_javascript` | `'jsdoc'` | `'jsdoc'` | @@ -138,6 +139,7 @@ Here is the full list of available doc standards per filetype: | `g:doge_doc_standard_r` | `'roxygen2'` | `'roxygen2'` | | `g:doge_doc_standard_cpp` | `'doxygen_javadoc'` | `'doxygen_javadoc'`, `'doxygen_javadoc_no_asterisk'`, `'doxygen_javadoc_banner'`, `'doxygen_qt'`, `'doxygen_qt_no_asterisk'` | | `g:doge_doc_standard_c` | `'doxygen_javadoc'` | `'kernel_doc'`, `'doxygen_javadoc'`, `'doxygen_javadoc_no_asterisk'`, `'doxygen_javadoc_banner'`, `'doxygen_qt'`, `'doxygen_qt_no_asterisk'` | +| `g:doge_doc_standard_sh` | `'google'` | `'google'` | ## Options @@ -310,6 +312,7 @@ DoGe is licensed under the GPL-3.0 license. [roxygen2]: https://github.com/klutometis/roxygen [doxygen]: http://www.doxygen.nl [kerneldoc]: https://www.kernel.org/doc/html/latest/doc-guide/kernel-doc.html +[sh-style-guide]: https://google.github.io/styleguide/shell.xml#Function_Comments [demo-readme]: https://github.com/kkoomen/vim-doge/blob/master/doc/demos [suggest-language]: https://github.com/kkoomen/vim-doge/issues/new?labels=enhancement&template=feature_request.md&title=Add+support+for+ diff --git a/autoload/doge/generate.vim b/autoload/doge/generate.vim index b2a521e8..282000ef 100644 --- a/autoload/doge/generate.vim +++ b/autoload/doge/generate.vim @@ -41,11 +41,11 @@ function! doge#generate#pattern(pattern) abort \ ) " Extract the primary tokens. - let l:tokens = doge#token#extract( + let l:tokens = get(doge#token#extract( \ l:curr_line, \ a:pattern['match'], \ a:pattern['match_group_names'] - \ )[0] + \ ), 0, {}) endif try diff --git a/autoload/doge/helpers.vim b/autoload/doge/helpers.vim index f40a9dd2..e3706b89 100644 --- a/autoload/doge/helpers.vim +++ b/autoload/doge/helpers.vim @@ -62,7 +62,7 @@ endfunction " If no context is specified the todo-pattern is returned to search for. function! doge#helpers#placeholder(...) abort if !has_key(a:, 1) - return '\(\[TODO:[[:alnum:] ]\+\]\|TODO\)' + return '\(\[TODO:[[:alnum:]-]\+\]\|TODO\)' else return printf('[TODO:%s]', a:1) endif diff --git a/autoload/doge/token.vim b/autoload/doge/token.vim index 01e9344a..05f1dc1e 100644 --- a/autoload/doge/token.vim +++ b/autoload/doge/token.vim @@ -79,7 +79,7 @@ function! s:token_replace(tokens, text) abort endfor " Replace ! with [TODO:]. - let l:text = substitute(l:text, '\m!\([[:alpha:]]\+\)', '[TODO:\1]', 'g') + let l:text = substitute(l:text, '\m!\([[:alpha:]-]\+\)', '[TODO:\1]', 'g') " Replace 2 or more white-spaces with 1 single white-space, except for leading " white-spaces and/or newlines. Those should be preserved. diff --git a/ftplugin/sh.vim b/ftplugin/sh.vim new file mode 100644 index 00000000..eef314d9 --- /dev/null +++ b/ftplugin/sh.vim @@ -0,0 +1,55 @@ +" ============================================================================== +" The R documentation should follow the 'Roxygen2' conventions. +" see https://github.com/klutometis/roxygen +" ============================================================================== + +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_supported_doc_standards = ['google'] +let b:doge_doc_standard = get(g:, 'doge_doc_standard_sh', b:doge_supported_doc_standards[0]) +if index(b:doge_supported_doc_standards, b:doge_doc_standard) < 0 + echoerr printf( + \ '[DoGe] %s is not a valid Shell doc standard, available doc standard are: %s', + \ b:doge_doc_standard, + \ join(b:doge_supported_doc_standards, ', ') + \ ) +endif + +let b:doge_patterns = [] + +" ============================================================================== +" Matches regular functions. +" ============================================================================== +" +" Matches the following scenarios: +" +" function test {} +" +" test() {} +call add(b:doge_patterns, { +\ 'match': '\m^\(function\s\+[[:alnum:]_-]\+\|[[:alnum:]_-]\+\s*(.\{-})\)\s*{', +\ 'match_group_names': [], +\ 'comment': { +\ 'insert': 'above', +\ 'template': { +\ 'google': [ +\ '################################################################################', +\ '# !description', +\ '# Globals:', +\ '# \t!var-name', +\ '# Arguments:', +\ '# \t$1: !description', +\ '# Returns:', +\ '# \t!description', +\ '################################################################################', +\ ], +\ }, +\ }, +\}) + +let &cpoptions = s:save_cpo +unlet s:save_cpo diff --git a/playground/test.sh b/playground/test.sh new file mode 100644 index 00000000..267a3a85 --- /dev/null +++ b/playground/test.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +################################################################################ +# [TODO:description] +# Globals: +# [TODO:var-name] +# Arguments: +# $1: [TODO:description] +# Returns: +# [TODO:description] +################################################################################ +function test { + print "foobar" +} + +################################################################################ +# [TODO:description] +# Globals: +# [TODO:var-name] +# Arguments: +# $1: [TODO:description] +# Returns: +# [TODO:description] +################################################################################ +test() { + print "foobar" +} diff --git a/test/filetypes/sh/functions.vader b/test/filetypes/sh/functions.vader new file mode 100644 index 00000000..90f84e8c --- /dev/null +++ b/test/filetypes/sh/functions.vader @@ -0,0 +1,43 @@ +# ============================================================================== +# Regular functions. +# ============================================================================== +Given sh (regular functions): + function test { + print "foobar" + } + + test() { + print "foobar" + } + +Do (trigger doge): + \ + :14\ + \ + +Expect sh (generated comments with a descriptios and 'Globals', 'Arguments' and 'Returns' keywords): + ################################################################################ + # [TODO:description] + # Globals: + # [TODO:var-name] + # Arguments: + # $1: [TODO:description] + # Returns: + # [TODO:description] + ################################################################################ + function test { + print "foobar" + } + + ################################################################################ + # [TODO:description] + # Globals: + # [TODO:var-name] + # Arguments: + # $1: [TODO:description] + # Returns: + # [TODO:description] + ################################################################################ + test() { + print "foobar" + }