Skip to content

Commit

Permalink
track open and closed files
Browse files Browse the repository at this point in the history
  • Loading branch information
ghentschke committed Feb 7, 2025
1 parent 51e69c7 commit 9871322
Showing 1 changed file with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
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;

/**
Expand All @@ -45,11 +44,14 @@
*/
public final class CLanguageServerEnableCache
implements IPreferenceChangeListener, IContentTypeChangeListener, IPartListener, IWindowListener {

private static final int BUFFER_SIZE = 100;
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 final Map<URI, Boolean> cache = Collections.synchronizedMap(new LRUCache<>(BUFFER_SIZE));
private static final Map<URI, Integer> counter = Collections.synchronizedMap(new LRUCache<>(BUFFER_SIZE));
private static CLanguageServerEnableCache instance = null;

private CLanguageServerEnableCache() {
Expand All @@ -62,6 +64,11 @@ private CLanguageServerEnableCache() {
}
}

private static void clearAll() {
cache.clear();
counter.clear();
}

public static void stop() {
if (instance != null) {
ContentTypeManager.getInstance().removeContentTypeChangeListener(instance);
Expand All @@ -70,11 +77,11 @@ public static void stop() {
Arrays.stream(workbench.getWorkbenchWindows()).map(IWorkbenchWindow::getPages).flatMap(Arrays::stream)
.forEach(p -> p.removePartListener(instance));
}
cache.clear();
clearAll();
}

public static void clear() {
cache.clear();
clearAll();
}

public static synchronized CLanguageServerEnableCache getInstance() {
Expand All @@ -95,7 +102,7 @@ public void put(URI uri, Boolean value) {
@Override
public void preferenceChange(PreferenceChangeEvent event) {
if (EditorMetadata.PREFER_LSP_KEY.contentEquals(event.getKey())) {
cache.clear();
clearAll();
}
}

Expand All @@ -104,7 +111,7 @@ public void contentTypeChanged(ContentTypeChangeEvent event) {
var id = event.getContentType().getId();
if (C_SOURCE.contentEquals(id) || CXX_SOURCE.contentEquals(id) || C_HEADER.contentEquals(id)
|| CXX_HEADER.contentEquals(id)) {
cache.clear();
clearAll();
}
}

Expand All @@ -118,10 +125,20 @@ public void partBroughtToTop(IWorkbenchPart part) {
// do nothing
}

// remove cache only if the URI is not opened in any other LSP based editor editor.
@Override
public void partClosed(IWorkbenchPart part) {
if (part instanceof TextEditor editor) {
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> cache.remove(uri));
if (part instanceof ExtensionBasedTextEditor editor
&& LspUtils.isCContentType(LspUtils.getContentType(editor.getEditorInput()))) {
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> {
var cnt = counter.getOrDefault(uri, 1);
if (--cnt <= 0) {
cache.remove(uri);
counter.remove(uri);
} else {
counter.put(uri, cnt);
}
});
}
}

Expand All @@ -134,7 +151,13 @@ public void partDeactivated(IWorkbenchPart part) {
public void partOpened(IWorkbenchPart part) {
if (part instanceof ExtensionBasedTextEditor editor
&& LspUtils.isCContentType(LspUtils.getContentType(editor.getEditorInput()))) {
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> cache.put(uri, true));
Optional.ofNullable(LSPEclipseUtils.toUri(editor.getEditorInput())).ifPresent(uri -> {
cache.put(uri, true);
var cnt = counter.putIfAbsent(uri, 1);
if (cnt != null) {
counter.put(uri, ++cnt);
}
});
}
}

Expand Down

0 comments on commit 9871322

Please sign in to comment.