Skip to content

Commit

Permalink
[#342] use default editor to open external file (#343)
Browse files Browse the repository at this point in the history
[#342] use default editor to open external file

...when no other editor is opened to inherit editor type from
  • Loading branch information
ghentschke authored May 31, 2024
1 parent cfc2c07 commit 6b08f2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IEditorDescriptor[] overrideEditors(IEditorInput editorInput, IContentTyp
if (isNoCElement(contentType)) {
return editorDescriptors;
}
if (isEnabledFor(editorInput)) {
if (isEnabledFor(editorInput, contentType)) {
return editorFilter(LspPlugin.C_EDITOR_ID, editorDescriptors); // remove CDT C-Editor
}
return editorFilter(LspPlugin.LSP_C_EDITOR_ID, editorDescriptors); // remove LSP based C-Editor
Expand Down Expand Up @@ -82,7 +82,7 @@ public IEditorDescriptor overrideDefaultEditor(String fileName, IContentType con
return editorDescriptor;
}

private boolean isEnabledFor(IEditorInput editorInput) {
private boolean isEnabledFor(IEditorInput editorInput, IContentType contentType) {
if (cLanguageServerProvider == null)
return false;
IResource resource = editorInput.getAdapter(IResource.class);
Expand All @@ -94,7 +94,7 @@ private boolean isEnabledFor(IEditorInput editorInput) {
return enabled;
}
// When resource == null it's an external file: Check if the file is already opened, if not check the active editor:
return LspUtils.isFileOpenedInLspEditor(editorInput);
return LspUtils.isFileOpenedInLspEditor(editorInput, contentType);
}

private void deleteCodanMarkers(IResource resource) {
Expand Down Expand Up @@ -141,7 +141,7 @@ private IEditorDescriptor getEditorDescriptor(IEditorInput editorInput, IContent
if (isNoCElement(contentType))
return null;

if (isEnabledFor(editorInput)) {
if (isEnabledFor(editorInput, contentType)) {
return getEditorDescriptorById(editorInput.getName(), LspPlugin.LSP_C_EDITOR_ID, contentType); // return LSP based C/C++ Editor
}
// TODO: return null; when either https://github.com/eclipse-cdt/cdt/pull/310 or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.ServiceCaller;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.lsp4e.LanguageServerWrapper;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.ui.IEditorInput;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static boolean isFileOpenedInLspEditor(URI uri) {
return false;
}

public static boolean isFileOpenedInLspEditor(IEditorInput editorInput) {
public static boolean isFileOpenedInLspEditor(IEditorInput editorInput, IContentType contentType) {
if (editorInput == null) {
return false;
}
Expand All @@ -100,7 +101,9 @@ public static boolean isFileOpenedInLspEditor(IEditorInput editorInput) {
// the file has not been opened yet:
return isLspEditorActive();
}
return false;
// check defaults:
var desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(editorInput.getName(), contentType);
return desc != null ? LspPlugin.LSP_C_EDITOR_ID.equals(desc.getId()) : false;
}

public static List<IEditorReference> getEditors() {
Expand Down

0 comments on commit 6b08f2f

Please sign in to comment.