glyph / python-docstring-mode

Emacs minor-mode for editing Python docstrings.
MIT License
70 stars 22 forks source link

support for vim #19

Closed glyph closed 2 years ago

glyph commented 8 years ago

The meat of the logic here lives in a Python script (in fact this was originally for use with Sublime Text), so it should be pretty straightforward to make it work for vim.

Something like this should work:

function! WrapDocstring()
    let l:where = getpos(".")
    ?"""?
    let l:indent = indent(".")
    let l:there = getpos(".")
    call setpos(".", l:where)
    exe "?\"\"\"?1,/\"\"\"/-1!python .../docstring_wrap.py --linewise --indent=" . l:indent
    nohl
endfunction

nnoremap gqaa :call WrapDocstring()<CR>

but it would be nice to get the cursor-position-preserving stuff in there, as well as correctly locating the relevant script.

glyph commented 8 years ago

other things that would be nice to have:

glyph commented 8 years ago

On the syntax highlighting front, maybe some hints will help jump-start it. I found this in a hacked-up python.vim file for the syntax highlighting part; first, this modification to pythonStrFormat:

  syn match pythonStrFormat "{\([a-zA-Z_][a-zA-Z0-9_]*\|\d\+\)\(\.[a-zA-Z_][a-zA-Z0-9_]*\|\[\(\d\+\|[^!:\}]\+\)\]\)*\(![rs]\)\=\(:\({\([a-zA-Z_][a-zA-Z0-9_]*\|\d\+\)}\|\([^}]\=[<>=^]\)\=[ +-]\=#\=0\=\d*\(\.\d\+\)\=[bcdeEfFgGnoxX%]\=\)\=\)\=}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString

and then:

syn match pythonEpytextInline "[ICBMULXEG]{.\{-}}" contained containedin=pythonString,pythonUniString,pythonRawString,pythonUniRawString

syn region pythonEpytextError   start="^\s*@" end=":" contained containedin=pythonString
syn match pythonEpytextField "^\s*@\(param\|parameter\|arg\|argument\|type\|keyword\|kwarg\|kwparam\|raise\|raises\|except\|exception\|ivar\|ivariable\|cvar\|cvariable\|var\|variable\|type\|group\|todo\|newfield\)\s\+[a-zA-Z_][a-zA-Z0-9_]*:" contained containedin=pythonString
syn match pythonEpytextField "^\s*@\(raise\|raises\|return\|returns\|rtype\|returntype\|type\|sort\|see\|seealso\|note\|attention\|bug\|warning\|warn\|version\|todo\|deprecated\|since\|status\|change\|changed\|permission\|requires\|require\|requirement\|precondition\|precond\|postcondition\|postcod\|invariant\|author\|organization\|org\|copyright\|(c)\|license\|contact\|summary\):" contained containedin=pythonString

  HiLink pythonStrTemplate      Special
  HiLink pythonEpytextField     Special
  HiLink pythonEpytextInline        Special
  HiLink pythonEpytextError     Error

I have no idea how to do this without just chopping up an existing python syntax file though. I guess maybe some of these could be run after the fact, as plugins in a command? Hopefully a more expert vim user will school me.