當初年少懵懂,那年夏天填志願選專業,父母聽其他長輩說選擇計算機專業好。從那以後,我的身上就有了計院深深的烙印。從寢室到機房,從機房到圖書館,C、C++、Java……只要是寫點自己感興趣的東西,一坐便是幾個小時,但那時年輕,起身收拾,一路小跑會女神,仍舊輕輕鬆鬆。
現在工作了,我毫無意外地做着開發工作,寫代碼一忙就會忘記起來活動一下,經常等到忙完了就感覺腰和腿十分不舒服。直到今年的體檢報告下來之後,我才幡然醒悟:沒有一個好身體,就不能好好工作,完成自己遠(sheng)大(zhi)的(jia)夢(xin)想。
那開發究竟如何防“沉迷”?以下是我在工作中經常見到的方式:
1. 定鬧鐘 2. 定時喝水水杯 3. 定時喚醒手環
以上歸根結底都是定時提醒,但到底有沒有從源頭(IDE) 上來解決呢?譬如,讓你的工作環境 (IDE)歇一會 ?
最近我在 IDEA 插件發現了一個有趣的項目 StopCoding 停止代碼編寫,思路十分的簡單。今天,我就藉助這個項目及 EOS 插件的相關開發經驗,爲大家介紹下 IDEA 插件開發的相關操作。
本文將從以下幾個方面來進行 IDEA 插件開發及 StopCoding 的實現介紹:
- IntelliJ IDEA 與 IntelliJ Platform
- 開發環境搭建
- 開發的一個簡單的插件
- StopCoding原理及源碼解析
<1> intelliJDEA與intelliJ Platform
IntelliJ IDEA 簡稱 IDEA,是 Jetbrains 公司旗下的一款 JAVA 開發工具,支持 Java、Scala、Groovy 等語言的開發,同時具備支持目前主流的技術和框架,擅長於企業應用、移動應用和 Web 應用的開發,提供了豐富的功能,智能代碼助手、代碼自動提示、重構、J2EE 支持、各類版本工具(git、svn等)、JUnit、CVS 整合、代碼分析、 創新的 GUI 設計等。
IntelliJ Platform 是一個構建 IDE 的開源平臺,基於它構建的 IDE 有 IntelliJ IDEA、WebStorm、DataGrip、以及 Android Studio 等等。IDEA 插件也是基於 IntelliJ Platform 開發的。
<2>開發環境搭建
官方開發文檔 IntelliJ Platform SDK :
https://plugins.jetbrains.com/docs/intellij/welcome.html
開發工具使用 Intellij IDEA,下載地址:
https://www.jetbrains.com/idea/
IDEA 分爲兩個版本:
- 社區版(Community): 完全免費,代碼開源,但是缺少一些旗艦版中的高級特性。
- 旗艦版(Ultimate): 30 天免費,支持全部功能,代碼不開源。
開發 IDEA 的插件推薦使用社區版,因爲社區版是開源的,在開發插件的時候,可以調試源代碼。
<3>開發一個簡單的插件
官方文檔指導給出了兩種開發方式:
- Gradle
- DevKit
1.Gradle 方式新建工程
新建工程目錄如下:
1 my_gradle_plugin
2├── build.gradle
3├── gradle
4│ └── wrapper
5│ ├── gradle-wrapper.jar
6│ └── gradle-wrapper.properties
7├── gradlew
8├── gradlew.bat
9├── settings.gradle
10└── src
11├── main
12│ ├── java
13│ └── resources
14│ └── META-INF
15│ └── plugin.xml
16└── test
17 ├── java
18 └── resources
根目錄 build.gradle 配置內容如下:
1plugins {
2 id 'java'
3 id 'org.jetbrains.intellij' version '0.6.5'
4}
5
6group 'com.your.company'
7version '1.0'
8sourceCompatibility = 1.8
9
10repositories {
11 mavenCentral()
12}
13dependencies {
14 testImplementation group: 'junit', name: 'junit', version: '4.12'
15}
16
17// See https://github.com/JetBrains/gradle-intellij-plugin/
18intellij {
19 version '2020.1'
20}
21patchPluginXml {
22 changeNotes """
23 Add change notes here.<br/>
24 <em>most HTML tags may be usedem>"""
25}
Gradle 類型工程 Running 方式:
推薦使用 gradle 方式開發插件,gradle 方式對本地環境要求較低,不需要去配置複雜的 sdk 等相關內容。
2.DevKit開發模式
啓用 Plugin DevKit
Plugin DevKit 是 IntelliJ 的一個插件,它使用 IntelliJ IDEA 自己的構建系統來爲開發 IDEA 插件提供支持。開發 IDEA 插件之前需要安裝並啓用 Plugin DevKit 。
打開 IDEA,導航到 Settings | Plugins,若插件列表中沒有 Plugin DevKit,點擊 Install JetBrains plugin,搜索並安裝。
配置 IntelliJ Platform Plugin SDK
- 導航到 File | Project Structure,選擇對話框左側欄 Platform Settings 下的 SDKs
- 點擊 + 按鈕,先選擇 JDK,指定 JDK 的路徑;再創建 IntelliJ Platform Plugin SDK,指定 home path 爲 IDEA 的安裝路徑,如圖:
這裏示例用官方 action 增加一個簡單的 action 作爲示例,原地址如下:
1、定義action
定義一個 Java class,繼承 AnAction 類,並重寫 actionPerformed 方法, 如:
1 public class PopupDialogAction extends AnAction {
2 @Override
3 public void actionPerformed(@NotNull AnActionEvent event) {
4// Using the event, create and show a dialog
5 Project currentProject = event.getProject();
6 StringBuffer dlgMsg = new StringBuffer(event.getPresentation().getText() + " Selected!");
7 String dlgTitle = event.getPresentation().getDescription();
8 // If an element is selected in the editor, add info about it.
9 Navigatable nav = event.getData**(**CommonDataKeys.NAVIGATABLE);
10 if (nav != null) {
11 dlgMsg.append(String.format("nSelected Element: %s", nav.toString()));
12 }
13 Messages.showMessageDialog(currentProject, dlgMsg.toString(), dlgTitle, Messages.getInformationIcon());14 }15}
2、註冊 Action
在 plugin.xml 文件的 元素內註冊。
1 <action< span=""> id="org.intellij.sdk.action.PopupDialogAction" class="org.intellij.sdk.action.PopupDialogAction"
2 text="Action Basics Plugin: Pop Dialog Action" description="SDK action example" icon="SdkIcons.Sdk_default_icon">
3
4 <override-< span="">text place="MainMenu" text="Pop Dialog Action"/>
5
6 <keyboard-shortcut< span=""> first-keystroke="control alt A" second-keystroke="C" keymap="$default"/>
7
8 <mouse-shortcut< span=""> keystroke="control button3 doubleClick" keymap="$default"/>
9
10 <add-< span="">to-group group-id="ToolsMenu" anchor="first"/>
11
12 </action>
上面示例會定義一個被添加到 IDEA tools 菜單的第一個位置(anchor=”first”)添加了”Pop Dialog Action”菜單,點擊該菜單將彈出一個“Action Basics Plugin: Pop Dialog Action Selected!” item,如圖:
點擊該 “Pop Dialog Action” item,彈出一個提示輸入名字的對話框。
<4>StopCoding原理及源碼解析
開源地址:
https://github.com/jogeen/StopCoding.git
Step1. 然後在菜單欄中tools->StopCoding。
Step2. 設置適合你的參數然後保存。
Step3. 快樂的Coding,再不用擔心自己會“沉迷”了。工作時間結束,就會彈出下框進行提醒。當然,這個框是關不掉的。 只有你休息了足夠的時間它纔會自動關閉。
工程結構分析:
1.
2├── image
3│ ├── step1.png
4│ ├── step2.png
5│ ├── step3.png
6│ └── step.gif
7├── LICENSE
8├── readme.md
9├── readme_ZH.md
10├── resources
11│ ├── img
12│ │ └── stop.png
13│ └── META-INF
14│ ├── pluginIcon_dark.svg
15│ ├── pluginIcon.svg
16│ └── plugin.xml
17├── src
18│ └── icu
19│ └── jogeen
20│ └── stopcoding
21│ ├── data
22│ │ ├── DataCenter.java
23│ │ └── SettingData.java
24│ ├── service
25│ │ └── TimerService.java
26│ ├── StopCodingSettingAction.java
27│ ├── task
28│ │ ├── RestTask.java
29│ │ └── WorkTask.java
30│ └── ui
31│ ├── SettingDialog.form
32│ ├── SettingDialog.java
33│ ├── TipsDialog.form
34│ └── TipsDialog.java
35└── StopCoding.iml
上圖可以看到,工程結構十分的簡單,基於devkit模式進行開發。
- data 包 SettingData,配置信息對應 model。DataCenter,作爲運行時的數據中心,都是些靜態的全局變量。
- service TimerService 這個定時計算的核心代碼。
- task RestTask 休息時的定時任務。WorkTask 工作時的定時任務。
- ui SettingDialog 設置信息的對話框。TipsDialog 休息時提醒的對話框。StopCodingSettingAction 啓動入口的 action。
- plugin.xml 這是插件工程的核心配置文件,裏面每一項的都添加了詳細的註釋,有疑問的小夥伴可以在公衆號後臺留言,我們一起探討。
1<idea-plugin>
2 <!-- 插件唯一id,不能和其他插件項目重複,所以推薦使用com.xxx.xxx的格式
3 插件不同版本之間不能更改,若沒有指定,則與插件名稱相同 -->
4 <id>icu.jogeen.StopCoding.id</id>
5 <!-- 插件名稱,別人在官方插件庫搜索你的插件時使用的名稱 -->
6
7 <name>StopCoding</name>
8 <!-- 插件版本號 -->
9
10 <version>1.2.1</version>
11 <!-- 供應商主頁和email(不能使用默認值,必須修改成自己的)-->
12
13 <vendor email="[email protected]" url="https://github.com/jogeen/StopCoding">jogeen</vendor>
14
15 <!-- 插件的描述 (不能使用默認值,必須修改成自己的。並且需要大於40個字符)-->
16
17 <description><![CDATA[
18 <p>This is a work timer.It can set every working period to remind you that it's time to have a rest, drink some water and exercise your body.
19 Only in this way can you really work healthily</p>
20 <ol>
21 <li>In the tools menu bar, open stopcoding.</li>
22 <li>Set working hours and rest time, and save them.</li>
23 <li>When the set time comes, there will be a pop-up box to remind you to rest, so that you can not operate idea temporarily.</li>
24 </ol>
25 <hr>
26 <p>如果你也經常沉迷於寫代碼,忘了起身休息喝水,那麼試試這個插件吧</p>
27 <ol>
28 <li>在菜單欄的Tools中,打開StopCoding插件進行設置</li>
29 <li>設置工作時間和休息時間,並且保存</li>
30 <li>當設置的時間一到,就會有彈框提醒你休息,讓你暫時不能操作idea</li>
31 </ol>
32 <p>項目地址:<a href="https://github.com/jogeen/StopCoding">https://github.com/jogeen/StopCoding</p>
33 ]]></description>
34
35 <!-- 插件版本變更信息,支持HTML標籤;
36 將展示在 settings | Plugins 對話框和插件倉庫的Web頁面 -->
37 <change-notes><![CDATA[
38 <ul>
39 <li>V1.2 add icon(Thanks for the icon provided by my good friend Hu Wei).</li>
40 <li>V1.1 update Guide to use.</li>
41 <li>V1.0 release.</li>
42 </ul>
43 ]]>
44 </change-notes>
45
46 <!-- 插件兼容IDEAbuild 號(最低版本號)-->
47 <idea-version since-build="173.0"/>
48 <!-- 插件所依賴的其他插件的id -->
49 <depends>com.intellij.modules.lang</depends>
50
51
52 <extensions defaultExtensionNs="com.intellij">
53 <!-- 聲明該插件對IDEA core或其他插件的擴展 -->
54
55 </extensions>
56
57 <!-- 編寫插件動作 -->
58 <actions>
59 <action id="StopCoding_setting_id" class="icu.jogeen.stopcoding.StopCodingSettingAction" text="StopCoding"
60 description="setting">
61 <add-to-group group-id="ToolsMenu" anchor="first"/>
62 <keyboard-shortcut keymap="$default" first-keystroke="ctrl S" second-keystroke="C"/>
63 </action>
64 </actions>
65
66</idea-plugin>
從 public.xml 可以看出,這個插件比較簡單,只有一個 StopCoding_setting_id 配置項,入口類也是
icu.jogeen.stopcoding.StopCodingSettingAction
1 public class StopCodingSettingAction extends AnAction {
2
3 @Override
4 public void actionPerformed(AnActionEvent e) {
5 SettingDialog settingDialog = new SettingDialog();
6 settingDialog.setVisible(true);
7 }
8 }
源碼中只是展示了SettingDialog,基於Swing畫了一個可視化界面。
icu.jogeen.stopcoding.ui.SettingDialog
1//綁定確定按鈕事件
2 buttonOK.addActionListener(new ActionListener() {
3 public void actionPerformed(ActionEvent e) {
4 onOK();
5 }
6 });
7 //綁定取消按鈕事件
8 buttonCancel.addActionListener(new ActionListener() {
9 public void actionPerformed(ActionEvent e) {
10 onCancel();
11 }
12 });
13 //綁定關閉按鈕事件
14 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
15 addWindowListener(new WindowAdapter() {
16 public void windowClosing(WindowEvent e) {
17 onCancel();
18 }
19 });
20 contentPane.registerKeyboardAction(new ActionListener() {
21 public void actionPerformed(ActionEvent e) {
22 onCancel();
23 }
24 }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
25 //綁定啓用選擇按鈕時間
26 openRbtn.addChangeListener(new ChangeListener() {
27 @Override
28 public void stateChanged(ChangeEvent e) {
29 openRbtn.setText(openRbtn.isSelected() ? "Running" : "Stopped");
30 }
31 });
在 SettingDialog 中對事件進行了監聽,其主要思路就是 schedule 去添加一個定時任務,和使用 cancel 去取消任務停止定時器。然後彈出一個 dialog 阻止持續 coding。
<5>最後
對大多數程序員來說,生活沒那麼多詩和遠方,大部分是加不完的班,寫不完的代碼和修不完的 bug。相信有了這些基本介紹,感興趣的小夥伴去看看源碼,並嘗試自己寫一個小插件就沒什麼太大問題了,要知道程序員不止眼前的邏輯和代碼,還應有健康的體魄和精氣神。最後,希望這篇文章能幫到作爲程序員的你,也希望大家都能做最好的自己。
推薦閱讀
歡迎點擊【京東科技】,瞭解開發者社區
更多精彩技術實踐與獨家乾貨解析
歡迎關注【京東科技開發者】公衆號