Skip to content

[#412] cache enablement for URI #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b5820e4
[#412] cash enablement for URI
ghentschke Feb 5, 2025
4d560b3
add URIEnableCache class
ghentschke Feb 6, 2025
5b1310f
remove content type change listener
ghentschke Feb 6, 2025
d15d636
rename URIEnableCache to URICache and clear cache on LSP stop
ghentschke Feb 6, 2025
7543ffd
rename to CLanguageServerEnableCache and enable on opening
ghentschke Feb 7, 2025
51e69c7
do not clear cache on LS stop
ghentschke Feb 7, 2025
9871322
track open and closed files
ghentschke Feb 7, 2025
852a92b
combine counter and enable buffer to prevent mismatch
ghentschke Feb 7, 2025
2a7e573
cache also enable for (header) files from external libraries
ghentschke Feb 7, 2025
828330e
remove no-op lines and update header
ghentschke Feb 7, 2025
ea319b3
remove typo
ghentschke Feb 7, 2025
00fb2d4
do not clean cache on LSP enable change
ghentschke Feb 7, 2025
65f334a
restore cache when content type has been modified
ghentschke Feb 7, 2025
42be18c
add comment
ghentschke Feb 7, 2025
f7d2f64
clean-up code
ghentschke Feb 7, 2025
60c13b0
improve changed content type handling
ghentschke Feb 7, 2025
f6c978c
reset restored after 1st selection
ghentschke Feb 8, 2025
ff6ea9f
minor improvements
ghentschke Feb 8, 2025
b35c302
remove unused changes
ghentschke Feb 8, 2025
ddb6457
Use editor part hashes to track opened editors
ghentschke Feb 9, 2025
f92c7b2
add unit test
ghentschke Feb 9, 2025
f2bf5f8
add sleep before check
ghentschke Feb 9, 2025
20f9d3f
add dependency
ghentschke Feb 9, 2025
d4b1e29
add ui test harness tp target
ghentschke Feb 9, 2025
6185633
fix bug in pom.xml
ghentschke Feb 10, 2025
0921988
add missing requirements for unit test
ghentschke Feb 10, 2025
10ce4b6
remove test harness
ghentschke Feb 10, 2025
3fcf933
Update bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/util/LspUt…
ghentschke Feb 11, 2025
906692a
Use Optional as return value for the cache and add unit tests
ghentschke Feb 11, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.ServiceCaller;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.ide.IDE;
import org.junit.jupiter.api.TestInfo;

Expand Down Expand Up @@ -83,6 +85,12 @@ public static IEditorPart openInEditor(IFile file) throws PartInitException {
return part;
}

public static IEditorPart openInEditorInNewWindow(URI uri, String editorID) throws PartInitException {
IEditorPart part = IDE.openEditor(getNewWorkbenchWindowPage(), uri, editorID, true);
part.setFocus();
return part;
}

public static boolean closeEditor(IEditorPart editor, boolean save) {
IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = workbenchWindow.getActivePage();
Expand All @@ -100,4 +108,13 @@ private static IWorkbenchPage getWorkbenchPage() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
}

private static IWorkbenchPage getNewWorkbenchWindowPage() {
try {
return PlatformUI.getWorkbench().openWorkbenchWindow(null).getActivePage();
} catch (WorkbenchException e) {
Platform.getLog(TestUtils.class).error(e.getMessage(), e);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*******************************************************************************
* Copyright (c) 2025 Contributors to the Eclipse Foundation.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/

package org.eclipse.cdt.lsp.test.internal.server;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.eclipse.cdt.lsp.internal.server.CLanguageServerEnableCache;
import org.eclipse.cdt.lsp.plugin.LspPlugin;
import org.eclipse.cdt.lsp.test.TestUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.ui.IEditorPart;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.io.TempDir;

class CLanguageServerEnableCacheTest {
private static final String HEADER_HPP = "MyProjectHeader.h";
private static final String EXTERNAL_HEADER_HPP = "ExternalHeader.hpp";
private static final String NO_C_FILE = "MyFile.hdr";
private IProject project;
private File externalHeaderFile;
private File externalNoCFile;
private IFile projectHeaderFile;
private IFile projectNoCFile;
private IEditorPart editor;
private IEditorPart editor2;
private CLanguageServerEnableCache cache = CLanguageServerEnableCache.getInstance();

@TempDir
private static File TEMP_DIR;

@BeforeEach
public void setUp(TestInfo testInfo) throws CoreException, IOException {
project = TestUtils.createCProject(TestUtils.getName(testInfo));
projectHeaderFile = TestUtils.createFile(project, HEADER_HPP, "");
projectNoCFile = TestUtils.createFile(project, NO_C_FILE, "");
externalHeaderFile = new File(TEMP_DIR, EXTERNAL_HEADER_HPP);
externalHeaderFile.createNewFile();
externalNoCFile = new File(TEMP_DIR, NO_C_FILE);
externalNoCFile.createNewFile();
}

@AfterEach
public void cleanUp() throws CoreException {
if (editor != null) {
TestUtils.closeEditor(editor, false);
}
if (editor2 != null) {
TestUtils.closeEditor(editor2, false);
}

TestUtils.deleteProject(project);
if (externalHeaderFile != null) {
externalHeaderFile.delete();
}
if (externalNoCFile != null) {
externalNoCFile.delete();
}
}

private void testC_File_URIopenedInEditor(URI uri) throws CoreException, IOException {
// GIVEN is an opened header file in the LSP based C/C++ editor:
editor = TestUtils.openInEditor(uri, LspPlugin.LSP_C_EDITOR_ID);
// WHEN accessing the cached enable value:
var cachedUri = cache.get(uri);
// THEN the cache for the given URI is present:
assertTrue(cachedUri.isPresent());
// AND the cache returns TRUE for the given file URI:
assertTrue(cachedUri.get().booleanValue());
// WHEN when the file gets closed:
TestUtils.closeEditor(editor, false);
// THEN the cached URI has been removed:
assertFalse(cache.get(uri).isPresent());
}

private void testC_File_URIopenedIn2EditorsIn2Windows(URI uri) throws CoreException, IOException {
// GIVEN two opened header files in the LSP based C/C++ editor in two workbench windows:
editor = TestUtils.openInEditor(uri, LspPlugin.LSP_C_EDITOR_ID);
editor2 = TestUtils.openInEditorInNewWindow(uri, LspPlugin.LSP_C_EDITOR_ID);
// WHEN accessing the cached enable value:
var cachedUri = cache.get(uri);
// THEN the cache for the given URI is present:
assertTrue(cachedUri.isPresent());
// AND the cache returns TRUE for the given file URI:
assertTrue(cachedUri.get().booleanValue());
// WHEN when the file gets closed in the first window:
TestUtils.closeEditor(editor, false);
// THEN the cached URI has NOT been removed:
assertTrue(cache.get(uri).isPresent());
assertTrue(cache.get(uri).get().booleanValue());
// WHEN the file gets closed in the second window:
TestUtils.closeEditor(editor2, false);
// THEN the cached URI has been removed:
assertFalse(cache.get(uri).isPresent());
}

private void test_File_URIopenedInEditor(URI uri) throws CoreException, IOException {
// GIVEN is an opened file in the LSP based C/C++ editor:
editor = TestUtils.openInEditor(uri, LspPlugin.LSP_C_EDITOR_ID);
// WHEN accessing the cached enable value:
var cachedUri = cache.get(uri);
// THEN the cache for the given URI is present:
assertTrue(cachedUri.isPresent());
// AND the cache returns FALSE for the given file URI:
assertFalse(cachedUri.get().booleanValue());
// WHEN when the file gets closed:
TestUtils.closeEditor(editor, false);
// THEN the cached URI has NOT been removed:
assertTrue(cache.get(uri).isPresent());
}

@Test
@DisplayName("Cached header file URI shall be removed after file gets closed in editor")
public void testCache1() throws CoreException, IOException {
testC_File_URIopenedInEditor(projectHeaderFile.getFullPath().toFile().toURI());
}

@Test
@DisplayName("Cached header file URI shall be removed after ALL opened editors gets closed")
public void testCache2() throws CoreException, IOException {
testC_File_URIopenedIn2EditorsIn2Windows(projectHeaderFile.getFullPath().toFile().toURI());
}

@Test
@DisplayName("Cached EXTERNAL header file URI shall be removed after file gets closed in editor")
public void testCache3() throws CoreException, IOException {
testC_File_URIopenedInEditor(externalHeaderFile.toURI());
}

@Test
@DisplayName("Cached EXTERNAL header file URI shall be removed after ALL opened editors gets closed")
public void testCache4() throws CoreException, IOException {
testC_File_URIopenedIn2EditorsIn2Windows(externalHeaderFile.toURI());
}

@Test
@DisplayName("Cached non C file URI shall return false and should not be removed after closing")
public void testCache5() throws CoreException, IOException {
test_File_URIopenedInEditor(projectNoCFile.getFullPath().toFile().toURI());
}

@Test
@DisplayName("Cached EXTERNAL non C file URI shall return false and should not be removed after closing")
public void testCache6() throws CoreException, IOException {
test_File_URIopenedInEditor(externalNoCFile.toURI());
}

}
Loading
Loading