From af2d69ff560716e5a44dc4e081b1d4369381e98a Mon Sep 17 00:00:00 2001 From: Liana Bakradze Date: Wed, 29 Jun 2016 17:01:42 +0300 Subject: [PATCH] EDU-658 Create placeholder without selection --- .../actions/CCAddAnswerPlaceholder.java | 22 +++++++++++-------- .../CCDeleteAllAnswerPlaceholdersAction.java | 2 +- .../jetbrains/edu/learning/core/EduNames.java | 3 ++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java index 2ad1d79faf1f..7de216ebcd34 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAnswerPlaceholder.java @@ -54,14 +54,13 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction { } final SelectionModel model = editor.getSelectionModel(); - final int offset = model.getSelectionStart(); + final int offset = model.hasSelection() ? model.getSelectionStart() : editor.getCaretModel().getOffset(); final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder(); answerPlaceholder.setOffset(offset); answerPlaceholder.setUseLength(false); - String selectedText = model.getSelectedText(); - answerPlaceholder.setPossibleAnswer(selectedText); + answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : EduNames.PLACEHOLDER); CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder); dlg.show(); @@ -69,6 +68,10 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction { return; } + if (!model.hasSelection()) { + DocumentUtil.writeInRunUndoTransparentAction(() -> document.insertString(offset, EduNames.PLACEHOLDER)); + } + TaskFile taskFile = state.getTaskFile(); int index = taskFile.getAnswerPlaceholders().size() + 1; answerPlaceholder.setIndex(index); @@ -147,7 +150,7 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction { presentation.setVisible(true); if (canAddPlaceholder(state) || canDeletePlaceholder(state)) { presentation.setEnabled(true); - presentation.setText((state.getAnswerPlaceholder() == null ? "Add " : "Delete ") + EduNames.PLACEHOLDER); + presentation.setText((state.getAnswerPlaceholder() == null ? "Add " : "Delete ") + EduNames.ANSWER_PLACEHOLDER); } } @@ -155,12 +158,13 @@ public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction { private static boolean canAddPlaceholder(@NotNull CCState state) { Editor editor = state.getEditor(); SelectionModel selectionModel = editor.getSelectionModel(); - if (!selectionModel.hasSelection()) { - return false; + if (selectionModel.hasSelection()) { + int start = selectionModel.getSelectionStart(); + int end = selectionModel.getSelectionEnd(); + return !arePlaceholdersIntersect(state.getTaskFile(), start, end); } - int start = selectionModel.getSelectionStart(); - int end = selectionModel.getSelectionEnd(); - return !arePlaceholdersIntersect(state.getTaskFile(), start, end); + int offset = editor.getCaretModel().getOffset(); + return state.getTaskFile().getAnswerPlaceholder(offset) == null; } private static boolean canDeletePlaceholder(@NotNull CCState state) { diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java index 7c10fba712b4..d5ca9e5b8838 100644 --- a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java +++ b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCDeleteAllAnswerPlaceholdersAction.java @@ -28,7 +28,7 @@ import java.util.List; public class CCDeleteAllAnswerPlaceholdersAction extends DumbAwareAction { - public static final String ACTION_NAME = "Delete All " + EduNames.PLACEHOLDER + "s"; + public static final String ACTION_NAME = "Delete All " + EduNames.ANSWER_PLACEHOLDER + "s"; public CCDeleteAllAnswerPlaceholdersAction() { super(ACTION_NAME); diff --git a/python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java b/python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java index 33dddd6a6a19..48fb4c23ef4a 100644 --- a/python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java +++ b/python/educational-core/student/src/com/jetbrains/edu/learning/core/EduNames.java @@ -46,7 +46,8 @@ public class EduNames { public static final String STUDY = "Study"; public static final String ADAPTIVE = "Adaptive"; - public static final String PLACEHOLDER = "Answer Placeholder"; + public static final String ANSWER_PLACEHOLDER = "Answer Placeholder"; + public static final String PLACEHOLDER = "placeholder"; public static final String SRC = "src"; private EduNames() { }