Skip to content

Commit

Permalink
Fixed #120 Open declaration on services should show all services with…
Browse files Browse the repository at this point in the history
… more and less specific types.
  • Loading branch information
ylussaud committed May 14, 2024
1 parent 0160a95 commit 6260360
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 8 deletions.
2 changes: 1 addition & 1 deletion plugins/org.eclipse.acceleo.aql.ide.ui/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src-module"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_additive_operator=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
Expand Down
6 changes: 4 additions & 2 deletions plugins/org.eclipse.acceleo.aql.ide.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.acceleo.aql.ide.ui;singleton:=true
Bundle-Version: 4.0.0.qualifier
Export-Package: org.eclipse.acceleo.aql.ide.ui,
org.eclipse.acceleo.aql.ide.ui.declaration,
org.eclipse.acceleo.aql.ide.ui.dialog,
org.eclipse.acceleo.aql.ide.ui.handlers,
org.eclipse.acceleo.aql.ide.ui.message,
Expand All @@ -17,7 +18,7 @@ Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.acceleo.aql.ide.ui.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.acceleo.aql;bundle-version="[4.0.0,5.0.0)",
org.eclipse.acceleo.aql.ls;bundle-version="[4.0.0,5.0.0)",
Expand All @@ -39,5 +40,6 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.acceleo.query.ide.ui,
org.eclipse.core.expressions,
org.eclipse.ui.editors,
org.eclipse.pde.core
org.eclipse.pde.core,
org.eclipse.ui.workbench.texteditor
Automatic-Module-Name: org.eclipse.acceleo.aql.ide.ui
13 changes: 12 additions & 1 deletion plugins/org.eclipse.acceleo.aql.ide.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?eclipse version="3.0"?>

<!--
Copyright (c) 2008, 2023 Obeo.
Copyright (c) 2008, 2024 Obeo.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v2.0
Expand Down Expand Up @@ -397,4 +397,15 @@
sequence="M2+M3+P">
</key>
</extension>
<extension
point="org.eclipse.ui.workbench.texteditor.hyperlinkDetectors">
<hyperlinkDetector
activate="true"
class="org.eclipse.acceleo.aql.ide.ui.declaration.AcceleoOpenDeclarationHyperlinkDetector"
description="Acceleo Open Declaration Hyperlink Detector"
id="org.eclipse.acceleo.aql.ide.ui.hyperlinkDetector"
name="Acceleo Open Declaration"
targetId="org.eclipse.ui.DefaultTextEditor">
</hyperlinkDetector>
</extension>
</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Mickael Istria (Red Hat Inc.) - initial implementation
* Michał Niewrzał (Rogue Wave Software Inc.) - hyperlink range detection
* Lucas Bullen (Red Hat Inc.) - [Bug 517428] Requests sent before initialization
* Martin Lippert (Pivotal Inc.) - [Bug 561270] labels include more details now
*******************************************************************************/
package org.eclipse.acceleo.aql.ide.ui.declaration;

import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.validation.NonNull;

// copied from org.eclipse.lsp4e.operations.declaration.LSBasedHyperlink
public class AcceleoLSBasedHyperlink implements IHyperlink {

private static final String DASH_SEPARATOR = " - "; //$NON-NLS-1$

private final Either<Location, LocationLink> location;

private final IRegion highlightRegion;

public AcceleoLSBasedHyperlink(Either<Location, LocationLink> location, IRegion highlightRegion) {
this.location = location;
this.highlightRegion = highlightRegion;
}

public AcceleoLSBasedHyperlink(Location location, IRegion linkRegion) {
this(Either.forLeft(location), linkRegion);
}

public AcceleoLSBasedHyperlink(LocationLink locationLink, IRegion linkRegion) {
this(Either.forRight(locationLink), linkRegion);
}

@Override
public IRegion getHyperlinkRegion() {
return this.highlightRegion;
}

@Override
public String getTypeLabel() {
return getLabel();
}

@Override
public String getHyperlinkText() {
return getLabel();
}

/**
* @return
* @noreference test only
*/
public Either<Location, LocationLink> getLocation() {
return location;
}

@Override
public void open() {
if (location.isLeft()) {
LSPEclipseUtils.openInEditor(location.getLeft());
} else {
LSPEclipseUtils.openInEditor(location.getRight());
}
}

private String getLabel() {
if (this.location != null) {
String uri = this.location.isLeft() ? this.location.getLeft().getUri()
: this.location.getRight().getTargetUri();
if (uri != null) {
if (uri.startsWith(LSPEclipseUtils.FILE_URI) && uri.length() > LSPEclipseUtils.FILE_URI
.length()) {
Range range = this.location.isLeft() ? this.location.getLeft().getRange()
: this.location.getRight().getTargetSelectionRange();
int line = -1;
if (range != null && range.getStart() != null) {
line = range.getStart().getLine();
}
return getFileBasedLabel(uri, line);
}
return getGenericUriBasedLabel(uri);
}
}

return "";
}

private String getGenericUriBasedLabel(String uri) {
return uri;
}

private String getFileBasedLabel(String uriStr, int line) {
URI uri = URI.create(uriStr);
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IFile[] files = workspaceRoot.findFilesForLocationURI(uri);
if (files != null && files.length == 1 && files[0].getProject() != null) {
IFile file = files[0];

@SuppressWarnings("null")
String lineContent = getLineContent(line, file);
if (lineContent != null) {
return lineContent;
} else {
IPath containerPath = file.getParent().getProjectRelativePath();
return file.getName() + DASH_SEPARATOR + file.getProject().getName() + (containerPath
.isEmpty() ? "" : IPath.SEPARATOR + containerPath.toString()); //$NON-NLS-1$
}
}
Path path = Paths.get(uri);
return path.getFileName() + (path.getParent() == null || path.getParent().getParent() == null ? "" //$NON-NLS-1$
: DASH_SEPARATOR + path.toString());
}

/**
* Gets the text content of the given line in the given {@link IFile}.
*
* @param line
* the line index
* @param file
* the {@link IFile}
* @return the text content of the given line in the given {@link IFile}
*/
public static String getLineContent(int line, final @NonNull IFile file) {
try (InputStream is = file.getContents()) {
String content = new String(is.readNBytes((int)file.getLocation().toFile().length()), Charset
.forName(file.getCharset()));
IDocument document = new Document(content);

int offset = document.getLineOffset(line);
int length = document.getLineLength(line);

return document.get(offset, length).trim();
} catch (Exception e) {
return null;
}
}
}
Loading

0 comments on commit 6260360

Please sign in to comment.