From 2b8ed56db043c0cf3d1ad81b4e38de59f39cd169 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 4 Apr 2016 13:11:46 +0200 Subject: [PATCH] create test: fix at last position (IDEA-154055) --- .../testIntegration/JavaTestCreator.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java b/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java index cd3106f81235..9eb53d0ad160 100644 --- a/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java +++ b/java/java-impl/src/com/intellij/testIntegration/JavaTestCreator.java @@ -29,22 +29,29 @@ public class JavaTestCreator implements TestCreator { @Override public boolean isAvailable(Project project, Editor editor, PsiFile file) { - CreateTestAction action = new CreateTestAction(); - PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); - + final int offset = editor.getCaretModel().getOffset(); + PsiElement element = findElement(file, offset); return CreateTestAction.isAvailableForElement(element); } public void createTest(Project project, Editor editor, PsiFile file) { try { CreateTestAction action = new CreateTestAction(); - PsiElement element = file.findElementAt(editor.getCaretModel().getOffset()); + PsiElement element = findElement(file, editor.getCaretModel().getOffset()); if (CreateTestAction.isAvailableForElement(element)) { - action.invoke(project, editor, file.getContainingFile()); + action.invoke(project, editor, element); } } catch (IncorrectOperationException e) { LOG.warn(e); } } + + private static PsiElement findElement(PsiFile file, int offset) { + PsiElement element = file.findElementAt(offset); + if (element == null && offset == file.getTextLength()) { + element = file.findElementAt(offset - 1); + } + return element; + } }