Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions autoload/vimpythondocstring.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,32 @@ sys.path[0:0] = deps
import pydocstring
EOF

function! s:handle_error(exception)
echohl ErrorMsg
echo join(map(split(a:exception, ":")[2:], 'trim(v:val)'), " : ")
echohl None
endfunction

function! vimpythondocstring#Full()
python3 pydocstring.Docstring().full_docstring()
try
python3 pydocstring.Docstring().full_docstring()
catch
call s:handle_error(v:exception)
endtry
endfunction

function! vimpythondocstring#FullTypes()
python3 pydocstring.Docstring().full_docstring(print_hints=True)
try
python3 pydocstring.Docstring().full_docstring(print_hints=True)
catch
call s:handle_error(v:exception)
endtry
endfunction

function! vimpythondocstring#Oneline()
python3 pydocstring.Docstring().oneline_docstring()
try
python3 pydocstring.Docstring().oneline_docstring()
catch
call s:handle_error(v:exception)
endtry
endfunction
22 changes: 12 additions & 10 deletions python/pydocstring.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#!/usr/bin/env python3
from string import Template
import re
import os
import ast
import abc
import ast
import os
import re
from string import Template

import ibis

from asthelper import ClassInstanceNameExtractor, ClassVisitor, MethodVisitor
from utils import *
from vimenv import *
from asthelper import ClassVisitor, MethodVisitor, ClassInstanceNameExtractor


class InvalidSyntax(Exception):
Expand Down Expand Up @@ -256,7 +255,10 @@ def __init__(self):

def _controller_factory(self, env, templater):
line = env.current_line
first_word = re.match(r"^\s*(\w+).*", line).groups()[0]
try:
first_word = re.match(r"^\s*(\w+).*", line).groups()[0]
except Exception:
first_word = None
if first_word == "def":
return MethodController(env, templater)
elif first_word == "class":
Expand All @@ -268,18 +270,18 @@ def _controller_factory(self, env, templater):
if second_word == "def":
return MethodController(env, templater)

raise DocstringUnavailable("Docstring cannot be created for selected object")
raise DocstringUnavailable("Docstring ERROR: Doctring cannot be created for selected object")

def full_docstring(self, print_hints=False):
"""Writes docstring containing arguments, returns, raises, ..."""
try:
self.obj_controller.write_docstring(print_hints=print_hints)
except Exception as e:
print(concat_("Doctring ERROR: ", e))
raise DocstringUnavailable(concat_("Docstring ERROR: ", e))

def oneline_docstring(self):
"""Writes only a one-line empty docstring"""
try:
self.obj_controller.write_simple_docstring()
except Exception as e:
print(concat_("Doctring ERROR: ", e))
raise DocstringUnavailable(concat_("Docstring ERROR: ", e))