From 4591084bd038cdb3f6e3542af810b568f1e538e7 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Mon, 12 Mar 2018 12:26:43 -0600 Subject: Map F4 to Generate Django Choices --- plugin/vim-options.vim | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'plugin') diff --git a/plugin/vim-options.vim b/plugin/vim-options.vim index f9e98ea..addfdc7 100644 --- a/plugin/vim-options.vim +++ b/plugin/vim-options.vim @@ -213,6 +213,30 @@ nnoremap g2 V:s/-/ /g:noh nnoremap g3 ^v$:s/\%V /-/g:noh nnoremap g4 V:s/ /_/g:noh nnoremap g5 ^v$:s/\%V /_/g:noh: +function! WordsToChoices() + python3 << EOF +import vim +buf = vim.current.buffer +(lnum1, col1) = buf.mark('<') +(lnum2, col2) = buf.mark('>') +lspaces = len(buf[lnum1]) - len(buf[lnum1].lstrip(' ')) +start_choices = lnum1 + (lnum2 - lnum1) +end_choices = lnum2 + (lnum2 - lnum1) +end_line = end_choices + 1 +choices_and_values = [] +for line_num in range(lnum1-1, lnum2): + word = buf[line_num].strip() + line = buf[line_num] + buf[line_num] = "{} = '{}'".format(line.upper(), word) + choices_and_values.append((word.upper(), "'{}'".format(word))) +buf[lnum2] = '{}TEST_CHOICES = ('.format(' ' * lspaces) +for line_num in range(start_choices + 1, end_choices + 2): + choice, value = choices_and_values.pop(0) + buf[line_num] = "{}({}, ({})),".format(' ' * (lspaces + 4), choice, value) +buf[end_choices + 2] = '{})'.format(' ' * lspaces) +EOF +endfunction +nnoremap `>op`]o`<:call WordsToChoices() "----------------------------------------------------------------------------------------------------------------------- -- cgit v1.2.3