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

@@ -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

@@ -0,0 +1,70 @@
/*******************************************************************************
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
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.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.ui.PartInitException;
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";
@Override
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) {
LanguageServerCommonsActivator.getInstance().getLog().log(new Status(IStatus.WARNING,
LanguageServerCommonsActivator.PLUGIN_ID, "Cannot find element: " + bindingKey));
} else {
try {
JavaUI.openInEditor(element);
} catch (PartInitException | JavaModelException e) {
throw new ExecutionException("Error opening java element in editor", e);
}
}
} else {
LanguageServerCommonsActivator.getInstance().getLog().log(new Status(IStatus.WARNING,
LanguageServerCommonsActivator.PLUGIN_ID, "Cannot find project: " + projectName));
}
}
return null;
}
}

View File

@@ -0,0 +1,72 @@
/*******************************************************************************
* 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons.commands;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
/**
* Command for opening an editor given URI of the resource. (Platform command
* takes platform URI or path relative to the workspace)
*
* @author Alex Boyko
*
*/
public class OpenResourceInEditor extends AbstractHandler {
private static final String PATH = "path";
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath rootPath = root.getLocation();
IPath resourcePath = new Path(event.getParameter(PATH));
IResource resource = root.findMember(resourcePath.makeRelativeTo(rootPath));
if (resource == null && !(resource instanceof IFile)) {
throw new ExecutionException("cannot find file: " + event.getParameter(PATH)); //$NON-NLS-1$
}
final IWorkbenchWindow window = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow();
if (window == null) {
throw new ExecutionException("no active workbench window"); //$NON-NLS-1$
}
final IWorkbenchPage page = window.getActivePage();
if (page == null) {
throw new ExecutionException("no active workbench page"); //$NON-NLS-1$
}
try {
IDE.openEditor(page, (IFile) resource, true);
} catch (final PartInitException e) {
throw new ExecutionException("error opening file in editor", e); //$NON-NLS-1$
}
return null;
}
}