Skip to content

Commit

Permalink
IllegalArgumentException in JDTUtils.getPackageName
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza authored and fbricon committed Oct 2, 2017
1 parent 5fb9d1e commit d70d4bf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static String getPackageName(IJavaProject javaProject, URI uri) {
if (content.isEmpty() && javaProject != null && ProjectsManager.DEFAULT_PROJECT_NAME.equals(javaProject.getProject().getName())) {
java.nio.file.Path path = Paths.get(uri);
java.nio.file.Path parent = path;
while (parent.getParent() != null) {
while (parent.getParent() != null && parent.getParent().getNameCount() > 0) {
parent = parent.getParent();
String name = parent.getName(parent.getNameCount() - 1).toString();
if (SRC.equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
Expand Down Expand Up @@ -74,8 +75,11 @@ private List<CompletionItem> computeContentAssist(ICompilationUnit unit, int lin
collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);

if (offset >-1 && !monitor.isCanceled()) {
unit.codeComplete(offset, collector, monitor);
proposals.addAll(collector.getCompletionItems());
IBuffer buffer = unit.getBuffer();
if (buffer != null && buffer.getLength() >= offset) {
unit.codeComplete(offset, collector, monitor);
proposals.addAll(collector.getCompletionItems());
}
}

return proposals;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public void testGetPackageNameURI() throws Exception {
assertEquals("java", packageName);
}

@Test
public void testGetPackageNameNoSrc() throws Exception {
URI uri = Paths.get("projects", "eclipse", "hello", "Foo.java").toUri();
ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri.toString());
String packageName = JDTUtils.getPackageName(cu.getJavaProject(), uri);
assertEquals("", packageName);
}

@Test
public void testResolveStandaloneCompilationUnit() throws CoreException {
Path helloSrcRoot = Paths.get("projects", "eclipse", "hello", "src").toAbsolutePath();
Expand Down

0 comments on commit d70d4bf

Please sign in to comment.