Skip to content

Commit

Permalink
Remove deprecated API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
picimako committed Oct 15, 2023
1 parent d94103c commit 533ad38
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## [1.62.0]
### Changed
- New supported IDE version range: 2022.1-2023.3
- Removed some deprecated API usage, and simplified some related code.

## [1.61.0]
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.github.kumaraman21.intellijbehave.creator;

import static com.github.kumaraman21.intellijbehave.language.StoryFileType.STORY_FILE_TYPE;

import com.intellij.ide.IdeBundle;
import com.intellij.ide.actions.CreateElementActionBase;
import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateManager;
import com.intellij.ide.highlighter.HtmlFileType;
import com.intellij.openapi.actionSystem.AnActionEvent;
Expand All @@ -28,49 +29,45 @@
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.codeStyle.CodeStyleManager;
import org.jetbrains.annotations.NotNull;

import static com.github.kumaraman21.intellijbehave.language.StoryFileType.STORY_FILE_TYPE;
import java.util.function.Consumer;

public class CreateStoryAction extends CreateElementActionBase {

public CreateStoryAction() {
super("Create New Story File", STORY_FILE_TYPE.getDescription(), STORY_FILE_TYPE.getIcon());
}

@NotNull
@Override
protected PsiElement[] invokeDialog(Project project, PsiDirectory directory) {
CreateElementActionBase.MyInputValidator validator = new CreateElementActionBase.MyInputValidator(project, directory);
protected void invokeDialog(@NotNull Project project, @NotNull PsiDirectory directory, @NotNull Consumer<? super PsiElement[]> elementsConsumer) {
var validator = new CreateElementActionBase.MyInputValidator(project, directory);
Messages.showInputDialog(project, "Enter a new file name:", "New Story File", Messages.getQuestionIcon(), "", validator);
return validator.getCreatedElements();
elementsConsumer.accept(validator.getCreatedElements());
}

@NotNull
@Override
protected PsiElement[] create(@NotNull String newName, PsiDirectory directory) throws Exception {
final FileTemplate template = FileTemplateManager.getDefaultInstance().getTemplate(STORY_FILE_TYPE.getName());
protected PsiElement @NotNull [] create(@NotNull String newName, PsiDirectory directory) throws Exception {
final var template = FileTemplateManager.getDefaultInstance().getTemplate(STORY_FILE_TYPE.getName());

String fileName = getFileName(newName);
Project project = directory.getProject();

directory.checkCreateFile(fileName);
PsiFile psiFile = PsiFileFactory.getInstance(project)
.createFileFromText(fileName, STORY_FILE_TYPE, template.getText());
var psiFile = PsiFileFactory.getInstance(project).createFileFromText(fileName, STORY_FILE_TYPE, template.getText());

if (template.isReformatCode()) {
CodeStyleManager.getInstance(project).reformat(psiFile);
}
psiFile = (PsiFile)directory.add(psiFile);

final VirtualFile virtualFile = psiFile.getVirtualFile();
FileEditorManager.getInstance(project).openFile(virtualFile, true);
FileEditorManager.getInstance(project).openFile(psiFile.getVirtualFile(), true);

return new PsiElement[]{psiFile};
}
Expand All @@ -80,27 +77,23 @@ protected String getErrorTitle() {
return "Cannot Create Story File";
}

@NotNull
@Override
protected String getActionName(PsiDirectory directory, String newName) {
protected String getActionName(PsiDirectory directory, @NotNull String newName) {
return IdeBundle.message("progress.creating.file", STORY_FILE_TYPE.getName(), newName, directory.getName());
}

public void update(final AnActionEvent e) {
@Override
public void update(final @NotNull AnActionEvent e) {
super.update(e);
Presentation presentation = e.getPresentation();
final FileTypeManager manager = FileTypeManager.getInstance();
final FileType fileType = manager.getFileTypeByExtension(HtmlFileType.DOT_DEFAULT_EXTENSION);
final FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(HtmlFileType.DOT_DEFAULT_EXTENSION);
if (fileType == FileTypes.PLAIN_TEXT) {
presentation.setEnabled(false);
presentation.setVisible(false);
presentation.setEnabledAndVisible(false);
}
}

private String getFileName(String name) {
if (name.endsWith("." + STORY_FILE_TYPE.getDefaultExtension())) {
return name;
} else {
return name + "." + STORY_FILE_TYPE.getDefaultExtension();
}
}
return name.endsWith("." + STORY_FILE_TYPE.getDefaultExtension()) ? name : name + "." + STORY_FILE_TYPE.getDefaultExtension();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.kumaraman21.intellijbehave.kotlin.KotlinConfigKt;
import com.github.kumaraman21.intellijbehave.kotlin.support.services.KotlinAnnotationsLoader;
import com.github.kumaraman21.intellijbehave.parser.JBehaveStep;
Expand All @@ -27,18 +18,25 @@
import com.intellij.psi.impl.java.stubs.index.JavaFullClassNameIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.QualifiedName;
import com.intellij.util.ReflectionUtil;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Project service that provides Java step definitions for JBehave Story steps.
*/
@Service(Service.Level.PROJECT)
public final class JBehaveStepsIndex {

//Argument is necessary for project-level service creation
public JBehaveStepsIndex(Project project) {
}

Expand Down Expand Up @@ -119,24 +117,11 @@ private static Collection<PsiAnnotation> getAllStepAnnotations(@NotNull final Ps

@Nullable
private PsiClass findStepAnnotation(String stepClass, Module module, GlobalSearchScope dependenciesScope) {
Method getPre20221 = ReflectionUtil.getDeclaredMethod(JavaFullClassNameIndex.class, "get", Integer.class, Project.class, GlobalSearchScope.class);
Method get20221 = ReflectionUtil.getDeclaredMethod(JavaFullClassNameIndex.class, "get", CharSequence.class, Project.class, GlobalSearchScope.class);

try {
Object javaFullClassNameIndexInstance = JavaFullClassNameIndex.class.getDeclaredMethod("getInstance").invoke(JavaFullClassNameIndex.class);
Collection<PsiClass> stepDefAnnotationCandidates = Collections.emptyList();
if (getPre20221 != null) {
stepDefAnnotationCandidates = (Collection<PsiClass>) getPre20221.invoke(javaFullClassNameIndexInstance, stepClass.hashCode(), module.getProject(), dependenciesScope);
} else if (get20221 != null) {
stepDefAnnotationCandidates = (Collection<PsiClass>) get20221.invoke(javaFullClassNameIndexInstance, stepClass, module.getProject(), dependenciesScope);
}
for (PsiClass stepDefAnnotations : stepDefAnnotationCandidates) {
if (stepClass.equals(stepDefAnnotations.getQualifiedName())) {
return stepDefAnnotations;
}
var stepDefAnnotationCandidates = JavaFullClassNameIndex.getInstance().get(stepClass, module.getProject(), dependenciesScope);
for (PsiClass stepDefAnnotations : stepDefAnnotationCandidates) {
if (stepClass.equals(stepDefAnnotations.getQualifiedName())) {
return stepDefAnnotations;
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
//fall through and return null
}
return null;
}
Expand Down

0 comments on commit 533ad38

Please sign in to comment.