Skip to content

Commit 6b08f2f

Browse files
authored
[#342] use default editor to open external file (#343)
[#342] use default editor to open external file ...when no other editor is opened to inherit editor type from
1 parent cfc2c07 commit 6b08f2f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/editor/CEditorAssociationOverride.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IEditorDescriptor[] overrideEditors(IEditorInput editorInput, IContentTyp
4949
if (isNoCElement(contentType)) {
5050
return editorDescriptors;
5151
}
52-
if (isEnabledFor(editorInput)) {
52+
if (isEnabledFor(editorInput, contentType)) {
5353
return editorFilter(LspPlugin.C_EDITOR_ID, editorDescriptors); // remove CDT C-Editor
5454
}
5555
return editorFilter(LspPlugin.LSP_C_EDITOR_ID, editorDescriptors); // remove LSP based C-Editor
@@ -82,7 +82,7 @@ public IEditorDescriptor overrideDefaultEditor(String fileName, IContentType con
8282
return editorDescriptor;
8383
}
8484

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

100100
private void deleteCodanMarkers(IResource resource) {
@@ -141,7 +141,7 @@ private IEditorDescriptor getEditorDescriptor(IEditorInput editorInput, IContent
141141
if (isNoCElement(contentType))
142142
return null;
143143

144-
if (isEnabledFor(editorInput)) {
144+
if (isEnabledFor(editorInput, contentType)) {
145145
return getEditorDescriptorById(editorInput.getName(), LspPlugin.LSP_C_EDITOR_ID, contentType); // return LSP based C/C++ Editor
146146
}
147147
// TODO: return null; when either https://github.com/eclipse-cdt/cdt/pull/310 or

bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUtils.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.core.resources.IWorkspace;
2525
import org.eclipse.core.runtime.Platform;
2626
import org.eclipse.core.runtime.ServiceCaller;
27+
import org.eclipse.core.runtime.content.IContentType;
2728
import org.eclipse.lsp4e.LanguageServerWrapper;
2829
import org.eclipse.lsp4e.LanguageServiceAccessor;
2930
import org.eclipse.ui.IEditorInput;
@@ -79,7 +80,7 @@ public static boolean isFileOpenedInLspEditor(URI uri) {
7980
return false;
8081
}
8182

82-
public static boolean isFileOpenedInLspEditor(IEditorInput editorInput) {
83+
public static boolean isFileOpenedInLspEditor(IEditorInput editorInput, IContentType contentType) {
8384
if (editorInput == null) {
8485
return false;
8586
}
@@ -100,7 +101,9 @@ public static boolean isFileOpenedInLspEditor(IEditorInput editorInput) {
100101
// the file has not been opened yet:
101102
return isLspEditorActive();
102103
}
103-
return false;
104+
// check defaults:
105+
var desc = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(editorInput.getName(), contentType);
106+
return desc != null ? LspPlugin.LSP_C_EDITOR_ID.equals(desc.getId()) : false;
104107
}
105108

106109
public static List<IEditorReference> getEditors() {

0 commit comments

Comments
 (0)