Skip to content

Commit

Permalink
Fixed issue where 'self' could be added to the arguments when auto-ge…
Browse files Browse the repository at this point in the history
…nerating docstrings.
  • Loading branch information
fabioz committed Aug 9, 2023
1 parent f4de9e0 commit c1fa097
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ public Tuple<List<String>, 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) {
Expand All @@ -537,15 +537,13 @@ public Tuple<List<String>, 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 {
Expand All @@ -572,7 +570,7 @@ public Tuple<List<String>, Integer> getInsideParentesisToks(boolean addSelf, int
}
}
}
addTrimmedBufToList(buf, params);
addTrimmedBufToList(buf, params, addSelf);
}
} catch (SyntaxErrorException e) {
throw new RuntimeException(e);
Expand All @@ -581,11 +579,14 @@ public Tuple<List<String>, Integer> getInsideParentesisToks(boolean addSelf, int
return new Tuple<List<String>, Integer>(params, j);
}

private static final void addTrimmedBufToList(FastStringBuffer buf, List<String> list) {
String trimmed = buf.toString().trim();
if (trimmed.length() > 0) {
list.add(trimmed);
private static final void addTrimmedBufToList(FastStringBuffer buf, List<String> 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" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public List<ICompletionProposalHandle> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -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<ICompletionProposalHandle> 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"
Expand Down

0 comments on commit c1fa097

Please sign in to comment.