@@ -18,90 +18,86 @@ class TypstAnnotatedTextBuilder(
18
18
private var codeModeString = false
19
19
private var codeModeBracketsCounter = 0
20
20
21
- @Suppress(" ReturnCount" , " ComplexMethod" )
22
21
override fun processCharacter () {
23
- if (processEscapeCharacter()) return
24
- if (processMathBlock()) return
25
- if (codeMode) {
26
- processCodeMode()
27
- return
28
- }
29
- if (addMarkupInternal(CODE_REGEX )) {
30
- codeModeBracketsCounter++
31
- codeMode = true
32
- return
33
- }
22
+ processEscapeCharacter()
23
+ processMathBlock()
24
+ processCodeMode()
34
25
35
26
if (this .isStartOfLine) {
36
- if ( addMarkupInternal(LIST_REGEX )) return
37
- if ( addMarkupInternal(LEADING_WHITESPACE_REGEX )) return
38
- if ( addMarkupInternal(HEADING_REGEX )) return
27
+ addMarkupInternal(LIST_REGEX )
28
+ addMarkupInternal(LEADING_WHITESPACE_REGEX )
29
+ addMarkupInternal(HEADING_REGEX )
39
30
}
40
31
41
- if ( addMarkupInternal(LINE_COMMENT_REGEX , " \n " )) return
42
- if ( addMarkupInternal(MULTILINELINE_COMMENT_REGEX , " \n " )) return
43
- if ( addMarkupInternal(MARKUP_REGEX )) return
44
- if ( addMarkupInternal(LET_REGEX )) return
45
- if ( addMarkupInternal(IMPORT_REGEX , " \n " )) return
46
-
47
- if ( addMarkupInternal(SQUARE_BRACKETS_REGEX )) return
32
+ addMarkupInternal(CODE_REGEX , " " , true )
33
+ addMarkupInternal(LINE_COMMENT_REGEX , " \n " )
34
+ addMarkupInternal(MULTILINELINE_COMMENT_REGEX , " \n " )
35
+ addMarkupInternal(MARKUP_REGEX )
36
+ addMarkupInternal(LET_REGEX )
37
+ addMarkupInternal( IMPORT_REGEX , " \n " )
38
+ addMarkupInternal(SQUARE_BRACKETS_REGEX )
48
39
49
40
addText(this .curString)
50
41
}
51
42
43
+ override fun addText (text : String? ): CharacterBasedCodeAnnotatedTextBuilder {
44
+ if (characterProcessed) return this
45
+ return super .addText(text)
46
+ }
47
+
52
48
private fun addMarkupInternal (
53
49
regex : Regex ,
54
50
interpretAs : String = "",
55
- ): Boolean {
51
+ startofCodeBlock : Boolean = false,
52
+ ) {
53
+ if (characterProcessed) return
56
54
var matchResult: MatchResult ?
57
55
matchResult = matchFromPosition(regex)
58
56
if (matchResult != null ) {
59
57
addMarkup(matchResult.value, interpretAs)
60
- return true
58
+ if (startofCodeBlock) {
59
+ codeModeBracketsCounter++
60
+ codeMode = true
61
+ }
61
62
}
62
- return false
63
63
}
64
64
65
- private fun processEscapeCharacter (): Boolean {
65
+ private fun processEscapeCharacter () {
66
+ if (characterProcessed) return
66
67
// Check for backslash escape character
67
68
if (this .curString == " \\ " ) {
68
69
addMarkup(this .curString)
69
70
// Add subsequent char as text if available
70
71
if (this .code.length > this .pos) {
72
+ characterProcessed = false
71
73
addText(this .code[this .pos].toString())
72
- return true
73
74
}
74
75
}
75
- return false
76
76
}
77
77
78
- private fun processMathBlock (): Boolean {
78
+ private fun processMathBlock () {
79
+ if (characterProcessed) return
79
80
if (this .curString == " $" ) {
80
81
// Start or end of math mode
81
82
mathMode = ! mathMode
82
83
addMarkup(this .curString)
83
- return true
84
84
} else if (mathMode) {
85
85
if (this .curString == " \" " ) {
86
86
// Start or end of String within math mode
87
87
mathModeString = ! mathModeString
88
88
addText(this .curString)
89
- return true
90
89
}
91
90
if (mathModeString) {
92
91
// String within math mode to be spell checked
93
92
addText(this .curString)
94
93
} else {
95
94
addMarkup(this .curString)
96
95
}
97
- return true
98
- } else {
99
- // No math mode
100
- return false
101
96
}
102
97
}
103
98
104
99
private fun processCodeMode () {
100
+ if (! codeMode || characterProcessed) return
105
101
when (this .curString) {
106
102
" (" -> {
107
103
codeModeBracketsCounter++
0 commit comments