// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.intellij.sdk.editor;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

import java.util.List;

/**
 * @author Anna Bulenkova
 */
public class EditorAreaIllustration extends AnAction {
  @Override
  public void actionPerformed(AnActionEvent anActionEvent) {
    final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
    CaretModel caretModel = editor.getCaretModel();

    System.out.println("\n");
    System.out.print("caretModel.isUpToDate: " + caretModel.isUpToDate() + ", ");
    System.out.println("Number of carets: " + caretModel.getAllCarets().size());
    Caret primary = caretModel.getPrimaryCaret();
    System.out.println("Primary caret is valid? " +  primary.isValid() + ", has: " + primary.getLogicalPosition().toString() + ", Offset: " + primary.getOffset());

    LogicalPosition logicalPosition = caretModel.getLogicalPosition();
    VisualPosition visualPosition = caretModel.getVisualPosition();
    int offset = caretModel.getOffset();
    Messages.showInfoMessage(logicalPosition.toString() + "\n" +
                             visualPosition.toString() + "\n" +
                             "Offset: " + offset,
                             "Caret Parameters Inside The Editor");
  }

  @Override
  public void update(AnActionEvent e) {
    //Get required data keys
    final Project project = e.getData(CommonDataKeys.PROJECT);
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    //Set visibility only in case of existing project and editor
    e.getPresentation().setVisible(project != null && editor != null);
  }
}

@@ -660,61 +660,20 @@
 ();%0A
-%0A
     
-System.out.println(%22%5Cn%22);%0A    System.out.print(%22
+boolean 
 care
@@ -678,27 +678,24 @@
 retModel
-.is
 UpToDate
 : %22 + ca
@@ -686,21 +686,18 @@
 UpToDate
-: %22 +
+ =
  caretMo
@@ -716,63 +716,34 @@
 te()
- + %22, %22);%0A    System.out.println(%22Number of carets: %22 +
+;%0A    Caret primaryCaret =
  car
@@ -757,64 +757,59 @@
 .get
-All
+Primary
 Caret
-s
 ()
-.size());%0A    Caret primary = caretModel.getP
+;%0A    boolean primaryCaretIsValid = p
 rima
@@ -807,32 +807,40 @@
 d = primaryCaret
+.isValid
 ();%0A    System.o
@@ -836,133 +836,276 @@
    S
-ystem.out.println(%22P
+tringBuilder report = new StringBuilder();%0A    if ( caretModelUpToDate && p
 rimary
- c
+C
 aret
- is valid? %22 +  primary.isValid() + %22, has: %22 + primary.getLogicalPosition().toString() + %22, 
+IsValid ) %7B%0A      report.append(primaryCaret.getLogicalPosition().toString() + %22%5Cn%22 );%0A      report.append(primaryCaret.getVisualPosition().toString() + %22%5Cn%22 );%0A      report.append(%22
 Offs
@@ -1119,16 +1119,21 @@
  primary
+Caret
 .getOffs
@@ -1140,400 +1140,206 @@
 et()
+ 
 );%0A
-%0A    LogicalPosition logicalPosition = caretModel.getLogicalPosition();%0A    VisualPosition visualPosition = caretModel.getVisualPosition();%0A    int offset = caretModel.getOffset();%0A    Messages.showInfoMessage(logicalPosition.toString() + %22%5Cn%22 +%0A                             visualPosition.toString() + %22%5Cn%22 +%0A                             %22Offset: %22 + offset,%0A                             
+    %7D else %7B%0A      report.append(%22Caret model up to date: %22 + caretModelUpToDate + %22%5Cn%22 + %22Primary Caret is valid: %22 + primaryCaretIsValid);%0A    %7D%0A    Messages.showInfoMessage(report.toString(),
 %22Car
@@ -1377,16 +1377,18 @@
 %22);%0A  %7D%0A
+  
 %0A  @Over

Convert to primary caret