Skip to content

Commit

Permalink
Fixed #973 The default label for multiple declarations is not useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Apr 24, 2024
1 parent f8e6b03 commit ce3d607
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
27 changes: 27 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Rubén Porras Campo (Avaloq) - Bug 576425 - Support Remote Files
* Pierre-Yves Bigourdan <pyvesdev@gmail.com> - Issue 29
* Bastian Doetsch (Snyk Ltd)
* Yvan Lussaud (Obeo) - Issue 973 show decraration line
*******************************************************************************/
package org.eclipse.lsp4e;

Expand All @@ -31,6 +32,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -82,6 +84,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextViewer;
Expand Down Expand Up @@ -1576,4 +1579,28 @@ public static boolean isReadOnly(final @NonNull IResource resource) {
ResourceAttributes attributes = resource.getResourceAttributes();
return attributes != null && attributes.isReadOnly();
}

/**
* 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2023 Red Hat Inc. and others.
* Copyright (c) 2016, 2024 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/
Expand All @@ -11,6 +11,7 @@
* 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
* Yvan Lussaud (Obeo) - Issue 973 show decraration line
*******************************************************************************/
package org.eclipse.lsp4e.operations.declaration;

Expand All @@ -29,6 +30,7 @@
import org.eclipse.lsp4e.LanguageServerPlugin;
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.ui.intro.config.IIntroURL;
import org.eclipse.ui.intro.config.IntroURLFactory;
Expand Down Expand Up @@ -92,7 +94,12 @@ private String getLabel() {
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()) {
return getFileBasedLabel(uri);
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);
}
else if (uri.startsWith(LSPEclipseUtils.INTRO_URL)) {
return getIntroUrlBasedLabel(uri);
Expand Down Expand Up @@ -125,15 +132,22 @@ private String getGenericUriBasedLabel(String uri) {
return locationType + DASH_SEPARATOR + uri;
}

private String getFileBasedLabel(String uriStr) {
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];
IPath containerPath = file.getParent().getProjectRelativePath();
return locationType + DASH_SEPARATOR + file.getName() + DASH_SEPARATOR + file.getProject().getName()
+ (containerPath.isEmpty() ? "" : IPath.SEPARATOR + containerPath.toString()); //$NON-NLS-1$

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

0 comments on commit ce3d607

Please sign in to comment.