// Copyright 2017 Google Inc.
//
// 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.google.bamboo.soy.typedhandlers;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.actionSystem.TypedActionHandler;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import org.jetbrains.annotations.NotNull;

/**
 * Automatically inserts a matching quote when typing one.
 */
public class QuoteHandler implements TypedActionHandler {
  private final TypedActionHandler myOriginalHandler;

  public QuoteHandler(TypedActionHandler originalHandler) {
    myOriginalHandler = originalHandler;
  }

  public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) {
    if (charTyped == '"') {
      int caretOffset = editor.getCaretModel().getOffset();
      String nextChar =
          caretOffset >= editor.getDocument().getTextLength()
              ? ""
              : editor.getDocument().getText(new TextRange(caretOffset, caretOffset + 1));

      if (!nextChar.equals(charTyped + "")) {
        editor.getDocument().insertString(editor.getCaretModel().getOffset(), charTyped + "");
        if (editor.getProject() != null) {
          PsiDocumentManager.getInstance(editor.getProject()).commitDocument(editor.getDocument());
        }
      }
    }

    myOriginalHandler.execute(editor, charTyped, dataContext);
  }
}

@@ -644,48 +644,140 @@
 com.
-intellij.openapi.actionSystem.DataContex
+google.common.collect.ImmutableSet;%0Aimport com.intellij.openapi.actionSystem.DataContext;%0Aimport com.intellij.openapi.editor.Documen
 t;%0Ai
@@ -1021,47 +1021,74 @@
 l;%0A%0A
-/**%0A * Automatically inserts a matching
+import java.util.Set;%0A%0A/**%0A *  Inserts a matching single or double
  quo
@@ -1169,16 +1169,92 @@
 ndler %7B%0A
+  private final Set%3CCharacter%3E matchingQuotes = ImmutableSet.of('%22', '%5C'');%0A
   privat
@@ -1516,27 +1516,93 @@
 if (
-charTyped == '%22') %7B
+matchingQuotes.contains(charTyped)) %7B%0A      Document document = editor.getDocument();
 %0A   
@@ -1654,24 +1654,25 @@
 etOffset();%0A
+%0A
       String
@@ -1676,169 +1676,102 @@
 ing 
-next
+prev
 Char =
-%0A          caretOffset %3E= editor.getDocument().getTextLength()%0A              ? %22%22%0A              : editor.getDocument().getText(new TextRange(caretOffse
+ getPreviousChar(document, caretOffset);%0A      String nextChar = getNextChar(documen
 t, c
@@ -1784,13 +1784,8 @@
 fset
- + 1)
 );%0A%0A
@@ -1794,16 +1794,40 @@
     if (
+prevChar.equals(%22 %22) && 
 !nextCha
@@ -1862,36 +1862,24 @@
         
-editor.getD
+d
 ocument
-()
 .insertS
@@ -2057,36 +2057,24 @@
 ocument(
-editor.getD
+d
 ocument
-()
 );%0A     
@@ -2159,11 +2159,372 @@
 xt);%0A  %7D
+%0A%0A  private String getPreviousChar(Document document, int offset) %7B%0A    return offset %3C= 0%0A        ? %22 %22%0A        : document.getText(new TextRange(offset - 1, offset));%0A  %7D%0A%0A  private String getNextChar(Document document, int offset) %7B%0A    return offset %3E= document.getTextLength()%0A        ? %22 %22%0A        : document.getText(new TextRange(offset, offset + 1));%0A  %7D
 %0A%7D%0A

Make quote handler smarter and add support for matching single quotes. (#3)