From daedb39b6674f573d1db7eae4701d8094a536bd0 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 25 Nov 2024 13:15:11 +0100 Subject: [PATCH] Cleanup (minor optimization; typos; formatting) (cherry picked from commit 6b29ba37fb1a74af33bbbf78fb4d353bb1f76a60) (cherry picked from commit 7e6df0a65da053bbc2aea9bdc08017565cfb5c8c) IJ-CR-155171 GitOrigin-RevId: c4bc63da9d5e7d0457376bfaa4380cde59ab54d1 --- .../core/jupyter/JupyterCorePluginListener.kt | 3 +- .../preview/JupyterCefHttpHandlerBase.kt | 37 +++++++------------ .../jupyter/core/lang/NotebookLanguage.kt | 3 +- .../viewOnly/JupyterViewOnlyHandler.kt | 9 ++--- 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/JupyterCorePluginListener.kt b/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/JupyterCorePluginListener.kt index c02edab9a236..760e042222fb 100644 --- a/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/JupyterCorePluginListener.kt +++ b/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/JupyterCorePluginListener.kt @@ -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 import com.intellij.ide.plugins.DynamicPluginListener @@ -31,4 +32,4 @@ class JupyterCorePluginListener : DynamicPluginListener { const val JUPYTER_PLUGIN_ID = "intellij.jupyter" } -} \ No newline at end of file +} diff --git a/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/preview/JupyterCefHttpHandlerBase.kt b/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/preview/JupyterCefHttpHandlerBase.kt index ab29bcc2d736..45f2ae2926b2 100644 --- a/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/preview/JupyterCefHttpHandlerBase.kt +++ b/jupyter/src/com/intellij/notebooks/jupyter/core/jupyter/preview/JupyterCefHttpHandlerBase.kt @@ -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. package com.intellij.notebooks.jupyter.core.jupyter.preview - import com.intellij.ide.plugins.cl.PluginAwareClassLoader import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.diagnostic.thisLogger import com.intellij.openapi.util.io.FileUtilRt -import com.intellij.openapi.util.io.toCanonicalPath import com.intellij.util.PathUtil import io.netty.channel.ChannelHandlerContext 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.jetbrains.ide.HttpRequestHandler import java.io.File -import java.net.URI import java.net.URL -import java.nio.file.Path /** * "Web server" to serve Jupyter HTML */ -abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collection = emptyList()) : HttpRequestHandler() { - +abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Set) : HttpRequestHandler() { companion object { private val allowedTypes = setOf("css", "js", "html", "svg", "woff", "woff2", "ttf") 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) @@ -41,15 +36,15 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti /** * 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 { // After optimizations in PluginClassLoader, classLoader.getResource return null in debug, // so we have additional logic with PluginClassLoader.pluginDescriptor. This is only for debugging purposes. 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 } // = out/classes/production/intellij.jupyter.plugin.frontend or out/classes/production/intellij.notebooks.plugin // 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 { - return super.isSupported(request) && request.uri().let { it.startsWith(PATH_PREFIX) || it in absolutePathFiles } - } + override fun isSupported(request: FullHttpRequest): Boolean = + super.isSupported(request) && request.uri().let { it.startsWith(PATH_PREFIX) || it in absolutePathFiles } - override fun process(urlDecoder: QueryStringDecoder, - request: FullHttpRequest, - context: ChannelHandlerContext): Boolean { - val str = request.uri() - val fullUri = URI(str).path - val uri = getFileFromUrl(fullUri) ?: return false + override fun process(urlDecoder: QueryStringDecoder, request: FullHttpRequest, context: ChannelHandlerContext): Boolean { + val fullPath = urlDecoder.path() + val uri = getFileFromUrl(fullPath) ?: 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 } @@ -93,7 +84,7 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti return readFile(uri) } else { - thisLogger().info("Extension not allowed: $extension") + thisLogger().info("Extension not allowed: ${extension}") } return null } @@ -123,7 +114,7 @@ abstract class JupyterCefHttpHandlerBase(private val absolutePathFiles: Collecti if (file.contains("BASE_EXTENSION_PATH")) return null val appName = appName - val resource = getResource(this::class.java, "$appName/$file") + val resource = getResource(this::class.java, "${appName}/${file}") return resource.readBytes() } } diff --git a/jupyter/src/com/intellij/notebooks/jupyter/core/lang/NotebookLanguage.kt b/jupyter/src/com/intellij/notebooks/jupyter/core/lang/NotebookLanguage.kt index 3ecd468e7789..8094c32ea8a1 100644 --- a/jupyter/src/com/intellij/notebooks/jupyter/core/lang/NotebookLanguage.kt +++ b/jupyter/src/com/intellij/notebooks/jupyter/core/lang/NotebookLanguage.kt @@ -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 import com.intellij.lang.Language @@ -6,4 +7,4 @@ import com.intellij.lang.Language * General language for notebooks. * To share some of the common features for the notebooks, their language should use [NotebookLanguage] as base one. */ -object NotebookLanguage: Language("Notebook") \ No newline at end of file +object NotebookLanguage: Language("Notebook") diff --git a/jupyter/viewOnly/src/com/intellij/jupyter/viewOnly/JupyterViewOnlyHandler.kt b/jupyter/viewOnly/src/com/intellij/jupyter/viewOnly/JupyterViewOnlyHandler.kt index 6d054a1103f1..6099c0478da7 100644 --- a/jupyter/viewOnly/src/com/intellij/jupyter/viewOnly/JupyterViewOnlyHandler.kt +++ b/jupyter/viewOnly/src/com/intellij/jupyter/viewOnly/JupyterViewOnlyHandler.kt @@ -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 import com.intellij.notebooks.jupyter.core.jupyter.preview.JupyterCefHttpHandlerBase -class JupyterViewOnlyHandler : JupyterCefHttpHandlerBase(absolutePathFiles = listOf( - "/ipywidgets.html", - "/ipywidgets.css", - "/ipywidgets.js")) { +class JupyterViewOnlyHandler : JupyterCefHttpHandlerBase( + absolutePathFiles = hashSetOf("/ipywidgets.html", "/ipywidgets.css", "/ipywidgets.js") +) { override val appName: String = "jupyter-view-only" }