Skip to content

Commit 235ea68

Browse files
Added real-time cursor location to status bar (#1514)
1 parent 7353586 commit 235ea68

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

CodeEdit/Features/StatusBar/Views/StatusBarItems/StatusBarCursorLocationLabel.swift

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,89 @@
66
//
77

88
import SwiftUI
9+
import CodeEditSourceEditor
910

1011
struct StatusBarCursorLocationLabel: View {
1112
@Environment(\.controlActiveState)
1213
private var controlActive
1314

1415
@EnvironmentObject private var model: UtilityAreaViewModel
16+
@EnvironmentObject private var editorManager: EditorManager
17+
18+
@State private var file: CEWorkspaceFile?
19+
@State private var cursorPositions: [CursorPosition]?
20+
21+
func updateSource() {
22+
file = editorManager.activeEditor.selectedTab
23+
}
24+
25+
func getLines(_ range: NSRange) -> Int {
26+
if let fileDocument = file?.fileDocument {
27+
let selection = fileDocument.content[range] ?? ""
28+
let lines = selection.components(separatedBy: "\n")
29+
30+
return lines.count
31+
}
32+
33+
return 0
34+
}
35+
36+
func getLabel(_ cursorPositions: [CursorPosition]) -> String {
37+
if cursorPositions.isEmpty {
38+
return ""
39+
}
40+
41+
if cursorPositions.count > 1 {
42+
return "\(cursorPositions.count) selected ranges"
43+
}
44+
45+
if cursorPositions[0].range.length > 0 {
46+
let lineCount = getLines(cursorPositions[0].range)
47+
48+
if lineCount > 1 {
49+
return "\(lineCount) lines"
50+
}
51+
52+
return "\(cursorPositions[0].range.length) characters"
53+
}
54+
55+
return "Line: \(cursorPositions[0].line) Col: \(cursorPositions[0].column)"
56+
}
1557

1658
var body: some View {
17-
Text("Line: \(model.cursorLocation.line) Col: \(model.cursorLocation.column)")
18-
.font(model.toolbarFont)
19-
.foregroundColor(foregroundColor)
20-
.fixedSize()
21-
.lineLimit(1)
22-
.onHover { isHovering($0) }
59+
Group {
60+
if let currentFile = file, let fileDocument = currentFile.fileDocument {
61+
Group {
62+
if let cursorPositions = cursorPositions {
63+
Text(getLabel(cursorPositions))
64+
} else {
65+
EmptyView()
66+
}
67+
}
68+
.onReceive(fileDocument.$cursorPositions) { val in
69+
cursorPositions = val
70+
}
71+
} else {
72+
EmptyView()
73+
}
74+
}
75+
.font(model.toolbarFont)
76+
.foregroundColor(foregroundColor)
77+
.fixedSize()
78+
.lineLimit(1)
79+
.onHover { isHovering($0) }
80+
.onAppear {
81+
updateSource()
82+
}
83+
.onReceive(editorManager.activeEditor.objectWillChange) { _ in
84+
updateSource()
85+
}
86+
.onChange(of: editorManager.activeEditor) { _ in
87+
updateSource()
88+
}
89+
.onChange(of: editorManager.activeEditor.selectedTab) { _ in
90+
updateSource()
91+
}
2392
}
2493

2594
private var foregroundColor: Color {

0 commit comments

Comments
 (0)