package com.jantvrdik.intellij.latte.editorActions;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.jantvrdik.intellij.latte.LatteLanguage;
import org.jetbrains.annotations.NotNull;

/**
 * Handles individual keystrokes.
 */
public class LatteTypedHandler extends TypedHandlerDelegate {
	@Override
	public Result beforeCharTyped(char charTyped, Project project, Editor editor, PsiFile file, FileType fileType) {
		// ignores typing '}' before '}'
		if (charTyped == '}' && project != null && file.getViewProvider().getLanguages().contains(LatteLanguage.INSTANCE)) {
			CaretModel caretModel = editor.getCaretModel();
			String text = editor.getDocument().getText();
			int offset = caretModel.getOffset();
			if (text.length() > offset && text.charAt(offset) == '}') {
				caretModel.moveToOffset(offset + 1);
				return Result.STOP;
			}
		}

		return super.beforeCharTyped(charTyped, project, editor, file, fileType);
	}

	@Override
	public Result charTyped(char charTyped, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
		// auto-inserts '}' after typing '{'
		if (charTyped == '{' && project != null && file.getViewProvider().getLanguages().contains(LatteLanguage.INSTANCE)) {
			int offset = editor.getCaretModel().getOffset();
			String text = editor.getDocument().getText();
			if (text.length() == offset || text.charAt(offset) != '}') {
				editor.getDocument().insertString(offset, "}");
				return Result.STOP;
			}
		}

		return super.charTyped(charTyped, project, editor, file);
	}
}

@@ -428,111 +428,524 @@
 l;%0A%0A
-/**%0A * Handles individual keystrokes.%0A */%0Apublic class LatteTypedHandler extends TypedHandlerDelegate %7B
+import java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A/**%0A * Handles individual keystrokes.%0A */%0Apublic class LatteTypedHandler extends TypedHandlerDelegate %7B%0A%0A%09private static Map%3CCharacter, Character%3E pairs = new HashMap%3CCharacter, Character%3E(3);%0A%09private static Set%3CCharacter%3E chars = new HashSet%3CCharacter%3E(3);%0A%0A%09static %7B%0A%09%09pairs.put('%7B', '%7D');%0A%09%09pairs.put('(', ')');%0A%09%09pairs.put('%5B', '%5D');%0A%09%09chars.add('%7D');%0A%09%09chars.add(')');%0A%09%09chars.add('%5D');%0A%09%7D%0A
 %0A%09@O
@@ -1115,20 +1115,29 @@
 char
-Typed == '%7D'
+s.contains(charTyped)
  && 
@@ -1425,19 +1425,25 @@
 set) == 
-'%7D'
+charTyped
 ) %7B%0A%09%09%09%09
@@ -1751,24 +1751,42 @@
 ng '%7B'%0A%09%09if 
+(pairs.containsKey
 (charTyped =
@@ -1787,15 +1787,9 @@
 yped
- == '%7B'
+)
  && 
@@ -1978,24 +1978,70 @@
 .getText();%0A
+%09%09%09Character pairChar = pairs.get(charTyped);%0A
 %09%09%09if (text.
@@ -2085,19 +2085,24 @@
 set) != 
-'%7D'
+pairChar
 ) %7B%0A%09%09%09%09
@@ -2147,11 +2147,27 @@
 et, 
-%22%7D%22
+pairChar.toString()
 );%0A%09

support for other types of brackets