mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
'remove unnecessary escape characters from string' intention
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
print <spot>'x = "abc"'</spot>
|
||||
+1
@@ -0,0 +1 @@
|
||||
print <spot>'x = \"abc\"'</spot>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<span style="font-family: verdana,serif; font-size: small;">This intention removes unnecessary escape characters from a selected string.</span>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1072,6 +1072,11 @@
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.conversions.strings.GrConvertStringToCharIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.conversions.strings.RemoveUnnecessaryEscapeCharactersIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
|
||||
+2
@@ -165,3 +165,5 @@ gr.convert.string.to.char.intention.name=Cast to char
|
||||
gr.convert.string.to.char.intention.family.name=Cast to char
|
||||
create.field.for.parameter.0 = Create Field for Parameter {0}
|
||||
create.field.for.parameter=Create Field for Parameter
|
||||
remove.unnecessary.escape.characters.intention.name=Remove unnecessary escape characters
|
||||
remove.unnecessary.escape.characters.intention.family.name=Remove unnecessary escape characters
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright 2000-2012 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy.intentions.conversions.strings;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.Intention;
|
||||
import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate;
|
||||
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString;
|
||||
|
||||
import static org.jetbrains.plugins.groovy.lang.psi.util.GrStringUtil.*;
|
||||
|
||||
/**
|
||||
* @author Max Medvedev
|
||||
*/
|
||||
public class RemoveUnnecessaryEscapeCharactersIntention extends Intention {
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element, Project project, Editor editor) throws IncorrectOperationException {
|
||||
final Document document = editor.getDocument();
|
||||
final TextRange range = element.getTextRange();
|
||||
|
||||
document.replaceString(range.getStartOffset(), range.getEndOffset(), removeUnnecessaryEscapeSymbols(((GrLiteral)element)));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected PsiElementPredicate getElementPredicate() {
|
||||
return new PsiElementPredicate() {
|
||||
@Override
|
||||
public boolean satisfiedBy(PsiElement element) {
|
||||
final String text = element.getText();
|
||||
return element instanceof GrLiteral &&
|
||||
getStartQuote(text) != null &&
|
||||
!removeUnnecessaryEscapeSymbols((GrLiteral)element).equals(text);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static String removeUnnecessaryEscapeSymbols(final GrLiteral literal) {
|
||||
final String text = literal.getText();
|
||||
final String quote = getStartQuote(text);
|
||||
final String value = removeQuotes(text);
|
||||
|
||||
final StringBuilder buffer = new StringBuilder();
|
||||
buffer.append(quote);
|
||||
|
||||
if (quote == "'") {
|
||||
escapeAndUnescapeSymbols(value, "", "\"$", buffer);
|
||||
}
|
||||
else if (quote == "'''") {
|
||||
int position = buffer.length();
|
||||
escapeAndUnescapeSymbols(value, "", "\"'$n", buffer);
|
||||
fixAllTripleQuotes(buffer, position);
|
||||
}
|
||||
else if (quote == "\"") {
|
||||
if (literal instanceof GrString) {
|
||||
final ASTNode node = literal.getNode();
|
||||
for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
||||
final IElementType type = child.getElementType();
|
||||
if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END) continue;
|
||||
if (type == GroovyElementTypes.GSTRING_INJECTION) {
|
||||
buffer.append(child.getText());
|
||||
}
|
||||
else {
|
||||
escapeAndUnescapeSymbols(child.getText(), "", "'", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
escapeAndUnescapeSymbols(value, "", "'", buffer);
|
||||
}
|
||||
}
|
||||
else if (quote == "\"\"\"") {
|
||||
if (literal instanceof GrString) {
|
||||
final ASTNode node = literal.getNode();
|
||||
for (ASTNode child = node.getFirstChildNode(); child != null; child = child.getTreeNext()) {
|
||||
final IElementType type = child.getElementType();
|
||||
if (type == GroovyTokenTypes.mGSTRING_BEGIN || type == GroovyTokenTypes.mGSTRING_END) continue;
|
||||
if (type == GroovyElementTypes.GSTRING_INJECTION) {
|
||||
buffer.append(child.getText());
|
||||
}
|
||||
else {
|
||||
final int position = buffer.length();
|
||||
escapeAndUnescapeSymbols(value, "", "\"'n", buffer);
|
||||
fixAllTripleDoubleQuotes(buffer, position);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
final int position = buffer.length();
|
||||
escapeAndUnescapeSymbols(value, "", "\"'n", buffer);
|
||||
fixAllTripleDoubleQuotes(buffer, position);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return text;
|
||||
}
|
||||
|
||||
buffer.append(quote);
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user