Skip to content

Commit 2b53241

Browse files
committed
Fix tab stop calculation
1 parent f63c421 commit 2b53241

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Sources/CodeEditorView/CodeEditing.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,14 @@ extension CodeView {
654654
}
655655

656656
func insertTab(in range: NSRange) -> NSRange {
657+
guard let firstLine = codeStorageDelegate.lineMap.lineOf(index: range.location),
658+
let lineInfo = codeStorageDelegate.lineMap.lookup(line: firstLine)
659+
else { return range }
657660

658-
let nextTabStopIndex = (range.location / indentation.tabWidth + 1) * indentation.tabWidth
659-
let replacementString = if indentation.preference == .preferTabs { "\t" }
660-
else { String(repeating: " ", count: nextTabStopIndex - range.location) }
661+
let column = range.location - lineInfo.range.location,
662+
nextTabStopIndex = (column / indentation.tabWidth + 1) * indentation.tabWidth,
663+
replacementString = if indentation.preference == .preferTabs { "\t" }
664+
else { String(repeating: " ", count: nextTabStopIndex - column) }
661665
codeStorage.replaceCharacters(in: range, with: replacementString)
662666

663667
return NSRange(location: range.location + replacementString.utf16.count, length: 0)

0 commit comments

Comments
 (0)