Add command to assert current file name

GitOrigin-RevId: 9c7f128eb50036c0613f6ec212bd534fc0c0bb56
This commit is contained in:
Maxim.Kolmakov
2024-03-25 11:03:15 +00:00
committed by intellij-monorepo-bot
parent 4ac62e3a82
commit ab4dce1226
3 changed files with 46 additions and 1 deletions
@@ -1033,4 +1033,11 @@ fun <T : CommandChain> T.disableKotlinNotification(): T = apply {
*/
fun <T : CommandChain> T.assertCaretPosition(line: Int, column: Int): T = apply {
addCommand("${CMD_PREFIX}assertCaretPosition $line $column")
}
/**
* Assert the current file in editor.
*/
fun <T : CommandChain> T.assertCurrentFile(name: String): T = apply {
addCommand("${CMD_PREFIX}assertCurrentFile $name")
}
@@ -112,7 +112,8 @@ public final class BaseCommandProvider implements CommandProvider {
Map.entry(AddContentRootToModule.PREFIX, AddContentRootToModule::new),
Map.entry(DisableKotlinNotificationCommand.PREFIX, DisableKotlinNotificationCommand::new),
Map.entry(RequestHeavyScanningOnNextStartCommand.PREFIX, RequestHeavyScanningOnNextStartCommand::new),
Map.entry(AssertCaretPositionCommand.PREFIX, AssertCaretPositionCommand::new)
Map.entry(AssertCaretPositionCommand.PREFIX, AssertCaretPositionCommand::new),
Map.entry(AssertCurrentFileCommand.PREFIX, AssertCurrentFileCommand::new)
);
}
}
@@ -0,0 +1,37 @@
package com.jetbrains.performancePlugin.commands
import com.intellij.openapi.application.readAction
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.ui.playback.PlaybackContext
import org.jetbrains.annotations.NonNls
/**
* Assert the current file in editor.
*
* Syntax: `%assertCurrentFile <file name>`
*
* Example: `%assertCurrentFile AssertCurrentFileCommand.kt`
*/
class AssertCurrentFileCommand(text: String, line: Int) : PerformanceCommandCoroutineAdapter(text, line) {
companion object {
const val NAME: @NonNls String = "assertCurrentFile"
const val PREFIX: String = CMD_PREFIX + NAME
}
override suspend fun doExecute(context: PlaybackContext) {
val fileName = extractCommandArgument(PREFIX).split(" ").filterNot { it.trim() == "" }.singleOrNull()
if (fileName == null) {
throw IllegalArgumentException("File name is not provided")
}
readAction {
val editor = checkNotNull(FileEditorManager.getInstance(context.project).selectedTextEditor)
val currentFileName = editor.virtualFile.name
if (fileName != currentFileName) {
throw Exception("Current file name is $currentFileName, expected $fileName")
}
}
}
override fun getName(): String = NAME
}