From 39d2658d67b7faf3e8e6982dbe21ac2e75ea20e6 Mon Sep 17 00:00:00 2001 From: jess Date: Mon, 6 Apr 2026 11:30:55 -0700 Subject: [PATCH] fix brace-depth tracking for syntax highlighting and line classification --- core/src/doc.rs | 21 +++++++-------------- src/EditorView.swift | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/core/src/doc.rs b/core/src/doc.rs index cecc4e6..c4b782f 100644 --- a/core/src/doc.rs +++ b/core/src/doc.rs @@ -59,20 +59,13 @@ fn is_cordial(line: &str) -> bool { return false; } - // while (...) { - if line.starts_with("while ") || line.starts_with("while(") { - return true; - } - - // fn name(...) { - if line.starts_with("fn ") { - return true; - } - - // lone closing brace - if line == "}" || line.starts_with("} ") { - return true; - } + if line.starts_with("while ") || line.starts_with("while(") { return true; } + if line.starts_with("fn ") { return true; } + if line.starts_with("if ") || line.starts_with("if(") { return true; } + if line.starts_with("else ") || line == "else" || line.starts_with("else{") { return true; } + if line.starts_with("for ") { return true; } + if line.starts_with("return ") || line == "return" { return true; } + if line == "}" || line.starts_with("} ") { return true; } if let Some(eq_pos) = line.find('=') { if eq_pos > 0 { diff --git a/src/EditorView.swift b/src/EditorView.swift index c8817cc..7416be4 100644 --- a/src/EditorView.swift +++ b/src/EditorView.swift @@ -1055,6 +1055,7 @@ func applySyntaxHighlighting(to textStorage: NSTextStorage) { let nsText = text as NSString var lineStart = 0 + var braceDepth = 0 while lineStart < nsText.length { let lineRange = nsText.lineRange(for: NSRange(location: lineStart, length: 0)) @@ -1068,7 +1069,15 @@ func applySyntaxHighlighting(to textStorage: NSTextStorage) { let trimmed = line.trimmingCharacters(in: .whitespaces) let isTableHeader = tableHeaderLines.contains(lineRange.location) - if highlightMarkdownLine(trimmed, line: line, lineRange: lineRange, textStorage: textStorage, baseFont: baseFont, palette: palette, isTableHeader: isTableHeader) { + let inBlock = braceDepth > 0 + + if isCordialLine(trimmed) || inBlock { + let opens = trimmed.filter { $0 == "{" }.count + let closes = trimmed.filter { $0 == "}" }.count + braceDepth += opens - closes + if braceDepth < 0 { braceDepth = 0 } + highlightCodeLine(line, lineRange: lineRange, textStorage: textStorage, syn: syn) + } else if highlightMarkdownLine(trimmed, line: line, lineRange: lineRange, textStorage: textStorage, baseFont: baseFont, palette: palette, isTableHeader: isTableHeader) { highlightInlineCode(textStorage: textStorage, lineRange: lineRange, palette: palette, baseFont: baseFont) highlightStrikethrough(textStorage: textStorage, lineRange: lineRange, palette: palette) highlightFootnoteRefs(textStorage: textStorage, lineRange: lineRange, palette: palette) @@ -1076,9 +1085,6 @@ func applySyntaxHighlighting(to textStorage: NSTextStorage) { continue } - if isCordialLine(trimmed) { - highlightCodeLine(line, lineRange: lineRange, textStorage: textStorage, syn: syn) - } highlightInlineCode(textStorage: textStorage, lineRange: lineRange, palette: palette, baseFont: baseFont) highlightStrikethrough(textStorage: textStorage, lineRange: lineRange, palette: palette) highlightFootnoteRefs(textStorage: textStorage, lineRange: lineRange, palette: palette) @@ -1601,9 +1607,14 @@ private func isCordialLine(_ trimmed: String) -> Bool { if trimmed.hasPrefix("/=") { return true } if trimmed.hasPrefix("//") { return true } if trimmed.hasPrefix("/*") { return true } - if trimmed.hasPrefix("let ") { - return trimmed.contains("=") - } + if trimmed.hasPrefix("let ") { return trimmed.contains("=") } + if trimmed.hasPrefix("fn ") { return true } + if trimmed.hasPrefix("while ") || trimmed.hasPrefix("while(") { return true } + if trimmed.hasPrefix("if ") || trimmed.hasPrefix("if(") { return true } + if trimmed.hasPrefix("else ") || trimmed == "else" || trimmed.hasPrefix("else{") { return true } + if trimmed.hasPrefix("for ") { return true } + if trimmed.hasPrefix("return ") || trimmed == "return" { return true } + if trimmed == "}" || trimmed.hasPrefix("} ") { return true } guard let eqIdx = trimmed.firstIndex(of: "=") else { return false } if eqIdx == trimmed.startIndex { return false } let after = trimmed.index(after: eqIdx)