Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugins/amazonq/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
import org.jetbrains.intellij.platform.gradle.tasks.aware.SplitModeAware
import software.aws.toolkits.gradle.changelog.tasks.GeneratePluginChangeLog
import software.aws.toolkits.gradle.intellij.IdeFlavor
import software.aws.toolkits.gradle.intellij.IdeVersions
Expand Down Expand Up @@ -49,3 +50,8 @@ tasks.check {
}
}
}

val runSplitIde by intellijPlatformTesting.runIde.registering {
splitMode = true
splitModeTarget = SplitModeAware.SplitModeTarget.BACKEND
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.intellij.ui.dsl.builder.Align
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.jcef.JBCefJSQuery
import org.cef.CefApp
import software.aws.toolkits.core.utils.error
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn
Expand All @@ -30,8 +29,8 @@
import software.aws.toolkits.jetbrains.core.region.AwsRegionProvider
import software.aws.toolkits.jetbrains.core.webview.BrowserMessage
import software.aws.toolkits.jetbrains.core.webview.BrowserState
import software.aws.toolkits.jetbrains.core.webview.LocalAssetJBCefRequestHandler
import software.aws.toolkits.jetbrains.core.webview.LoginBrowser
import software.aws.toolkits.jetbrains.core.webview.WebviewResourceHandlerFactory
import software.aws.toolkits.jetbrains.isDeveloperMode
import software.aws.toolkits.jetbrains.services.amazonq.util.createBrowser
import software.aws.toolkits.jetbrains.utils.isQConnected
Expand All @@ -53,7 +52,7 @@
val component = panel {
row {
cell(webviewContainer)
.align(Align.FILL)

Check warning on line 55 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L55

Added line #L55 was not covered by tests
}.resizableRow()

if (isDeveloperMode()) {
Expand All @@ -67,7 +66,7 @@
)
},
)
.align(Align.FILL)

Check warning on line 69 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L69

Added line #L69 was not covered by tests
}
}
}
Expand Down Expand Up @@ -108,25 +107,14 @@
class QWebviewBrowser(val project: Project, private val parentDisposable: Disposable) :
LoginBrowser(
project,
QWebviewBrowser.DOMAIN,
QWebviewBrowser.WEB_SCRIPT_URI
),
Disposable {
// TODO: confirm if we need such configuration or the default is fine
override val jcefBrowser = createBrowser(parentDisposable)
private val query = JBCefJSQuery.create(jcefBrowser)
private val assetHandler = LocalAssetJBCefRequestHandler(jcefBrowser)

Check warning on line 115 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L115

Added line #L115 was not covered by tests

init {
CefApp.getInstance()
.registerSchemeHandlerFactory(
"http",
domain,
WebviewResourceHandlerFactory(
domain = "http://$domain/",
assetUri = "/webview/assets/"
),
)

loadWebView(query)

query.addHandler(jcefHandler)
Expand All @@ -145,7 +133,7 @@

when (message) {
is BrowserMessage.PrepareUi -> {
this.prepareBrowser(BrowserState(FeatureId.AmazonQ, false))

Check warning on line 136 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L136

Added line #L136 was not covered by tests
WebviewTelemetry.amazonqSignInOpened(
project,
reAuth = isQExpired(project)
Expand Down Expand Up @@ -176,15 +164,15 @@
ToolkitConnectionManager.getInstance(project)
.activeConnectionForFeature(QConnection.getInstance()) as? AwsBearerTokenConnection
)?.let { connection ->
runInEdt {
SsoLogoutAction(connection).actionPerformed(
AnActionEvent.createFromDataContext(
"qBrowser",
null,
DataContext.EMPTY_CONTEXT

Check warning on line 172 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L167-L172

Added lines #L167 - L172 were not covered by tests
)
)
}

Check warning on line 175 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L175

Added line #L175 was not covered by tests
}
}

Expand All @@ -199,7 +187,7 @@
is BrowserMessage.SendUiClickTelemetry -> {
val signInOption = message.signInOptionClicked
if (signInOption.isNullOrEmpty()) {
LOG.warn { "Unknown sign in option" }

Check warning on line 190 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L190

Added line #L190 was not covered by tests
} else {
UiTelemetry.click(project, signInOption)
}
Expand Down Expand Up @@ -273,12 +261,15 @@
}

override fun loadWebView(query: JBCefJSQuery) {
jcefBrowser.loadHTML(getWebviewHTML(webScriptUri, query))
val webScriptUri = assetHandler.createResource(
"js/getStart.js",
QWebviewBrowser::class.java.getResourceAsStream("/webview/assets/js/getStart.js")

Check warning on line 266 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L264-L266

Added lines #L264 - L266 were not covered by tests
)

jcefBrowser.loadURL(assetHandler.createResource("content.html", getWebviewHTML(webScriptUri, query)))

Check warning on line 269 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/QLoginWebview.kt#L269

Added line #L269 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we assign the file names to consts

}

companion object {
private val LOG = getLogger<QWebviewBrowser>()
private const val WEB_SCRIPT_URI = "http://webview/js/getStart.js"
private const val DOMAIN = "webview"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.cef.browser.CefFrame
import org.cef.handler.CefLoadHandlerAdapter
import software.aws.toolkits.jetbrains.core.coroutines.disposableCoroutineScope
import software.aws.toolkits.jetbrains.core.webview.LocalAssetJBCefRequestHandler
import software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AmazonQToolWindow
import software.aws.toolkits.jetbrains.services.amazonq.webview.theme.EditorThemeAdapter
import software.aws.toolkits.resources.message
Expand Down Expand Up @@ -63,7 +64,7 @@
}
}

JBCefJSQuery.Response(null)

Check warning on line 67 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/gettingstarted/QGettingStartedContent.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/gettingstarted/QGettingStartedContent.kt#L67

Added line #L67 was not covered by tests
}
receiveMessageQuery.addHandler(handler)
}
Expand All @@ -72,7 +73,7 @@

private fun loadWebView() {
// load the web app
jcefBrowser.loadHTML(getWebviewHTML())
jcefBrowser.loadURL(LocalAssetJBCefRequestHandler(jcefBrowser).createResource("content.html", getWebviewHTML()))

Check warning on line 76 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/gettingstarted/QGettingStartedContent.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/gettingstarted/QGettingStartedContent.kt#L76

Added line #L76 was not covered by tests
}

private fun getWebviewHTML(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import com.intellij.openapi.Disposable
import com.intellij.openapi.util.Disposer
import com.intellij.ui.jcef.JBCefJSQuery
import org.cef.CefApp
import software.aws.toolkits.jetbrains.core.webview.LocalAssetJBCefRequestHandler
import software.aws.toolkits.jetbrains.services.amazonq.util.HighlightCommand
import software.aws.toolkits.jetbrains.services.amazonq.util.createBrowser
import software.aws.toolkits.jetbrains.settings.MeetQSettings
import java.nio.file.Paths

/*
Displays the web view for the Amazon Q tool window
Expand All @@ -21,6 +22,17 @@

val receiveMessageQuery = JBCefJSQuery.create(jcefBrowser)

private val assetRequestHandler = LocalAssetJBCefRequestHandler(jcefBrowser)

Check warning on line 25 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L25

Added line #L25 was not covered by tests

init {
assetRequestHandler.addWildcardHandler("mynah") { path ->
val asset = path.replaceFirst("mynah/", "/mynah-ui/assets/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow up later

Paths.get(asset).normalize().toString().let {
this::class.java.getResourceAsStream(it)
}

Check warning on line 32 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L27-L32

Added lines #L27 - L32 were not covered by tests
}
}

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L34

Added line #L34 was not covered by tests

fun init(
isCodeTransformAvailable: Boolean,
isFeatureDevAvailable: Boolean,
Expand All @@ -29,15 +41,7 @@
isCodeTestAvailable: Boolean,
highlightCommand: HighlightCommand?,
) {
// register the scheme handler to route http://mynah/ URIs to the resources/assets directory on classpath
CefApp.getInstance()
.registerSchemeHandlerFactory(
"http",
"mynah",
AssetResourceHandler.AssetResourceHandlerFactory(),
)

loadWebView(isCodeTransformAvailable, isFeatureDevAvailable, isDocAvailable, isCodeScanAvailable, isCodeTestAvailable, highlightCommand)

Check warning on line 44 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L44

Added line #L44 was not covered by tests
}

override fun dispose() {
Expand All @@ -63,9 +67,13 @@
// setup empty state. The message request handlers use this for storing state
// that's persistent between page loads.
jcefBrowser.setProperty("state", "")

// load the web app
jcefBrowser.loadHTML(
getWebviewHTML(isCodeTransformAvailable, isFeatureDevAvailable, isDocAvailable, isCodeScanAvailable, isCodeTestAvailable, highlightCommand)
jcefBrowser.loadURL(
assetRequestHandler.createResource(
"webview/chat.html",
getWebviewHTML(isCodeTransformAvailable, isFeatureDevAvailable, isDocAvailable, isCodeScanAvailable, isCodeTestAvailable, highlightCommand)

Check warning on line 75 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L72-L75

Added lines #L72 - L75 were not covered by tests
)
)
}

Expand All @@ -84,7 +92,7 @@
val postMessageToJavaJsCode = receiveMessageQuery.inject("JSON.stringify(message)")

val jsScripts = """
<script type="text/javascript" src="$WEB_SCRIPT_URI" defer onload="init()"></script>
<script type="text/javascript" src="http://toolkitasset/mynah/js/mynah-ui.js" defer onload="init()"></script>
<script type="text/javascript">
const init = () => {
mynahUI.createMynahUI(
Expand All @@ -94,13 +102,13 @@
}
},
${MeetQSettings.getInstance().reinvent2024OnboardingCount < MAX_ONBOARDING_PAGE_COUNT},
${MeetQSettings.getInstance().disclaimerAcknowledged},

Check warning on line 105 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L105

Added line #L105 was not covered by tests
$isFeatureDevAvailable, // whether /dev is available
$isCodeTransformAvailable, // whether /transform is available
$isDocAvailable, // whether /doc is available
$isCodeScanAvailable, // whether /scan is available
$isCodeTestAvailable, // whether /test is available
${OBJECT_MAPPER.writeValueAsString(highlightCommand)}

Check warning on line 111 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L108-L111

Added lines #L108 - L111 were not covered by tests
);
}
</script>
Expand All @@ -120,8 +128,7 @@
}

companion object {
private const val WEB_SCRIPT_URI = "http://mynah/js/mynah-ui.js"
private const val MAX_ONBOARDING_PAGE_COUNT = 3
private val OBJECT_MAPPER = jacksonObjectMapper()

Check warning on line 132 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt

View check run for this annotation

Codecov / codecov/patch

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/webview/Browser.kt#L132

Added line #L132 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

package software.aws.toolkits.jetbrains.services.cwc.editor.context.file

import com.intellij.idea.AppMode
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.client.ClientKind
import com.intellij.openapi.client.sessions
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.ClientFileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
Expand All @@ -17,9 +22,13 @@
class FileContextExtractor(private val fqnWebviewAdapter: FqnWebviewAdapter?, private val project: Project) {
private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FileContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
val editor = if (AppMode.isRemoteDevHost()) {

Check warning on line 25 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.idea.AppMode' is marked unstable with @ApiStatus.Internal

Check warning on line 25 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'isRemoteDevHost()' is declared in unstable class 'com.intellij.idea.AppMode' marked with @ApiStatus.Internal
project.sessions(ClientKind.REMOTE).firstOrNull()?.service<ClientFileEditorManager>()?.getSelectedTextEditor() ?: return null

Check warning on line 26 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'getSelectedTextEditor()' is declared in unstable 'com.intellij.openapi.fileEditor.ClientFileEditorManager' marked with @ApiStatus.Internal

Check warning on line 26 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'REMOTE' is declared in unstable 'com.intellij.openapi.client.ClientKind' marked with @ApiStatus.Experimental

Check warning on line 26 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'sessions(com.intellij.openapi.project.Project, com.intellij.openapi.client.ClientKind)' is marked unstable with @ApiStatus.Internal

Check warning on line 26 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.openapi.client.ClientKind' is marked unstable with @ApiStatus.Experimental

Check warning on line 26 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/file/FileContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.openapi.fileEditor.ClientFileEditorManager' is marked unstable with @ApiStatus.Internal
} else {
computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
}

val fileLanguage = computeOnEdt {
languageExtractor.extractLanguageNameFromCurrentFile(editor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

package software.aws.toolkits.jetbrains.services.cwc.editor.context.focusArea

import com.intellij.idea.AppMode
import com.intellij.openapi.client.ClientKind
import com.intellij.openapi.client.sessions
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.SelectionModel
import com.intellij.openapi.fileEditor.ClientFileEditorManager
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
Expand All @@ -25,10 +30,13 @@

private val languageExtractor: LanguageExtractor = LanguageExtractor()
suspend fun extract(): FocusAreaContext? {
val editor = computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null

val editor = if (AppMode.isRemoteDevHost()) {

Check warning on line 33 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.idea.AppMode' is marked unstable with @ApiStatus.Internal

Check warning on line 33 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'isRemoteDevHost()' is declared in unstable class 'com.intellij.idea.AppMode' marked with @ApiStatus.Internal
project.sessions(ClientKind.REMOTE).firstOrNull()?.service<ClientFileEditorManager>()?.getSelectedTextEditor() ?: return null

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'getSelectedTextEditor()' is declared in unstable 'com.intellij.openapi.fileEditor.ClientFileEditorManager' marked with @ApiStatus.Internal

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'sessions(com.intellij.openapi.project.Project, com.intellij.openapi.client.ClientKind)' is marked unstable with @ApiStatus.Internal

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'REMOTE' is declared in unstable 'com.intellij.openapi.client.ClientKind' marked with @ApiStatus.Experimental

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.openapi.fileEditor.ClientFileEditorManager' is marked unstable with @ApiStatus.Internal

Check warning on line 34 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/focusArea/FocusAreaContextExtractor.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unstable API Usage

'com.intellij.openapi.client.ClientKind' is marked unstable with @ApiStatus.Experimental
} else {
computeOnEdt {
FileEditorManager.getInstance(project).selectedTextEditor
} ?: return null
}
if (editor.document.text.isBlank()) return null

// Get 10k characters around the cursor
Expand Down Expand Up @@ -109,7 +117,7 @@
languageExtractor.extractLanguageNameFromCurrentFile(editor)
}
val fileText = editor.document.text
val fileName = FileEditorManager.getInstance(project).selectedFiles.first().name
val fileName = editor.virtualFile.name

// Offset the selection range to the start of the trimmedFileText
val selectionInsideTrimmedFileTextRange = codeSelectionRange.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@file:Suppress("all")

// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// adapted from https://github.com/JetBrains/intellij-community/blob/54429f3ba00c695c22d09e164135b0713f2cfc0f/platform/ui.jcef/jcef/utils/JBCefLocalRequestHandler.kt

package contrib.org.intellij.images.editor.impl.jcef

import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.callback.CefCallback
import org.cef.handler.CefRequestHandlerAdapter
import org.cef.handler.CefResourceHandler
import org.cef.handler.CefResourceHandlerAdapter
import org.cef.handler.CefResourceRequestHandler
import org.cef.handler.CefResourceRequestHandlerAdapter
import org.cef.misc.BoolRef
import org.cef.network.CefRequest
import java.net.URL

/**
* Handles local protocol-specific CEF resource requests for a defined `protocol` and `authority`.
*
* This class implements a mechanism to serve protocol-specific resources based on mappings provided
* through the `addResource` function. Only requests matching the configured protocol and authority are processed,
* while others are rejected.
*
* @param myProtocol The protocol to handle (e.g., "http", "file").
* @param myAuthority The authority of the requests (e.g., "localhost", "mydomain").
*/
open class JBCefLocalRequestHandler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be used directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a straight copy from jetbrains so we should keep it slim to make it easy to copy over any changes later

private val myProtocol: String,
private val myAuthority: String,
) : CefRequestHandlerAdapter() {
private val myResources: MutableMap<String, () -> CefResourceHandler?> = HashMap()

private val REJECTING_RESOURCE_HANDLER: CefResourceHandler = object : CefResourceHandlerAdapter() {

Check notice on line 36 in plugins/core/jetbrains-community/src/contrib/org/intellij/images/editor/impl/jcef/JBCefLocalRequestHandler.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Private property naming convention

Private property name `REJECTING_RESOURCE_HANDLER` should not contain underscores in the middle or the end

Check notice

Code scanning / QDJVMC

Private property naming convention Note

Private property name REJECTING_RESOURCE_HANDLER should not contain underscores in the middle or the end
override fun processRequest(request: CefRequest, callback: CefCallback): Boolean {
callback.cancel()
return false
}
}

private val RESOURCE_REQUEST_HANDLER = resourceHandlerWrapper { path ->

Check notice on line 43 in plugins/core/jetbrains-community/src/contrib/org/intellij/images/editor/impl/jcef/JBCefLocalRequestHandler.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Private property naming convention

Private property name `RESOURCE_REQUEST_HANDLER` should not contain underscores in the middle or the end

Check notice

Code scanning / QDJVMC

Private property naming convention Note

Private property name RESOURCE_REQUEST_HANDLER should not contain underscores in the middle or the end
myResources[path]?.let { it() }
}

protected fun resourceHandlerWrapper(handler: (String) -> CefResourceHandler?): CefResourceRequestHandler =
object : CefResourceRequestHandlerAdapter() {
override fun getResourceHandler(browser: CefBrowser?, frame: CefFrame?, request: CefRequest): CefResourceHandler {
val url = URL(request.url)
url.protocol
if (!url.protocol.equals(myProtocol) || !url.authority.equals(myAuthority)) {
return REJECTING_RESOURCE_HANDLER
}
return try {
val path = url.path.trim('/')
handler(path) ?: REJECTING_RESOURCE_HANDLER
} catch (e: RuntimeException) {
println(e.message)
REJECTING_RESOURCE_HANDLER
}
}
}

fun addResource(resourcePath: String, resourceProvider: () -> CefResourceHandler?) {
val normalisedPath = resourcePath.trim('/')
myResources[normalisedPath] = resourceProvider
}

fun createResource(resourcePath: String, resourceProvider: () -> CefResourceHandler?): String {
val normalisedPath = resourcePath.trim('/')
myResources[normalisedPath] = resourceProvider
return "$myProtocol://$myAuthority/$normalisedPath"
}

override fun getResourceRequestHandler(
browser: CefBrowser?,
frame: CefFrame?,
request: CefRequest?,
isNavigation: Boolean,
isDownload: Boolean,
requestInitiator: String?,
disableDefaultHandling: BoolRef?,
): CefResourceRequestHandler {
return RESOURCE_REQUEST_HANDLER
}
}
Loading
Loading