Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import org.jetbrains.mcpserverplugin.general.SearchInFilesContentTool
import org.jetbrains.mcpserverplugin.general.WaitTool
import org.jetbrains.mcpserverplugin.git.GetVcsStatusTool
import org.jetbrains.mcpserverplugin.general.ReformatCurrentFileTool
import org.jetbrains.mcpserverplugin.general.OptimizeImportsTool
import org.jetbrains.mcpserverplugin.general.ReformatFileTool

class McpToolManager {
Expand Down Expand Up @@ -75,6 +76,7 @@ class McpToolManager {
GetCurrentFileErrorsTool(),
ReformatCurrentFileTool(),
ReformatFileTool(),
OptimizeImportsTool(),
GetProblemsTools(),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.mcpserverplugin.general

import com.intellij.codeInsight.actions.OptimizeImportsProcessor
import com.intellij.codeInsight.actions.ReformatCodeProcessor
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runReadAction
Expand All @@ -21,7 +22,7 @@ class ReformatCurrentFileTool : AbstractMcpTool<NoArgs>(NoArgs.serializer()) {
override val description: String = """
Reformats the opened file in the JetBrains IDE editor.
This tool doesn't require any parameters as it operates on the file currently open in the editor.

Returns one of two possible responses:
- "ok" if the file was successfully reformatted
- "file doesn't exist or can't be opened" if there is no file currently selected in the editor
Expand Down Expand Up @@ -54,7 +55,7 @@ class ReformatFileTool : AbstractMcpTool<PathInProject>(PathInProject.serializer
Reformats a specified file in the JetBrains IDE.
Use this tool to apply code formatting rules to a file identified by its path.
Requires a pathInProject parameter specifying the file location relative to the project root.

Returns one of these responses:
- "ok" if the file was successfully reformatted
- error "project dir not found" if project directory cannot be determined
Expand Down Expand Up @@ -89,3 +90,45 @@ class ReformatFileTool : AbstractMcpTool<PathInProject>(PathInProject.serializer
return Response("ok")
}
}

class OptimizeImportsTool : AbstractMcpTool<PathInProject>(PathInProject.serializer()) {
override val name: String = "optimize_imports"
override val description: String = """
Optimizes imports in a specified file in the JetBrains IDE.
Removes unused imports and reorders remaining imports according to project code style settings.
Requires a pathInProject parameter specifying the file location relative to the project root.

Returns one of these responses:
- "ok" if the imports were successfully optimized
- error "project dir not found" if project directory cannot be determined
- error "file doesn't exist or can't be opened" if the file doesn't exist or cannot be accessed
""".trimIndent()

override fun handle(project: Project, args: PathInProject): Response {
val latch = CountDownLatch(1)

val projectDir = project.guessProjectDir()?.toNioPathOrNull()
?: return Response(error = "project dir not found")

val file = runReadAction {
LocalFileSystem.getInstance()
.refreshAndFindFileByNioFile(projectDir.resolveRel(args.pathInProject))
}

if (file == null) {return Response(error = "file doesn't exist or can't be opened")}

val psiFile = runReadAction {
return@runReadAction PsiManager.getInstance(project).findFile(file)
}

if (psiFile == null) {return Response(error = "file doesn't exist or can't be opened")}

val processor = OptimizeImportsProcessor(project, psiFile)
processor.setPostRunnable(Runnable {
latch.countDown()
})
ApplicationManager.getApplication().invokeLater(Runnable { processor.run() })
latch.await()
return Response("ok")
}
}