Skip to content

Commit a2529ce

Browse files
committed
feat(compiler): add URL support for structure command #397
Support fetching HTML from URLs in StructureInCommand. When a URL is provided, it fetches the HTML content and creates a scratch file for processing. Also updates output format to include URL paths.
1 parent c28151c commit a2529ce

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/StructureInCommand.kt

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,33 @@ import cc.unitmesh.devti.command.InsCommand
55
import cc.unitmesh.devti.command.dataprovider.BuiltinCommand
66
import cc.unitmesh.devti.language.utils.lookupFile
77
import cc.unitmesh.devti.util.relativePath
8+
import com.intellij.ide.scratch.ScratchRootType
9+
import com.intellij.lang.html.HTMLLanguage
810
import com.intellij.openapi.application.ApplicationManager
911
import com.intellij.openapi.application.runReadAction
1012
import com.intellij.openapi.diagnostic.logger
1113
import com.intellij.openapi.project.Project
12-
import com.intellij.openapi.project.guessProjectDir
1314
import com.intellij.openapi.vfs.VirtualFile
1415
import com.intellij.psi.PsiFile
1516
import com.intellij.psi.PsiManager
1617
import kotlinx.coroutines.Dispatchers
1718
import kotlinx.coroutines.withContext
19+
import org.jsoup.Jsoup
20+
import java.net.MalformedURLException
21+
import java.net.URL
1822

1923

2024
class StructureInCommand(val myProject: Project, val prop: String) : InsCommand {
2125
override val commandName: BuiltinCommand = BuiltinCommand.STRUCTURE
2226

2327
private val logger = logger<StructureInCommand>()
2428
override suspend fun execute(): String? {
25-
val virtualFile = file(myProject, prop)
29+
val virtualFile = if (isUrl(prop)) {
30+
fetchHtmlFromUrl(myProject, prop)
31+
} else {
32+
file(myProject, prop)
33+
}
34+
2635
if (virtualFile == null) {
2736
logger.warn("File not found: $prop")
2837
return null
@@ -37,13 +46,41 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
3746
} ?: return null
3847

3948
val structure = StructureCommandUtil.getFileStructure(myProject, virtualFile, psiFile)
40-
val filepath = virtualFile.relativePath(myProject)
41-
return "// $filepath\n```\n$structure\n```"
49+
val filepath = if (isUrl(prop)) {
50+
prop
51+
} else {
52+
virtualFile.relativePath(myProject)
53+
}
54+
55+
return "# structure: $filepath\n```\n$structure\n```"
4256
}
4357

4458
fun file(project: Project, path: String): VirtualFile? {
4559
val filename = path.split("#")[0]
4660
val virtualFile = project.lookupFile(filename)
4761
return virtualFile
4862
}
49-
}
63+
64+
private fun isUrl(str: String): Boolean {
65+
return try {
66+
val url = URL(str)
67+
url.protocol == "http" || url.protocol == "https"
68+
} catch (e: MalformedURLException) {
69+
false
70+
}
71+
}
72+
73+
private suspend fun fetchHtmlFromUrl(project: Project, url: String): VirtualFile? {
74+
return try {
75+
val htmlContent = withContext(Dispatchers.IO) {
76+
Jsoup.connect(url).get().outerHtml()
77+
}
78+
79+
ScratchRootType.getInstance()
80+
.createScratchFile(project, "autodev-structure.html", HTMLLanguage.INSTANCE, htmlContent)
81+
} catch (e: Exception) {
82+
logger.warn("Failed to fetch HTML from URL: $url", e)
83+
null
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)