IDEA插件:快速刪除Java代碼中的註釋

背景

有時,咱們須要刪除Java源代碼中的註釋。目前有很多方法,好比:html

  • 實現狀態機。該方式較爲通用,適用於多種語言(取決於狀態機支持的註釋符號)。java

  • 正則匹配。該方式容易誤判,尤爲是容易誤刪字符串。git

  • 利用第三方庫。該方式侷限性較強,好比不一樣語言可能有不一樣的第三方庫。

本文針對Java語言,介紹一種利用第三方庫的方式,能夠方便快速地移除代碼中的註釋。github

原理
 
這個第三方庫叫作JavaParser。它能夠分析Java源碼,並生成語法分析樹(AST),其中註釋也屬於AST中的節點。

所以核心思路即爲:maven

  1. JavaParser解析源碼並獲得AST。編輯器

  2. 識別出注釋類型的節點並將其刪掉。ide

  3. 將AST中剩餘的節點按必定規則打印出來。
在實踐以前,咱們先要了解Java中的幾種註釋類型:
  • LineComment 單行註釋。性能

  • BlockComent 塊註釋。測試

  • JavadocComment Java文檔註釋。ui

下面舉個簡單例子,說明三種註釋的區別:
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @author xiaoxi666
 * @date 2021-02-15 17:13
 * 我是 Javadoc 註釋
 */
public class Input {
    /**
     * 我是 Javadoc 註釋
     *
     * @param param1
     * @param param2
     */
    public static void someMethod(String param1, // 我是單行註釋
                                  String param2
// 我是單行註釋 String param3,
/* 我是塊註釋 String param4,
                                  String param5,
                                  String param6 */
        /* 我是塊註釋 String param4 */)
 {
        // 我是單行註釋
        int a = 1;
        /* 我是塊註釋,注意我和Javadoc註釋的區別,我只有一個星號 */
        int b = 2;
        /*
         * 我是塊註釋
         */
        int c = 3;
        String s1 = "// 我是字符串中的內容,不是註釋";
        String s2 = "/* 我是字符串中的內容,不是註釋 */";
        String s3 = "/** 我是字符串中的內容,不是註釋 */";
    }
}
下面咱們實踐一下,看看怎麼移除源碼中的註釋。
我這裏使用maven管理項目,首先引入JavaParser依賴:
<dependencies>
    <dependency>
         <groupId>com.github.javaparser</groupId>
         <artifactId>javaparser-symbol-solver-core</artifactId>
         <version>3.18.0</version>
    </dependency>
</dependencies>
而後編寫核心代碼:
package core;

import
com.github.javaparser.JavaParser; import com.github.javaparser.ParseResult; import com.github.javaparser.ParserConfiguration; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.comments.BlockComment; import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.LineComment; import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * @author xiaoxi666 * @date 2021-02-15 20:09 * 幾個註釋的概念: * LineComment * BlockComment * JavadocComment */ public final class CommentsRemover { private CommentsRemover() {} public static String doAction(String content) { JavaParser javaParser = createJavaParser(); ParseResult<CompilationUnit> result = javaParser.parse(content); Optional<CompilationUnit> optionalCompilationUnit = result.getResult(); if (!optionalCompilationUnit.isPresent()) { return ""; } CompilationUnit compilationUnit = optionalCompilationUnit.get(); removeComments(compilationUnit); return LexicalPreservingPrinter.print(compilationUnit); } private static void removeComments(CompilationUnit compilationUnit) { List<Comment> comments = compilationUnit.getAllContainedComments(); List<Comment> unwantedComments = comments .stream() .filter(CommentsRemover::isValidCommentType) .collect(Collectors.toList()); unwantedComments.forEach(Node::remove); } /** * 建立源碼解析器。咱們設置LexicalPreservationEnabled爲true,保留源碼中的全部語法。 * * @return JavaParser */ private static JavaParser createJavaParser() { ParserConfiguration parserConfiguration = new ParserConfiguration(); parserConfiguration.setLexicalPreservationEnabled(true); return new JavaParser(parserConfiguration); } /** * 咱們只識別單行註釋和塊註釋 * * @param comment * @return true if meet the correct type */ private static boolean isValidCommentType(Comment comment) { return comment instanceof LineComment || comment instanceof BlockComment; } }
 
在上面的代碼中,咱們首先建立JavaParser,再解析源碼,而後移除單行註釋和塊註釋,最後再用LexicalPreservingPrinter將處理後的源碼打印出來,這個打印器能夠保留源代碼全部詞法,好比空格、換行之類的元素。上述代碼已有註釋,所以再也不詳述。
 
封裝爲IDEA插件
 
考慮到咱們平時可能會大量使用該功能,所以將其封裝爲了IDEA插件,名爲remove.comments。下面簡要介紹該插件的工做原理及使用方式。PS:本文不會詳細介紹如何編寫IDEA插件。

IDEA插件的原理基本都是事件驅動,以下圖所示,咱們建立了一個事件監聽器,當檢測到編輯器中點擊右鍵後,便可彈出菜單,咱們的插件在菜單中的第一個位置。

Image

接下來,實現事件處理器:

其中包含兩段核心代碼:
  • 刪除源碼註釋。首先讀取當前文件內容也即源碼,而後交給前面已經介紹過的CommentsRemover.doAction處理,就拿到了刪除註釋後的源碼。

  • 格式化代碼。刪除註釋後,可能會引入多餘的空格,所以咱們自動格式化,這樣用戶就不用再手動格式化一次了。

/**
 * 移除代碼中的註釋
 *
 * @param editor
 * @return true if remove comments successfully
 */
private boolean removeComments(Editor editor) {
    String src = editor.getDocument().getText();
    if (Strings.isNullOrEmpty(src)) {
        return false;
    }
    String dst = CommentsRemover.doAction(checkEndLineAndModifyIfNeed(src));
    if (Strings.isNullOrEmpty(dst)) {
        return false;
    }
    editor.getDocument().setText(dst);
    return true;
}

/**
 * 因爲咱們保留了源碼格式,移除註釋以後會引入沒必要要的空格,所以須要再格式化一下
 *
 * @param editor
 * @param project
 */
private void reformat(Editor editor, Project project) {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file == null) {
        return;
    }
    LastRunReformatCodeOptionsProvider provider = new LastRunReformatCodeOptionsProvider(PropertiesComponent.getInstance());
    ReformatCodeRunOptions currentRunOptions = provider.getLastRunOptions(file);
    TextRangeType processingScope = TextRangeType.WHOLE_FILE;
    currentRunOptions.setProcessingScope(processingScope);
    (new FileInEditorProcessor(file, editor, currentRunOptions)).processCode();
}
而後打包插件:

Image
 
插件打包好以後,用戶就能夠從本地磁盤安裝了:
 
Image

在彈出的目錄樹中,選中remove.comments.zip安裝包,肯定便可。

重啓IDEA後,能夠看到插件已安裝成功:

Image

此時咱們就能夠使用該插件,一鍵刪除代碼中的註釋了。演示一下效果:

Image

Image

不嚴格性能測試(響應時間包括插件處理時間和IDEA界面更新時間):

  • 對於500行左右的文件,響應時間約200ms,幾乎瞬間完成。

  • 對於1000行左右的文件,響應時間約爲1s。

  • 對於3000行左右的文件,響應時間約需2s。

  • 對於5000行左右的文件,響應時間約需3s。

總之,平常使用毫無壓力。 

 

總結

本文首先介紹了若干刪除註釋的手段;繼而介紹了一種利用第三方庫JavaParser刪除Java註釋的思路,並加以分析和實踐;最終將其封裝爲IDEA插件,方便其餘用戶使用。

另外,因爲本人對JavaParser的認知不是特別深刻,不免存在未考慮到的場景。若你們在使用過程當中發現bug,歡迎到github提issue甚至pr。

 

資源

源碼均已放在github:https://github.com/xiaoxi666/remove.comments

插件也已經上傳至github,可點擊下載。或者關注公衆號「xiaoxi666」,後臺回覆「刪除註釋」,便可收到插件下載地址。

擴展
 
針對文中提出的第一種狀態機思路,以前也寫文章介紹過。有興趣的讀者可嘗試動手實現一下。連接:http://www.javashuo.com/article/p-gqtqfiwc-ck.html
 
 
最後
 
再給本身的公衆號打個廣告,歡迎你們關注個人公衆號:xiaoxi666,一塊兒來玩一塊兒學!
 
相關文章
相關標籤/搜索