/*
 * Copyright 2000-2014 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.codeInsight;

import com.intellij.codeInsight.editorActions.TypedHandlerDelegate;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.psi.PyStringLiteralExpression;

/**
 * Handles overtyping ':' in definitions.
 * User: dcheryasov
 * Date: May 29, 2009 4:42:03 AM
 */
public class PyKeywordTypedHandler extends TypedHandlerDelegate {
  @Override
  public Result beforeCharTyped(char character, Project project, Editor editor, PsiFile file, FileType fileType) {
    if (!(fileType instanceof PythonFileType)) return Result.CONTINUE; // else we'd mess up with other file types!
    if (character == ':') {
      final Document document = editor.getDocument();
      final int offset = editor.getCaretModel().getOffset();

      PsiElement token = file.findElementAt(offset - 1);
      if (token == null || offset >= document.getTextLength()) return Result.CONTINUE; // sanity check: beyond EOL

      PsiElement here_elt = file.findElementAt(offset);
      if (here_elt == null) return Result.CONTINUE; 
      if (here_elt instanceof PyStringLiteralExpression || here_elt.getParent() instanceof PyStringLiteralExpression) return Result.CONTINUE;

      // double colons aren't found in Python's syntax, so we can safely overtype a colon everywhere but strings.
      String here_text = here_elt.getText();
      if (":".equals(here_text)) {
        editor.getCaretModel().moveToOffset(offset + 1); // overtype, that is, jump over
        return Result.STOP;
      }
      PyUnindentingInsertHandler.unindentAsNeeded(project, editor, file);
    }

    return Result.CONTINUE; // the default
  }

}

@@ -1057,16 +1057,59 @@
 ression;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0A/**%0A *
@@ -1551,825 +1551,85 @@
     
-final Document document = editor.getDocument();%0A      final int offset = editor.getCaretModel().getOffset();%0A%0A      PsiElement token = file.findElementAt(offset - 1);%0A      if (token == null %7C%7C offset %3E= document.getTextLength()) return Result.CONTINUE; // sanity check: beyond EOL%0A%0A      PsiElement here_elt = file.findElementAt(offset);%0A      if (here_elt == null) return Result.CONTINUE; %0A      if (here_elt instanceof PyStringLiteralExpression %7C%7C here_elt.getParent() instanceof PyStringLiteralExpression) return Result.CONTINUE;%0A%0A      // double colons aren't found in Python's syntax, so we can safely overtype a colon everywhere but strings.%0A      String here_text = here_elt.getText();%0A      if (%22:%22.equals(here_text)) %7B%0A        editor.getCaretModel().moveToOffset(offset + 1); // overtype, that is, jump over
+Result res = getOverTypeResult(editor, file);%0A      if (res == Result.STOP) %7B
 %0A   
@@ -1640,27 +1640,19 @@
  return 
-R
+r
 es
-ult.STOP
 ;%0A      
@@ -1733,17 +1733,16 @@
 ;%0A    %7D%0A
-%0A
     retu
@@ -1781,10 +1781,941 @@
 lt%0A  %7D%0A%0A
+  @Nullable%0A  public Result getOverTypeResult(Editor editor, PsiFile file) %7B%0A    final Document document = editor.getDocument();%0A    final int offset = editor.getCaretModel().getOffset();%0A%0A    PsiElement token = file.findElementAt(offset - 1);%0A    if (token == null %7C%7C offset %3E= document.getTextLength()) return Result.CONTINUE; // sanity check: beyond EOL%0A%0A    PsiElement here_elt = file.findElementAt(offset);%0A    if (here_elt == null) return Result.CONTINUE;%0A    if (here_elt instanceof PyStringLiteralExpression %7C%7C here_elt.getParent() instanceof PyStringLiteralExpression) return Result.CONTINUE;%0A%0A    // double colons aren't found in Python's syntax, so we can safely overtype a colon everywhere but strings.%0A    String here_text = here_elt.getText();%0A    if (%22:%22.equals(here_text)) %7B%0A      editor.getCaretModel().moveToOffset(offset + 1); // overtype, that is, jump over%0A      return Result.STOP;%0A    %7D%0A    return null;%0A  %7D%0A
 %7D%0A

Fix PY-20836: Fix unindent when at end of document. Especially noticable in multiline console