Skip to content

Commit

Permalink
[WIP] Remove filesystem plugin
Browse files Browse the repository at this point in the history
Linked resource for .project works more reliably
  • Loading branch information
mickaelistria committed Jun 22, 2023
1 parent c7f6ac0 commit 711168f
Show file tree
Hide file tree
Showing 23 changed files with 141 additions and 484 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*******************************************************************************
* 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(false, false, null);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e);
}
});
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.internal.net.ProxySelector;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.net.proxy.IProxyData;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -113,7 +115,7 @@ public class JavaLanguageServerPlugin extends Plugin {
private ProjectsManager projectsManager;
private DigestStore digestStore;
private ContentProviderManager contentProviderManager;

private FixDotProjectPathResourceListener fixDotProjectListener;
private BaseJDTLanguageServer protocol;

private PreferenceManager preferenceManager;
Expand Down Expand Up @@ -155,6 +157,9 @@ public void start(BundleContext bundleContext) throws Exception {
preferenceManager = new StandardPreferenceManager();
projectsManager = new StandardProjectsManager(preferenceManager);
}
fixDotProjectListener = new FixDotProjectPathResourceListener();
ResourcesPlugin.getWorkspace().addResourceChangeListener(fixDotProjectListener, IResourceChangeEvent.POST_CHANGE);
((Workspace)ResourcesPlugin.getWorkspace()).addLifecycleListener(fixDotProjectListener);
digestStore = new DigestStore(getStateLocation().toFile());
try {
ResourcesPlugin.getWorkspace().addSaveParticipant(IConstants.PLUGIN_ID, projectsManager);
Expand Down Expand Up @@ -365,6 +370,7 @@ public void stop(BundleContext bundleContext) throws Exception {
JavaLanguageServerPlugin.pluginInstance = null;
JavaLanguageServerPlugin.context = null;
ResourcesPlugin.getWorkspace().removeSaveParticipant(IConstants.PLUGIN_ID);
ResourcesPlugin.getWorkspace().removeResourceChangeListener(fixDotProjectListener);
projectsManager = null;
contentProviderManager = null;
languageServer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -486,6 +487,25 @@ public static void linkResources(IProject project) throws CoreException {
}
}

public static void linkDotProject(IProject project) throws CoreException {
IFile dotProject = project.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
if (dotProject.isLinked()) {
return;
}
File targetDiskFile = getMetaDataFilePath(project.getName(), dotProject.getProjectRelativePath()).toFile();
if (!targetDiskFile.exists()) {
try {
targetDiskFile.getParentFile().mkdirs();
Files.copy(dotProject.getLocation().toPath(), targetDiskFile.toPath());
} catch (Exception ex) {
throw new CoreException(Status.error(targetDiskFile + " cannot be created", ex)); //$NON-NLS-1$
}
}
File sourceDiskFile = dotProject.getLocation().toFile();
dotProject.createLink(IPath.fromFile(targetDiskFile), IResource.FORCE | IResource.REPLACE, null);
sourceDiskFile.delete();
}

@Override
public Job updateProject(IProject project, boolean force) {
if (project == null || ProjectUtils.isInternalBuildSupport(BuildSupportManager.find(project).orElse(null))) {
Expand Down
7 changes: 0 additions & 7 deletions org.eclipse.jdt.ls.filesystem/.classpath

This file was deleted.

45 changes: 0 additions & 45 deletions org.eclipse.jdt.ls.filesystem/.project

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions org.eclipse.jdt.ls.filesystem/META-INF/MANIFEST.MF

This file was deleted.

6 changes: 0 additions & 6 deletions org.eclipse.jdt.ls.filesystem/build.properties

This file was deleted.

14 changes: 0 additions & 14 deletions org.eclipse.jdt.ls.filesystem/plugin.properties

This file was deleted.

10 changes: 0 additions & 10 deletions org.eclipse.jdt.ls.filesystem/plugin.xml

This file was deleted.

13 changes: 0 additions & 13 deletions org.eclipse.jdt.ls.filesystem/pom.xml

This file was deleted.

This file was deleted.

Loading

0 comments on commit 711168f

Please sign in to comment.