Skip to content

Commit

Permalink
feat: add OpenWebpage processor
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Sep 19, 2024
1 parent bd7d233 commit 24c2bf2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@ enum class PostProcessorType(var handleName: String) {
Diff("diff"),

UpdateEditorText("updateEditorText"),

// openWebpage
OpenWebpage("openWebpage"),

// showWebView
ShowWebView("showWebView"),
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.phodal.shirecore.middleware.builtin

import com.intellij.execution.ui.ConsoleView
import com.intellij.ide.DataManager
import com.intellij.ide.IdeBundle
import com.intellij.ide.browsers.BrowserLauncher
import com.intellij.ide.browsers.OpenInBrowserRequest
import com.intellij.ide.browsers.WebBrowserService
import com.intellij.ide.browsers.WebBrowserUrlProvider
import com.intellij.ide.browsers.actions.findUsingBrowser
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.phodal.shirecore.middleware.PostProcessor
import com.phodal.shirecore.middleware.PostProcessorContext
import com.phodal.shirecore.middleware.PostProcessorType

class OpenWebpageProcessor : PostProcessor {
override val processorName: String get() = PostProcessorType.OpenWebpage.handleName

override fun isApplicable(context: PostProcessorContext): Boolean {
return context.genText?.contains("<html>") ?: false
}

override fun execute(project: Project, context: PostProcessorContext, console: ConsoleView?, args: List<Any>): Any {
val dataContext = DataManager.getInstance().dataContextFromFocusAsync.blockingGet(10000)
?: throw IllegalStateException("No data context")
val editor = CommonDataKeys.EDITOR.getData(dataContext) ?: return ""
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.document)
?: throw IllegalStateException("No PSI file")

val request = object : OpenInBrowserRequest(psiFile, true) {
private val lazyElement by lazy { file.findElementAt(editor.caretModel.offset) }

override val element: PsiElement
get() = lazyElement ?: file
}


try {
val browser = findUsingBrowser()
val urls = WebBrowserService.getInstance().getUrlsToOpen(request, true)
if (!urls.isEmpty()) {
val url = urls.first()
runInEdt {
FileDocumentManager.getInstance().saveAllDocuments()
}

BrowserLauncher.instance.browse(url.toExternalForm(), browser, request.project)
}
} catch (e: WebBrowserUrlProvider.BrowserException) {
Messages.showErrorDialog(e.message, IdeBundle.message("browser.error"))
} catch (e: Exception) {
logger<OpenWebpageProcessor>().warn(e)
}

return ""
}
}
3 changes: 2 additions & 1 deletion core/src/main/resources/com.phodal.shirecore.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,13 @@
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.AppendProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.InsertNewlineProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.UpdateEditorTextProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.ParseCommentProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.SaveFileProcessor"/>
<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.OpenFileProcessor"/>

<shirePostProcessor implementation="com.phodal.shirecore.middleware.builtin.OpenWebpageProcessor"/>

<!-- LocationInteractionProvider-->
<shireLocationInteraction implementation="com.phodal.shirecore.config.interaction.EditorInteractionProvider"/>

Expand Down

0 comments on commit 24c2bf2

Please sign in to comment.