@@ -144,6 +144,13 @@ object JsDialogProcessor {
144144
145145 private fun createDialog (jsTestsModel : JsTestsModel ? ) = jsTestsModel?.let { JsDialogWindow (it) }
146146
147+ private fun unblockDocument (project : Project , document : Document ) {
148+ PsiDocumentManager .getInstance(project).apply {
149+ commitDocument(document)
150+ doPostponedOperationsAndUnblockDocument(document)
151+ }
152+ }
153+
147154 private fun createTests (model : JsTestsModel , containingFilePath : String , editor : Editor ? , contents : String ) {
148155 val normalizedContainingFilePath = containingFilePath.replace(File .separator, " /" )
149156 (object : Task .Backgroundable (model.project, " Generate tests" ) {
@@ -154,7 +161,9 @@ object JsDialogProcessor {
154161 model.testSourceRoot!!
155162 )
156163 val testFileName = normalizedContainingFilePath.substringAfterLast(" /" ).replace(Regex (" .js" ), " Test.js" )
157- val testGenerator = JsTestGenerator (fileText = contents,
164+ currentFileText = model.file.getContent()
165+ val testGenerator = JsTestGenerator (
166+ fileText = contents,
158167 sourceFilePath = normalizedContainingFilePath,
159168 projectPath = model.project.basePath?.replace(File .separator, " /" )
160169 ? : throw IllegalStateException (" Can't access project path." ),
@@ -208,44 +217,6 @@ object JsDialogProcessor {
208217
209218 private fun JSFile.getContent (): String = this .viewProvider.contents.toString()
210219
211- private fun manageExports (
212- editor : Editor ? , project : Project , model : JsTestsModel , exports : List <String >
213- ) {
214- AppExecutorUtil .getAppExecutorService().submit {
215- invokeLater {
216- val exportSection = exports.joinToString(" \n " ) { " exports.$it = $it " }
217- val fileText = model.file.getContent()
218- when {
219- fileText.contains(exportSection) -> {}
220-
221- fileText.contains(startComment) && ! fileText.contains(exportSection) -> {
222- val regex = Regex (" $startComment ((\\ r\\ n|\\ n|\\ r|.)*)$endComment " )
223- regex.find(fileText)?.groups?.get(1 )?.value?.let { existingSection ->
224- val exportRegex = Regex (" exports[.](.*) =" )
225- val existingExports = existingSection.split(" \n " ).filter { it.contains(exportRegex) }
226- val existingExportsSet = existingExports.map { rawLine ->
227- exportRegex.find(rawLine)?.groups?.get(1 )?.value ? : throw IllegalStateException ()
228- }.toSet()
229- val resultSet = existingExportsSet + exports.toSet()
230- val resSection = resultSet.joinToString(" \n " ) { " exports.$it = $it " }
231- val swappedText = fileText.replace(existingSection, " \n $resSection \n " )
232- project.setNewText(editor, model.containingFilePath, swappedText)
233- }
234- }
235-
236- else -> {
237- val line = buildString {
238- append(" \n $startComment \n " )
239- append(exportSection)
240- append(" \n $endComment " )
241- }
242- project.setNewText(editor, model.containingFilePath, fileText + line)
243- }
244- }
245- }
246- }
247- }
248-
249220 private fun Project.setNewText (editor : Editor ? , filePath : String , text : String ) {
250221 editor?.let {
251222 runWriteAction {
@@ -261,12 +232,38 @@ object JsDialogProcessor {
261232 } ? : run {
262233 File (filePath).writeText(text)
263234 }
235+ currentFileText = text
264236 }
265237
266- private fun unblockDocument (project : Project , document : Document ) {
267- PsiDocumentManager .getInstance(project).apply {
268- commitDocument(document)
269- doPostponedOperationsAndUnblockDocument(document)
238+ // Needed for continuous exports managing
239+ private var currentFileText = " "
240+
241+ private fun manageExports (
242+ editor : Editor ? ,
243+ project : Project ,
244+ model : JsTestsModel ,
245+ swappedText : (String? , String ) -> String
246+ ) {
247+ AppExecutorUtil .getAppExecutorService().submit {
248+ invokeLater {
249+ when {
250+ currentFileText.contains(startComment) -> {
251+ val regex = Regex (" $startComment ((\\ r\\ n|\\ n|\\ r|.)*)$endComment " )
252+ regex.find(currentFileText)?.groups?.get(1 )?.value?.let { existingSection ->
253+ val newText = swappedText(existingSection, currentFileText)
254+ project.setNewText(editor, model.containingFilePath, newText)
255+ }
256+ }
257+
258+ else -> {
259+ val line = buildString {
260+ append(" \n " )
261+ appendLine(swappedText(null , currentFileText))
262+ }
263+ project.setNewText(editor, model.containingFilePath, currentFileText + line)
264+ }
265+ }
266+ }
270267 }
271268 }
272269}
0 commit comments