├── demo1.gif ├── autoload └── context │ ├── comments.vim │ └── commentstring.vim ├── samples └── vim │ └── example.vim ├── README.adoc ├── plugin └── context-commentstring.vim └── doc └── context-commentstring.txt /demo1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jasonlong/vim-context-commentstring/master/demo1.gif -------------------------------------------------------------------------------- /autoload/context/comments.vim: -------------------------------------------------------------------------------- 1 | " This file is part of vim-context-commentstring. 2 | " Copyright: © 2013-2021 Alejandro Exojo Piqueras 3 | " License: MIT (see doc for details). 4 | 5 | let g:context#comments#table = {} 6 | 7 | let g:context#comments#table['vim'] = { 8 | \ 'vimLuaRegion' : ':--', 9 | \} 10 | 11 | let g:context#comments#table['vue'] = { 12 | \ 'htmlTag': 's:', 13 | \ 'vue_typescript': 's1:/*,mb:*,ex:*/,://', 14 | \ 'cssStyle': 's1:/*,mb:*,ex:*/,://', 15 | \ } 16 | 17 | -------------------------------------------------------------------------------- /samples/vim/example.vim: -------------------------------------------------------------------------------- 1 | " Sample from `:h :lua` 2 | 3 | function! CurrentLineInfo() 4 | lua << EOF 5 | local linenr = vim.api.nvim_win_get_cursor(0)[1] 6 | local curline = vim.api.nvim_buf_get_lines( 7 | 0, linenr, linenr + 1, false)[1] 8 | -- This line should be properly wrapped when using `gw`. Also, typing after the 9 | -- comment block, it should break the line to the next, and automatically insert 10 | -- the comment character. Without 'comments' properly set, it doesn't. 11 | print(string.format("Current line [%d] has %d bytes", 12 | linenr, #curline)) 13 | EOF 14 | endfunction 15 | 16 | 17 | " Sample from `:h :python` 18 | 19 | function! IcecreamInitialize() 20 | python << EOF 21 | class StrawberryIcecream: 22 | def __call__(self): 23 | print 'EAT ME' 24 | EOF 25 | endfunction 26 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | == vim-context-commentstring 2 | 3 | This is a very simple plugin to automatically set the 'commentstring' and 4 | 'comments' Vim options in file types which contain more than one syntax (like 5 | JavaScript nested inside HTML, or Lua nested in VimL). 6 | 7 | It uses the names of the syntax highlighting groups to know where the cursor is, 8 | and just set the value of those Vim settings to one appropriate for that part of 9 | the file. 10 | 11 | This will make Vim properly use the features, which by default are just fixed 12 | values which are set by file type. This allows to use a plugin like 13 | https://github.com/tpope/vim-commentary[Tim Pope's vim-commentary] to toggle the 14 | comments, or the native Vim functionality of wrapping text when you are typing 15 | a lines of comments. 16 | 17 | image::demo1.gif[Demo] 18 | 19 | See the documentation in the doc/context-commentstring.txt file, or browse it 20 | online from the hosted source code in: 21 | 22 | https://raw.github.com/suy/vim-context-commentstring/master/doc/context-commentstring.txt 23 | -------------------------------------------------------------------------------- /autoload/context/commentstring.vim: -------------------------------------------------------------------------------- 1 | " This file is part of vim-context-commentstring. 2 | " Copyright: © 2013-2021 Alejandro Exojo Piqueras 3 | " License: MIT (see doc for details). 4 | 5 | let g:context#commentstring#table = {} 6 | 7 | let g:context#commentstring#table['vim'] = { 8 | \ 'vimLuaRegion' : '--%s', 9 | \ 'vimPerlRegion' : '#%s', 10 | \ 'vimPythonRegion' : '#%s', 11 | \} 12 | 13 | let g:context#commentstring#table['html'] = { 14 | \ 'javaScript' : '//%s', 15 | \ 'cssStyle' : '/*%s*/', 16 | \} 17 | 18 | let g:context#commentstring#table['xhtml'] = g:context#commentstring#table['html'] 19 | 20 | let g:context#commentstring#table['javascript.jsx'] = { 21 | \ 'jsComment' : '//%s', 22 | \ 'jsImport' : '//%s', 23 | \ 'jsxStatment' : '//%s', 24 | \ 'jsxRegion' : '{/*%s*/}', 25 | \ 'jsxTag' : '{/*%s*/}', 26 | \} 27 | 28 | let g:context#commentstring#table['typescriptreact'] = 29 | \ g:context#commentstring#table['javascript.jsx'] 30 | 31 | let g:context#commentstring#table['typescriptreact'] = { 32 | \ 'jsComment' : '//%s', 33 | \ 'jsImport' : '//%s', 34 | \ 'jsxStatment' : '//%s', 35 | \ 'jsxRegion' : '{/*%s*/}', 36 | \ 'jsxTag' : '{/*%s*/}', 37 | \} 38 | 39 | let g:context#commentstring#table['vue'] = { 40 | \ 'javaScript' : '//%s', 41 | \ 'cssStyle' : '/*%s*/', 42 | \ 'vue_scss' : '/*%s*/', 43 | \} 44 | 45 | -------------------------------------------------------------------------------- /plugin/context-commentstring.vim: -------------------------------------------------------------------------------- 1 | " This file is part of vim-context-commentstring. 2 | " Copyright: © 2013-2014 Alejandro Exojo Piqueras 3 | " License: MIT (see doc for details). 4 | 5 | if exists('g:loaded_context_commentstring') 6 | finish 7 | endif 8 | 9 | 10 | augroup ContextCommentstringBootstrap 11 | autocmd! 12 | autocmd FileType * call Setup() 13 | augroup END 14 | 15 | 16 | function! s:Setup() 17 | augroup ContextCommentstringEnabled 18 | " Clear previous autocommands first in all cases, in case the filetype 19 | " changed from something in the table, to something NOT in the table. 20 | autocmd! CursorMoved 21 | if !empty(&filetype) && has_key(g:context#commentstring#table, &filetype) 22 | let b:original_commentstring=&l:commentstring 23 | autocmd CursorMoved call UpdateCommentString() 24 | endif 25 | if !empty(&filetype) && has_key(g:context#comments#table, &filetype) 26 | let b:original_comments=&l:comments 27 | autocmd CursorMoved call UpdateComments() 28 | endif 29 | augroup END 30 | endfunction 31 | 32 | 33 | function! s:UpdateCommentString() 34 | let stack = synstack(line('.'), col('.')) 35 | if !empty(stack) 36 | for name in map(stack, 'synIDattr(v:val, "name")') 37 | if has_key(g:context#commentstring#table[&filetype], name) 38 | let &l:commentstring = g:context#commentstring#table[&filetype][name] 39 | return 40 | endif 41 | endfor 42 | endif 43 | let &l:commentstring = b:original_commentstring 44 | endfunction 45 | 46 | 47 | function! s:UpdateComments() 48 | let stack = synstack(line('.'), col('.')) 49 | call reverse(stack) 50 | if !empty(stack) 51 | for name in map(stack, 'synIDattr(v:val, "name")') 52 | if has_key(g:context#comments#table[&filetype], name) 53 | let &l:comments = g:context#comments#table[&filetype][name] 54 | return 55 | endif 56 | endfor 57 | endif 58 | let &l:comments = b:original_comments 59 | endfunction 60 | 61 | 62 | let g:loaded_context_commentstring = 1 63 | -------------------------------------------------------------------------------- /doc/context-commentstring.txt: -------------------------------------------------------------------------------- 1 | *context-commentstring.txt* Change 'commentstring' according to context. 2 | 3 | Version 0.1.0 4 | Script ID: ???? 5 | Copyright © 2013 Alejandro Exojo Piqueras 6 | License: MIT license {{{ 7 | Permission is hereby granted, free of charge, to any person obtaining 8 | a copy of this software and associated documentation files (the 9 | "Software"), to deal in the Software without restriction, including 10 | without limitation the rights to use, copy, modify, merge, publish, 11 | distribute, sublicense, and/or sell copies of the Software, and to 12 | permit persons to whom the Software is furnished to do so, subject to 13 | the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included 16 | in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | }}} 26 | 27 | CONTENTS *context-commentstring-contents* 28 | 29 | Introduction |context-commentstring-introduction| 30 | Installation |context-commentstring-installation| 31 | Configuration and usage |context-commentstring-configuration| 32 | Customization |context-commentstring-customization| 33 | Bugs and limitations |context-commentstring-bugs| 34 | Credits and considerations |context-commentstring-credits| 35 | Changelog |context-commentstring-changelog| 36 | 37 | 38 | ============================================================================== 39 | INTRODUCTION *context-commentstring-introduction* 40 | 41 | *context-commentstring* is a Vim plugin that sets the value of 'commentstring' 42 | to a different value depending on the region of the file you are in. Is only 43 | useful for |file-types| where is possible to have a language embedded in a 44 | language. The most common example are small pieces of CSS or JavaScript in an 45 | HTML file, but also Lua, Python or Perl blocks embedded in Vim code. 46 | 47 | Note that it relies on the parsing done by the |syntax| highlighting system, 48 | so it works out of the box for several well known |file-types|, but you will 49 | have to write some |syntax-highlighting| file yourself if you don't have any 50 | and want to use this plugin for your |file-type|. 51 | 52 | The use of 'commentstring' is not very popular, but is necessary for Vim's 53 | |zf| action if you use the |fold-marker| feature. Is also exploited by Tim 54 | Pope's |commentary.txt| plugin, a utility to comment and uncomment lines of 55 | code. Find the plugin at: 56 | https://github.com/tpope/vim-commentary 57 | 58 | Additionally, this plugin sets the value of the 'comments' setting 59 | automatically. The 'comments' setting describes how inserting new lines should 60 | be handled when inside a comment region. This automatic formatting can be 61 | configured via the 'formatoptions' setting. 62 | 63 | ------------------------------------------------------------------------------ 64 | INSTALLATION *context-commentstring-installation* 65 | 66 | The usual for any other Vim plugin: copy the files where Vim will look for 67 | them with the same directory layout that came with the plugin. If you use 68 | other plugins, color schemes, etc. that are installed by you, you probably 69 | know where that is. In UNIX-like systems (Linux, Mac OS X), is some place like 70 | $HOME/.vim, and on Windows $HOME/vimfiles. See |vimfiles| for details. 71 | 72 | If you don't have installed any plugins yet, I strongly recommend to first 73 | install and configure a plugin manager (a plugin to help you with other 74 | plugins), and then install |context-commentstring|. My recommendation is that 75 | you start with Pathogen because is very simple and it works very well with a 76 | version control system. Find it at: 77 | https://github.com/tpope/vim-pathogen 78 | 79 | ------------------------------------------------------------------------------ 80 | CONFIGURATION AND USAGE *context-commentstring-configuration* 81 | 82 | It should work out of the box for HTML and VimL (Vim script) files. It will 83 | simply change the value of 'commentstring' when you are in the appropriate 84 | region. See below for how to customize or extend it. 85 | 86 | ============================================================================== 87 | CUSTOMIZATION *context-commentstring-customization* 88 | 89 | The plugin works by checking if the current syntax group matches a group of an 90 | embedded language, for example, JavaScript inside HTML. You can check the 91 | default values by looking the source code, or simply with > 92 | echo g:context#commentstring#table 93 | < 94 | 95 | This variable is of type |Dictionary|, and you can modify it to your will. You 96 | can add values or overwrite existing ones. If you think that some missing 97 | values are useful to you, contact the author so maybe others can benefit as 98 | well. 99 | 100 | The keys of the |Dictionary| variable are the |file-type| where you want the 101 | 'commentstring' to change, and the values are nested |Dictionaries| that have 102 | pairs of syntax groups and values for 'commentstring' as keys and values 103 | respectively. 104 | 105 | You will need to know the names of the syntax group. For that, you can use the 106 | |zS| command provided by |scriptease|, or use this simple but convenient 107 | snippet of code: > 108 | augroup temporary_test 109 | autocmd CursorMoved 110 | \ echo map(synstack(line('.'), col('.')), 111 | \ 'synIDattr(v:val, "name")') 112 | < 113 | 114 | That will echo to the |Command-line| an array of the values of the current 115 | syntax groups. When you are done with your testing, remove the echo doing: > 116 | autocmd! 117 | augroup END 118 | < 119 | 120 | The 'comments' setting can be configured the exact same way as 'commentstring' 121 | but the variable to change is > 122 | g:context#commentstring#comments_table 123 | 124 | 125 | ============================================================================== 126 | BUGS *context-commentstring-bugs* 127 | 128 | At the moment, there is only one known issue: it doesn't work properly at the 129 | edges of the embedded language, where the boundary is. Is unlikely that this 130 | will have a solution, and is an uncommon case anyway. Try to move the cursor 131 | one extra line further. 132 | 133 | Feel free to contact the author through Gitorious or GitHub: 134 | https://gitorious.org/vim-for-qt-kde/vim-context-commentstring 135 | https://github.com/suy/vim-context-commentstring 136 | 137 | ============================================================================== 138 | CREDITS AND CONSIDERATIONS *context-commentstring-credits* 139 | 140 | This plugin was created almost solely to be used as a sidekick for 141 | |commentary.txt|, by Tim Pope. And I only had the guts to attempt to implement 142 | it because I could look at the code of |scriptease.txt| (also by Tim Pope), 143 | where the names of the syntax groups are provided by |zS|. That was almost 144 | half the work, and to my taste the more boring one. So kudos to him. If you 145 | don't know his work yet, be sure to check it out. Closely tied with Kana 146 | Natsuno and Shougo Matsushita, is probably the most brilliant and active Vim 147 | plugin author. 148 | 149 | ============================================================================== 150 | CHANGELOG *context-commentstring-changelog* 151 | 152 | 0.1.1 2014-06-06 153 | - Clear the autocommand always, in case the filetype is changed for 154 | whatever reason. 155 | 156 | 0.1.0 2013-06-06 157 | - First public release. 158 | 159 | ============================================================================== 160 | vim:tw=80:ts=8:ft=help:norl:fen:fdl=0:fdm=marker: 161 | --------------------------------------------------------------------------------