Skip to content

Commit

Permalink
Fix smart paste to not import when pasting into string or text block (#…
Browse files Browse the repository at this point in the history
…1732)

- fix ClipBoardOperationAction.doPasteWithImports() to check if target
  is in a String or text block in which case do not add imports
- fixes #1716
  • Loading branch information
jjohnstn authored Oct 23, 2024
1 parent 44f42de commit 348433b
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2021 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -45,9 +45,14 @@

import org.eclipse.jface.viewers.ISelection;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPartitioningException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IRewriteTarget;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;

import org.eclipse.ui.IEditorPart;
Expand Down Expand Up @@ -86,6 +91,7 @@
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
import org.eclipse.jdt.ui.text.IJavaPartitions;

import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
import org.eclipse.jdt.internal.ui.JavaPlugin;
Expand Down Expand Up @@ -563,7 +569,9 @@ private void doPasteWithImportsOperation() {
}
try {
fOperationTarget.doOperation(fOperationCode);
addImports((ICompilationUnit)inputElement, importsData);
if (!targetIsInString()) {
addImports((ICompilationUnit)inputElement, importsData);
}
} catch (CoreException e) {
JavaPlugin.log(e);
} finally {
Expand All @@ -579,6 +587,25 @@ private void doPasteWithImportsOperation() {
}
}

private boolean targetIsInString() {
ITextEditor editor= getTextEditor();
IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput());
if (document != null) {
int offset= ((ITextSelection)editor.getSelectionProvider().getSelection()).getOffset();
try {
IDocumentExtension3 extension= (IDocumentExtension3) document;
ITypedRegion region= extension.getPartition(IJavaPartitions.JAVA_PARTITIONING, offset, false);
String partitionType= region.getType();
if (partitionType.equals(IJavaPartitions.JAVA_STRING) ||
partitionType.equals(IJavaPartitions.JAVA_MULTI_LINE_STRING)) {
return true;
}
} catch (BadLocationException | BadPartitioningException e) {
}
}
return false;
}

private void addImports(final ICompilationUnit unit, ClipboardData data) throws CoreException {
final ImportRewrite rewrite= StubUtility.createImportRewrite(unit, true);
for (String type : data.getTypeImports()) {
Expand Down

0 comments on commit 348433b

Please sign in to comment.