Skip to content
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

Intellisense does not work when attached javadoc can't be read #1314

Merged
merged 1 commit into from
Dec 31, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.handlers.CompletionResolveHandler;
Expand Down Expand Up @@ -137,7 +138,14 @@ private StringBuilder appendUnboundedParameterList(StringBuilder buffer, Complet
// TODO remove once https://bugs.eclipse.org/bugs/show_bug.cgi?id=85293
// gets fixed.
char[] signature= SignatureUtil.fix83600(methodProposal.getSignature());
char[][] parameterNames= methodProposal.findParameterNames(null);
char[][] parameterNames;
try {
parameterNames = methodProposal.findParameterNames(null);
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
parameterNames = CompletionEngine.createDefaultParameterNames(Signature.getParameterCount(signature));
methodProposal.setParameterNames(parameterNames);
}
char[][] parameterTypes= Signature.getParameterTypes(signature);

for (int i= 0; i < parameterTypes.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
import org.eclipse.jdt.ls.core.internal.ChangeUtil;
Expand Down Expand Up @@ -443,7 +444,15 @@ private void appendMethodNameReplacement(StringBuilder buffer, CompletionProposa
}

private void appendGuessingCompletion(StringBuilder buffer, CompletionProposal proposal) {
char[][] parameterNames= proposal.findParameterNames(null);
char[][] parameterNames;
try {
parameterNames = proposal.findParameterNames(null);
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
char[] signature = SignatureUtil.fix83600(proposal.getSignature());
parameterNames = CompletionEngine.createDefaultParameterNames(Signature.getParameterCount(signature));
proposal.setParameterNames(parameterNames);
}

int count= parameterNames.length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,13 @@ public List<CompletionItem> getCompletionItems() {
CompletionProposalReplacementProvider proposalProvider = new CompletionProposalReplacementProvider(unit, getContext(), response.getOffset(), preferenceManager.getClientPreferences());
for (int i = 0; i < limit; i++) {
CompletionProposal proposal = proposals.get(i);
CompletionItem item = toCompletionItem(proposal, i);
proposalProvider.updateReplacement(proposal, item, '\0');
completionItems.add(item);
try {
CompletionItem item = toCompletionItem(proposal, i);
proposalProvider.updateReplacement(proposal, item, '\0');
completionItems.add(item);
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
return completionItems;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,11 @@ public void appendMethodLabel(IMethod method, long flags) {
}
String[] names= null;
if (getFlag(flags, JavaElementLabels.M_PARAMETER_NAMES) && method.exists()) {
names= method.getParameterNames();
try {
names= method.getParameterNames();
} catch (Exception e) {
names = method.getRawParameterNames();
}
if (isPolymorphic) {
// handled specially below
} else if (types == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.internal.corext.util.MethodOverrideTester;
import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels;

/**
Expand Down Expand Up @@ -590,7 +591,12 @@ public static String getHTMLContent(IJavaElement element, boolean useAttachedJav
if (sourceJavadoc == null || sourceJavadoc.length() == 0 || sourceJavadoc.trim().equals("{@inheritDoc}")) { //$NON-NLS-1$
if (useAttachedJavadoc) {
if (element.getOpenable().getBuffer() == null) { // only if no source available
return element.getAttachedJavadoc(null);
try {
return element.getAttachedJavadoc(null);
} catch (Exception e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
return null;
}
}
IMember member = null;
if (element instanceof ILocalVariable) {
Expand Down
40 changes: 40 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/aspose/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the build, reporting and profiles sections are not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aspose</groupId>
<artifactId>aspose</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.12.0</version>
<classifier>jdk16</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.12.0</version>
<classifier>javadoc</classifier>
</dependency>
</dependencies>

<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.sample;

import java.io.File;

import com.aspose.words.Document;
public final class TestJavadoc {
public static void main(String[] args) throws Exception {
File file = File.createTempFile("test", "txt");
Document doc = new Document(file.getAbsolutePath());
doc.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.Job;
Expand Down Expand Up @@ -2563,6 +2565,28 @@ public void testCompletion_FilterTypesKeepMethods() throws JavaModelException {
}
}

@Test
public void testCompletion_InvalidJavadoc() throws Exception {
importProjects("maven/aspose");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the project folder name to be consistent with the artifact id aspose-word-test in the pom.xml?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

IProject project = null;
ICompilationUnit unit = null;
try {
project = ResourcesPlugin.getWorkspace().getRoot().getProject("aspose");
IJavaProject javaProject = JavaCore.create(project);
unit = (ICompilationUnit) javaProject.findElement(new Path("org/sample/TestJavadoc.java"));
unit.becomeWorkingCopy(null);
int[] loc = findCompletionLocation(unit, "doc.");
CompletionParams position = JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]));
CompletionList list = server.completion(position).join().getRight();
CompletionItem ci = list.getItems().stream().filter(item -> item.getLabel().equals("accept(DocumentVisitor visitor) : boolean")).findFirst().orElse(null);
assertNotNull(ci);
} finally {
if (unit != null) {
unit.discardWorkingCopy();
}
}
}

@Test
public void testCompletion_ConstantDefaultValue() throws JavaModelException {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.ls.core.internal.ClassFileUtil;
import org.eclipse.jdt.ls.core.internal.DependencyUtil;
Expand Down Expand Up @@ -190,6 +192,32 @@ public void testMissingUnit() throws Exception {
assertEquals("Should find empty hover for " + payload, "", hover.getContents().getLeft().get(0).getLeft());
}

@Test
public void testInvalidJavadoc() throws Exception {
importProjects("maven/aspose");
IProject project = null;
ICompilationUnit unit = null;
try {
project = ResourcesPlugin.getWorkspace().getRoot().getProject("aspose");
IJavaProject javaProject = JavaCore.create(project);
IType type = javaProject.findType("org.sample.TestJavadoc");
unit = type.getCompilationUnit();
unit.becomeWorkingCopy(null);
String uri = JDTUtils.toURI(unit);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uri);
TextDocumentPositionParams position = new TextDocumentPositionParams(textDocument, new Position(8, 24));
Hover hover = handler.hover(position, monitor);
assertNotNull(hover);
assertNotNull(hover.getContents());
assertEquals(1, hover.getContents().getLeft().size());
assertEquals("com.aspose.words.Document.Document(String fileName) throws Exception", hover.getContents().getLeft().get(0).getRight().getValue());
} finally {
if (unit != null) {
unit.discardWorkingCopy();
}
}
}

String createHoverRequest(String file, int line, int kar) {
URI uri = project.getFile(file).getRawLocationURI();
return createHoverRequest(uri, line, kar);
Expand Down