From 65f334a71143e1feba87b9b457e02ee1c97fb442 Mon Sep 17 00:00:00 2001 From: Gesa Hentschke Date: Sat, 8 Feb 2025 00:13:57 +0100 Subject: [PATCH] restore cache when content type has been modified - all opened files in the LSP based editor are added to cache again if the content is still a C/C++ content. - clean up API of CLanguageServerEnableCache --- .../server/CLanguageServerEnableCache.java | 27 ++++++++------ .../HasLanguageServerPropertyTester.java | 2 +- .../org/eclipse/cdt/lsp/util/LspUtils.java | 35 +++++++++++++++---- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/CLanguageServerEnableCache.java b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/CLanguageServerEnableCache.java index efba2e06..ea47e304 100644 --- a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/CLanguageServerEnableCache.java +++ b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/CLanguageServerEnableCache.java @@ -43,15 +43,15 @@ public final class CLanguageServerEnableCache implements IContentTypeChangeListe private class Data { boolean enable = false; - int counter = 0; // reflects the opened LSP based C/C++ Editors for this URI + int opened = 0; // reflects the opened LSP based C/C++ Editors for this URI private Data(boolean enable) { this.enable = enable; } - private Data(boolean enable, int counter) { + private Data(boolean enable, int opened) { this.enable = enable; - this.counter = counter; + this.opened = opened; } } @@ -88,10 +88,6 @@ public static void stop() { clearAll(); } - public static void clear() { - clearAll(); - } - public static synchronized CLanguageServerEnableCache getInstance() { if (instance == null) { instance = new CLanguageServerEnableCache(); @@ -104,8 +100,8 @@ public Boolean get(URI uri) { return data != null ? data.enable : null; } - public void put(URI uri, boolean value) { - cache.put(uri, new Data(value)); + public void disable(URI uri) { + cache.put(uri, new Data(false)); } @Override @@ -114,6 +110,15 @@ public void contentTypeChanged(ContentTypeChangeEvent event) { if (C_SOURCE.contentEquals(id) || CXX_SOURCE.contentEquals(id) || C_HEADER.contentEquals(id) || CXX_HEADER.contentEquals(id)) { clearAll(); + // add all opened files again if content type is still a C/C++ source or header: + LspUtils.getFilesInLspBasedEditor().stream().forEach(uri -> { + var data = cache.get(uri); + if (data != null) { + ++data.opened; + } else { + cache.put(uri, new Data(true, 1)); + } + }); } } @@ -134,7 +139,7 @@ public void partClosed(IWorkbenchPart part) { Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> { var data = cache.get(uri); if (data != null) { - if (--data.counter <= 0) { + if (--data.opened <= 0) { cache.remove(uri); } } @@ -154,7 +159,7 @@ public void partOpened(IWorkbenchPart part) { var data = cache.get(uri); if (data != null) { data.enable = true; - ++data.counter; + ++data.opened; } else { cache.put(uri, new Data(true, 1)); } diff --git a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/HasLanguageServerPropertyTester.java b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/HasLanguageServerPropertyTester.java index f5f107ae..9a70fa01 100644 --- a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/HasLanguageServerPropertyTester.java +++ b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/server/HasLanguageServerPropertyTester.java @@ -57,7 +57,7 @@ public boolean test(Object receiver, String property, Object[] args, Object expe return value.booleanValue(); } if (!validContentType(uri)) { - cache.put(uri, false); + cache.disable(uri); return false; } // when getProject is empty, it's an external file: Check if the file is already opened, if not check the active editor: diff --git a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUtils.java b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUtils.java index 0ffd0864..1faa84a6 100644 --- a/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUtils.java +++ b/bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUtils.java @@ -19,7 +19,6 @@ import java.util.Optional; import org.eclipse.cdt.lsp.plugin.LspPlugin; -import org.eclipse.core.internal.content.ContentTypeManager; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspace; @@ -84,6 +83,32 @@ public static boolean isFileOpenedInLspEditor(URI uri) { return false; } + public static List getFilesInLspBasedEditor() { + var uris = new ArrayList(); + var editors = getEditors(); + if (!editors.isEmpty()) { + for (IEditorReference editor : editors) { + if (LspPlugin.LSP_C_EDITOR_ID.equals(editor.getId())) { + IEditorInput editorInput = null; + try { + editorInput = editor.getEditorInput(); + } catch (PartInitException e) { + Platform.getLog(LspUtils.class).error(e.getMessage(), e); + continue; + } + if (checkForCContentType(editorInput)) { + if (editorInput instanceof IURIEditorInput uriEditorInput) { + uris.add(uriEditorInput.getURI()); + } else if (editorInput instanceof FileEditorInput fileEditorInput) { + uris.add(fileEditorInput.getFile().getLocationURI()); + } + } + } + } + } + return uris; + } + public static boolean isFileOpenedInLspEditor(IEditorInput editorInput, IContentType contentType) { if (editorInput == null) { return false; @@ -145,11 +170,9 @@ public static boolean checkForCContentType(IEditorInput editorInput) { } else if (editorInput instanceof FileStoreEditorInput fileStore) { File file = fileStore.getAdapter(File.class); if (file != null) { - var contentTypes = ContentTypeManager.getInstance().findContentTypesFor(file.getName()); - for (var contentType : contentTypes) { - if (isCContentType(contentType.getId())) { - return true; - } + var contentType = Platform.getContentTypeManager().findContentTypeFor(file.getName()); + if (contentType != null) { + return isCContentType(contentType.getId()); } } }