@@ -28,10 +28,7 @@ import java.nio.file.Path
2828import java.util.concurrent.Executors
2929import java.util.concurrent.Future
3030import java.util.concurrent.TimeUnit
31- import javax.swing.BoxLayout
32- import javax.swing.JComponent
33- import javax.swing.JLabel
34- import javax.swing.JPanel
31+ import javax.swing.*
3532
3633
3734class GenerateDocumentationAction : FileContextAction <GenerateDocumentationAction .Settings >() {
@@ -43,6 +40,9 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
4340 }
4441
4542 class SettingsUI {
43+ @Name(" Single Output File" )
44+ val singleOutputFile = JCheckBox (" Produce a single output file" , true )
45+
4646 @Name(" Files to Process" )
4747 val filesToProcess = CheckBoxList <Path >()
4848
@@ -57,6 +57,7 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
5757 var transformationMessage : String = " Create user documentation" ,
5858 var outputFilename : String = " compiled_documentation.md" ,
5959 var filesToProcess : List <Path > = listOf(),
60+ var singleOutputFile : Boolean = true
6061 )
6162
6263 class Settings (
@@ -79,8 +80,9 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
7980 }
8081 val dialog = DocumentationCompilerDialog (project, settingsUI)
8182 dialog.show()
82- val result = dialog.isOK
8383 val settings: UserSettings = dialog.userSettings
84+ settings.singleOutputFile = settingsUI.singleOutputFile.isSelected
85+ val result = dialog.isOK
8486 settings.filesToProcess = when {
8587 result -> files.filter { path -> settingsUI.filesToProcess.isItemSelected(path) }.toList()
8688 else -> listOf ()
@@ -116,22 +118,32 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
116118 val fileContent =
117119 IOUtils .toString(FileInputStream (path.toFile()), " UTF-8" ) ? : return @submit null
118120 val transformContent = transformContent(fileContent, transformationMessage)
119- markdownContent.append(" # ${root.relativize(path)} \n\n " )
120- markdownContent.append(transformContent.replace(" (?s)(?<![^\\ n])#" .toRegex(), " \n ##" ))
121- markdownContent.append(" \n\n " )
121+ if (config?.settings?.singleOutputFile == true ) {
122+ markdownContent.append(" # ${root.relativize(path)} \n\n " )
123+ markdownContent.append(transformContent.replace(" (?s)(?<![^\\ n])#" .toRegex(), " \n ##" ))
124+ } else {
125+ root.relativize(path).let { it.parent.resolve(it.fileName.toString().split(' .' ).dropLast(1 ).joinToString(" ." ) + " _" + outputPath.fileName) }
126+ val individualOutputPath = root.resolve(" ${root.relativize(path)} .md" )
127+ Files .write(individualOutputPath, transformContent.toByteArray())
128+ }
122129 path
123130 }
124131 }.toTypedArray().map { future ->
125132 try {
126- future.get() ? : return @map null
133+ future.get()
127134 } catch (e: Exception ) {
128135 log.warn(" Error processing file" , e)
129136 return @map null
130137 }
131138 }.filterNotNull()
132- Files .write(outputPath, markdownContent.toString().toByteArray())
133- open(config?.project!! , outputPath)
134- return arrayOf(outputPath.toFile())
139+ if (config?.settings?.singleOutputFile == true ) {
140+ Files .write(outputPath, markdownContent.toString().toByteArray())
141+ open(config?.project!! , outputPath)
142+ return arrayOf(outputPath.toFile())
143+ } else {
144+ open(config?.project!! , outputPath)
145+ return pathList.toList().map { it.toFile() }.toTypedArray()
146+ }
135147 } finally {
136148 executorService.shutdown()
137149 }
@@ -199,7 +211,6 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
199211 val filesScrollPane = JBScrollPane (settingsUI.filesToProcess).apply {
200212 preferredSize = Dimension (400 , 300 ) // Adjust the preferred size as needed
201213 }
202- add(JLabel (" Files to Process" ), BorderLayout .NORTH )
203214 add(filesScrollPane, BorderLayout .CENTER ) // Make the files list the dominant element
204215
205216 val optionsPanel = JPanel ().apply {
@@ -208,6 +219,7 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
208219 add(settingsUI.transformationMessage)
209220 add(JLabel (" Output File" ))
210221 add(settingsUI.outputFilename)
222+ add(settingsUI.singleOutputFile)
211223 }
212224 add(optionsPanel, BorderLayout .SOUTH )
213225 }
@@ -222,6 +234,7 @@ class GenerateDocumentationAction : FileContextAction<GenerateDocumentationActio
222234// userSettings.filesToProcess = settingsUI.filesToProcess.selectedValuesList
223235 userSettings.filesToProcess =
224236 settingsUI.filesToProcess.items.filter { path -> settingsUI.filesToProcess.isItemSelected(path) }
237+ userSettings.singleOutputFile = settingsUI.singleOutputFile.isSelected
225238 }
226239 }
227240}
@@ -233,4 +246,4 @@ val <T> CheckBoxList<T>.items: List<T>
233246 items.add(getItemAt(i)!! )
234247 }
235248 return items
236- }
249+ }
0 commit comments