unescape copied text from regexps

This commit is contained in:
Maxim.Medvedev
2011-11-23 10:57:55 +04:00
parent 8f64d212c0
commit c42128d115
4 changed files with 53 additions and 4 deletions
@@ -98,7 +98,7 @@ public class StringLiteralCopyPasteProcessor implements CopyPastePreProcessor {
numberOfSymbolsToCopy = Math.min(escapedEndOffset, fileEndOffset) - Math.max(fileStartOffset, escapedStartOffset);
if (numberOfSymbolsToCopy > 0) {
textWasChanged = true;
buffer.append(unescapeStringCharacters(text.substring(givenTextStartOffset, givenTextStartOffset + numberOfSymbolsToCopy)));
buffer.append(unescape(text.substring(givenTextStartOffset, givenTextStartOffset + numberOfSymbolsToCopy), element));
givenTextStartOffset += numberOfSymbolsToCopy;
}
@@ -112,7 +112,12 @@ public class StringLiteralCopyPasteProcessor implements CopyPastePreProcessor {
}
return textWasChanged ? buffer.toString() : null;
}
@NotNull
protected String unescape(String text, PsiElement token) {
return unescapeStringCharacters(text);
}
public String preprocessOnPaste(final Project project, final PsiFile file, final Editor editor, String text, final RawText rawText) {
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
@@ -1655,6 +1655,32 @@ public class StringUtil {
idx += 2;
}
}
public static String unescapeSlashes(@NotNull final String str) {
final StringBuilder buf = StringBuilderSpinAllocator.alloc();
try {
unescapeSlashes(buf, str);
return buf.toString();
}
finally {
StringBuilderSpinAllocator.dispose(buf);
}
}
private static void unescapeSlashes(StringBuilder buf, String str) {
final int length = str.length();
final int last = length - 1;
for (int i = 0; i < length; i++) {
char ch = str.charAt(i);
if (ch == '\\' && i != last) {
i++;
ch = str.charAt(i);
if (ch != '/') buf.append('\\');
}
buf.append(ch);
}
}
public static void quote(@NotNull final StringBuilder builder) {
quote(builder, '\"');
@@ -64,7 +64,12 @@ public class GroovyLiteralCopyPasteProcessor extends StringLiteralCopyPasteProce
IElementType tokenType = token.getNode().getElementType();
if (tokenType == mREGEX_CONTENT || tokenType == mREGEX_LITERAL) {
return StringUtil.escapeSlashes(s);
if (escapeSlashes) {
return StringUtil.escapeSlashes(s);
}
else {
return s;
}
}
String chars;
@@ -87,4 +92,17 @@ public class GroovyLiteralCopyPasteProcessor extends StringLiteralCopyPasteProce
StringUtil.escapeStringCharacters(s.length(), s, chars, escapeSlashes, buffer);
return buffer.toString();
}
@NotNull
@Override
protected String unescape(String text, PsiElement token) {
final IElementType tokenType = token.getNode().getElementType();
if (tokenType == mREGEX_CONTENT || tokenType == mREGEX_LITERAL) {
return StringUtil.unescapeSlashes(text);
}
return super.unescape(text, token);
}
}
@@ -54,7 +54,7 @@ class GroovyCopyPasteTest extends LightCodeInsightFixtureTestCase {
myFixture.performEditorAction IdeActions.ACTION_COPY
myFixture.configureByText 'b.groovy', 'def x = "smth<caret>h"'
myFixture.performEditorAction IdeActions.ACTION_PASTE
myFixture.checkResult 'def x = "smth$a<caret>h"'
myFixture.checkResult 'def x = "smth\\$a<caret>h"'
}