Skip to content

Commit c468f0b

Browse files
Show Cursor Events as Range Highlighter
When the ethersync client sends cursor events, the Intellij plugin will highlight the cursors of remote ethersync clients as range highlights in the editor.
1 parent 2b45029 commit c468f0b

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

src/main/kotlin/io/github/ethersync/EthersyncService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package io.github.ethersync
22

33
interface EthersyncService {
44

5-
fun connectToPeer(multiAddress: String)
6-
}
5+
fun connectToPeer(peer: String)
6+
}

src/main/kotlin/io/github/ethersync/EthersyncServiceImpl.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.intellij.openapi.project.Project
77
import com.intellij.util.io.awaitExit
88
import com.intellij.util.io.readLineAsync
99
import io.github.ethersync.protocol.EthersyncEditorProtocol
10-
import io.github.ethersync.protocol.EthersyncEditorProtocolImpl
1110
import kotlinx.coroutines.CoroutineScope
1211
import kotlinx.coroutines.launch
1312
import org.eclipse.lsp4j.jsonrpc.Launcher
@@ -87,7 +86,7 @@ class EthersyncServiceImpl(
8786
clientProcess.outputStream,
8887
Executors.newCachedThreadPool(),
8988
{ c -> c },
90-
{ gsonBuilder -> {} }
89+
{ _ -> run {} }
9190
)
9291

9392
val listening = launcher.startListening()
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,65 @@
11
package io.github.ethersync.protocol
22

3+
import com.intellij.openapi.application.ApplicationManager
4+
import com.intellij.openapi.application.ModalityState
35
import com.intellij.openapi.components.Service
6+
import com.intellij.openapi.editor.LogicalPosition
7+
import com.intellij.openapi.editor.markup.HighlighterLayer
8+
import com.intellij.openapi.editor.markup.HighlighterTargetArea
9+
import com.intellij.openapi.editor.markup.RangeHighlighter
10+
import com.intellij.openapi.editor.markup.TextAttributes
411
import com.intellij.openapi.fileEditor.FileEditorManager
12+
import com.intellij.openapi.fileEditor.TextEditor
513
import com.intellij.openapi.project.Project
14+
import com.intellij.ui.JBColor
15+
import java.util.LinkedList
616

717
@Service(Service.Level.PROJECT)
818
class EthersyncEditorProtocolImpl(private val project: Project) : EthersyncEditorProtocol {
19+
20+
private val highlighter = LinkedList<RangeHighlighter>()
21+
922
override fun cursor(cursorEvent: CursorEvent) {
10-
System.out.printf("Cursor: %s, %s\n", cursorEvent.documentUri, cursorEvent.ranges)
1123

12-
val fileEditorManager = FileEditorManager.getInstance(project);
13-
val editor = fileEditorManager.allEditors
14-
.find { editor -> editor.file.url == cursorEvent.documentUri } ?: return
24+
val fileEditorManager = FileEditorManager.getInstance(project)
25+
26+
val fileEditor = fileEditorManager.allEditors
27+
.first { editor -> editor.file.url == cursorEvent.documentUri } ?: return
28+
29+
if (fileEditor is TextEditor) {
30+
val editor = fileEditor.editor
31+
ApplicationManager.getApplication().invokeLater({
32+
synchronized(highlighter) {
33+
val markupModel = editor.markupModel
34+
35+
for (hl in highlighter) {
36+
markupModel.removeHighlighter(hl)
37+
}
38+
highlighter.clear()
39+
40+
for(range in cursorEvent.ranges) {
41+
val startPosition = editor.logicalPositionToOffset(LogicalPosition(range.start.line, range.start.character))
42+
val endPosition = editor.logicalPositionToOffset(LogicalPosition(range.end.line, range.end.character))
43+
44+
val textAttributes = TextAttributes().apply {
45+
backgroundColor = JBColor(JBColor.YELLOW, JBColor.DARK_GRAY)
46+
// TODO: unclear which is the best effect type
47+
// effectType = EffectType.LINE_UNDERSCORE
48+
// effectColor = JBColor(JBColor.YELLOW, JBColor.DARK_GRAY)
49+
}
50+
51+
val hl = markupModel.addRangeHighlighter(
52+
startPosition,
53+
endPosition + 1,
54+
HighlighterLayer.ADDITIONAL_SYNTAX,
55+
textAttributes,
56+
HighlighterTargetArea.EXACT_RANGE
57+
)
1558

16-
// TODO find a way how to create an additional cursor
59+
highlighter.add(hl)
60+
}
61+
}
62+
}, ModalityState.nonModal())
63+
}
1764
}
18-
}
65+
}

src/main/resources/META-INF/pluginIcon.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 14 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)