fix brace-depth tracking for syntax highlighting and line classification
This commit is contained in:
parent
3be834f22e
commit
39d2658d67
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue