Last active
June 2, 2020 04:06
-
-
Save ikouchiha47/c962e5067042852e38b5c70ff4fb6e38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function! s:isEmptyLine(line) | |
| return a:line =~ '^\s*$' | |
| endfunction | |
| function! s:trim_spaces(l, r) | |
| return [trim(a:l), trim(a:r)] | |
| endfunction | |
| function! s:isCommented(line, left_literal, right_literal) | |
| let [l, r] = s:trim_spaces(a:left_literal, a:right_literal) | |
| return len(a:line) && (stridx(a:line,l) >= 0 && a:line[strlen(a:line)-strlen(r) : -1] ==# r) | |
| endfunction | |
| function! s:commentLine(line, left_literal, right_literal) | |
| let [l, r] = [a:left_literal, a:right_literal] | |
| " matches anything (.*) 0 or more times to the end of line $ | |
| " which is preceeded (\zs) | |
| " by spaces from start of string ^(\s*) | |
| return substitute(a:line, '^\(^\s*\|\s*\)\zs.*\S\@<=', '\=l.submatch(0).r' ,'') | |
| endfunction | |
| function! s:unCommentLine(line, left_literal, right_literal) | |
| " works if no space from legacu comments | |
| let [l, r] = s:trim_spaces(a:left_literal, a:right_literal) | |
| " uncommenet line | |
| return substitute(a:line, '\S.*\s\@<!', '\=submatch(0)[strlen(l):-strlen(r)-1]', '') | |
| endfunction | |
| " uses commentstring of vim | |
| " sometimes me get multiple comments from commentstring | |
| " even if its single line | |
| function! s:getCommentLiterals() | |
| let comment_formats = { 'python': '# %s', 'vim': '" %s' } | |
| let literal = get(b:, 'comment_format', &commentstring) | |
| if s:isEmptyLine(literal) | |
| let literal = get(comment_formats, &filetype, ' %s') | |
| endif | |
| " adds single spaces to either side for beauty | |
| let literal = substitute(literal, '\S\zs%s', ' %s ' ,'') | |
| return split(literal, '%s', 1) | |
| endfunction | |
| " unused for now | |
| "function! s:getLines() | |
| " let [line_start, column_start] = getpos("'<")[1:2] | |
| " let [line_end, column_end] = getpos("'>")[1:2] | |
| " let lines = getline(line_start, line_end) | |
| "endfunction | |
| function! Commentator() abort | |
| try | |
| let success = 1 | |
| " match anything thats a \S, .* upto last no white space | |
| let text = matchstr(getline('.'), '\S.*\s\@<!') | |
| let comment_literals = s:getCommentLiterals() | |
| if s:isEmptyLine(text) | |
| return | |
| endif | |
| if s:isCommented(text, comment_literals[0], comment_literals[1]) | |
| let text = s:unCommentLine(text, comment_literals[0], comment_literals[1]) | |
| else | |
| let text = s:commentLine(text, comment_literals[0], comment_literals[1]) | |
| endif | |
| finally | |
| if !exists('success') | |
| echo "error" | |
| else | |
| call setline('.', text) | |
| endif | |
| endtry | |
| endfunction | |
| nnoremap <leader>/ :call Commentator()<CR> | |
| vnoremap <leader>/ :call Commentator()<CR> | |
| inoremap <C-_> <C-O>:call Commentator()<CR> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function! InlineExec() | |
| let shell = $SHELL | |
| let shell_executor = split(shell, '/')[-1] | |
| let executors = { | |
| \'python': '!python', | |
| \'vim': 'source', | |
| \'sh': '!'.shell_executor, | |
| \'javascript': '!node', | |
| \'go': '!go run', | |
| \'ruby': '!ruby' | |
| \} | |
| let executor = get(executors, &filetype, '') | |
| if executor ==# '' | |
| echo "no executable" | |
| return | |
| endif | |
| execute ': ' . executor . ' '. bufname("%") | |
| endfunction | |
| nnoremap <buffer> <leader><CR> :call InlineExec()<CR> | |
| nnoremap <S-Up> :m-2<CR> | |
| nnoremap <S-Down> :m+<CR> | |
| inoremap <S-Up> <Esc>:m-2<CR> | |
| inoremap <S-Down> <Esc>:m+<CR> | |
| vnoremap <S-Up> :m '<-2<CR> | |
| vnoremap <S-Up> :m '>+1<CR> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment