Skip to content

Commit

Permalink
tuning icons, instantly update internal infoview and open links of ex…
Browse files Browse the repository at this point in the history
…ternal infoview in browser
  • Loading branch information
onriv committed Oct 27, 2024
1 parent a76e050 commit 78741c1
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lean4ij.infoview.external

import com.intellij.ide.BrowserUtil
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
Expand All @@ -12,7 +13,20 @@ import com.intellij.ui.content.ContentFactory
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.jcef.JBCefApp
import com.intellij.ui.jcef.JBCefBrowser
import com.intellij.ui.jcef.JBCefClient

Check warning on line 16 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import lean4ij.util.notify
import org.cef.CefSettings

Check warning on line 18 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.cef.browser.CefBrowser
import org.cef.browser.CefFrame
import org.cef.callback.CefAuthCallback
import org.cef.callback.CefCallback
import org.cef.handler.CefDisplayHandler

Check warning on line 23 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import directive

Unused import directive
import org.cef.handler.CefLoadHandler
import org.cef.handler.CefRequestHandler
import org.cef.handler.CefResourceRequestHandler
import org.cef.misc.BoolRef
import org.cef.network.CefRequest
import org.cef.security.CefSSLInfo

/**
* TODO the name like infoview and infoView is inconsistent in the whole codebase...
Expand Down Expand Up @@ -62,6 +76,78 @@ class JcefInfoviewService(private val project: Project) {
val browser: JBCefBrowser? = if (JBCefApp.isSupported()) {
val browser = JBCefBrowser()
defaultZoomLevel = browser.zoomLevel

// handle link clicks, which should be opened in real browser but not jcef

browser.jbCefClient
.addRequestHandler(object : CefRequestHandler {
override fun onBeforeBrowse(
browser: CefBrowser?,
frame: CefFrame?,
request: CefRequest?,
user_gesture: Boolean,

Check notice on line 88 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `user_gesture` should not contain underscores
is_redirect: Boolean

Check notice on line 89 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `is_redirect` should not contain underscores
): Boolean {
if (request == null) return false
val isInfoview = request.url.startsWith(_url!!)
if (isInfoview) {
return false
} else {
BrowserUtil.browse(request.url)
return true
}
}

override fun onOpenURLFromTab(
browser: CefBrowser?,
frame: CefFrame?,
target_url: String?,

Check notice on line 104 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `target_url` should not contain underscores
user_gesture: Boolean

Check notice on line 105 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `user_gesture` should not contain underscores
): Boolean {
return false
}

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

override fun getAuthCredentials(
browser: CefBrowser?,
origin_url: String?,

Check notice on line 124 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `origin_url` should not contain underscores
isProxy: Boolean,
host: String?,
port: Int,
realm: String?,
scheme: String?,
callback: CefAuthCallback?
): Boolean {
return false
}

override fun onCertificateError(
browser: CefBrowser?,
cert_error: CefLoadHandler.ErrorCode?,

Check notice on line 137 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `cert_error` should not contain underscores
request_url: String?,

Check notice on line 138 in src/main/kotlin/lean4ij/infoview/external/JcefInfoviewTooWindowFactory.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Local variable naming convention

Local variable name `request_url` should not contain underscores
sslInfo: CefSSLInfo?,
callback: CefCallback?
): Boolean {
return false
}

override fun onRenderProcessTerminated(
browser: CefBrowser?,
status: CefRequestHandler.TerminationStatus?
) {
}
}, browser.cefBrowser )
browser
} else {
// TODO make this shorter
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/lean4ij/project/LeanFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ class LeanFile(private val leanProjectService: LeanProjectService, private val f
* more smoothly and independently
* TODO maybe try psi for infoview tool window
* TODO passing things like editor etc seems cumbersome, maybe add some implement for context
* TODO this should maybe named as [updateInternalInfoview], but it contains a switch...
* The switch should put in [updateInternalInfoview]
*/
fun updateCaret(editor: Editor, logicalPosition: LogicalPosition) {
if (lean4Settings.enableNativeInfoview) {
Expand Down Expand Up @@ -455,13 +457,24 @@ class LeanFile(private val leanProjectService: LeanProjectService, private val f
position
)
allMessage = getInteractiveDiagnostics(diagnosticsParams)
// after getting all Messages, do an update intermediately...
// to avoid lag
updateCaretIntermediately()
lastMaxLine = maxLine
maxLine = -1
}
}
}
}

private fun updateCaretIntermediately() {
FileEditorManager.getInstance(project).selectedTextEditor?.let { editor ->
if (editor.virtualFile.path == unquotedFile) {
updateCaret(editor, editor.caretModel.logicalPosition)
}
}
}

/**
* checking a bug for all messages not updated correctly. It shows that there is a cases like:
* [Trace - 10:59:11] Received notification 'textDocument/publishDiagnostics'
Expand Down
33 changes: 31 additions & 2 deletions src/main/kotlin/lean4ij/project/LeanProjectActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.event.DocumentEvent
import com.intellij.openapi.editor.event.DocumentListener
import com.intellij.openapi.editor.ex.EditorEventMulticasterEx
import com.intellij.openapi.editor.ex.FocusChangeListener
import com.intellij.openapi.module.ModuleUtilCore
Expand All @@ -20,6 +23,11 @@ import com.intellij.openapi.module.Module
import lean4ij.lsp.LeanLanguageServerFactory
import lean4ij.util.OsUtil
import java.awt.event.FocusEvent
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.util.text.LineColumn
import com.intellij.openapi.util.text.StringUtil
import lean4ij.util.LeanUtil


fun Module.addExcludeFolder(basePath: String) {
ModuleRootModificationUtil.updateModel(this) { rootModule ->
Expand Down Expand Up @@ -84,7 +92,7 @@ class LeanProjectActivity : ProjectActivity {

override suspend fun execute(project: Project) {
setupModule(project)
setupEditorFocusChangeEventListener()
setupEditorFocusChangeEventListener(project)
project.service<LeanProjectService>()
project.service<LeanFileCaretListener>()
project.service<ExternalInfoViewService>()
Expand All @@ -96,7 +104,7 @@ class LeanProjectActivity : ProjectActivity {
* this is for avoiding didOpen request that make the lean lsp server handling it and improve performance
* but it may have some false positive event though
*/
private fun setupEditorFocusChangeEventListener() {
private fun setupEditorFocusChangeEventListener(project: Project) {
(EditorFactory.getInstance().eventMulticaster as? EditorEventMulticasterEx)?.let { ex ->
ex.addFocusChangeListener(object: FocusChangeListener {
override fun focusGained(editor: Editor) {
Expand Down Expand Up @@ -203,6 +211,27 @@ class LeanProjectActivity : ProjectActivity {
// at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
// at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
}
// ref: https://intellij-support.jetbrains.com/hc/en-us/community/posts/360006419280-DocumentListener-for-getting-what-line-was-changed
// TODO extract DocumentListener
ex.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
val document = event.document
val file = FileDocumentManager.getInstance().getFile(document)?:return
if (!LeanUtil.isLeanFile(file)) {
return
}
val leanProjectService = project.service<LeanProjectService>()
val editor = EditorFactory.getInstance().getEditors(document).firstOrNull()?:return
val lineCol : LineColumn = StringUtil.offsetToLineColumn(document.text, event.offset) ?: return
val position = LogicalPosition(lineCol.line, lineCol.column)
// TODO this may be duplicated with caret events some times
// but without this there are cases no caret events but document changed events
// maybe some debounce
leanProjectService.file(file).updateCaret(editor, position)
}
}) {
// TODO Disposable
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<!-- check https://plugins.jetbrains.com/docs/intellij/icons.html#mapping-entries for making icons to respect theme -->
<toolWindow factoryClass="lean4ij.infoview.LeanInfoViewWindowFactory"
id="LeanInfoViewWindow"
icon="/icons/lean_icon.svg"
icon="/icons/infoview_icon.svg"
/>
<toolWindow factoryClass="lean4ij.infoview.external.JcefInfoviewTooWindowFactory"
id="LeanInfoviewJcef"
Expand Down
50 changes: 50 additions & 0 deletions src/main/resources/icons/infoview_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions src/main/resources/icons/infoview_icon_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions src/main/resources/icons/jcef_icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 78741c1

Please sign in to comment.