@@ -5,24 +5,33 @@ import cc.unitmesh.devti.command.InsCommand
55import cc.unitmesh.devti.command.dataprovider.BuiltinCommand
66import cc.unitmesh.devti.language.utils.lookupFile
77import cc.unitmesh.devti.util.relativePath
8+ import com.intellij.ide.scratch.ScratchRootType
9+ import com.intellij.lang.html.HTMLLanguage
810import com.intellij.openapi.application.ApplicationManager
911import com.intellij.openapi.application.runReadAction
1012import com.intellij.openapi.diagnostic.logger
1113import com.intellij.openapi.project.Project
12- import com.intellij.openapi.project.guessProjectDir
1314import com.intellij.openapi.vfs.VirtualFile
1415import com.intellij.psi.PsiFile
1516import com.intellij.psi.PsiManager
1617import kotlinx.coroutines.Dispatchers
1718import kotlinx.coroutines.withContext
19+ import org.jsoup.Jsoup
20+ import java.net.MalformedURLException
21+ import java.net.URL
1822
1923
2024class StructureInCommand (val myProject : Project ,val prop : String ) : InsCommand {
2125override val commandName: BuiltinCommand = BuiltinCommand .STRUCTURE
2226
2327private val logger= logger<StructureInCommand >()
2428override 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+
2635if (virtualFile== null ) {
2736 logger.warn(" File not found:$prop " )
2837return null
@@ -37,13 +46,41 @@ class StructureInCommand(val myProject: Project, val prop: String) : InsCommand
3746 }? : return null
3847
3948val 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
4458fun file (project : Project ,path : String ):VirtualFile ? {
4559val filename= path.split(" #" )[0 ]
4660val virtualFile= project.lookupFile(filename)
4761return 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+ }