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

Add command to list Java projects #1238

Merged
merged 3 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions java-extension/com.microsoft.java.test.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<command id="vscode.java.test.get.testpath" />
<command id="vscode.java.test.junit.argument" />
<command id="vscode.java.test.generateTests" />
<command id="vscode.java.test.findJavaProjects" />
</delegateCommandHandler>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class TestDelegateCommandHandler implements IDelegateCommandHandler {
private static final String GET_TEST_SOURCE_PATH = "vscode.java.test.get.testpath";
private static final String RESOLVE_JUNIT_ARGUMENT = "vscode.java.test.junit.argument";
private static final String GENERATE_TESTS = "vscode.java.test.generateTests";
private static final String FIND_JAVA_PROJECT = "vscode.java.test.findJavaProjects";

@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor monitor) throws Exception {
Expand All @@ -42,6 +43,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
return JUnitLaunchUtils.resolveLaunchArgument(arguments, monitor);
case GENERATE_TESTS:
return TestGenerationUtils.generateTests(arguments, monitor);
case FIND_JAVA_PROJECT:
return TestSearchUtils.findJavaProjects(arguments, monitor);
default:
throw new UnsupportedOperationException(
String.format("Java test plugin doesn't support the command '%s'.", commandId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*******************************************************************************
* Copyright (c) 2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/

package com.microsoft.java.test.plugin.model;

import org.eclipse.lsp4j.Range;

import java.util.ArrayList;
import java.util.List;

public class JavaTestItem {
private String id;

private String label;

private String fullName;

private List<JavaTestItem> children;

private TestLevel testLevel;

private TestKind testKind;

private String projectName;

private String uri;

private Range range;

private String jdtHandler;

public JavaTestItem() {}

public JavaTestItem(String displayName, String fullName, String project, String uri,
Range range, TestLevel level, TestKind kind) {
this.label = displayName;
this.fullName = fullName;
this.testLevel = level;
this.testKind = kind;
this.projectName = project;
this.uri = uri;
this.range = range;

if (level.equals(TestLevel.PROJECT)) {
this.id = fullName;
} else {
this.id = project + "@" + fullName;
}
}

public Range getRange() {
return range;
}

public void setRange(Range range) {
this.range = range;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}

public String getJdtHandler() {
return jdtHandler;
}

public void setJdtHandler(String jdtHandler) {
this.jdtHandler = jdtHandler;
}

public String getId() {
return id;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public List<JavaTestItem> getChildren() {
return children;
}

public void setChildren(List<JavaTestItem> children) {
this.children = children;
}

public TestLevel getTestLevel() {
return testLevel;
}

public void setTestLevel(TestLevel testLevel) {
this.testLevel = testLevel;
}

public TestKind getTestKind() {
return testKind;
}

public void setTestKind(TestKind testKind) {
this.testKind = testKind;
}

public String getProjectName() {
return projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

public void addChild(JavaTestItem child) {
if (this.children == null) {
this.children = new ArrayList<>();
}
this.children.add(child);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null) {
return false;
}

if (getClass() != obj.getClass()) {
return false;
}
final JavaTestItem other = (JavaTestItem) obj;
if (id == null) {
if (other.id != null) {

return false;
}
} else if (!id.equals(other.id)){
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -18,14 +18,41 @@ public enum TestLevel {
ROOT,

@SerializedName("1")
FOLDER,
WORKSPACE,

@SerializedName("2")
PACKAGE,
WORKSPACE_FOLDER,

@SerializedName("3")
CLASS,
PROJECT,

@SerializedName("4")
PACKAGE,

@SerializedName("5")
CLASS,

@SerializedName("6")
METHOD;

public static TestLevel fromInteger(Integer i) {
switch(i) {
case 0:
return ROOT;
case 1:
return WORKSPACE;
case 2:
return WORKSPACE_FOLDER;
case 3:
return PROJECT;
case 4:
return PACKAGE;
case 5:
return CLASS;
case 6:
return METHOD;
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Microsoft Corporation and others.
* Copyright (c) 2018-2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,7 +11,7 @@

package com.microsoft.java.test.plugin.util;

import com.microsoft.java.test.plugin.model.TestItem;
import com.microsoft.java.test.plugin.model.JavaTestItem;
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;

Expand All @@ -22,47 +22,53 @@
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.hover.JavaElementLabels;
import org.eclipse.lsp4j.Range;

@SuppressWarnings("restriction")
public class TestItemUtils {

public static final String DEFAULT_PACKAGE_NAME = "<Default Package>";

public static TestItem constructTestItem(IJavaElement element, TestLevel level) throws JavaModelException {
return constructTestItem(element, level, null);
}

public static TestItem constructTestItem(IJavaElement element, TestLevel level, TestKind kind)
public static JavaTestItem constructJavaTestItem(IJavaElement element, TestLevel level, TestKind kind)
throws JavaModelException {
final String displayName;
final String fullName;
if (element instanceof IPackageFragment && ((IPackageFragment) element).isDefaultPackage()) {
displayName = DEFAULT_PACKAGE_NAME;
fullName = DEFAULT_PACKAGE_NAME;
fullName = "";
Copy link
Member

Choose a reason for hiding this comment

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

is empty string working?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes default package's element name is empty.

While here I should get the fullName in just one call, no need to handle separately, will update it.

} else {
displayName = element.getElementName();
fullName = parseTestItemFullName(element, level);
displayName = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
fullName = parseFullName(element, level);
}
final String uri = JDTUtils.getFileURI(element.getResource());
final Range range = parseTestItemRange(element);
Range range = null;
if (level == TestLevel.CLASS || level == TestLevel.METHOD) {
range = parseTestItemRange(element);
}

final String projectName = element.getJavaProject().getProject().getName();

return new TestItem(displayName, fullName, uri, projectName, range, level, kind);
final JavaTestItem result = new JavaTestItem(displayName, fullName, projectName, uri, range, level, kind);
result.setJdtHandler(element.getHandleIdentifier());
return result;
}

public static Range parseTestItemRange(IJavaElement element) throws JavaModelException {
if (element instanceof ISourceReference) {
// getSourceRange() is not used here because we want the Code Lens appear above the
// method name, not above the annotation.
final ISourceRange range = ((ISourceReference) element).getNameRange();
return JDTUtils.toRange(element.getOpenable(), range.getOffset(), range.getLength());
final ISourceRange sourceRange = ((ISourceReference) element).getSourceRange();
final ISourceRange nameRange = ((ISourceReference) element).getNameRange();
if (SourceRange.isAvailable(sourceRange) && SourceRange.isAvailable(nameRange)) {
return JDTUtils.toRange(element.getOpenable(), nameRange.getOffset(),
sourceRange.getLength() - nameRange.getOffset() + sourceRange.getOffset());
Copy link
Member

Choose a reason for hiding this comment

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

sourceRange.getLength() - nameRange.getOffset() + sourceRange.getOffset() equals to nameRange.getLength()?

can you briefly explain?

Copy link
Member Author

Choose a reason for hiding this comment

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

sourceRange contains JavaDocs, so (nameRange.getOffset() - sourceRange.getOffset()) equals to the length of JavaDoc, I'll add a comment for it.

}
}
return new Range();
return null;
}

public static String parseTestItemFullName(IJavaElement element, TestLevel level) {
public static String parseFullName(IJavaElement element, TestLevel level) {
switch (level) {
case CLASS:
final IType type = (IType) element;
Expand Down
Loading