mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
EDU-809 Possible to break answer placeholder
This commit is contained in:
+3
-2
@@ -5,6 +5,7 @@ import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.fileEditor.FileEditorManager;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -54,7 +55,7 @@ public class StudyAnswerPlaceholderExtendWordHandler implements ExtendWordSelect
|
||||
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
|
||||
AnswerPlaceholder placeholder = getAnswerPlaceholder(e, cursorOffset);
|
||||
assert placeholder != null;
|
||||
int startOffset = placeholder.getOffset();
|
||||
return Collections.singletonList(new TextRange(startOffset, startOffset + placeholder.getRealLength()));
|
||||
final Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(placeholder, editor.getDocument());
|
||||
return Collections.singletonList(new TextRange(offsets.getFirst(), offsets.getSecond()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.intellij.openapi.ui.MessageType;
|
||||
import com.intellij.openapi.ui.popup.Balloon;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.io.FileUtilRt;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
@@ -47,6 +48,7 @@ import com.intellij.psi.PsiFile;
|
||||
import com.intellij.ui.JBColor;
|
||||
import com.intellij.ui.awt.RelativePoint;
|
||||
import com.intellij.ui.content.Content;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.TimeoutUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -831,4 +833,18 @@ public class StudyUtils {
|
||||
}
|
||||
return FileUtil.getRelativePath(taskDir.getPath(), file.getPath(), '/');
|
||||
}
|
||||
|
||||
public static Pair<Integer, Integer> getPlaceholderOffsets(@NotNull final AnswerPlaceholder answerPlaceholder,
|
||||
@NotNull final Document document) {
|
||||
int startOffset = answerPlaceholder.getOffset();
|
||||
int delta = 0;
|
||||
final int length = answerPlaceholder.getRealLength();
|
||||
int nonSpaceCharOffset = DocumentUtil.getFirstNonSpaceCharOffset(document, startOffset, startOffset + length);
|
||||
if (nonSpaceCharOffset != startOffset) {
|
||||
delta = startOffset - nonSpaceCharOffset;
|
||||
startOffset = nonSpaceCharOffset;
|
||||
}
|
||||
final int endOffset = startOffset + length + delta;
|
||||
return Pair.create(startOffset, endOffset);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -10,6 +10,7 @@ import com.intellij.openapi.editor.event.EditorMouseAdapter;
|
||||
import com.intellij.openapi.editor.event.EditorMouseEvent;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.ToolWindow;
|
||||
import com.intellij.openapi.wm.ToolWindowManager;
|
||||
@@ -46,8 +47,8 @@ public class StudyEditorFactoryListener implements EditorFactoryListener {
|
||||
if (answerPlaceholder == null || answerPlaceholder.getSelected()) {
|
||||
return;
|
||||
}
|
||||
int startOffset = answerPlaceholder.getOffset();
|
||||
editor.getSelectionModel().setSelection(startOffset, startOffset + answerPlaceholder.getRealLength());
|
||||
final Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(answerPlaceholder, editor.getDocument());
|
||||
editor.getSelectionModel().setSelection(offsets.getFirst(), offsets.getSecond());
|
||||
answerPlaceholder.setSelected(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user