Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/serize02/kotlin-stats

Analyze and understand Kotlin codebases with this comprehensive tool for IntelliJ IDEA, powered by PSI (Program Structure Interface).
https://github.com/serize02/kotlin-stats

intellij-api intellij-idea intellij-platform intellij-plugin kotlin plugin-development

Last synced: 3 months ago
JSON representation

Analyze and understand Kotlin codebases with this comprehensive tool for IntelliJ IDEA, powered by PSI (Program Structure Interface).

Awesome Lists containing this project

README

        

# about IntelliJ IDEA Kotlin Stats Plugin

This plugin offers a comprehensive analysis tool for Kotlin files within your IntelliJ IDEA environment. Developed using the [intellij-platform-plugin-template](https://github.com/JetBrains/intellij-platform-plugin-template) as a foundation, this plugin provides real-time insights into your Kotlin source code, including line counts, TODO comment tracking, and detailed function analysis.

## Features

- **Kotlin File Statistics**: Displays the total number of lines and lines containing TODO comments.
- **Function Analysis**: Identifies and lists the longest function in terms of lines, along with its name and body content.
- **Real-time Updates**: Automatically updates the displayed statistics as you make changes to the file or switch between files in the editor.

## Main Components

### `KotlinFileStats.kt`

- **Purpose**: Represents the statistics of a Kotlin file.
- **Key Attributes**:
- `totalLines`: Total number of lines in the file.
- `todoLines`: Number of lines containing TODO comments.
- `function`: The longest function in the file.
- **Key Methods**:
- `getFunctionContent()`: Returns the body content of the longest function.
- `getFunctionLines()`: Returns the number of lines in the longest function.
- `getFunctionName()`: Returns the name of the longest function.

`KotlinFileStats` acts as a data container for file metrics. It effectively aggregates relevant statistics and provides utility functions to access detailed information about the longest function in the file.

### `FileInspector.kt`

- **Purpose**: Analyzes a given Kotlin file to gather statistics.
- **Implementation Details**:
- Utilizes the PSI (Program Structure Interface) of IntelliJ to parse and interpret Kotlin files.
- Implements a depth-first search (DFS) algorithm to navigate the PSI tree and identify KtFunction nodes.

`FileInspector` acts as the core analysis engine. By leveraging the PSI, it efficiently extracts and computes the required statistics from the abstract syntax tree (AST) of the Kotlin file. The DFS algorithm ensures that all function nodes are visited and compared, providing an accurate representation of the longest function.

### `ProjectStatsPanel.kt`

- **Purpose**: User interface component that displays the statistics in a panel within the IntelliJ IDEA.
- **Key Attributes**:
- `content`: Main panel content displaying the statistics.
- **Key Methods**:
- `updateStats()`: Updates the displayed statistics for the currently selected file. Fetches the latest statistics from `FileInspector` and refreshes the UI accordingly.
- `setupListeners()`: Sets up listeners for document changes and file selection events to trigger real-time updates.

`ProjectStatsPanel` forms the bridge between the backend analysis and the user interface. It dynamically updates the display to show current file statistics and ensures the user has immediate insights into the code they are working on. Listeners are set up to monitor any changes in file content or selection, providing a seamless and responsive experience.

### Summary

The integration of these three components ensures a robust, real-time analysis tool for Kotlin files:

- **PSI Utilization**: The Platform Structure Interface of IntelliJ is paramount for parsing and interpreting the Kotlin files, making it possible to traverse and analyze the abstract syntax tree.
- **DFS Algorithm**: Efficiently implemented to navigate through the PSI tree, ensuring thorough and precise identification of functions within the file.
- **User Interface**: A responsive and user-friendly interface that keeps developers informed about the critical metrics of their Kotlin files, promoting better code management and quality.

This combination effectively provides developers with not just statistics, but actionable insights into their codebase, fostering an environment of continuous improvement and efficient coding practices.

## Benefits for Development

- **Code Quality Monitoring**: Track TODO comments and large functions, which can be indicators of areas needing refactoring or additional attention.
- **Efficient Navigation**: Quickly understand the structure and complexity of a Kotlin file at a glance.
- **Continuous Feedback**: Stay informed about the file’s metrics as you develop, enabling better decision-making and code management.

## Acknowledgements

This plugin was created using the [intellij-platform-plugin-template](https://github.com/JetBrains/intellij-platform-plugin-template). Special thanks to JetBrains for their robust plugin development tools and templates.