-
Notifications
You must be signed in to change notification settings - Fork 408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completing /** should generate Javadoc with params #744
Conversation
@snjeza I haven't checked yet but is there a way for the server, through LSP config/requests to perform what the client does in https://github.com/redhat-developer/vscode-java/blob/1f6783957c699e261a33d05702f2da356017458d/src/extension.ts#L263-L303? Because your solution is to generate code completion items, which gives a different UX than the expected autocompletion |
The feature I have created works in a similar way as the VS Code typescript feature does. |
Of course it can't be implemented on the client side, I just wondered whether we could move that part (API implementation) currently on the client side to be processed in the backend. |
// ignore | ||
} | ||
final CompletionItem ci = new CompletionItem(); | ||
ci.setInsertText(buf.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insertText is deprecated, you should use a TextEdit.
Also, according to the spec, for completionItem textEdits:
The range of the edit must be a single line range and it must contain the position at which completion has been requested
That means each extra line must be added by an additional textedit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the range must be a single line range.
import org.eclipse.lsp4j.CompletionItemKind; | ||
import org.eclipse.lsp4j.InsertTextFormat; | ||
|
||
public class JavaDocCompletionProposal { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few eclipse.jface.text interfaces that would need to move to eclipse.text. There is a reference to StubUtility and CodeGeneration which is still in the works by Roland. There are two methods (isSmartMode() and getCompilationUnit()) that drag in all the eclipse.ui stuff such as PlatformUI and IEditorPart. The class could be split into core and non-core and the smartMode setting plus ICompilationUnit could be set for core so as to avoid eclipse.ui. There are also a few Preferences to reference/set like we have done before (e.g. Member sort order).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a lot of changes due to which, I think, we won't be able to use the same code in jdt.ls and jdt.ui
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -319,6 +320,30 @@ public void testCompletion_import_package() throws JavaModelException{ | |||
//Not checking the range end character | |||
} | |||
|
|||
@Test | |||
public void testCompletion_javadocComment() throws JavaModelException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add some more tests for:
- already existing javadoc
- partially existing javadoc (has only 1
@param
doc'ed) - no completion after regular comment /* */
- no completion for no-param methods
ci.setSortText(SortTextHelper.convertRelevance(0)); | ||
ci.setKind(CompletionItemKind.Snippet); | ||
ci.setInsertTextFormat(InsertTextFormat.Snippet); | ||
ci.setDocumentation(ci.getInsertText()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
example documentation returned: "documentation": " \n * @param foo\n * @param bar\n"
Leading empty lines should be trimmed from documentation
@@ -0,0 +1,235 @@ | |||
/******************************************************************************* | |||
* Copyright (c) 2016-2017 Red Hat Inc. and others. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Original copyright should be retained
* Based on org.eclipse.jdt.internal.ui.text.javadoc.JavaDocAutoIndentStrategy | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
} | ||
List<CompletionItem> result = new ArrayList<>(); | ||
IDocument d = JsonRpcHelpers.toDocument(cu.getBuffer()); | ||
if (offset == -1 || d.getLength() == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
offset < 0
ci.setSortText(SortTextHelper.convertRelevance(0)); | ||
ci.setKind(CompletionItemKind.Snippet); | ||
ci.setInsertTextFormat(InsertTextFormat.Snippet); | ||
String documentation = buf.toString().replaceFirst("\\n", ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, this leads to an ugly indentation. 1st empty lines should be removed
All lines should be trimmed on the right. |
ci.setLabel(JAVA_DOC_COMMENT); | ||
ci.setSortText(SortTextHelper.convertRelevance(0)); | ||
ci.setKind(CompletionItemKind.Snippet); | ||
ci.setInsertTextFormat(InsertTextFormat.Snippet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CompletionItemKind.Snippet is fine, but I don't think InsertTextFormat.Snippet is appropriate here, since there will be no placeholders. Clients not supporting Snippets might puke when receiving those items. We should send InsertTextFormat.PlainText
} | ||
final CompletionItem ci = new CompletionItem(); | ||
Range range = JDTUtils.toRange(unit, offset, 0); | ||
ci.setTextEdit(new TextEdit(range, buf.toString())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textedit should also get the rtrim treatment here.
buf.append(rtrim(line)); | ||
buf.append(lineDelimiter); | ||
} | ||
return buf.toString().replaceFirst(lineDelimiter, ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only if lineDelimiter position is 0
assertEquals(CompletionItemKind.Snippet, item.getKind()); | ||
assertEquals("999999999", item.getSortText()); | ||
assertNotNull(item.getTextEdit()); | ||
assertEquals(" \n * @param i\n * @param s\n", item.getTextEdit().getNewText()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new text should be "\n * @param i\n * @param s\n"
(no leading spaces)
a2d9b34
to
4ac94d1
Compare
|
||
public class JavaDocCompletionProposal { | ||
|
||
public static final String JAVA_DOC_COMMENT = "JavaDoc comment"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/JavaDoc/Javadoc/g
no camelcase in Javadoc
return result; | ||
} | ||
|
||
private String rtrim(String text) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is implemented is a ltrim, i.e. whitespaces on the left are removed. We want to remove the trailing spaces, those on the right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringUtils.stripEnd(text, null) should work
cd62ae9
to
50807c7
Compare
03e485f
to
0177969
Compare
So the new implementation now adds an extra empty comment line before the parameters :-/ But if no snippet is supported, then we shouldn't add that extra 1st empty line |
Signed-off-by: Snjezana Peco <[email protected]>
test this please |
@fbricon I have updated the PR. |
Works really nicely now! Nice job @snjeza |
Fixes redhat-developer/vscode-java#228
Signed-off-by: Snjezana Peco [email protected]