Skip to content

Commit

Permalink
restore cache when content type has been modified
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ghentschke committed Feb 7, 2025
1 parent 00fb2d4 commit 65f334a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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));
}
});
}
}

Expand All @@ -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);
}
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +83,32 @@ public static boolean isFileOpenedInLspEditor(URI uri) {
return false;
}

public static List<URI> getFilesInLspBasedEditor() {
var uris = new ArrayList<URI>();
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;
Expand Down Expand Up @@ -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());
}
}
}
Expand Down

0 comments on commit 65f334a

Please sign in to comment.