From c1fa0973c8f2a3a15b3a7ca60502ee057db6a631 Mon Sep 17 00:00:00 2001 From: Fabio Zadrozny Date: Wed, 9 Aug 2023 14:08:20 -0300 Subject: [PATCH] Fixed issue where 'self' could be added to the arguments when auto-generating docstrings. --- .../pydev/core/docutils/PySelection.java | 21 ++++---- .../docstrings/AssistDocString.java | 3 +- .../correctionassist/AssistDocStringTest.java | 53 +++++++++++++++++++ 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java b/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java index b6a7af356a..90adbbaa7f 100644 --- a/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java +++ b/plugins/org.python.pydev.core/src/org/python/pydev/core/docutils/PySelection.java @@ -513,7 +513,7 @@ public Tuple, Integer> getInsideParentesisToks(boolean addSelf, int for (int i = 0; i < len; i++) { char c = insideParentesisTok.charAt(i); if (c == ',') { - addTrimmedBufToList(buf, params); + addTrimmedBufToList(buf, params, addSelf); buf.clear(); } else { switch (c) { @@ -537,15 +537,13 @@ public Tuple, Integer> getInsideParentesisToks(boolean addSelf, int } } } - addTrimmedBufToList(buf, params); + addTrimmedBufToList(buf, params, addSelf); } else { boolean shouldAppendBuf = true; for (int i = 0; i < len; i++) { char c = insideParentesisTok.charAt(i); if (c == ',') { - if (addSelf || !"self".equals(buf.toString().trim())) { - addTrimmedBufToList(buf, params); - } + addTrimmedBufToList(buf, params, addSelf); buf.clear(); shouldAppendBuf = true; } else { @@ -572,7 +570,7 @@ public Tuple, Integer> getInsideParentesisToks(boolean addSelf, int } } } - addTrimmedBufToList(buf, params); + addTrimmedBufToList(buf, params, addSelf); } } catch (SyntaxErrorException e) { throw new RuntimeException(e); @@ -581,11 +579,14 @@ public Tuple, Integer> getInsideParentesisToks(boolean addSelf, int return new Tuple, Integer>(params, j); } - private static final void addTrimmedBufToList(FastStringBuffer buf, List list) { - String trimmed = buf.toString().trim(); - if (trimmed.length() > 0) { - list.add(trimmed); + private static final void addTrimmedBufToList(FastStringBuffer buf, List list, boolean addSelf) { + if (addSelf || !"self".equals(buf.toString().trim())) { + String trimmed = buf.toString().trim(); + if (trimmed.length() > 0) { + list.add(trimmed); + } } + } public static final String[] TOKENS_BEFORE_ELSE = new String[] { "if", "for", "except", "while", "elif" }; diff --git a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/docstrings/AssistDocString.java b/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/docstrings/AssistDocString.java index 0e0046f322..f787cc0331 100644 --- a/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/docstrings/AssistDocString.java +++ b/plugins/org.python.pydev/src/org/python/pydev/editor/correctionassist/docstrings/AssistDocString.java @@ -110,7 +110,8 @@ public List getProps(PySelection ps, IImageCache imag } final String preferredDocstringStyle2 = preferredDocstringStyle; - if (preferredDocstringStyle.equals(DocstringsPrefPage.DOCSTRINGSTYLE_GOOGLE)) { + if (inFunctionLine && params.size() > 0 + && preferredDocstringStyle.equals(DocstringsPrefPage.DOCSTRINGSTYLE_GOOGLE)) { buf.append("Args:"); } l.add(CompletionProposalFactory.get().createAssistDocstringCompletionProposal("", offsetPosToAdd, 0, diff --git a/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/AssistDocStringTest.java b/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/AssistDocStringTest.java index 291470a9ed..656e50d60a 100644 --- a/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/AssistDocStringTest.java +++ b/plugins/org.python.pydev/tests/org/python/pydev/editor/correctionassist/AssistDocStringTest.java @@ -43,12 +43,14 @@ public static void main(String[] args) { protected void setUp() throws Exception { super.setUp(); assist = new AssistDocString(); + DocstringsPrefPage.GENERATE_TYPE_DOCSTRING_ON_TESTS = true; CompletionProposalFactory.set(new DefaultCompletionProposalFactory()); } @Override protected void tearDown() throws Exception { super.tearDown(); + DocstringsPrefPage.GENERATE_TYPE_DOCSTRING_ON_TESTS = true; CompletionProposalFactory.set(null); } @@ -109,6 +111,35 @@ public TestEntry(String declaration, boolean expectedResult) { } } + public void testApplyGoogle() throws Exception { + String expected; + expected = "def foo(a): #comment\r\n" + + " '''\r\n" + + " Args:\r\n" + + " a:\r\n" + + " '''"; + checkGoogle(expected, "def foo(a): #comment", 1); + } + + public void testApplyGoogle1() throws Exception { + String expected; + expected = " def foo(a): #comment\r\n" + + " '''\r\n" + + " Args:\r\n" + + " a:\r\n" + + " '''"; + checkGoogle(expected, " def foo(a): #comment", 1); + } + + public void testApplyGoogle2() throws Exception { + String expected; + expected = " def foo(self): #comment\r\n" + + " '''\r\n" + + " \r\n" + + " '''"; + checkGoogle(expected, " def foo(self): #comment", 1); + } + public void testApply() throws Exception { String expected; expected = "def foo(a): #comment\r\n" + @@ -241,6 +272,28 @@ private void check(String expected, String initial, int proposals) throws BadLoc } } + private void checkGoogle(String expected, String initial, int proposals) throws BadLocationException { + Document doc = new Document(initial); + PySelection ps = new PySelection(doc, 0, 0); + AssistDocString assist = new AssistDocString("G"); + DocstringsPrefPage.GENERATE_TYPE_DOCSTRING_ON_TESTS = false; + List props = assist.getProps(ps, null, null, null, null, + ps.getAbsoluteCursorOffset()); + assertEquals(proposals, props.size()); + if (props.size() > 0) { + props.get(0).apply(doc); + String expect = StringUtils.replaceNewLines(expected, "\n"); + String obtained = StringUtils.replaceNewLines(doc.get(), "\n"); + if (!expect.equals(obtained)) { + System.out.println("====Expected===="); + System.out.println(expect); + System.out.println("====Obtained===="); + System.out.println(obtained); + assertEquals(expect, obtained); + } + } + } + public void testUpdateDocstring() { assertEquals("'''\n" + " test\n"