Skip to content

Commit d4226f0

Browse files
Display Cursor Name
When receiving cursor events the remote user's name will be displayed at the end of the line.
1 parent 2bc7072 commit d4226f0

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
.gradle
22
.intellijPlatform/
3+
.kotlin/
34
build/
45
!gradle/wrapper/gradle-wrapper.jar
56
!**/src/main/**/build/
67
!**/src/test/**/build/
78
kls_database.db
89

10+
.ethersync/
11+
912
### IntelliJ IDEA ###
1013
.idea/modules.xml
1114
.idea/jarRepositories.xml

src/main/kotlin/io/github/ethersync/sync/Cursortracker.kt

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.ethersync.sync
22

3+
import com.intellij.openapi.editor.Editor
34
import com.intellij.openapi.editor.LogicalPosition
5+
import com.intellij.openapi.editor.colors.EditorFontType
46
import com.intellij.openapi.editor.event.CaretEvent
57
import com.intellij.openapi.editor.event.CaretListener
68
import com.intellij.openapi.editor.markup.*
@@ -18,6 +20,8 @@ import kotlinx.coroutines.launch
1820
import org.eclipse.lsp4j.Position
1921
import org.eclipse.lsp4j.Range
2022
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
23+
import java.awt.Graphics
24+
import java.awt.Graphics2D
2125
import java.util.*
2226
import kotlin.collections.HashMap
2327

@@ -54,7 +58,7 @@ class Cursortracker(
5458
}
5559

5660
val newHighlighter = LinkedList<RangeHighlighter>()
57-
for(range in cursorEvent.ranges) {
61+
for((i, range) in cursorEvent.ranges.withIndex()) {
5862
val startPosition = editor.logicalPositionToOffset(LogicalPosition(range.start.line, range.start.character))
5963
val endPosition = editor.logicalPositionToOffset(LogicalPosition(range.end.line, range.end.character))
6064

@@ -68,9 +72,29 @@ class Cursortracker(
6872
HighlighterLayer.ADDITIONAL_SYNTAX,
6973
textAttributes,
7074
HighlighterTargetArea.EXACT_RANGE
71-
)
72-
if (cursorEvent.name != null) {
73-
hl.errorStripeTooltip = cursorEvent.name
75+
).apply {
76+
customRenderer = object : CustomHighlighterRenderer {
77+
override fun paint(editor: Editor, rl: RangeHighlighter, g: Graphics) {
78+
if (i > 0) {
79+
return
80+
}
81+
82+
if (g is Graphics2D) {
83+
val position = editor.offsetToVisualPosition(rl.endOffset)
84+
val endOfLineOffset = editor.document.getLineEndOffset(position.line)
85+
val endOfLinePosition = editor.offsetToVisualPosition(endOfLineOffset)
86+
val endOfLineVisualPosition = editor.visualPositionToXY(endOfLinePosition)
87+
88+
val font = editor.colorsScheme.getFont(EditorFontType.PLAIN);
89+
g.font = font
90+
91+
val metrics = g.getFontMetrics(font)
92+
val bounds = metrics.getStringBounds(cursorEvent.name, g)
93+
94+
g.drawString(cursorEvent.name, endOfLineVisualPosition.x + 10f, endOfLineVisualPosition.y + bounds.height.toFloat())
95+
}
96+
}
97+
}
7498
}
7599

76100
newHighlighter.add(hl)
@@ -126,4 +150,4 @@ class Cursortracker(
126150
}
127151
}
128152
}
129-
}
153+
}

0 commit comments

Comments
 (0)