Use the Statistic plug-in in Idea to count the code volume of engineering projects

1. Functional background

The company wanted to conduct code statistics on a project. With so many categories, I couldn't get data one by one, so I thought of the Statistic plug-in. Let’s take a look at how to use the Statistic plug-in.

2. Statistic plug-in

First, you need to know the number of lines of code in the Idea statistics project. It mainly uses the Statistic plug-in to count. Click File->Settings, as shown in the figure below:

image-20230831164908348

After entering the Settings interface, click Plugins, then click Marketplace in the middle below, as shown in the figure below:

image-20230831165623377

Search for Statistic, select it, and click Install on the right to install the plug-in. When the installation is complete, click Apply->Ok.

Then you can see Statistics in the lower left corner. If you can't see it, just restart the idea.

3. Count the amount of code

Click the Statistic tab on the taskbar below, as shown below:

image-20230831165817480

There is no statistics at this time. If statistics are needed, click "Refresh" to scan the project code, as shown below:

image-20230831165850829

Overview parameter description (such as java)

  • Count: the number of java files
  • Size SUM: The total hard disk size occupied by all java files
  • Size MIN: The minimum size of the hard disk occupied by the java file
  • Size MAX: The maximum size of the hard disk occupied by the java file
  • Size AVG: average hard disk size occupied by java files
  • Lines: The total number of lines in the java file
  • Lines MIN: The minimum number of lines in the java file
  • Lines MAX: The maximum number of lines in a java file
  • Lines AVG: average number of lines in java files

The above statistics are all codes. If we count the corresponding type of code, we can click on the label we need to count, such as Java, and we can see the code statistics of the corresponding object, as shown below:

image-20230831170047999

Description of statistical parameters for specified file type (such as java)

  • Total Lines: Total lines of code (including comments, blank lines)
  • Source Code Lines: Number of source code lines (excluding comments, blank lines)
  • Source Code Lines(%): Percentage of source code lines (Source Code Lines/Total Lines)
  • Comment Lines: number of comment lines
  • Comment Lines(%): Percentage of comment lines (Comment Lines/Total Lines)
  • Blank Lines: Number of blank lines
  • Blank Lines(%): Blank Lines/Total Lines

It can be seen that the code amount of engineering projects can be counted for different types and from different dimensions, and the number of lines of code in the project can be counted.

4. Test it out

I admit that I am just here to make up the numbers~

Just find any class and use it hereInitDataConfig.java

package com.maple.inputdb.config;

import com.maple.inputdb.bean.InitModel;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author 笑小枫 <https://xiaoxiaofeng.com/>
 * @date 2023/3/10
 */
@Slf4j
@Component
@AllArgsConstructor
public class InitDataConfig {
    
    

    private final DynamicDatasourceConfig dynamicDatasourceConfig;

    private final InitConfigProperties initConfigProperties;

    public void initData(InitModel initModel) {
    
    

        if (DbStatusSingleton.getInstance().getDbStatus()
                || Boolean.TRUE.equals(dynamicDatasourceConfig.checkDataSource())) {
    
    
            throw new RuntimeException("数据已完成初始化,请勿重复操作");
        }

        // 检查数据库连接是否正确
        checkConnection(initModel);

        if (!new File(initConfigProperties.getInitFilePath() + initConfigProperties.getInitUserName()).exists()) {
    
    
            File file = createFile(initConfigProperties.getInitFilePath(), initConfigProperties.getInitUserName());
            try (FileWriter out = new FileWriter(file);
                 BufferedWriter bw = new BufferedWriter(out)) {
    
    
                bw.write("userName=" + initModel.getSysUserName());
                bw.newLine();
                bw.write("password=" + initModel.getSysPassword());
                bw.flush();
            } catch (IOException e) {
    
    
                log.info("写入管理员信息文件失败", e);
                throw new RuntimeException("写入管理员信息文件失败,请重试");
            }
        }

        if (!new File(initConfigProperties.getInitFilePath() + initConfigProperties.getInitDbName()).exists()) {
    
    
            File file = createFile(initConfigProperties.getInitFilePath(), initConfigProperties.getInitDbName());
            try (FileWriter out = new FileWriter(file);
                 BufferedWriter bw = new BufferedWriter(out)) {
    
    

                bw.write(String.format("jdbcUrl=jdbc:mysql://%s:%s/%s?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF-8",
                        initModel.getDatabaseHost(), initModel.getDatabasePort(), initModel.getDatabaseName()));
                bw.newLine();
                bw.write("username=" + initModel.getUser());
                bw.newLine();
                bw.write("password=" + initModel.getPassword());
                bw.newLine();
                bw.write("driverClassName=com.mysql.cj.jdbc.Driver");
                bw.flush();

            } catch (IOException e) {
    
    
                log.info("写入数据库文件失败", e);
                throw new RuntimeException("写入数据库文件失败,请重试");
            }
        }

        boolean isOk = dynamicDatasourceConfig.checkDataSource();
        if (!isOk) {
    
    
            throw new RuntimeException("初始化数据库信息失败,请检查配置是否正确");
        }
    }

    /**
     * 检查数据库连接是否正确
     *
     * @param initModel 连接信息
     */
    private void checkConnection(InitModel initModel) {
    
    
        try {
    
    
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s",
                    initModel.getDatabaseHost(), initModel.getDatabasePort(), initModel.getDatabaseName()), initModel.getUser(), initModel.getPassword());
            log.info("校验数据库连接成功,开始进行数据库配置" + conn.getCatalog());
            conn.close();
        } catch (SQLException | ClassNotFoundException e) {
    
    
            log.info("校验数据库连接失败", e);
            throw new RuntimeException("初始化数据库信息失败,请检查配置是否正确:" + e.getMessage());
        }
    }

    private File createFile(String path, String fileName) {
    
    
        File pathFile = new File(path);
        if (pathFile.mkdirs()) {
    
    
            log.info(path + " is not exist, this is auto created.");
        }
        File file = new File(path + File.separator + fileName);
        try {
    
    
            if (!file.createNewFile()) {
    
    
                throw new RuntimeException(String.format("创建%s文件失败,请重试", fileName));
            }
        } catch (IOException e) {
    
    
            log.error(String.format("创建%s文件失败", fileName), e);
            throw new RuntimeException(String.format("创建%s文件失败,请重试", fileName));
        }
        return file;
    }

    public void deleteFile() {
    
    
        File sqlFile = new File(initConfigProperties.getInitFilePath() + initConfigProperties.getInitDbName());
        if (sqlFile.exists()) {
    
    
            log.info(sqlFile.getName() + " --- delete sql file result:" + sqlFile.delete());
        }

        File userFile = new File(initConfigProperties.getInitFilePath() + initConfigProperties.getInitUserName());
        if (userFile.exists()) {
    
    
            log.info(userFile.getName() + " --- delete user file result:" + userFile.delete());
        }

        dynamicDatasourceConfig.stopDataSource();

        // 数据初始化状态设为false
        DbStatusSingleton.getInstance().setDbStatus(false);
        log.info("初始化数据重置完成");
    }
}

When we open the file and select it Refresh on selection, we can see the statistics of a single file.

image-20230831171247624

Well, once you learn it, you won’t be confused anymore. This article ends here.

Follow me and I will teach you more tips.

Guess you like

Origin blog.csdn.net/qq_34988304/article/details/132607408