PT #151978919 Link for type and link for resource

This commit is contained in:
BoykoAlex
2018-02-09 11:48:43 -05:00
parent 5c19bdefe6
commit 09badf6c3b
4 changed files with 63 additions and 17 deletions

View File

@@ -38,18 +38,19 @@ public class LiveHoverUtils {
String newline = " \n"+indentStr; //Note: the double space before newline makes markdown see it as a real line break
String type = bean.getType(true);
// Try creating a URL link to open source for the type
Optional<String> url = SourceLinks.sourceLinkUrl(project, type);
if (url.isPresent()) {
return Renderables.link(type, url.get()).toMarkdown();
}
StringBuilder buf = new StringBuilder("Bean: ");
buf.append(bean.getId());
if (type != null) {
// Try creating a URL link to open source for the type
buf.append(newline);
buf.append("Type: `" + type + "`");
buf.append("Type: ");
Optional<String> url = SourceLinks.sourceLinkUrlForFQName(project, type);
if (url.isPresent()) {
buf.append(Renderables.link(type, url.get()).toMarkdown());
} else {
buf.append("`" + type + "`");
}
}
String resource = bean.getResource();
if (StringUtil.hasText(resource)) {

View File

@@ -31,11 +31,11 @@ import org.springframework.ide.vscode.commons.util.Log;
*/
public class SourceLinks {
private static final String JAR = ".jar";
private static final String JAVA = ".java";
private static final String CLASS = ".class";
static final String JAR = ".jar";
static final String JAVA = ".java";
static final String CLASS = ".class";
public static Optional<String> sourceLinkUrl(IJavaProject project, String fqName) {
public static Optional<String> sourceLinkUrlForFQName(IJavaProject project, String fqName) {
switch (LspClient.currentClient()) {
case VSCODE:
return createVSCodeSourceLink(project, fqName);
@@ -44,6 +44,37 @@ public class SourceLinks {
}
}
public static Optional<String> sourceLinkUrlForClasspathResource(IJavaProject project, String path) {
switch (LspClient.currentClient()) {
case VSCODE:
return createVSCodeSourceLinkForClasspathResource(project, path);
default:
return Optional.empty();
}
}
public static Optional<String> sourceLinkForResourcePath(Path path) {
switch (LspClient.currentClient()) {
case VSCODE:
return Optional.of(createVSCodeLink(path));
default:
return Optional.empty();
}
}
private static Optional<String> createVSCodeSourceLinkForClasspathResource(IJavaProject project, String path) {
int idx = path.lastIndexOf(SourceLinks.CLASS);
if (idx >= 0) {
Path p = Paths.get(path.substring(0, idx));
return SourceLinks.sourceLinkUrlForFQName(project, p.toString().replace(File.separator, "."));
}
return Optional.empty();
}
private static String createVSCodeLink(Path path) {
return path.toUri().toString();
}
private static Optional<String> createVSCodeSourceLink(IJavaProject project, String fqName) {
Optional<File> classpathResource = project.getClasspath().findClasspathResourceContainer(fqName);
if (classpathResource.isPresent()) {
@@ -97,7 +128,7 @@ public class SourceLinks {
query.append("=");
query.append(project.getElementName());
query.append("/");
String convertedPath = String.join("\\/", jarFile.toString().replaceAll("\\\\", "/").split("/"));
String convertedPath = jarFile.toString().replace(File.separator, "\\/");
query.append(convertedPath);
query.append("<");
query.append(packageName);

View File

@@ -10,13 +10,17 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.utils;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.ide.vscode.commons.java.IClasspath;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.util.Renderable;
import org.springframework.ide.vscode.commons.util.Renderables;
/**
* Helper class, represents parsed info from a Resource, and provide method(s) to
@@ -48,11 +52,21 @@ public class SpringResource {
if (type==null) {
return path; //path is just the raw text in this case
}
Optional<String> linkUrl;
switch (type) {
case FILE:
return "`"+projectRelativePath(path)+"`";
String relativePath = projectRelativePath(path);
if (relativePath != path && path.endsWith(SourceLinks.CLASS)) {
linkUrl = SourceLinks.sourceLinkUrlForClasspathResource(project, relativePath);
} else {
linkUrl = SourceLinks.sourceLinkForResourcePath(Paths.get(path));
}
// not a project relative path
return linkUrl.isPresent() ? Renderables.link(relativePath, linkUrl.get()).toMarkdown()
: "`" + projectRelativePath(path) + "`";
case CLASS_PATH_RESOURCE:
return "`"+path+"`";
linkUrl = SourceLinks.sourceLinkUrlForClasspathResource(project, path);
return linkUrl.isPresent() ? Renderables.link(path, linkUrl.get()).toMarkdown() : "`"+path+"`";
default:
return path;
}

View File

@@ -63,7 +63,7 @@ public class VSCodeSourceLinksTest {
@Test
public void testJavaSourceUrl() throws Exception {
MavenJavaProject project = mavenProjectsCache.get("empty-boot-15-web-app");
Optional<String> url = SourceLinks.sourceLinkUrl(project, "com.example.EmptyBoot15WebAppApplication");
Optional<String> url = SourceLinks.sourceLinkUrlForFQName(project, "com.example.EmptyBoot15WebAppApplication");
assertTrue(url.isPresent());
Path projectPath = Paths.get(project.pom().getParent());
Path relativePath = projectPath.relativize(Paths.get(new URL(url.get()).toURI()));
@@ -73,7 +73,7 @@ public class VSCodeSourceLinksTest {
@Test
public void testJarUrl() throws Exception {
MavenJavaProject project = mavenProjectsCache.get("empty-boot-15-web-app");
Optional<String> url = SourceLinks.sourceLinkUrl(project, "org.springframework.boot.autoconfigure.SpringBootApplication");
Optional<String> url = SourceLinks.sourceLinkUrlForFQName(project, "org.springframework.boot.autoconfigure.SpringBootApplication");
assertTrue(url.isPresent());
String headerPart = url.get().substring(0, url.get().indexOf('?'));
assertEquals("jdt://contents/spring-boot-autoconfigure-1.5.8.RELEASE.jar/org.springframework.boot.autoconfigure/SpringBootApplication.class", headerPart);
@@ -82,7 +82,7 @@ public class VSCodeSourceLinksTest {
@Test
public void testJarUrlInnerType() throws Exception {
MavenJavaProject project = mavenProjectsCache.get("empty-boot-15-web-app");
Optional<String> url = SourceLinks.sourceLinkUrl(project, "org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback");
Optional<String> url = SourceLinks.sourceLinkUrlForFQName(project, "org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback");
assertTrue(url.isPresent());
String headerPart = url.get().substring(0, url.get().indexOf('?'));
assertEquals("jdt://contents/spring-web-4.3.12.RELEASE.jar/org.springframework.web.client/RestTemplate$AcceptHeaderRequestCallback.class", headerPart);