From d5c25174736e9253212132f6e4cbbbdff0fe09b0 Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Wed, 11 Dec 2024 19:42:16 +0100 Subject: [PATCH 1/2] Allow docstring to use single quotes and to not be displayed By specifying None for the docstring no docstring is displayed. By specifying the quotation mark in the docstring the docstring is not put into triple quotes allowing a docstring in signle quotes. The implementation puts the responsibility to add the correct quotation marks to the python side so we can be more flexible. The line numbers are now counted from the function body if the docstring is empty. --- js/widget.js | 13 +++++++------ src/widget_code_input/__init__.py | 19 ++++++++++++++----- src/widget_code_input/utils.py | 5 ++--- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/js/widget.js b/js/widget.js index 0a4bafa..fca6ff0 100644 --- a/js/widget.js +++ b/js/widget.js @@ -205,18 +205,19 @@ function signatureValueChanged() { function updateLineNumbers() { const linesSignature = mySignatureCodeMirror.state.doc.toString().split('\n').length; - const linesDocstring = myDocstringCodeMirror.state.doc.toString().split('\n').length; + const docstring = myDocstringCodeMirror.state.doc.toString(); + const linesDocstring = docstring == "" ? 0 : docstring.split('\n').length; // increment line numbers in docstring text area by the number of lines in the signature myDocstringCodeMirror.dispatch({ - effects: guttercomp.reconfigure( - lineNumbers({ formatNumber: n=>linesSignature+n })) + effects: guttercomp.reconfigure( + lineNumbers({ formatNumber: n=> linesDocstring == 0 ? "" : linesSignature+n })) }); myBodyCodeMirror.dispatch({ - effects: guttercomp.reconfigure( - lineNumbers({ formatNumber: n=>linesSignature+linesDocstring+n})) + effects: guttercomp.reconfigure( + lineNumbers({ formatNumber: n=>linesSignature+linesDocstring+n})) }); } @@ -225,7 +226,7 @@ function signatureValueChanged() { // Set the value from python into the CodeMirror widget in the // frontend. - const newDocstring = '"""' + model.get('docstring') + '"""'; + const newDocstring = model.get('docstring'); if (newDocstring !== myDocstringCodeMirror.state.doc.toString()) { myDocstringCodeMirror.dispatch({ diff --git a/src/widget_code_input/__init__.py b/src/widget_code_input/__init__.py index f1458f5..fb88249 100644 --- a/src/widget_code_input/__init__.py +++ b/src/widget_code_input/__init__.py @@ -62,8 +62,6 @@ def _valid_docstring(self, docstring): """ Validate that the docstring do not contain triple double quotes """ - if '"""' in docstring['value']: - raise TraitError('The docstring cannot contain triple double quotes (""")') return docstring['value'] @@ -73,7 +71,7 @@ def __init__( # pylint: disable=too-many-arguments self, function_name, function_parameters="", - docstring="\n", + docstring=None, function_body="", code_theme="basicLight", ): @@ -94,7 +92,18 @@ def __init__( # pylint: disable=too-many-arguments self.function_name = function_name self.function_parameters = function_parameters - self.docstring = docstring + if docstring is None: + # we cannot store docstring as None so we use + # a variable to signify that it was None + self.docstring = "" + self._display_docstring = False + elif docstring.startswith("\"") and docstring.endswith("\""): + # assume the quotation marks have been added so we do not need to add them + self.docstring = docstring + self._display_docstring = True + else: + self.docstring = f"\"\"\"{docstring}\"\"\"" + self._display_docstring = True self.function_body = function_body self.code_theme = code_theme self.widget_instance_count_trait=f"{WidgetCodeInput.widget_instance_count}" @@ -114,7 +123,7 @@ def full_function_code(self): including signature, docstring and body """ return build_function( - self.function_signature, self.docstring, self.function_body + self.function_signature, self.docstring if self._display_docstring else None, self.function_body ) @property diff --git a/src/widget_code_input/utils.py b/src/widget_code_input/utils.py index 1189de7..6670075 100644 --- a/src/widget_code_input/utils.py +++ b/src/widget_code_input/utils.py @@ -47,12 +47,11 @@ def build_pre_body(signature, docstring, indent_level=4): :param docstring: the (unindented) docstring :param indent_level: integer number of spaces to prepend to the docstring """ - if '"""' in docstring: - raise ValueError('Triple double quotes (""") not allowed in docstring') return "{}\n{}".format( signature, - prepend_indent('"""{}"""'.format(docstring), indent_level=indent_level), + prepend_indent('' if docstring is None else '"""{}"""'.format(docstring), + indent_level=indent_level), ) From 90f731ca929725ef3fcad3da7bcb3e3672d9eefd Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Fri, 13 Dec 2024 10:22:09 +0100 Subject: [PATCH 2/2] fragments of rebasing --- src/widget_code_input/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widget_code_input/utils.py b/src/widget_code_input/utils.py index 6670075..6af837c 100644 --- a/src/widget_code_input/utils.py +++ b/src/widget_code_input/utils.py @@ -50,7 +50,7 @@ def build_pre_body(signature, docstring, indent_level=4): return "{}\n{}".format( signature, - prepend_indent('' if docstring is None else '"""{}"""'.format(docstring), + prepend_indent('' if docstring is None else docstring, indent_level=indent_level), )