do not remove trailing spaces for placeholders with indent
authorLiana.Bakradze <liana.bakradze@jetbrains.com>
Wed, 26 Oct 2016 16:36:58 +0000 (19:36 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Thu, 17 Nov 2016 14:08:33 +0000 (17:08 +0300)
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/placeholder/CCChangePlaceholderVisibility.java
python/educational-core/student/src/com/jetbrains/edu/learning/core/EduAnswerPlaceholderPainter.java

index 27acf180559fbdafda27a2c96b0c5c9b3e81e951..e8a038bb4b6e433dafcdafaf2b58d0dafc3a4b39 100644 (file)
@@ -4,6 +4,9 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.command.undo.BasicUndoableAction;
 import com.intellij.openapi.command.undo.UnexpectedUndoException;
+import com.intellij.openapi.editor.Document;
+import com.intellij.openapi.util.TextRange;
+import com.intellij.util.DocumentUtil;
 import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduUtils;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
@@ -42,9 +45,28 @@ public abstract class CCChangePlaceholderVisibility extends CCAnswerPlaceholderA
     placeholder.getActiveSubtaskInfo().setNeedInsertText(visible);
     int length = isVisible() ? placeholder.getTaskText().length() : 0;
     placeholder.setLength(length);
+    saveIndent(placeholder, state, !visible);
     StudyUtils.drawAllWindows(state.getEditor(), state.getTaskFile());
   }
 
+  private static void saveIndent(AnswerPlaceholder placeholder, CCState state, boolean visible) {
+    Document document = state.getEditor().getDocument();
+    int offset = placeholder.getOffset();
+    int lineNumber = document.getLineNumber(offset);
+    int nonSpaceCharOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, lineNumber);
+    int newOffset = offset;
+    int endOffset = offset + placeholder.getRealLength();
+    if (!visible && nonSpaceCharOffset == offset) {
+      newOffset = document.getLineStartOffset(lineNumber);
+    }
+    if (visible) {
+      newOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, offset, endOffset);
+    }
+    placeholder.setOffset(newOffset);
+    int delta = offset - newOffset;
+    placeholder.setPossibleAnswer(document.getText(TextRange.create(newOffset, newOffset + delta + placeholder.getRealLength())));
+  }
+
   protected abstract String getName();
 
   protected abstract boolean isVisible();
index 5628182bd2a16bfff7184717fd54ebfe534c363b..7d29f15b7e71f628824af3e26cca63562a3bb9b9 100644 (file)
@@ -11,7 +11,9 @@ import com.intellij.openapi.editor.markup.*;
 import com.intellij.openapi.project.Project;
 import com.intellij.ui.JBColor;
 import com.intellij.ui.SimpleTextAttributes;
+import com.intellij.util.DocumentUtil;
 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
 import org.jetbrains.annotations.NotNull;
 
@@ -31,7 +33,7 @@ public class EduAnswerPlaceholderPainter {
                                            @NotNull final JBColor color) {
     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
     final TextAttributes textAttributes = new TextAttributes(scheme.getDefaultForeground(), scheme.getDefaultBackground(), null,
-                                                                    EffectType.BOXED, Font.PLAIN);
+                                                             EffectType.BOXED, Font.PLAIN);
     textAttributes.setEffectColor(color);
     if (placeholder.isActive()) {
       drawAnswerPlaceholder(editor, placeholder, textAttributes, PLACEHOLDERS_LAYER);
@@ -45,12 +47,22 @@ public class EduAnswerPlaceholderPainter {
                                            @NotNull AnswerPlaceholder placeholder,
                                            TextAttributes textAttributes,
                                            int placeholdersLayer) {
-    final int startOffset = placeholder.getOffset();
-    if (startOffset == - 1) {
+    int startOffset = placeholder.getOffset();
+    if (startOffset == -1) {
       return;
     }
     final int length = placeholder.getRealLength();
-    final int endOffset = startOffset + length;
+    int delta = 0;
+    AnswerPlaceholderSubtaskInfo info = placeholder.getActiveSubtaskInfo();
+    if (info != null && info.isNeedInsertText()) {
+      Document document = editor.getDocument();
+      int nonSpaceCharOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, startOffset, startOffset + length);
+      if (nonSpaceCharOffset != startOffset) {
+        delta = startOffset - nonSpaceCharOffset;
+        startOffset = nonSpaceCharOffset;
+      }
+    }
+    final int endOffset = startOffset + length + delta;
     drawAnswerPlaceholder(editor, startOffset, endOffset, textAttributes, placeholdersLayer);
   }
 
@@ -61,7 +73,7 @@ public class EduAnswerPlaceholderPainter {
                                            int placeholdersLayer) {
     final Project project = editor.getProject();
     assert project != null;
-    if (start == - 1) {
+    if (start == -1) {
       return;
     }
     RangeHighlighter
@@ -72,7 +84,6 @@ public class EduAnswerPlaceholderPainter {
   }
 
 
-
   public static void drawAnswerPlaceholderFromPrevStep(@NotNull Editor editor, int start, int end) {
     EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
     Color color = scheme.getColor(EditorColors.TEARLINE_COLOR);
@@ -112,5 +123,4 @@ public class EduAnswerPlaceholderPainter {
       }
     }
   }
-
 }