From fe175daafcf03577ef7a1b5cceb1cd67637e5134 Mon Sep 17 00:00:00 2001 From: Hope Hadfield Date: Wed, 29 Nov 2023 14:23:13 -0500 Subject: [PATCH] a --- org.eclipse.jdt.ls.core/plugin.xml | 3 + .../internal/JDTDelegateCommandHandler.java | 2 + .../internal/handlers/PasteEventHandler.java | 80 +++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/org.eclipse.jdt.ls.core/plugin.xml b/org.eclipse.jdt.ls.core/plugin.xml index 85cf439840..3ec808c310 100644 --- a/org.eclipse.jdt.ls.core/plugin.xml +++ b/org.eclipse.jdt.ls.core/plugin.xml @@ -136,6 +136,9 @@ + + arguments, IProgress return new SmartDetectionHandler(smartDetectionParams).getLocation(monitor); case VmCommand.GET_ALL_INSTALL_COMMAND_ID: return VmCommand.getAllVmInstalls(); + case "java.project.resolveText": + return PasteEventHandler.handleFilePasteEvent((String) arguments.get(0), (String) arguments.get(1), monitor); default: break; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java index 133ce05f74..2c87919616 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/PasteEventHandler.java @@ -12,9 +12,11 @@ *******************************************************************************/ package org.eclipse.jdt.ls.core.internal.handlers; +import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.function.Function; @@ -30,8 +32,12 @@ import org.eclipse.jdt.core.IBuffer; import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.ISourceRange; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.WorkingCopyOwner; import org.eclipse.jdt.core.dom.AST; @@ -39,10 +45,12 @@ import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.StringLiteral; +import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jdt.internal.corext.refactoring.rename.RenameAnalyzeUtil; import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; +import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler.ImportCandidate; import org.eclipse.jdt.ls.core.internal.handlers.OrganizeImportsHandler.ImportSelection; import org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor; @@ -334,6 +342,78 @@ private static String getEol(String text) { return text.contains("\r\n") ? "\r\n" : "\n"; } + public static String handleFilePasteEvent(String path, String content, IProgressMonitor monitor) throws JavaModelException { + String desiredPath = ""; + final ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); + parser.setResolveBindings(false); + parser.setKind(ASTParser.K_COMPILATION_UNIT); + parser.setStatementsRecovery(true); + parser.setBindingsRecovery(false); + parser.setIgnoreMethodBodies(true); + parser.setEnvironment(new String[0], new String[0], null, true); + Map javaOptions = JavaCore.getOptions(); + parser.setCompilerOptions(javaOptions); + parser.setSource(content.toCharArray()); + CompilationUnit root = (CompilationUnit) parser.createAST(monitor); + // Check for package, import, and/or type declaration to determine if java file + if (root != null && (root.getPackage() != null || !root.imports().isEmpty() || !root.types().isEmpty())) { + String pathRoot = path; + if (root.getPackage() != null) { + String packageName = root.getPackage().getName().toString(); + IJavaProject[] projects = ProjectUtils.getJavaProjects(); + boolean pathFound = false; + String partialPathAddition = ""; + for (int i = 0; i < projects.length; i++) { + if (!projects[i].getElementName().equals("jdt.ls-java-project") && !pathFound) { + IPackageFragment[] packages = projects[i].getPackageFragments(); + for (int j = 0; j < packages.length; j++) { + if (packages[j].getKind() == IPackageFragmentRoot.K_SOURCE && !packages[j].isDefaultPackage()) { + pathRoot = packages[j].getParent().getResource().getLocation().toOSString(); + String name = packages[j].getElementName(); + if (name.equals(packageName)) { + desiredPath = packages[j].getResource().getLocation().toOSString(); + pathFound = true; + break; + } else if (packageName.startsWith(name) && packageName.substring(0, name.length() + 1).endsWith(".")) { + String tempPath = packages[j].getResource().getLocation().toOSString(); + if (tempPath.length() > desiredPath.length()) { + desiredPath = tempPath; + partialPathAddition = packageName.substring(name.length()).replaceAll("\\.", "/"); + } + } + } + } + } + } + + if (!pathFound && !partialPathAddition.isEmpty()) { + desiredPath = desiredPath + partialPathAddition; + } else if (!pathFound) { + packageName = packageName.replaceAll("\\.", "/"); + desiredPath = pathRoot + "/" + packageName; + } + } else { + desiredPath = pathRoot; + } + + String fileName = "Untitled"; + if (!root.types().isEmpty()) { + fileName = ((TypeDeclaration) root.types().get(0)).getName().toString(); + } else { + int counter = 1; + File testExists = new File(desiredPath + "/" + fileName + ".java"); + while (testExists.exists()) { + fileName = "Untitled" + Integer.toString(counter); + testExists = new File(desiredPath + "/" + fileName + ".java"); + counter++; + } + } + return desiredPath + "/" + fileName + ".java"; + } else { + return null; + } + } + } final class StringRangeFinder extends ASTVisitor {