-
Notifications
You must be signed in to change notification settings - Fork 13
[#47] Add action to switch between Source/Header #48
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...or.ui.test/src/org/eclipse/cdt/lsp/editor/ui/test/commands/LspEditorActiveTesterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 COSEDA Technologies GmbH 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: | ||
* Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation | ||
*******************************************************************************/ | ||
package org.eclipse.cdt.lsp.editor.ui.test.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.eclipse.cdt.lsp.LspPlugin; | ||
import org.eclipse.cdt.lsp.editor.ui.commands.LspEditorActiveTester; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IEditorSite; | ||
import org.eclipse.ui.texteditor.ITextEditor; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LspEditorActiveTesterTest { | ||
|
||
LspEditorActiveTester tut; | ||
|
||
IEditorPart editorPart; | ||
IEditorSite editorSite; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
tut = new LspEditorActiveTester(); | ||
|
||
editorPart = mock(IEditorPart.class); | ||
editorSite = mock(IEditorSite.class); | ||
|
||
when(editorPart.getSite()).thenReturn(editorSite); | ||
} | ||
|
||
/** | ||
* Assert that a receiver which is not an editor part is not detected as c | ||
* editor. | ||
*/ | ||
@Test | ||
public void nonEditorPart() { | ||
assertFalse(tut.test(new String("not an edit part"), LspEditorActiveTester.IS_LSP_CEDITOR_PROPERTY, null, tut)); | ||
} | ||
|
||
/** | ||
* Assert that tester only tests for the correct property, even if the given | ||
* receiver is an {@link IEditorPart}. | ||
*/ | ||
@Test | ||
public void editorPartNonMatchingProperty() { | ||
assertFalse(tut.test(editorPart, "someOtherProperty", null, tut)); | ||
} | ||
|
||
/** | ||
* Assert that an {@link IEditorPart} with some other id is not detected as c editor. | ||
*/ | ||
@Test | ||
public void matchingEditorPartWithNonMatchingId() { | ||
when(editorSite.getId()).thenReturn("someId"); | ||
assertTestResult(false); | ||
} | ||
|
||
/** | ||
* Assert that an {@link IEditorPart} with the c editor id is detected as c editor. | ||
*/ | ||
@Test | ||
public void matchingEditorPartWithMatchingId() { | ||
when(editorSite.getId()).thenReturn(LspPlugin.LSP_C_EDITOR_ID); | ||
assertTestResult(true); | ||
} | ||
|
||
/** | ||
* Assert that an {@link IEditorPart} with an "inner editor" (part adapts to | ||
* {@link ITextEditor.class}) is not detected as c editor when the inner editor | ||
* doesn't have the c editor id. | ||
*/ | ||
@Test | ||
public void editorPartWithInnerNonMatchingEditor() { | ||
ITextEditor innerEditor = mock(ITextEditor.class); | ||
IEditorSite innerEditorSite = mock(IEditorSite.class); | ||
when(innerEditor.getSite()).thenReturn(innerEditorSite); | ||
when(innerEditorSite.getId()).thenReturn("someId"); | ||
when(editorPart.getAdapter(ITextEditor.class)).thenReturn(innerEditor); | ||
when(editorSite.getId()).thenReturn("someOtherId"); | ||
|
||
assertTestResult(false); | ||
} | ||
|
||
/** | ||
* Assert that an {@link IEditorPart} with an "inner editor" (part adapts to | ||
* {@link ITextEditor.class}) is detected as c editor when the inner editor | ||
* has the c editor id. | ||
*/ | ||
@Test | ||
public void editorPartWithInnerMatchingEditor() { | ||
ITextEditor innerEditor = mock(ITextEditor.class); | ||
IEditorSite innerEditorSite = mock(IEditorSite.class); | ||
when(innerEditor.getSite()).thenReturn(innerEditorSite); | ||
when(innerEditorSite.getId()).thenReturn(LspPlugin.LSP_C_EDITOR_ID); | ||
when(editorPart.getAdapter(ITextEditor.class)).thenReturn(innerEditor); | ||
when(editorSite.getId()).thenReturn("someOtherId"); | ||
|
||
assertTestResult(true); | ||
} | ||
|
||
/** | ||
* Helper method. | ||
*/ | ||
private void assertTestResult(boolean expectedResult) { | ||
assertEquals(expectedResult, tut.test(editorPart, LspEditorActiveTester.IS_LSP_CEDITOR_PROPERTY, null, tut)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ source.. = src/ | |
output.. = bin/ | ||
bin.includes = META-INF/,\ | ||
.,\ | ||
plugin.xml | ||
plugin.xml,\ | ||
plugin.properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Commands.ToggleSourceAndHeader.name=Toggle Source/Header | ||
PopupMenu.ToggleSourceAndHeader.label=Toggle Source/Header |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...e.cdt.lsp.editor.ui/src/org/eclipse/cdt/lsp/editor/ui/commands/LspEditorActiveTester.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 COSEDA Technologies GmbH 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: | ||
* Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation | ||
*******************************************************************************/ | ||
package org.eclipse.cdt.lsp.editor.ui.commands; | ||
|
||
import java.util.Optional; | ||
|
||
import org.eclipse.cdt.lsp.LspPlugin; | ||
import org.eclipse.core.expressions.PropertyTester; | ||
import org.eclipse.core.runtime.Adapters; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.texteditor.ITextEditor; | ||
|
||
/** | ||
* Property tester for checking if a receiver is a c editor. Will evaluate | ||
* {@code true} if the receiver can adapt to {@link ITextEditor} and has the c | ||
* editor id. | ||
*/ | ||
public class LspEditorActiveTester extends PropertyTester { | ||
public final static String IS_LSP_CEDITOR_PROPERTY = "isLspCEditor"; | ||
|
||
@Override | ||
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { | ||
if (receiver instanceof IEditorPart && IS_LSP_CEDITOR_PROPERTY.equals(property)) { | ||
IEditorPart innerEditor = Optional.ofNullable((IEditorPart) Adapters.adapt(receiver, ITextEditor.class)) | ||
.orElse((IEditorPart) receiver); | ||
|
||
return LspPlugin.LSP_C_EDITOR_ID.equals(innerEditor.getSite().getId()); | ||
} | ||
return false; | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...or.ui/src/org/eclipse/cdt/lsp/editor/ui/commands/ToggleSourceAndHeaderCommandHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 COSEDA Technologies GmbH 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: | ||
* Dominic Scharfe (COSEDA Technologies GmbH) - initial implementation | ||
*******************************************************************************/ | ||
package org.eclipse.cdt.lsp.editor.ui.commands; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.cdt.lsp.LspPlugin; | ||
import org.eclipse.cdt.lsp.editor.ui.LspEditorUiPlugin; | ||
import org.eclipse.cdt.lsp.services.ClangdLanguageServer; | ||
import org.eclipse.core.commands.AbstractHandler; | ||
import org.eclipse.core.commands.ExecutionEvent; | ||
import org.eclipse.core.commands.ExecutionException; | ||
import org.eclipse.core.commands.IHandler; | ||
import org.eclipse.core.runtime.Adapters; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
import org.eclipse.ui.IEditorInput; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.eclipse.ui.IFileEditorInput; | ||
import org.eclipse.ui.IURIEditorInput; | ||
import org.eclipse.ui.IWorkbenchPage; | ||
import org.eclipse.ui.PartInitException; | ||
import org.eclipse.ui.handlers.HandlerUtil; | ||
import org.eclipse.ui.ide.IDE; | ||
import org.eclipse.ui.statushandlers.StatusManager; | ||
import org.eclipse.ui.texteditor.ITextEditor; | ||
|
||
public class ToggleSourceAndHeaderCommandHandler extends AbstractHandler implements IHandler { | ||
@Override | ||
public Object execute(ExecutionEvent event) throws ExecutionException { | ||
return execute(HandlerUtil.getActiveEditor(event)); | ||
} | ||
|
||
@SuppressWarnings("restriction") | ||
private Object execute(IEditorPart activeEditor) { | ||
// Try to adapt to ITextEditor (e.g. to support editors embedded in | ||
// MultiPageEditorParts), otherwise use activeEditor. | ||
IEditorPart innerEditor = Optional.ofNullable((IEditorPart) Adapters.adapt(activeEditor, ITextEditor.class)) | ||
.orElse(activeEditor); | ||
|
||
getUri(innerEditor).ifPresent(fileUri -> { | ||
IDocument document = org.eclipse.lsp4e.LSPEclipseUtils.getDocument(innerEditor.getEditorInput()); | ||
org.eclipse.lsp4e.LanguageServers.forDocument(document) | ||
.computeFirst( | ||
server -> server instanceof ClangdLanguageServer | ||
? ((ClangdLanguageServer) server) | ||
.switchSourceHeader(new TextDocumentIdentifier(fileUri.toString())) | ||
: null) | ||
.thenAccept(otherFileUri -> otherFileUri | ||
.ifPresent(uri -> openEditor(innerEditor.getEditorSite().getPage(), URI.create(uri)))); | ||
}); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Returns the URI of the given editor depending on the type of its | ||
* {@link IEditorPart#getEditorInput()}. | ||
* | ||
* @return the URI or an empty {@link Optional} if the URI couldn't be | ||
* determined. | ||
*/ | ||
private static Optional<URI> getUri(IEditorPart editor) { | ||
IEditorInput editorInput = editor.getEditorInput(); | ||
|
||
if (editorInput instanceof IFileEditorInput) { | ||
return Optional.of(((IFileEditorInput) editor.getEditorInput()).getFile().getLocationURI()); | ||
} else if (editorInput instanceof IURIEditorInput) { | ||
return Optional.of(((IURIEditorInput) editorInput).getURI()); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private static void openEditor(IWorkbenchPage page, URI fileUri) { | ||
page.getWorkbenchWindow().getShell().getDisplay().asyncExec(() -> { | ||
try { | ||
IDE.openEditor(page, fileUri, LspPlugin.LSP_C_EDITOR_ID, true); | ||
} catch (PartInitException e) { | ||
StatusManager.getManager().handle(e, LspEditorUiPlugin.PLUGIN_ID); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.