Skip to content

Commit

Permalink
rename to CLanguageServerEnableCache and enable on opening
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke committed Feb 7, 2025
1 parent d15d636 commit 7543ffd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.cdt.lsp.editor.EditorMetadata;
import org.eclipse.cdt.lsp.editor.EditorOptions;
import org.eclipse.cdt.lsp.editor.LanguageServerEnable;
import org.eclipse.cdt.lsp.internal.server.URICache;
import org.eclipse.cdt.lsp.internal.server.CLanguageServerEnableCache;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IScopeContext;
Expand All @@ -30,7 +30,7 @@ public EditorPreferredOptions(EditorMetadata metadata, String qualifier, IScopeC
LanguageServerEnable enable) {
super(metadata, qualifier, scopes);
this.enable = enable;
this.addPreferenceChangedListener(URICache.getInstance());
this.addPreferenceChangedListener(CLanguageServerEnableCache.getInstance());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.eclipse.cdt.internal.core.LRUCache;
import org.eclipse.cdt.lsp.editor.EditorMetadata;
import org.eclipse.cdt.lsp.util.LspUtils;
import org.eclipse.core.internal.content.ContentTypeManager;
import org.eclipse.core.runtime.content.IContentTypeManager.ContentTypeChangeEvent;
import org.eclipse.core.runtime.content.IContentTypeManager.IContentTypeChangeListener;
Expand All @@ -31,23 +32,28 @@
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor;

/**
* Caches the enable status for a given resource URI. Used by {@link HasLanguageServerPropertyTester#test(Object, String, Object[], Object)}
* The cache is getting cleared: on changes in the C/C++ content types or the prefer LSP editor option has been changed (workspace or project level).
* Caches the Language Server enable for a given resource URI. Used by {@link HasLanguageServerPropertyTester#test(Object, String, Object[], Object)}
* The cache is getting cleared:
* - on changes in the C/C++ related content types or
* - the prefer LSP editor option has been changed (workspace or project level) or
* - the LS has been stopped.
* A resource URI shall be removed from the cache if it's getting closed in the editor.
* The enable Language Server is cached when the file has been opened in the LSP based C/C++ editor.
*/
public final class URICache
public final class CLanguageServerEnableCache
implements IPreferenceChangeListener, IContentTypeChangeListener, IPartListener, IWindowListener {
private static final String C_SOURCE = "org.eclipse.cdt.core.cSource"; //$NON-NLS-1$
private static final String CXX_SOURCE = "org.eclipse.cdt.core.cxxSource"; //$NON-NLS-1$
private static final String C_HEADER = "org.eclipse.cdt.core.cHeader"; //$NON-NLS-1$
private static final String CXX_HEADER = "org.eclipse.cdt.core.cxxHeader"; //$NON-NLS-1$
private static final Map<URI, Boolean> cache = Collections.synchronizedMap(new LRUCache<>(100));
private static URICache instance = null;
private static CLanguageServerEnableCache instance = null;

private URICache() {
private CLanguageServerEnableCache() {
ContentTypeManager.getInstance().addContentTypeChangeListener(this);
if (PlatformUI.isWorkbenchRunning()) {
var workbench = PlatformUI.getWorkbench();
Expand All @@ -72,9 +78,9 @@ public static void clear() {
cache.clear();
}

public static synchronized URICache getInstance() {
public static synchronized CLanguageServerEnableCache getInstance() {
if (instance == null) {
instance = new URICache();
instance = new CLanguageServerEnableCache();
}
return instance;
}
Expand Down Expand Up @@ -115,7 +121,7 @@ public void partBroughtToTop(IWorkbenchPart part) {

@Override
public void partClosed(IWorkbenchPart part) {
if (part instanceof ExtensionBasedTextEditor editor) {
if (part instanceof TextEditor editor) {
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> cache.remove(uri));
}
}
Expand All @@ -127,7 +133,10 @@ public void partDeactivated(IWorkbenchPart part) {

@Override
public void partOpened(IWorkbenchPart part) {
// do nothing
if (part instanceof ExtensionBasedTextEditor editor
&& LspUtils.isCContentType(LspUtils.getContentType(editor.getEditorInput()))) {
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> cache.put(uri, true));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void stop() {
super.stop();
// then close output stream.
getLogProvider().ifPresent(lp -> lp.close());
URICache.clear();
CLanguageServerEnableCache.clear();
}

private boolean logEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HasLanguageServerPropertyTester extends PropertyTester {
private final ICLanguageServerProvider cLanguageServerProvider;
private final ServiceCaller<InitialUri> initial;
private final ServiceCaller<IWorkspace> workspace;
private final URICache cache = URICache.getInstance();
private final CLanguageServerEnableCache cache = CLanguageServerEnableCache.getInstance();
private Optional<IProject> project;

public HasLanguageServerPropertyTester() {
Expand All @@ -62,7 +62,6 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
}
// when getProject is empty, it's an external file: Check if the file is already opened, if not check the active editor:
var isEnabled = enabledFor(uri);
cache.put(uri, isEnabled);
if (isEnabled) {
initial.call(iu -> iu.register(uri));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.logging.Logger;

import org.eclipse.cdt.lsp.internal.server.CLanguageServerRegistry;
import org.eclipse.cdt.lsp.internal.server.URICache;
import org.eclipse.cdt.lsp.internal.server.CLanguageServerEnableCache;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void start(BundleContext context) throws Exception {

@Override
public void stop(BundleContext context) throws Exception {
URICache.stop();
CLanguageServerEnableCache.stop();
plugin = null;
super.stop(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.CoreException;
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;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

public class LspUtils {
private static final String EMPTY = ""; //$NON-NLS-1$

/**
* Checks if given ContentType id matches the content types for C/C++ files.
Expand Down Expand Up @@ -128,6 +131,18 @@ private static boolean isLspEditorActive() {
return false;
}

public static String getContentType(IEditorInput editorInput) {
if (editorInput instanceof IFileEditorInput fileEditorInput) {
try {
return Optional.ofNullable(fileEditorInput.getFile().getContentDescription())
.map(cd -> cd.getContentType()).map(ct -> ct.getId()).orElse(EMPTY);
} catch (CoreException e) {
// do nothing
}
}
return EMPTY;
}

//FIXME: AF: consider removing, since it doesn't recognize containers, use UriResource instead
public static Optional<IProject> getProject(URI uri) {
return getFile(uri).map(file -> file.getProject());
Expand Down

0 comments on commit 7543ffd

Please sign in to comment.