/*
 * Copyright 2000-2016 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 com.jetbrains.python.psi.impl;

import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.AbstractElementManipulator;
import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.PyStringLiteralUtil;
import org.jetbrains.annotations.NotNull;

/**
 * @author traff
 */
public class PyStringLiteralExpressionManipulator extends AbstractElementManipulator<PyStringLiteralExpressionImpl> {

  @Override
  public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element,
                                                           @NotNull TextRange range,
                                                           String newContent) {
    final String newName = range.replace(element.getText(), newContent);

    final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(element.getProject());
    final PyStringLiteralExpression escaped = elementGenerator.createStringLiteralAlreadyEscaped(newName);

    return (PyStringLiteralExpressionImpl)element.replace(escaped);
  }

  @Override
  public PyStringLiteralExpressionImpl handleContentChange(@NotNull PyStringLiteralExpressionImpl element, String newContent)
    throws IncorrectOperationException {
    return handleContentChange(element, super.getRangeInElement(element), newContent);
  }

  @NotNull
  @Override
  public TextRange getRangeInElement(@NotNull PyStringLiteralExpressionImpl element) {
    final Pair<String, String> pair = PyStringLiteralUtil.getQuotes(element.getText());
    if (pair != null) {
      return TextRange.from(pair.first.length(), element.getTextLength() - pair.first.length() - pair.second.length());
    }
    return super.getRangeInElement(element);
  }
}

@@ -1462,82 +1462,8 @@
 ) %7B%0A
-    final String newName = range.replace(element.getText(), newContent);%0A%0A
     
@@ -1556,24 +1556,116 @@
 Project());%0A
+    final String escapedText = calculateEscapedText(element.getText(), range, newContent);%0A%0A
     final Py
@@ -1753,15 +1753,19 @@
 ped(
-newName
+escapedText
 );%0A%0A
@@ -2060,39 +2060,51 @@
 nt, 
-super.getRangeInElement(element
+TextRange.create(0, element.getTextLength()
 ), n
@@ -2238,41 +2238,14 @@
     
-final Pair%3CString, String%3E pair =
+return
  PyS
@@ -2268,14 +2268,28 @@
 .get
-Quotes
+StringValueTextRange
 (ele
@@ -2311,199 +2311,967 @@
 ;%0A  
-  if (pair != null) %7B%0A      return TextRange.from(pair.first.length(), element.getTextLength() - pair.first.length() - pair.second.length()
+%7D%0A%0A  @NotNull%0A  private static String calculateEscapedText(@NotNull String prevText,%0A                                             @NotNull TextRange range,%0A                                             String newContent) %7B%0A    final String newText = range.replace(prevText, newContent);%0A%0A    if (PyStringLiteralUtil.isQuoted(newText)) %7B%0A      return newText;%0A    %7D%0A%0A    final Pair%3CString, String%3E quotes = calculateQuotes(prevText);%0A    return quotes.first + newText + quotes.second;%0A  %7D%0A%0A  @NotNull%0A  private static Pair%3CString, String%3E calculateQuotes(@NotNull String text) %7B%0A    final Pair%3CString, String%3E quotes = PyStringLiteralUtil.getQuotes(text);%0A%0A    if (quotes == null %7C%7C quotes.first == null && quotes.second == null) return Pair.createNonNull(%22%5C%22%22, %22%5C%22%22
 );%0A
+%0A
     
-%7D%0A    return super.getRangeInElement(element)
+if (quotes.first == null) return Pair.createNonNull(quotes.second, quotes.second);%0A    if (quotes.second == null) return Pair.createNonNull(quotes.first, quotes.first);%0A%0A    return quotes
 ;%0A  

Update PyStringLiteralExpressionManipulator to correctly process unquoted newContent. Fix tests in AppEngineMoveTest.