Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
hopehadfield committed Nov 29, 2023
1 parent 2afaa6c commit fe175da
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@
<command
id="java.vm.getAllInstalls">
</command>
<command
id="java.project.resolveText">
</command>
</delegateCommandHandler>
</extension>
<extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public Object executeCommand(String commandId, List<Object> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,19 +32,25 @@
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;
import org.eclipse.jdt.core.dom.ASTParser;
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;
Expand Down Expand Up @@ -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<String, String> 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 {
Expand Down

0 comments on commit fe175da

Please sign in to comment.