Top Banner
IntelliJ IDEA PLUGIN DEVELOPMENT
29

IntelliJ IDEA Plugin Development

Feb 12, 2017

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IntelliJ IDEA Plugin Development

IntelliJ IDEA

PLUGIN DEVELOPMENT

Page 2: IntelliJ IDEA Plugin Development

Plugin Components

Components are the fundamental concept of plugin integration. There are three kinds of components:

Application-level Project-level Module-level

Page 3: IntelliJ IDEA Plugin Development

Plugin Extensions and Extension Points

The IntelliJ Platform provides the concept of extensions and extension points that allows a plugin to interact with other plugins or with the IDE itself.

Page 4: IntelliJ IDEA Plugin Development

Plugin Services

A service is a plugin component loaded on demand. IntelliJ Platform ensures that only one instance of a service is loaded even though the service is called several times.

Page 5: IntelliJ IDEA Plugin Development

Plugin Actions

Intellij IDEA provides the concept of actions. An action is a class, derived from the AnAction class, whose actionPerformed method is called when the menu item or toolbar button is selected.

Page 6: IntelliJ IDEA Plugin Development

Plugin Content

.IntelliJIDEAx0 plugins sample.jar/ com/foo/..... ... ... META-INF plugin.xml

Page 7: IntelliJ IDEA Plugin Development

Android Styler plugin development

Page 8: IntelliJ IDEA Plugin Development

layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">

    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:text="@string/stub"        android:textColor="@android:color/black"/>

</LinearLayout>

Page 9: IntelliJ IDEA Plugin Development

styles.xml

<style name="TextStub">     <item name="android:layout_width">wrap_content</item>     <item name="android:layout_height">wrap_content</item>     <item name="android:layout_gravity">center</item>     <item name="android:textColor">@android:color/black</item></style>

Page 10: IntelliJ IDEA Plugin Development

Default solution

Page 11: IntelliJ IDEA Plugin Development

Extract style

Page 12: IntelliJ IDEA Plugin Development

Usage

copy lines with future style from your layout.xml file paste it to styles.xml file with Ctrl+Shift+D  enter name of new style in the modal window  your style is prepared! 

Page 13: IntelliJ IDEA Plugin Development

Plan

Prepare environment Create project and implement functionality Build jar and upload it into Plugin Repository

Page 14: IntelliJ IDEA Plugin Development

Preparations

Install IntellijIDEA Community Edition Download sources of IntellijIDEA Community Edition

(optional)

Page 15: IntelliJ IDEA Plugin Development

Create project

Page 16: IntelliJ IDEA Plugin Development

plugin.xml<idea-plugin version="2"> <!-- … --> <depends>com.intellij.modules.lang</depends>

<!-- … -->

<actions> <!-- Add your actions here --> <action id="3421" class="pro.alex_zaitsev.androidstyler.PasteAction" text="Paste Style" description="Paste Style"> <add-to-group group-id="EditorPopupMenu" anchor="after" relative-to-action="PasteMultiple"/> <add-to-group group-id="ConsoleView.PopupMenu" anchor="after" relative-to-action="ConsoleView.Copy"/> <add-to-group group-id="EditorActions" anchor="first"/> <keyboard-shortcut keymap="$default" first-keystroke="ctrl shift D"/> </action> </actions>

</idea-plugin>

Page 17: IntelliJ IDEA Plugin Development

<depends> tag

<depends>com.intellij.modules.lang</depends>

Page 18: IntelliJ IDEA Plugin Development

<actions> tag

<actions> <!-- Add your actions here --> <action id="3421" class="pro.alex_zaitsev.androidstyler.PasteAction" text="Paste Style" description="Paste Style"> <add-to-group group-id="EditorPopupMenu" anchor="after" relative-to-action="PasteMultiple"/> <add-to-group group-id="ConsoleView.PopupMenu" anchor="after" relative-to-action="ConsoleView.Copy"/> <add-to-group group-id="EditorActions" anchor="first"/> <keyboard-shortcut keymap="$default" first-keystroke="ctrl shift D"/> </action></actions>

Page 19: IntelliJ IDEA Plugin Development

PasteActionpublic class PasteAction extends EditorAction {

public PasteAction(EditorActionHandler defaultHandler) { super(defaultHandler);

}

public PasteAction() {

this(new StylePasteHandler()); }

private static class StylePasteHandler extends EditorWriteActionHandler { private StylePasteHandler() {

}

@Override public void executeWriteAction(Editor editor, DataContext dataContext) {

// implementation }

}

}

Page 20: IntelliJ IDEA Plugin Development

Implementation

Page 21: IntelliJ IDEA Plugin Development

Get text from buffer using CopyPasteManager

private String getCopiedText() { try { return (String) CopyPasteManager.getInstance().getContents().getTransferData(DataFlavor.stringFlavor); } catch (NullPointerException | IOException | UnsupportedFlavorException e) { e.printStackTrace(); } return null;}

Page 22: IntelliJ IDEA Plugin Development

Get new style name

private String getStyleName() { return (String) JOptionPane.showInputDialog( new JFrame(), Consts.DIALOG_NAME_CONTENT, Consts.DIALOG_NAME_TITLE, JOptionPane.PLAIN_MESSAGE, null, null, "");}

Page 23: IntelliJ IDEA Plugin Development

Delete selected text

private void deleteSelectedText(Editor editor, Document document) { SelectionModel selectionModel = editor.getSelectionModel(); document.deleteString(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());}

Page 24: IntelliJ IDEA Plugin Development

Insert result, move caret and scroll

CaretModel caretModel = editor.getCaretModel();// insert new string into the documentdocument.insertString(caretModel.getOffset(), output);// move caret to the end of inserted textcaretModel.moveToOffset(caretModel.getOffset() + output.length());// scroll to the end of inserted texteditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);

Page 25: IntelliJ IDEA Plugin Development

Publish

Page 26: IntelliJ IDEA Plugin Development

Build jar

Page 27: IntelliJ IDEA Plugin Development

Upload to JetBrains Plugin Repository

https://plugins.jetbrains.com/

Wait for approve (up to 3 work days)

Page 28: IntelliJ IDEA Plugin Development

Share

Page 29: IntelliJ IDEA Plugin Development

Have a good plugin development!