VcsSelection (@NotNull DataContext context) { final Editor editor = context.getData(CommonDataKeys.EDITOR); if (editor == null) return null; PsiElement psiElement = TargetElementUtil.findTargetElement(editor, TargetElementUtil.ELEMENT_NAME_ACCEPTED); if (psiElement == null) { return null; } if (!psiElement.isValid()) { return null; } if (psiElement instanceof PsiCompiledElement) { return null; } final String actionName; if (psiElement instanceof PsiClass) { actionName = VcsBundle.message("action.name.show.history.for.class"); } else if (psiElement instanceof PsiField) { actionName = VcsBundle.message("action.name.show.history.for.field"); } else if (psiElement instanceof PsiMethod) { actionName = VcsBundle.message("action.name.show.history.for.method"); } else if (psiElement instanceof PsiCodeBlock) { actionName = VcsBundle.message("action.name.show.history.for.code.block"); } else if (psiElement instanceof PsiStatement) { actionName = VcsBundle.message("action.name.show.history.for.statement"); } else { return null; } TextRange textRange = psiElement.getTextRange(); if (textRange == null) { return null; } VirtualFile virtualFile = psiElement.getContainingFile().getVirtualFile(); if (virtualFile == null) { return null; } if (!virtualFile.isValid()) { return null; } Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) { return null; } return new VcsSelection(document, textRange, actionName); }