Cleanup (minor optimization; typos; formatting)

(cherry picked from commit 6b29ba37fb1a74af33bbbf78fb4d353bb1f76a60)


(cherry picked from commit 7e6df0a65da053bbc2aea9bdc08017565cfb5c8c)

IJ-CR-155171

GitOrigin-RevId: c4bc63da9d5e7d0457376bfaa4380cde59ab54d1
This commit is contained in:
Roman Shevchenko
2024-11-25 13:15:11 +01:00
committed by intellij-monorepo-bot
parent 9c2cf149ef
commit daedb39b66
4 changed files with 22 additions and 30 deletions

View File

@@ -1,3 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.notebooks.jupyter.core.jupyter package com.intellij.notebooks.jupyter.core.jupyter
import com.intellij.ide.plugins.DynamicPluginListener import com.intellij.ide.plugins.DynamicPluginListener
@@ -31,4 +32,4 @@ class JupyterCorePluginListener : DynamicPluginListener {
const val JUPYTER_PLUGIN_ID = "intellij.jupyter" const val JUPYTER_PLUGIN_ID = "intellij.jupyter"
} }
} }

View File

@@ -1,12 +1,10 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.notebooks.jupyter.core.jupyter.preview package com.intellij.notebooks.jupyter.core.jupyter.preview
import com.intellij.ide.plugins.cl.PluginAwareClassLoader import com.intellij.ide.plugins.cl.PluginAwareClassLoader
import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.util.io.FileUtilRt import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.openapi.util.io.toCanonicalPath
import com.intellij.util.PathUtil import com.intellij.util.PathUtil
import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.EmptyHttpHeaders import io.netty.handler.codec.http.EmptyHttpHeaders
@@ -15,22 +13,19 @@ import io.netty.handler.codec.http.QueryStringDecoder
import org.apache.http.client.utils.URIBuilder import org.apache.http.client.utils.URIBuilder
import org.jetbrains.ide.HttpRequestHandler import org.jetbrains.ide.HttpRequestHandler
import java.io.File import java.io.File
import java.net.URI
import java.net.URL import java.net.URL
import java.nio.file.Path
/** /**
* "Web server" to serve Jupyter HTML * "Web server" to serve Jupyter HTML
*/ */
abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collection<String> = emptyList()) : HttpRequestHandler() { abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Set<String>) : HttpRequestHandler() {
companion object { companion object {
private val allowedTypes = setOf("css", "js", "html", "svg", "woff", "woff2", "ttf") private val allowedTypes = setOf("css", "js", "html", "svg", "woff", "woff2", "ttf")
private const val JUPYTER_HTTP_URI = "jupyter" private const val JUPYTER_HTTP_URI = "jupyter"
private const val PATH_PREFIX = "/$JUPYTER_HTTP_URI" private const val PATH_PREFIX = "/${JUPYTER_HTTP_URI}"
/** /**
* Jupyter HTTP files can be accessed with this url * Jupyter HTTP files can be accessed with this URL
*/ */
fun getJupyterHttpUrl(): URIBuilder = getJupyterBaseUrl("http").addPathSegment(JUPYTER_HTTP_URI) fun getJupyterHttpUrl(): URIBuilder = getJupyterBaseUrl("http").addPathSegment(JUPYTER_HTTP_URI)
@@ -41,15 +36,15 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti
/** /**
* Resources are in different folders when launched locally versus installation. * Resources are in different folders when launched locally versus installation.
* This method handles this difference. See .groovy build scripts * This method handles this difference; see build scripts.
*/ */
private fun getResource(javaClass: Class<*>, path: String): URL { private fun getResource(javaClass: Class<*>, path: String): URL {
// After optimizations in PluginClassLoader, classLoader.getResource return null in debug, // After optimizations in PluginClassLoader, classLoader.getResource return null in debug,
// so we have additional logic with PluginClassLoader.pluginDescriptor. This is only for debugging purposes. // so we have additional logic with PluginClassLoader.pluginDescriptor. This is only for debugging purposes.
var url = javaClass.classLoader.getResource(path) var url = javaClass.classLoader.getResource(path)
?: (javaClass.classLoader as? PluginAwareClassLoader)?.pluginDescriptor?.getPluginPath()?.let { Path.of(it.toCanonicalPath(), path) }?.toUri()?.toURL() ?: (javaClass.classLoader as? PluginAwareClassLoader)?.pluginDescriptor?.getPluginPath()?.normalize()?.resolve(path)?.toUri()?.toURL()
// In remote dev, when we running remote-front via 'split (dev-build) run config, we have: // In remote dev, when we run remote-front via 'split (dev-build)' run config, we have:
// (javaClass.classLoader as PluginClassLoader).getAllParents().mapNotNull{it as? PluginClassLoader}.map() { loader -> loader.pluginDescriptor.pluginPath } // (javaClass.classLoader as PluginClassLoader).getAllParents().mapNotNull{it as? PluginClassLoader}.map() { loader -> loader.pluginDescriptor.pluginPath }
// = out/classes/production/intellij.jupyter.plugin.frontend or out/classes/production/intellij.notebooks.plugin // = out/classes/production/intellij.jupyter.plugin.frontend or out/classes/production/intellij.notebooks.plugin
// PathUtil.getJarPathForClass(javaClass) = out/classes/production/intellij.jupyter.core // PathUtil.getJarPathForClass(javaClass) = out/classes/production/intellij.jupyter.core
@@ -70,18 +65,14 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti
} }
} }
override fun isSupported(request: FullHttpRequest): Boolean { override fun isSupported(request: FullHttpRequest): Boolean =
return super.isSupported(request) && request.uri().let { it.startsWith(PATH_PREFIX) || it in absolutePathFiles } super.isSupported(request) && request.uri().let { it.startsWith(PATH_PREFIX) || it in absolutePathFiles }
}
override fun process(urlDecoder: QueryStringDecoder, override fun process(urlDecoder: QueryStringDecoder, request: FullHttpRequest, context: ChannelHandlerContext): Boolean {
request: FullHttpRequest, val fullPath = urlDecoder.path()
context: ChannelHandlerContext): Boolean { val uri = getFileFromUrl(fullPath) ?: return false
val str = request.uri()
val fullUri = URI(str).path
val uri = getFileFromUrl(fullUri) ?: return false
val readBytes = processInternalLibs(uri) ?: return false val readBytes = processInternalLibs(uri) ?: return false
sendData(readBytes, "$appName/$uri", request, context.channel(), EmptyHttpHeaders.INSTANCE) sendData(readBytes, "${appName}/${uri}", request, context.channel(), EmptyHttpHeaders.INSTANCE)
return true return true
} }
@@ -93,7 +84,7 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti
return readFile(uri) return readFile(uri)
} }
else { else {
thisLogger().info("Extension not allowed: $extension") thisLogger().info("Extension not allowed: ${extension}")
} }
return null return null
} }
@@ -123,7 +114,7 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti
if (file.contains("BASE_EXTENSION_PATH")) if (file.contains("BASE_EXTENSION_PATH"))
return null return null
val appName = appName val appName = appName
val resource = getResource(this::class.java, "$appName/$file") val resource = getResource(this::class.java, "${appName}/${file}")
return resource.readBytes() return resource.readBytes()
} }
} }

View File

@@ -1,3 +1,4 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.notebooks.jupyter.core.lang package com.intellij.notebooks.jupyter.core.lang
import com.intellij.lang.Language import com.intellij.lang.Language
@@ -6,4 +7,4 @@ import com.intellij.lang.Language
* General language for notebooks. * General language for notebooks.
* To share some of the common features for the notebooks, their language should use [NotebookLanguage] as base one. * To share some of the common features for the notebooks, their language should use [NotebookLanguage] as base one.
*/ */
object NotebookLanguage: Language("Notebook") object NotebookLanguage: Language("Notebook")

View File

@@ -1,11 +1,10 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.jupyter.viewOnly package com.intellij.jupyter.viewOnly
import com.intellij.notebooks.jupyter.core.jupyter.preview.JupyterCefHttpHandlerBase import com.intellij.notebooks.jupyter.core.jupyter.preview.JupyterCefHttpHandlerBase
class JupyterViewOnlyHandler : JupyterCefHttpHandlerBase(absolutePathFiles = listOf( class JupyterViewOnlyHandler : JupyterCefHttpHandlerBase(
"/ipywidgets.html", absolutePathFiles = hashSetOf("/ipywidgets.html", "/ipywidgets.css", "/ipywidgets.js")
"/ipywidgets.css", ) {
"/ipywidgets.js")) {
override val appName: String = "jupyter-view-only" override val appName: String = "jupyter-view-only"
} }