-
Notifications
You must be signed in to change notification settings - Fork 135
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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; | ||
|
||
|
@@ -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 = ""; | ||
} 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
can you briefly explain? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sourceRange contains JavaDocs, so |
||
} | ||
} | ||
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; | ||
|
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.
is empty string working?
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.
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.