Skip to content

Commit

Permalink
feat(http-client): add support for creating temporary scratch files #198
Browse files Browse the repository at this point in the history
 && closed #198

Added functionality to create temporary scratch files for HTTP requests in the Intellij HTTP client executor.

- Create temporary scratch file for HTTP request
- Set file path in configuration settings
- Add configuration to run manager
  • Loading branch information
phodal committed May 30, 2024
1 parent 30efd49 commit 71c13c9
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ import com.intellij.httpClient.http.request.run.HttpRequestExecutorExtensionFact
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.ScratchFileService
import com.intellij.ide.scratch.ScratchRootType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager

class IntellijHttpClientExecutor : HttpClientProvider {
override fun execute(project: Project, virtualFile: VirtualFile, text: String) {
val psiFile: PsiFile = PsiManager.getInstance(project).findFile(virtualFile) ?: return
val originFile: PsiFile = PsiManager.getInstance(project).findFile(virtualFile) ?: return

// create temporary file for http request
ScratchFileService.getInstance()
val scratchFile = ScratchRootType.getInstance()
.createScratchFile(project, "autodev-http-request.http", originFile.language, text) ?: return

val psiFile = PsiManager.getInstance(project).findFile(scratchFile) ?: return

val runner: RunnerAndConfigurationSettings = ConfigurationContext(psiFile)
.configurationsFromContext
Expand All @@ -28,10 +37,12 @@ class IntellijHttpClientExecutor : HttpClientProvider {
val configuration = HttpRequestRunConfiguration(project, factory, "HttpRequest")

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

configuration.settings.filePath = virtualFile.path

runManager.setUniqueNameIfNeeded(configuration)
runner.isTemporary = true
runManager.addConfiguration(runner)

val selectedRunner = runManager.selectedConfiguration
if ((selectedRunner == null || selectedRunner.isTemporary) && runManager.shouldSetRunConfigurationFromContext()) {
Expand Down

0 comments on commit 71c13c9

Please sign in to comment.