Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for textDocument/selectionRange #562

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support for textDocument/selectionRange
Fixes #561
angelozerr committed Mar 22, 2023
commit 3edc09127a1c2a4a26315f97a4687d6b05fc29f7
7 changes: 5 additions & 2 deletions org.eclipse.lsp4e/plugin.properties
Original file line number Diff line number Diff line change
@@ -29,5 +29,8 @@ command.toggle.outline.sort.name = Sort
command.toggle.outline.sort.label = Sort
command.open.call.hierarchy.name = Open Call Hierarchy
command.open.call.hierarchy.description = Open Call Hierarchy for the selected item
markers.name = Language Servers

command.selection.range.up.name=Enclosing Element
command.selection.range.up.description=Expand Selection To Enclosing Element
command.selection.range.down.name=Restore To Last Selection
command.selection.range.down.description=Expand Selection To Restore To Last Selection
markers.name = Language Servers
38 changes: 38 additions & 0 deletions org.eclipse.lsp4e/plugin.xml
Original file line number Diff line number Diff line change
@@ -96,6 +96,18 @@
id="org.eclipse.lsp4e.openCallHierarchy"
name="%command.open.call.hierarchy.name">
</command>
<command
categoryId="org.eclipse.lsp4e.category"
description="%command.selection.range.up.decription"
id="org.eclipse.lsp4e.selectionRange.up"
name="%command.selection.range.up.name">
</command>
<command
categoryId="org.eclipse.lsp4e.category"
description="%command.selection.range.down.decription"
id="org.eclipse.lsp4e.selectionRange.down"
name="%command.selection.range.down.name">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
@@ -156,6 +168,20 @@
class="org.eclipse.lsp4e.callhierarchy.CallHierarchyCommandHandler"
commandId="org.eclipse.lsp4e.openCallHierarchy">
</handler>
<handler
class="org.eclipse.lsp4e.operations.selectionRange.LSPSelectionRangeUpHandler"
commandId="org.eclipse.lsp4e.selectionRange.up">
<activeWhen>
<reference definitionId="org.eclipse.lsp4e.textSelectionHasLanguageServer" />
</activeWhen>
</handler>
<handler
class="org.eclipse.lsp4e.operations.selectionRange.LSPSelectionRangeDownHandler"
commandId="org.eclipse.lsp4e.selectionRange.down">
<activeWhen>
<reference definitionId="org.eclipse.lsp4e.textSelectionHasLanguageServer" />
</activeWhen>
</handler>
</extension>
<extension
point="org.eclipse.search.searchResultViewPages">
@@ -350,6 +376,18 @@
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
sequence="Ctrl+Alt+H">
</key>

<!-- edit -->
<key
sequence="M2+M3+ARROW_UP"
contextId="org.eclipse.ui.genericeditor.genericEditorContext"
commandId="org.eclipse.lsp4e.selectionRange.up"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
<key
sequence="M2+M3+ARROW_DOWN"
contextId="org.eclipse.ui.genericeditor.genericEditorContext"
commandId="org.eclipse.lsp4e.selectionRange.down"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"/>
</extension>
<extension
point="org.eclipse.core.runtime.adapters">
26 changes: 17 additions & 9 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.RewriteSessionEditProcessor;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.lsp4e.refactoring.CreateFileChange;
import org.eclipse.lsp4e.refactoring.DeleteExternalFile;
@@ -195,6 +196,17 @@ public static CompletionParams toCompletionParams(URI fileUri, int offset, IDocu
return param;
}

public static ISelection toSelection(Range range, IDocument document) {
try {
int offset = toOffset(range.getStart(), document);
int endOffset = toOffset(range.getEnd(), document);
return new TextSelection(offset, endOffset > offset ? endOffset - offset : 0);
} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
return null;
}
}

/**
* @param fileUri
* @param offset
@@ -704,16 +716,12 @@ protected static void openFileLocationInEditor(String uri, IWorkbenchPage page,
targetDocument = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
}

try {
if (targetDocument != null) {
ISelectionProvider selectionProvider = part.getEditorSite().getSelectionProvider();
int offset = toOffset(optionalRange.getStart(), targetDocument);
int endOffset = toOffset(optionalRange.getEnd(), targetDocument);
selectionProvider
.setSelection(new TextSelection(offset, endOffset > offset ? endOffset - offset : 0));
if (targetDocument != null) {
ISelectionProvider selectionProvider = part.getEditorSite().getSelectionProvider();
ISelection selection = toSelection(optionalRange, targetDocument);
if (selection != null) {
selectionProvider.setSelection(selection);
}
} catch (BadLocationException e) {
LanguageServerPlugin.logError(e);
}
}
}
11 changes: 11 additions & 0 deletions org.eclipse.lsp4e/src/org/eclipse/lsp4e/LanguageServerWrapper.java
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.Registration;
import org.eclipse.lsp4j.RegistrationParams;
import org.eclipse.lsp4j.SelectionRangeRegistrationOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.TextDocumentSyncOptions;
@@ -902,6 +903,16 @@ void registerCapability(RegistrationParams params) {
final Either<Boolean, WorkspaceSymbolOptions> beforeRegistration = serverCapabilities.getWorkspaceSymbolProvider();
serverCapabilities.setWorkspaceSymbolProvider(Boolean.TRUE);
addRegistration(reg, () -> serverCapabilities.setWorkspaceSymbolProvider(beforeRegistration));
} else if ("textDocument/selectionRange".equals(reg.getMethod())) { //$NON-NLS-1$
Either<Boolean, SelectionRangeRegistrationOptions> selectionRangeProvider = serverCapabilities
.getSelectionRangeProvider();
if (selectionRangeProvider == null || selectionRangeProvider.isLeft()) {
serverCapabilities.setSelectionRangeProvider(Boolean.TRUE);
addRegistration(reg, () -> serverCapabilities.setSelectionRangeProvider(selectionRangeProvider));
} else {
serverCapabilities.setSelectionRangeProvider(selectionRangeProvider.getRight());
addRegistration(reg, () -> serverCapabilities.setSelectionRangeProvider(selectionRangeProvider));
}
}
});
}
Original file line number Diff line number Diff line change
@@ -43,6 +43,7 @@
import org.eclipse.lsp4j.ReferencesCapabilities;
import org.eclipse.lsp4j.RenameCapabilities;
import org.eclipse.lsp4j.ResourceOperationKind;
import org.eclipse.lsp4j.SelectionRangeCapabilities;
import org.eclipse.lsp4j.ShowDocumentCapabilities;
import org.eclipse.lsp4j.SignatureHelpCapabilities;
import org.eclipse.lsp4j.SymbolCapabilities;
@@ -113,6 +114,8 @@ public class SupportedFeatures {
textDocumentClientCapabilities.setSignatureHelp(new SignatureHelpCapabilities());
textDocumentClientCapabilities
.setSynchronization(new SynchronizationCapabilities(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE));
SelectionRangeCapabilities selectionRange = new SelectionRangeCapabilities();
textDocumentClientCapabilities.setSelectionRange(selectionRange);
return textDocumentClientCapabilities;
}

Loading