Skip to content

Commit

Permalink
feat(httpclient): refactor runFile method and add createConfiguration…
Browse files Browse the repository at this point in the history
… method

Refactored the runFile method in HttpClientFileRunService.kt to improve readability and maintainability. The method now uses the readText method from the VirtualFile class instead of manually retrieving the text from the PsiFile. Also, added a new method, createConfiguration, to create a RunConfiguration for a given virtual file.
  • Loading branch information
phodal committed Sep 16, 2024
1 parent 25f9cc7 commit 7a4048f
Showing 1 changed file with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import com.intellij.execution.Executor
import com.intellij.execution.RunManager
import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.configurations.RunProfile
import com.intellij.httpClient.http.request.HttpRequestLanguage
import com.intellij.httpClient.http.request.HttpRequestPsiFile
import com.intellij.httpClient.http.request.run.HttpRequestExecutorExtensionFactory
import com.intellij.httpClient.http.request.run.HttpRequestRunConfigurationExecutor
import com.intellij.httpClient.http.request.run.config.HttpRequestRunConfiguration
import com.intellij.httpClient.http.request.run.config.HttpRequestRunConfigurationType
import com.intellij.ide.scratch.ScratchRootType
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.readText
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
Expand All @@ -24,44 +28,55 @@ class HttpClientFileRunService : FileRunService {
}

override fun runFile(project: Project, virtualFile: VirtualFile, psiElement: PsiElement?): String? {
val originFile: PsiFile = PsiManager.getInstance(project).findFile(virtualFile) ?: return null
val text = originFile.text
// val originFile: PsiFile = PsiManager.getInstance(project).findFile(virtualFile) ?: return null
// val text = originFile.text
val scratchFile = ScratchRootType.getInstance()
.createScratchFile(project, "shireRequest.http", HttpRequestLanguage.INSTANCE, virtualFile.readText())
?: return null

return executeFile(project, text)
}
}

fun executeFile(project: Project, text: String): String? {
val scratchFile = ScratchRootType.getInstance()
.createScratchFile(project, "shireRequest.http", HttpRequestLanguage.INSTANCE, text) ?: return null
val runner: RunnerAndConfigurationSettings = runReadAction {
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)
?: return@runReadAction null

val psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: return null
ConfigurationContext(psiFile)
.configurationsFromContext
?.firstOrNull()
?.configurationSettings
} ?: return null

val runner: RunnerAndConfigurationSettings = ConfigurationContext(psiFile)
.configurationsFromContext
?.firstOrNull()
?.configurationSettings ?: return null
val factory = HttpRequestRunConfigurationType.getInstance().configurationFactories[0]
val configuration = HttpRequestRunConfiguration(project, factory, "HttpRequest")

val factory = HttpRequestRunConfigurationType.getInstance().configurationFactories[0]
val configuration = HttpRequestRunConfiguration(project, factory, "HttpRequest")
val runManager: RunManager = RunManager.getInstance(project)
configuration.settings.filePath = scratchFile.path

val runManager: RunManager = RunManager.getInstance(project)
runManager.setUniqueNameIfNeeded(configuration)
runner.isTemporary = true
runManager.addConfiguration(runner)

configuration.settings.filePath = scratchFile.path
val selectedRunner = runManager.selectedConfiguration
if ((selectedRunner == null || selectedRunner.isTemporary) && runManager.shouldSetRunConfigurationFromContext()) {
runManager.selectedConfiguration = runner
}

runManager.setUniqueNameIfNeeded(configuration)
runner.isTemporary = true
runManager.addConfiguration(runner)
val executor: Executor = HttpRequestExecutorExtensionFactory.getRunExtension().executor ?: return null
HttpRequestRunConfigurationExecutor.getInstance().execute(
project, runner, executor
)

val selectedRunner = runManager.selectedConfiguration
if ((selectedRunner == null || selectedRunner.isTemporary) && runManager.shouldSetRunConfigurationFromContext()) {
runManager.selectedConfiguration = runner
return "Run Success"
}

val executor: Executor = HttpRequestExecutorExtensionFactory.getRunExtension().executor ?: return null
HttpRequestRunConfigurationExecutor.getInstance().execute(
project, runner, executor
)
override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
/// should be HttpRequestPsiFile
val psiFile = runReadAction {
PsiManager.getInstance(project).findFile(virtualFile) as? HttpRequestPsiFile
} ?: return null

return "Run Success"
}
val factory = HttpRequestRunConfigurationType.getInstance().configurationFactories[0]
val configuration = HttpRequestRunConfiguration(project, factory, "HttpRequest")
configuration.settings.filePath = virtualFile.path

return configuration
}
}

0 comments on commit 7a4048f

Please sign in to comment.