Skip to content

Commit 6edee1b

Browse files
committed
Listen to editor events instead of polling
1 parent a468eba commit 6edee1b

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed

src/main/code_view/client_idea/actions/ToggleShowingCodeAction.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ class ToggleShowingCodeAction : AnAction() {
6767
override fun actionPerformed(e: AnActionEvent) {
6868
context(e) {
6969
if (it.sync.syncing) {
70+
it.listener.stop()
7071
it.sync.stop()
7172
} else {
7273
if (it.sync.start()) {
74+
it.listener.listen()
7375
showSyncingNotification(it.sync.project, it.sync.session)
7476
} else {
7577
showErrorNotification(it.sync.project)

src/main/code_view/client_idea/services/SyncService.kt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@ package code_view.client_idea.services
22

33
import code_view.client_idea.Client
44
import code_view.client_idea.Session
5-
import code_view.client_idea.utils.SyncLoopThread
65
import code_view.client_idea.settings.url
76
import code_view.client_idea.utils.context
7+
import com.intellij.openapi.application.ApplicationManager
88
import com.intellij.openapi.components.ServiceManager
99
import com.intellij.openapi.editor.Caret
1010
import com.intellij.openapi.editor.Editor
1111
import com.intellij.openapi.fileEditor.FileEditorManager
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.psi.PsiFile
1414
import com.intellij.psi.util.PsiUtilBase
15+
import java.util.concurrent.LinkedBlockingQueue
1516

1617

1718
class SyncService {
1819
lateinit var project: Project
1920
lateinit var client: Client
2021
lateinit var session: Session
21-
var previousSession: Session? = null
22+
val queue = LinkedBlockingQueue<Session>()
2223
var syncing = false
2324

2425
init {
25-
SyncLoopThread {
26-
if (syncing && project is Project) {
27-
onDispatchThread {
28-
session = updateSession(session)
29-
}
30-
26+
ApplicationManager.getApplication().executeOnPooledThread {
27+
var previousSession: Session? = null
28+
while (true) {
29+
val session = queue.take()
3130
if (session != previousSession && session.fileName != null) {
3231
client.updateSession(session)
3332
previousSession = session
@@ -74,6 +73,15 @@ class SyncService {
7473
return this
7574
}
7675

76+
fun sync() {
77+
session = updateSession(session)
78+
if (!queue.isEmpty()) {
79+
queue.clear()
80+
}
81+
82+
queue.offer(session)
83+
}
84+
7785
companion object {
7886
fun getInstance(project: Project) = ServiceManager
7987
.getService(project, SyncService::class.java)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package code_view.client_idea.utils
2+
3+
import com.intellij.openapi.editor.EditorFactory
4+
import com.intellij.openapi.editor.event.*
5+
6+
7+
class EditorListener(val ctx: Context) : CaretListener, DocumentListener, SelectionListener, VisibleAreaListener {
8+
val editorManager: EditorEventMulticaster = EditorFactory.getInstance().eventMulticaster
9+
10+
fun listen() {
11+
editorManager.addDocumentListener(this)
12+
editorManager.addSelectionListener(this)
13+
editorManager.addCaretListener(this)
14+
editorManager.addVisibleAreaListener(this)
15+
}
16+
17+
fun stop() {
18+
editorManager.removeSelectionListener(this)
19+
editorManager.removeDocumentListener(this)
20+
editorManager.removeCaretListener(this)
21+
editorManager.removeVisibleAreaListener(this)
22+
}
23+
24+
override fun documentChanged(event: DocumentEvent) {
25+
ctx.sync.sync()
26+
}
27+
28+
override fun caretAdded(caretEvent: CaretEvent) {
29+
ctx.sync.sync()
30+
}
31+
32+
override fun caretRemoved(caretEvent: CaretEvent) {
33+
ctx.sync.sync()
34+
}
35+
36+
override fun beforeDocumentChange(event: DocumentEvent) {
37+
ctx.sync.sync()
38+
}
39+
40+
override fun caretPositionChanged(event: CaretEvent) {
41+
ctx.sync.sync()
42+
}
43+
44+
override fun visibleAreaChanged(event: VisibleAreaEvent) {
45+
ctx.sync.sync()
46+
}
47+
48+
override fun selectionChanged(event: SelectionEvent) {
49+
ctx.sync.sync()
50+
}
51+
}

src/main/code_view/client_idea/utils/SyncLoopThread.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/code_view/client_idea/utils/context.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project
99
class Context(val project: Project) {
1010
val sync by lazy { SyncService.getInstance(project) }
1111
val settings by lazy { PropertiesComponent.getInstance(project)!! }
12+
val listener by lazy { EditorListener(this) }
1213
}
1314

1415
fun <T> context(project: Project, run: (context: Context) -> T) =

0 commit comments

Comments
 (0)