<depends>com.jetbrains.edu.interactivelearning</depends>
<extensions defaultExtensionNs="com.intellij">
- <projectService serviceImplementation="com.jetbrains.edu.coursecreator.CCProjectService"/>
<treeStructureProvider implementation="com.jetbrains.edu.coursecreator.projectView.CCTreeStructureProvider"/>
<refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
- <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCLessonMoveHandlerDelegate" order="first"/>
- <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCTaskMoveHandlerDelegate" order="first"/>
- <renameHandler implementation="com.jetbrains.edu.coursecreator.CCTaskRenameHandler" order="first"/>
- <renameHandler implementation="com.jetbrains.edu.coursecreator.CCLessonRenameHandler" order="first"/>
+ <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCLessonMoveHandlerDelegate" order="first"/>
+ <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCTaskMoveHandlerDelegate" order="first"/>
+ <renameHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCTaskRenameHandler" order="first"/>
+ <renameHandler implementation="com.jetbrains.edu.coursecreator.handlers.CCLessonRenameHandler" order="first"/>
</extensions>
<extensions defaultExtensionNs="Edu">
<studyActionsProvider implementation="com.jetbrains.edu.coursecreator.CCStudyActionsProvider"/>
</application-components>
<project-components>
- <!-- Add your project components here -->
<component>
<implementation-class>com.jetbrains.edu.coursecreator.CCProjectComponent</implementation-class>
</component>
<action id="AddTaskFile" class="com.jetbrains.edu.coursecreator.actions.CCAddAsTaskFile" text="Add As Task File">
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
</action>
- <action id="AddTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCAddAnswerPlaceholder">
- <add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
- </action>
+ <group id="AnswerPlaceholderGroup">
+ <action id="AddTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCAddAnswerPlaceholder"/>
+ <separator/>
+ <add-to-group group-id="EditorPopupMenu" anchor="first"/>
+ </group>
<action id="ShowTaskWindowDetails" class="com.jetbrains.edu.coursecreator.actions.CCShowAnswerPlaceholderDetails">
<add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
</action>
- <action id="DeleteTaskWindow" class="com.jetbrains.edu.coursecreator.actions.CCDeleteAnswerPlaceholder">
- <add-to-group group-id="EditorPopupMenu" anchor="before" relative-to-action="CopyReference"/>
- </action>
<action id="ShowPreview" class="com.jetbrains.edu.coursecreator.actions.CCShowPreview">
<add-to-group group-id="ProjectViewPopupMenu" anchor="first"/>
<add-to-group group-id="EditorTabPopupMenu"/>
package com.jetbrains.edu.coursecreator.actions;
import com.intellij.openapi.actionSystem.AnActionEvent;
-import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
-import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
-import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.ui.JBColor;
-import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
-import com.jetbrains.edu.coursecreator.CCProjectService;
import com.jetbrains.edu.coursecreator.ui.CCCreateAnswerPlaceholderDialog;
-import com.jetbrains.edu.learning.courseFormat.*;
+import com.jetbrains.edu.learning.StudyUtils;
+import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
+import com.jetbrains.edu.learning.core.EduNames;
+import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
+import com.jetbrains.edu.learning.courseFormat.TaskFile;
import org.jetbrains.annotations.NotNull;
import java.util.List;
-public class CCAddAnswerPlaceholder extends DumbAwareAction {
- private static final Logger LOG = Logger.getInstance(CCAddAnswerPlaceholder.class);
+public class CCAddAnswerPlaceholder extends CCAnswerPlaceholderAction {
public CCAddAnswerPlaceholder() {
- super("Add Answer Placeholder", "Add answer placeholder", null);
+ super("Add/Delete Answer Placeholder", "Add/Delete answer placeholder", null);
}
return false;
}
- @Override
- public void actionPerformed(@NotNull AnActionEvent e) {
- final Project project = e.getData(CommonDataKeys.PROJECT);
- if (project == null) {
+ private static void addPlaceholder(@NotNull CCState state) {
+ Editor editor = state.getEditor();
+ Project project = state.getProject();
+ PsiFile file = state.getFile();
+
+ final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
+ if (document == null) {
return;
}
- final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
- if (file == null) return;
- final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
- if (editor == null) return;
+
final SelectionModel model = editor.getSelectionModel();
- final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
- if (document == null) return;
final int start = model.getSelectionStart();
- final int end = model.getSelectionEnd();
final int lineNumber = document.getLineNumber(start);
int realStart = start - document.getLineStartOffset(lineNumber);
-
- final CCProjectService service = CCProjectService.getInstance(project);
- final PsiDirectory taskDir = file.getContainingDirectory();
- final PsiDirectory lessonDir = taskDir.getParent();
- if (lessonDir == null) return;
-
- final TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
- if (taskFile == null) {
- return;
- }
- if (arePlaceholdersIntersect(taskFile, document, start, end)) {
- return;
- }
final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
answerPlaceholder.setLine(lineNumber);
answerPlaceholder.setStart(realStart);
- answerPlaceholder.setPossibleAnswer(model.getSelectedText());
+ String selectedText = model.getSelectedText();
+ answerPlaceholder.setPossibleAnswer(selectedText);
- CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder
- );
+ CCCreateAnswerPlaceholderDialog dlg = new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder);
dlg.show();
if (dlg.getExitCode() != DialogWrapper.OK_EXIT_CODE) {
return;
}
+
+ TaskFile taskFile = state.getTaskFile();
int index = taskFile.getAnswerPlaceholders().size() + 1;
answerPlaceholder.setIndex(index);
taskFile.addAnswerPlaceholder(answerPlaceholder);
}
@Override
- public void update(@NotNull AnActionEvent event) {
- if (!CCProjectService.setCCActionAvailable(event)) {
+ protected void performAnswerPlaceholderAction(@NotNull CCState state) {
+ if (canAddPlaceholder(state)) {
+ addPlaceholder(state);
return;
}
+ if (canDeletePlaceholder(state)) {
+ deletePlaceholder(state);
+ }
+ }
+
+ private static void deletePlaceholder(@NotNull CCState state) {
+ Project project = state.getProject();
+ PsiFile psiFile = state.getFile();
+ final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
+ if (document == null) return;
+ TaskFile taskFile = state.getTaskFile();
+ AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
+ final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
+ if (answerPlaceholders.contains(answerPlaceholder)) {
+ answerPlaceholders.remove(answerPlaceholder);
+ final Editor editor = state.getEditor();
+ editor.getMarkupModel().removeAllHighlighters();
+ StudyUtils.drawAllWindows(editor, taskFile);
+ EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, false);
+ }
+ }
+
+ @Override
+ public void update(@NotNull AnActionEvent event) {
final Presentation presentation = event.getPresentation();
- final Project project = event.getData(CommonDataKeys.PROJECT);
- if (project == null) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
+ presentation.setEnabledAndVisible(false);
+
+ CCState state = getState(event);
+ if (state == null) {
return;
}
- final Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
- final PsiFile file = CommonDataKeys.PSI_FILE.getData(event.getDataContext());
- if (editor == null || file == null) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
+
+ presentation.setVisible(true);
+ if (canAddPlaceholder(state) || canDeletePlaceholder(state)) {
+ presentation.setEnabled(true);
+ presentation.setText((state.getAnswerPlaceholder() == null ? "Add " : "Delete ") + EduNames.PLACEHOLDER);
}
+ }
+
+
+ private static boolean canAddPlaceholder(@NotNull CCState state) {
+ Editor editor = state.getEditor();
SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
+ return false;
}
int start = selectionModel.getSelectionStart();
int end = selectionModel.getSelectionEnd();
+ return !arePlaceholdersIntersect(state.getTaskFile(), editor.getDocument(), start, end);
+ }
- final CCProjectService service = CCProjectService.getInstance(project);
- final PsiDirectory taskDir = file.getContainingDirectory();
- final PsiDirectory lessonDir = taskDir.getParent();
- if (lessonDir == null) return;
-
- final Course course = service.getCourse();
- final Lesson lesson = course.getLesson(lessonDir.getName());
- if (lesson == null) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
- }
- final Task task = lesson.getTask(taskDir.getName());
- if (task == null) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
+ private static boolean canDeletePlaceholder(@NotNull CCState state) {
+ if (state.getEditor().getSelectionModel().hasSelection()) {
+ return false;
}
- TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
- if (taskFile == null) {
- LOG.info("could not find task file");
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
- }
- if (arePlaceholdersIntersect(taskFile, editor.getDocument(), start, end)) {
- presentation.setVisible(false);
- presentation.setEnabled(false);
- return;
- }
- presentation.setVisible(true);
- presentation.setEnabled(true);
+ return state.getAnswerPlaceholder() != null;
}
}
\ No newline at end of file
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
-import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
+import com.jetbrains.edu.coursecreator.CCUtils;
+import com.jetbrains.edu.learning.StudyUtils;
import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.coursecreator.CCProjectService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
}
@Nullable
- private static CCState getState(@NotNull AnActionEvent e) {
+ protected static CCState getState(@NotNull AnActionEvent e) {
final Project project = e.getProject();
- if (project == null || CCProjectService.getInstance(project).getCourse() == null) {
+ if (project == null || !CCUtils.isCourseCreator(project)) {
return null;
}
final PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
if (editor == null) {
return null;
}
- TaskFile taskFile = CCProjectService.getInstance(project).getTaskFile(virtualFile);
+ TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
if (taskFile == null) {
return null;
}
AnswerPlaceholder answerPlaceholder = taskFile.getAnswerPlaceholder(editor.getDocument(),
editor.getCaretModel().getLogicalPosition(),
true);
- if (answerPlaceholder == null) {
- return null;
- }
return new CCState(taskFile, answerPlaceholder, psiFile, editor, project);
}
- @Override
- public void update(@NotNull AnActionEvent e) {
- Presentation presentation = e.getPresentation();
- boolean isAvailable = getState(e) != null;
- presentation.setEnabled(isAvailable);
- presentation.setVisible(isAvailable);
- }
-
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
CCState state = getState(e);
private Project myProject;
public CCState(@NotNull final TaskFile taskFile,
- @NotNull final AnswerPlaceholder answerPlaceholder,
+ @Nullable final AnswerPlaceholder answerPlaceholder,
@NotNull final PsiFile file,
@NotNull final Editor editor,
@NotNull final Project project) {
return myTaskFile;
}
- @NotNull
+ @Nullable
public AnswerPlaceholder getAnswerPlaceholder() {
return myAnswerPlaceholder;
}
+++ /dev/null
-package com.jetbrains.edu.coursecreator.actions;
-
-import com.intellij.openapi.editor.Document;
-import com.intellij.openapi.editor.Editor;
-import com.intellij.openapi.project.Project;
-import com.intellij.psi.PsiDocumentManager;
-import com.intellij.psi.PsiFile;
-import com.jetbrains.edu.learning.core.EduAnswerPlaceholderPainter;
-import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
-import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.coursecreator.CCProjectService;
-import org.jetbrains.annotations.NotNull;
-
-import java.util.List;
-
-public class CCDeleteAnswerPlaceholder extends CCAnswerPlaceholderAction {
-
- public CCDeleteAnswerPlaceholder() {
- super("Delete Answer Placeholder","Delete answer placeholder", null);
- }
-
- @Override
- protected void performAnswerPlaceholderAction(@NotNull CCState state) {
- Project project = state.getProject();
- PsiFile psiFile = state.getFile();
- final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
- if (document == null) return;
- TaskFile taskFile = state.getTaskFile();
- AnswerPlaceholder answerPlaceholder = state.getAnswerPlaceholder();
- final List<AnswerPlaceholder> answerPlaceholders = taskFile.getAnswerPlaceholders();
- if (answerPlaceholders.contains(answerPlaceholder)) {
- answerPlaceholders.remove(answerPlaceholder);
- final Editor editor = state.getEditor();
- editor.getMarkupModel().removeAllHighlighters();
- CCProjectService.getInstance(project).drawAnswerPlaceholders(psiFile.getVirtualFile(), editor);
- EduAnswerPlaceholderPainter.createGuardedBlocks(editor, taskFile, false);
- }
- }
-}
\ No newline at end of file