Skip to content

Commit

Permalink
Listen to editor events instead of polling
Browse files Browse the repository at this point in the history
  • Loading branch information
nvbn committed Jul 14, 2016
1 parent a468eba commit 6edee1b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ class ToggleShowingCodeAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
context(e) {
if (it.sync.syncing) {
it.listener.stop()
it.sync.stop()
} else {
if (it.sync.start()) {
it.listener.listen()
showSyncingNotification(it.sync.project, it.sync.session)
} else {
showErrorNotification(it.sync.project)
Expand Down
24 changes: 16 additions & 8 deletions src/main/code_view/client_idea/services/SyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ package code_view.client_idea.services

import code_view.client_idea.Client
import code_view.client_idea.Session
import code_view.client_idea.utils.SyncLoopThread
import code_view.client_idea.settings.url
import code_view.client_idea.utils.context
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiUtilBase
import java.util.concurrent.LinkedBlockingQueue


class SyncService {
lateinit var project: Project
lateinit var client: Client
lateinit var session: Session
var previousSession: Session? = null
val queue = LinkedBlockingQueue<Session>()
var syncing = false

init {
SyncLoopThread {
if (syncing && project is Project) {
onDispatchThread {
session = updateSession(session)
}

ApplicationManager.getApplication().executeOnPooledThread {
var previousSession: Session? = null
while (true) {
val session = queue.take()
if (session != previousSession && session.fileName != null) {
client.updateSession(session)
previousSession = session
Expand Down Expand Up @@ -74,6 +73,15 @@ class SyncService {
return this
}

fun sync() {
session = updateSession(session)
if (!queue.isEmpty()) {
queue.clear()
}

queue.offer(session)
}

companion object {
fun getInstance(project: Project) = ServiceManager
.getService(project, SyncService::class.java)
Expand Down
51 changes: 51 additions & 0 deletions src/main/code_view/client_idea/utils/EditorListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package code_view.client_idea.utils

import com.intellij.openapi.editor.EditorFactory
import com.intellij.openapi.editor.event.*


class EditorListener(val ctx: Context) : CaretListener, DocumentListener, SelectionListener, VisibleAreaListener {
val editorManager: EditorEventMulticaster = EditorFactory.getInstance().eventMulticaster

fun listen() {
editorManager.addDocumentListener(this)
editorManager.addSelectionListener(this)
editorManager.addCaretListener(this)
editorManager.addVisibleAreaListener(this)
}

fun stop() {
editorManager.removeSelectionListener(this)
editorManager.removeDocumentListener(this)
editorManager.removeCaretListener(this)
editorManager.removeVisibleAreaListener(this)
}

override fun documentChanged(event: DocumentEvent) {
ctx.sync.sync()
}

override fun caretAdded(caretEvent: CaretEvent) {
ctx.sync.sync()
}

override fun caretRemoved(caretEvent: CaretEvent) {
ctx.sync.sync()
}

override fun beforeDocumentChange(event: DocumentEvent) {
ctx.sync.sync()
}

override fun caretPositionChanged(event: CaretEvent) {
ctx.sync.sync()
}

override fun visibleAreaChanged(event: VisibleAreaEvent) {
ctx.sync.sync()
}

override fun selectionChanged(event: SelectionEvent) {
ctx.sync.sync()
}
}
28 changes: 0 additions & 28 deletions src/main/code_view/client_idea/utils/SyncLoopThread.kt

This file was deleted.

1 change: 1 addition & 0 deletions src/main/code_view/client_idea/utils/context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project
class Context(val project: Project) {
val sync by lazy { SyncService.getInstance(project) }
val settings by lazy { PropertiesComponent.getInstance(project)!! }
val listener by lazy { EditorListener(this) }
}

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

0 comments on commit 6edee1b

Please sign in to comment.