IDEADEV-41309 flex: "inject language" should not be offered for numeric literals
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / resolve / reference / impl / manipulators / StringLiteralManipulator.java
blob732a558b08c761bbe6b4cdd2c213dbe49dfb4520
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.impl.source.resolve.reference.impl.manipulators;
18 import com.intellij.openapi.util.TextRange;
19 import com.intellij.openapi.util.text.StringUtil;
20 import com.intellij.psi.AbstractElementManipulator;
21 import com.intellij.psi.JavaPsiFacade;
22 import com.intellij.psi.PsiExpression;
23 import com.intellij.psi.PsiLiteralExpression;
24 import com.intellij.util.IncorrectOperationException;
26 /**
27 * @author ven
29 public class StringLiteralManipulator extends AbstractElementManipulator<PsiLiteralExpression> {
30 public PsiLiteralExpression handleContentChange(PsiLiteralExpression expr, TextRange range, String newContent) throws IncorrectOperationException {
31 if (!(expr.getValue() instanceof String)) throw new IncorrectOperationException("cannot handle content change");
32 String oldText = expr.getText();
33 newContent = StringUtil.escapeStringCharacters(newContent);
34 String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
35 final PsiExpression newExpr = JavaPsiFacade.getInstance(expr.getProject()).getElementFactory().createExpressionFromText(newText, null);
36 return (PsiLiteralExpression)expr.replace(newExpr);
39 public TextRange getRangeInElement(final PsiLiteralExpression element) {
40 return getValueRange(element);
43 public static TextRange getValueRange(PsiLiteralExpression element) {
44 if (!(element.getValue() instanceof String)) return TextRange.from(0, element.getTextLength());
45 return new TextRange(1, Math.max(1, element.getTextLength() - 1));