PT #163527410: Don't load source for Eclipse. Ask JDT LS for locations.

This commit is contained in:
BoykoAlex
2019-02-04 18:48:40 -05:00
parent 5bb448b0a0
commit 7fe0d75412
19 changed files with 245 additions and 56 deletions

View File

@@ -104,14 +104,6 @@
<extension
point="org.eclipse.ui.handlers">
<handler
class="org.springframework.tooling.boot.ls.commands.OpenJavaElementInEditor"
commandId="org.springframework.tooling.boot.ls.OpenJavaElement">
</handler>
<handler
class="org.springframework.tooling.boot.ls.commands.OpenResourceInEditor"
commandId="org.springframework.tooling.boot.ls.OpenResourceInEditor">
</handler>
<handler
class="org.springframework.tooling.boot.ls.commands.ToggleComment"
commandId="org.springframework.tooling.boot.ls.ToggleComment">
@@ -133,29 +125,6 @@
id="org.springframework.ide.eclipse.commands"
name="Spring Generic Text Editor">
</category>
<command
id="org.springframework.tooling.boot.ls.OpenJavaElement"
name="Open Java Element in Editor">
<commandParameter
id="bindingKey"
name="bindingKey"
optional="false">
</commandParameter>
<commandParameter
id="projectName"
name="projectName"
optional="false">
</commandParameter>
</command>
<command
id="org.springframework.tooling.boot.ls.OpenResourceInEditor"
name="Open File in Editor">
<commandParameter
id="path"
name="path"
optional="false">
</commandParameter>
</command>
<command
id="org.springframework.tooling.boot.ls.ToggleComment"
categoryId="org.springframework.ide.eclipse.commands"

View File

@@ -80,4 +80,41 @@
</enabledWhen>
</codeMiningProvider>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="org.springframework.tooling.ls.eclipse.commons.commands.OpenJavaElementInEditor"
commandId="org.springframework.tooling.ls.eclipse.commons.commands.OpenJavaElementInEditor">
</handler>
<handler
class="org.springframework.tooling.ls.eclipse.commons.commands.OpenResourceInEditor"
commandId="org.springframework.tooling.ls.eclipse.commons.commands.OpenResourceInEditor">
</handler>
</extension>
<extension
point="org.eclipse.ui.commands">
<command
id="org.springframework.tooling.ls.eclipse.commons.commands.OpenJavaElementInEditor"
name="Open Java Element in Editor">
<commandParameter
id="bindingKey"
name="bindingKey"
optional="false">
</commandParameter>
<commandParameter
id="projectName"
name="projectName"
optional="false">
</commandParameter>
</command>
<command
id="org.springframework.tooling.ls.eclipse.commons.commands.OpenResourceInEditor"
name="Open File in Editor">
<commandParameter
id="path"
name="path"
optional="false">
</commandParameter>
</command>
</extension>
</plugin>

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 Pivotal, Inc.
* Copyright (c) 2017, 2019 Pivotal, Inc.
* 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
@@ -12,6 +12,7 @@ package org.springframework.tooling.ls.eclipse.commons;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.services.LanguageClient;
@@ -51,4 +52,7 @@ public interface STS4LanguageClient extends LanguageClient {
@JsonRequest("sts/javadocHoverLink")
CompletableFuture<JavadocHoverLinkResponse> javadocHoverLink(JavaDataParams params);
@JsonRequest("sts/javaLocation")
CompletableFuture<Location> javaLocation(JavaDataParams params);
}

View File

@@ -23,6 +23,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementLinks;
import org.eclipse.jface.action.IStatusLineManager;
@@ -42,6 +43,7 @@ import org.eclipse.jface.text.source.ISourceViewerExtension5;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageClientImpl;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Location;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
@@ -63,6 +65,7 @@ import org.springframework.tooling.jdt.ls.commons.java.JavaTypeResponse;
import org.springframework.tooling.jdt.ls.commons.java.JavadocHoverLinkResponse;
import org.springframework.tooling.jdt.ls.commons.javadoc.JavadocResponse;
import org.springframework.tooling.jdt.ls.commons.javadoc.JavadocUtils;
import org.springframework.tooling.jdt.ls.commons.resources.ResourceUtils;
import org.springframework.tooling.ls.eclipse.commons.javadoc.JavaDoc2MarkdownConverter;
import org.springframework.tooling.ls.eclipse.commons.preferences.PreferenceConstants;
@@ -367,4 +370,23 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
return CompletableFuture.completedFuture(response);
}
@Override
public CompletableFuture<Location> javaLocation(JavaDataParams params) {
return CompletableFuture.supplyAsync(() -> {
try {
URI projectUri = params.getProjectUri() == null ? null : URI.create(params.getProjectUri());
IJavaElement element = JavaData.findElement(projectUri, params.getBindingKey(), JavaDataParams.isLookInOtherProjects(params));
if (element != null ) {
IJavaProject project = element.getJavaProject() == null ? ResourceUtils.getJavaProject(projectUri) : element.getJavaProject();
if (project != null) {
return new Location(Utils.eclipseIntroUri(project.getElementName(), params.getBindingKey()).toString(), null);
}
}
} catch (Exception e) {
LanguageServerCommonsActivator.logError(e, "Failed to find java element for key " + params.getBindingKey());
}
return null;
});
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* Copyright (c) 2018, 2019 Pivotal, Inc.
* 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
@@ -10,7 +10,9 @@
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
@@ -29,6 +31,17 @@ import org.eclipse.ui.PlatformUI;
@SuppressWarnings("restriction")
public class Utils {
private static final String URL_PREFIX = "http://org.eclipse.ui.intro/execute?command=";
private static final String EQUALS = "=";
private static final String PARAMETERS_SEPARATOR = ",";
private static final String PARAMETERS_START = "(";
private static final String PARAMETERS_END = ")";
private static final String JAVA_ELEMENT_COMMAND = "org.springframework.tooling.ls.eclipse.commons.commands.OpenJavaElementInEditor";
private static final String BINDING_KEY_PARAMETER_ID = "bindingKey";
private static final String PROJECT_NAME_PARAMETER_ID = "projectName";
public static Stream<ITextViewer> getActiveTextViewers() {
return getActiveEditors()
.map(editorPart -> editorPart.getAdapter(ITextViewer.class))
@@ -105,4 +118,23 @@ public class Utils {
return null;
}
public static URI eclipseIntroUri(String projectName, String bindingKey) throws UnsupportedEncodingException {
StringBuilder paramBuilder = new StringBuilder(JAVA_ELEMENT_COMMAND);
paramBuilder.append(PARAMETERS_START);
paramBuilder.append(BINDING_KEY_PARAMETER_ID);
paramBuilder.append(EQUALS);
paramBuilder.append(bindingKey);
if (projectName != null) {
paramBuilder.append(PARAMETERS_SEPARATOR);
paramBuilder.append(PROJECT_NAME_PARAMETER_ID);
paramBuilder.append(EQUALS);
paramBuilder.append(projectName);
}
paramBuilder.append(PARAMETERS_END);
StringBuilder urlBuilder = new StringBuilder(URL_PREFIX);
urlBuilder.append(URLEncoder.encode(paramBuilder.toString(), "UTF8"));
return URI.create(urlBuilder.toString());
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* Copyright (c) 2018, 2019 Pivotal, Inc.
* 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
@@ -8,7 +8,7 @@
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.boot.ls.commands;
package org.springframework.tooling.ls.eclipse.commons.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
@@ -23,17 +23,17 @@ import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.ui.PartInitException;
import org.springframework.tooling.boot.ls.BootLanguageServerPlugin;
import org.springframework.tooling.ls.eclipse.commons.LanguageServerCommonsActivator;
import org.springframework.tooling.ls.eclipse.commons.Utils;
/**
* Command for opening Java type given by its fully qualified name in an editor
*
*
* @author Alex Boyko
*
*/
public class OpenJavaElementInEditor extends AbstractHandler {
private static final String BINDING_KEY = "bindingKey";
private static final String PROJECT_NAME = "projectName";
@@ -41,15 +41,16 @@ public class OpenJavaElementInEditor extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
String bindingKey = event.getParameter(BINDING_KEY);
String projectName = event.getParameter(PROJECT_NAME);
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
if (project != null && bindingKey != null) {
IJavaProject javaProject = JavaCore.create(project);
if (javaProject != null) {
IJavaElement element = Utils.findElement(javaProject, bindingKey);
if (element == null) {
BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "Cannot find element: " + bindingKey));
LanguageServerCommonsActivator.getInstance().getLog().log(new Status(IStatus.WARNING,
LanguageServerCommonsActivator.PLUGIN_ID, "Cannot find element: " + bindingKey));
} else {
try {
JavaUI.openInEditor(element);
@@ -58,11 +59,12 @@ public class OpenJavaElementInEditor extends AbstractHandler {
}
}
} else {
BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "Cannot find project: " + projectName));
LanguageServerCommonsActivator.getInstance().getLog().log(new Status(IStatus.WARNING,
LanguageServerCommonsActivator.PLUGIN_ID, "Cannot find project: " + projectName));
}
}
return null;
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* Copyright (c) 2018, 2019 Pivotal, Inc.
* 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
@@ -8,7 +8,7 @@
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.boot.ls.commands;
package org.springframework.tooling.ls.eclipse.commons.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;