Skip to content

Commit

Permalink
Fix tab stop calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
mchakravarty committed Jan 18, 2025
1 parent f63c421 commit 2b53241
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Sources/CodeEditorView/CodeEditing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,14 @@ extension CodeView {
}

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

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

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

0 comments on commit 2b53241

Please sign in to comment.