-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use linked resource instead of filesystem
Use linked resources instead of filesystem hack as this one can return invalid locations through standard API. This removes the filesystem bundle, the logic is now moved in ProjectManagers and a workspace listener takes care of using linked resources for metadata. This also remove the InvisibleProjectMetadataTest.testMetadataFileLocation() test because creating a new project in Eclipse workspace now enforces creation of a .settings/org.eclipse.core.resources.prefs file. Where the metadata for the invisible project is stored doesn't matter as the internal project is internal and not visible to user anyway. Also removes some usage of gson types which can cause conflicts when multiple versions are loaded. [WIP] Remove filesystem plugin Linked resource for .project works more reliably
- Loading branch information
1 parent
2803413
commit fae8c0e
Showing
32 changed files
with
445 additions
and
616 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...e.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/FixDotProjectPathResourceListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributeView; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.time.Instant; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.core.internal.events.ILifecycleListener; | ||
import org.eclipse.core.internal.events.LifecycleEvent; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IProjectDescription; | ||
import org.eclipse.core.resources.IResource; | ||
import org.eclipse.core.resources.IResourceChangeEvent; | ||
import org.eclipse.core.resources.IResourceChangeListener; | ||
import org.eclipse.core.resources.WorkspaceJob; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.ICoreRunnable; | ||
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager; | ||
|
||
public class FixDotProjectPathResourceListener implements IResourceChangeListener, ILifecycleListener { | ||
|
||
private Map<IProject, Instant> createdProjects = new HashMap<>(); | ||
|
||
@Override | ||
public void resourceChanged(IResourceChangeEvent event) { | ||
if (ProjectsManager.generatesMetadataFilesAtProjectRoot() || event.getDelta() == null) { | ||
return; | ||
} | ||
try { | ||
event.getDelta().accept(delta -> { | ||
if (delta.getResource() instanceof IProject project && // | ||
project.isOpen() && // | ||
createdProjects.containsKey(project)) { | ||
Instant projectCreation = createdProjects.remove(project); | ||
Path dotProjectOnDisk = project.getLocation().append(IProjectDescription.DESCRIPTION_FILE_NAME).toPath(); | ||
try { | ||
BasicFileAttributes attributes = Files.getFileAttributeView(dotProjectOnDisk, BasicFileAttributeView.class).readAttributes(); | ||
if (attributes.creationTime().toInstant().isAfter(projectCreation)) { | ||
// cannot link to resource in current workspace task, plan it next | ||
WorkspaceJob.createSystem("Use linked .project for " + project, (ICoreRunnable) (monitor -> { | ||
ProjectsManager.linkDotProject(project); | ||
ProjectsManager.linkResources(project); | ||
})).schedule(); | ||
} | ||
} catch (IOException ex) { | ||
JavaLanguageServerPlugin.logException(ex); | ||
} | ||
} | ||
return delta.getResource().getType() == IResource.ROOT; | ||
}); | ||
} catch (CoreException e) { | ||
JavaLanguageServerPlugin.logException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void handleEvent(LifecycleEvent event) throws CoreException { | ||
if (event.kind == LifecycleEvent.PRE_PROJECT_CREATE) { | ||
if (event.resource instanceof IProject project) { | ||
createdProjects.put(project, Instant.now()); | ||
} | ||
} else if (event.kind == LifecycleEvent.PRE_REFRESH) { | ||
unlinkIfLocal(event.resource); | ||
} | ||
} | ||
|
||
private void unlinkIfLocal(IResource resource) { | ||
if (resource instanceof IProject project && project.isOpen()) { | ||
try { | ||
Arrays.stream(project.members()) | ||
.filter(IFile.class::isInstance) | ||
.map(IFile.class::cast) | ||
.filter(IFile::isLinked) | ||
.filter(file -> file.getProject().getLocation().append(file.getProjectRelativePath()).toFile().isFile()) | ||
.forEach(file -> { | ||
try { | ||
file.delete(true, false, null); | ||
file.getProject().refreshLocal(IResource.DEPTH_ONE, null); | ||
} catch (CoreException e) { | ||
JavaLanguageServerPlugin.logException(e); | ||
} | ||
}); | ||
} catch (CoreException e) { | ||
JavaLanguageServerPlugin.logException(e); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.