Skip to content

Commit 4968d38

Browse files
committed
Improve syntax highlighting of multiline strings
1 parent bc68805 commit 4968d38

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

Diff for: v2/highlight.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ func (e *Editor) WriteLines(c *vt100.Canvas, fromline, toline LineIndex, cx, cy
4141
arrowBeforeCommentMarker bool
4242
inListItem bool
4343
inCodeBlock bool // used when highlighting Doc, Markdown, Python, Nim, Mojo or Starlark
44-
threeQuoteStart bool
45-
threeQuoteEnd bool
4644
ok bool
4745
codeBlockFound bool
4846
foundDocstringMarker bool
@@ -155,20 +153,7 @@ func (e *Editor) WriteLines(c *vt100.Canvas, fromline, toline LineIndex, cx, cy
155153
for li = LineIndex(0); li < fromline; li++ {
156154
line = e.Line(li)
157155
trimmedLine = strings.TrimSpace(line)
158-
// return """ can be the start of a multiline string, where """ is the end of the multiline string!
159-
threeQuoteStart = strings.HasPrefix(trimmedLine, "\"\"\"") || strings.HasPrefix(trimmedLine, "'''")
160-
threeQuoteEnd = strings.HasSuffix(trimmedLine, "\"\"\"") || strings.HasSuffix(trimmedLine, "'''")
161-
if threeQuoteStart && threeQuoteEnd {
162-
inCodeBlock = false
163-
} else if trimmedLine == "return \"\"\"" || trimmedLine == "return '''" {
164-
inCodeBlock = true
165-
} else if strings.HasSuffix(trimmedLine, " = \"\"\"") || strings.HasSuffix(trimmedLine, " = '''") {
166-
inCodeBlock = true
167-
} else if inCodeBlock && strings.Contains(trimmedLine, "\"\"\"") {
168-
inCodeBlock = false
169-
} else if threeQuoteStart || threeQuoteEnd {
170-
inCodeBlock = !inCodeBlock
171-
}
156+
inCodeBlock, _ = checkMultiLineString(trimmedLine, inCodeBlock)
172157
}
173158
}
174159

Diff for: v2/strings.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,19 @@ func trimRightSpace(str string) string {
228228
// checkMultiLineString detects and updates the inCodeBlock state.
229229
// For languages like Nim, Mojo, Python and Starlark.
230230
func checkMultiLineString(trimmedLine string, inCodeBlock bool) (bool, bool) {
231+
trimmedLine = strings.TrimPrefix(trimmedLine, "return ")
231232
foundDocstringMarker := false
233+
// Check for special syntax patterns that indicate the start of a multiline string
232234
if trimmedLine == "\"\"\"" || trimmedLine == "'''" { // only 3 letters
233235
inCodeBlock = !inCodeBlock
234236
foundDocstringMarker = true
235-
} else if strings.HasPrefix(trimmedLine, "\"\"\"") && strings.HasSuffix(trimmedLine, "\"\"\"") { // this could be 6 lte
237+
} else if strings.HasSuffix(trimmedLine, " = \"\"\"") || strings.HasSuffix(trimmedLine, " = '''") {
238+
inCodeBlock = true
239+
foundDocstringMarker = true
240+
} else if strings.HasPrefix(trimmedLine, "\"\"\"") && strings.HasSuffix(trimmedLine, "\"\"\"") { // this could be 6 letters
236241
inCodeBlock = false
237242
foundDocstringMarker = true
238-
} else if strings.HasPrefix(trimmedLine, "'''") && strings.HasSuffix(trimmedLine, "'''") { // this could be 6 lettersre
243+
} else if strings.HasPrefix(trimmedLine, "'''") && strings.HasSuffix(trimmedLine, "'''") { // this could be 6 letters
239244
inCodeBlock = false
240245
foundDocstringMarker = true
241246
} else if strings.HasPrefix(trimmedLine, "\"\"\"") || strings.HasPrefix(trimmedLine, "'''") { // this is more than 3 ts
@@ -244,7 +249,7 @@ func checkMultiLineString(trimmedLine string, inCodeBlock bool) (bool, bool) {
244249
foundDocstringMarker = true
245250
}
246251
} else if strings.HasSuffix(trimmedLine, "\"\"\"") || strings.HasSuffix(trimmedLine, "'''") { // this is more than 3 ts
247-
if (strings.Count(trimmedLine, "\"\"\"") % 2 != 0 || strings.Count(trimmedLine, "'''") % 2 != 0) {
252+
if strings.Count(trimmedLine, "\"\"\"")%2 != 0 || strings.Count(trimmedLine, "'''")%2 != 0 {
248253
inCodeBlock = !inCodeBlock
249254
}
250255
if inCodeBlock {

0 commit comments

Comments
 (0)