-
Intellij 플러그인 개발기 - ② [기능 코드 작성하기]툴 사용법/IntelliJ 2023. 5. 6. 13:37
[1] update method orveride
기능을 구현하기 전에 먼저 해당 액션이 사용가능한 곳에서만 보일 수 있게 하여야 하는데,
이는 update메서드를 통해서 적용시킬 수 있다.
인텔리 제이가 제공하는 샘플코드는 다음과 같으며
@Override public void update(@NotNull AnActionEvent e) { final PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE); final Editor editor = e.getData(CommonDataKeys.EDITOR); boolean visible = false; if (psiFile != null && editor != null) { visible = isClassElement(psiFile, editor); } e.getPresentation().setEnabledAndVisible(visible); }
final PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
PsiFile은 파일로부터 계층구조를 읽어오는 인텔리제이가 제공하는 인터페이스이다.
final Editor editor = e.getData(CommonDataKeys.EDITOR);Editor는 사용자가 접근한 편집기 (Caret, Cursor등을 알아 올 수 있는 객체)
해당 Caret의 위치가 클래스 요소가 존재하는 곳에 있는지 확인 후 존재할 경우에만 해당 버튼이 활성화 되도록 하였다.
[2] actionPerformed method orveride
버튼이 클릭되었을 때 동작해야하는 로직을 작성하는 메서드이다.
@Override public void actionPerformed(AnActionEvent e) { PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE); Editor editor = e.getData(CommonDataKeys.EDITOR); if (psiFile == null || editor == null) return; PsiElement currentCaretElement = psiFile.findElementAt(editor.getCaretModel().getOffset()); String menuText = e.getPresentation().getText(); ResultMapMenu resultMapMenu = ResultMapMenu.findResultMapMenu(menuText); if (resultMapMenu == null) return; PsiClass psiClass = PsiTreeUtil.getParentOfType(currentCaretElement, PsiClass.class); int maxRecursiveCount = resultMapMenu.getMaxRecursiveCount(); AtomicInteger maxCount = new AtomicInteger(maxRecursiveCount); Map<String, ResultMapStructure> resultMap = new HashMap<>(); extractResultMap(psiClass, resultMap, maxCount); String resultMapString = createResultMapBuilder(resultMap); Toolkit.getDefaultToolkit() .getSystemClipboard() .setContents(new StringSelection(resultMapString), null); }
메뉴 버튼과, Class 요소를 통해 각각 재귀가능한 횟수, ResultMap으로 바꿔줄 클래스를 지정해주었고,
재귀를 통해 만들어진 Map을 통해 MyBatis의 ResultMap XML파일을 만들어 줄 수 있도록 제작하였다.
맨 밑의 Toolkit은 OS Clipboard에 해당 내용을 복사하여 붙여넣기만 하면 바로 사용할 수 있도록 하였다.
'툴 사용법 > IntelliJ' 카테고리의 다른 글
Intellij 플러그인 개발기 - ④ [GIF 업로드] (2) 2023.05.07 Intellij 플러그인 개발기 - ③ [프로젝트 서명, 게시하기] (0) 2023.05.07 Intellij 플러그인 개발기 - ① [프로젝트 생성하기] (0) 2023.05.06 스프링부트 프로젝트 (Gradle) 의존성 업데이트하기 (0) 2022.07.13