Skip to content

Commit 21d7205

Browse files
committed
Refactor
1 parent 42d3541 commit 21d7205

File tree

2 files changed

+61
-34
lines changed

2 files changed

+61
-34
lines changed

Diff for: v2/quotestate.go

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"errors"
5+
"strings"
56

67
"github.com/xyproto/mode"
78
)
@@ -236,3 +237,63 @@ func (q *QuoteState) ParBraCount(line string) (int, int) {
236237
qCopy.Process(line)
237238
return qCopy.parCount, qCopy.braCount
238239
}
240+
241+
// checkMultiLineString detects and updates the inCodeBlock state.
242+
// For languages like Nim, Mojo, Python and Starlark.
243+
func checkMultiLineString(trimmedLine string, inCodeBlock bool) (bool, bool) {
244+
trimmedLine = strings.TrimPrefix(trimmedLine, "return ")
245+
foundDocstringMarker := false
246+
// Check for special syntax patterns that indicate the start of a multiline string
247+
if trimmedLine == "\"\"\"" || trimmedLine == "'''" { // only 3 letters
248+
inCodeBlock = !inCodeBlock
249+
foundDocstringMarker = true
250+
} else if strings.HasSuffix(trimmedLine, " = \"\"\"") || strings.HasSuffix(trimmedLine, " = '''") {
251+
inCodeBlock = true
252+
foundDocstringMarker = true
253+
} else if strings.HasPrefix(trimmedLine, "\"\"\"") && strings.HasSuffix(trimmedLine, "\"\"\"") { // this could be 6 letters
254+
inCodeBlock = false
255+
foundDocstringMarker = true
256+
} else if strings.HasPrefix(trimmedLine, "'''") && strings.HasSuffix(trimmedLine, "'''") { // this could be 6 letters
257+
inCodeBlock = false
258+
foundDocstringMarker = true
259+
} else if strings.HasPrefix(trimmedLine, "\"\"\"") || strings.HasPrefix(trimmedLine, "'''") { // this is more than 3 ts
260+
inCodeBlock = !inCodeBlock
261+
if inCodeBlock {
262+
foundDocstringMarker = true
263+
}
264+
} else if strings.HasSuffix(trimmedLine, "\"\"\"") || strings.HasSuffix(trimmedLine, "'''") { // this is more than 3 ts
265+
if strings.Count(trimmedLine, "\"\"\"")%2 != 0 || strings.Count(trimmedLine, "'''")%2 != 0 {
266+
inCodeBlock = !inCodeBlock
267+
}
268+
if inCodeBlock {
269+
foundDocstringMarker = true
270+
}
271+
}
272+
return inCodeBlock, foundDocstringMarker
273+
}
274+
275+
// checkMultiLineString2 detects and updates the inCodeBlock state.
276+
// For languages like Nim, Mojo, Python and Starlark.
277+
func checkMultiLineString2(trimmedLine string, inCodeBlock bool) (bool, bool) {
278+
foundDocstringMarker := false
279+
if trimmedLine == "return \"\"\"" || trimmedLine == "return '''" {
280+
inCodeBlock = true
281+
foundDocstringMarker = false
282+
} else if trimmedLine == "\"\"\"" || trimmedLine == "'''" { // only 3 letters
283+
inCodeBlock = !inCodeBlock
284+
foundDocstringMarker = true
285+
} else if strings.HasPrefix(trimmedLine, "\"\"\"") && strings.HasSuffix(trimmedLine, "\"\"\"") {
286+
inCodeBlock = false
287+
foundDocstringMarker = true
288+
} else if strings.HasPrefix(trimmedLine, "'''") && strings.HasSuffix(trimmedLine, "'''") {
289+
inCodeBlock = false
290+
foundDocstringMarker = true
291+
} else if strings.HasPrefix(trimmedLine, "\"\"\"") || strings.HasPrefix(trimmedLine, "'''") {
292+
inCodeBlock = !inCodeBlock
293+
foundDocstringMarker = true
294+
} else if strings.HasSuffix(trimmedLine, "\"\"\"") || strings.HasSuffix(trimmedLine, "'''") {
295+
inCodeBlock = !inCodeBlock
296+
foundDocstringMarker = true
297+
}
298+
return inCodeBlock, foundDocstringMarker
299+
}

Diff for: v2/strings.go

-34
Original file line numberDiff line numberDiff line change
@@ -235,40 +235,6 @@ func trimRightSpace(str string) string {
235235
return strings.TrimRightFunc(str, unicode.IsSpace)
236236
}
237237

238-
// checkMultiLineString detects and updates the inCodeBlock state.
239-
// For languages like Nim, Mojo, Python and Starlark.
240-
func checkMultiLineString(trimmedLine string, inCodeBlock bool) (bool, bool) {
241-
trimmedLine = strings.TrimPrefix(trimmedLine, "return ")
242-
foundDocstringMarker := false
243-
// Check for special syntax patterns that indicate the start of a multiline string
244-
if trimmedLine == "\"\"\"" || trimmedLine == "'''" { // only 3 letters
245-
inCodeBlock = !inCodeBlock
246-
foundDocstringMarker = true
247-
} else if strings.HasSuffix(trimmedLine, " = \"\"\"") || strings.HasSuffix(trimmedLine, " = '''") {
248-
inCodeBlock = true
249-
foundDocstringMarker = true
250-
} else if strings.HasPrefix(trimmedLine, "\"\"\"") && strings.HasSuffix(trimmedLine, "\"\"\"") { // this could be 6 letters
251-
inCodeBlock = false
252-
foundDocstringMarker = true
253-
} else if strings.HasPrefix(trimmedLine, "'''") && strings.HasSuffix(trimmedLine, "'''") { // this could be 6 letters
254-
inCodeBlock = false
255-
foundDocstringMarker = true
256-
} else if strings.HasPrefix(trimmedLine, "\"\"\"") || strings.HasPrefix(trimmedLine, "'''") { // this is more than 3 ts
257-
inCodeBlock = !inCodeBlock
258-
if inCodeBlock {
259-
foundDocstringMarker = true
260-
}
261-
} else if strings.HasSuffix(trimmedLine, "\"\"\"") || strings.HasSuffix(trimmedLine, "'''") { // this is more than 3 ts
262-
if strings.Count(trimmedLine, "\"\"\"")%2 != 0 || strings.Count(trimmedLine, "'''")%2 != 0 {
263-
inCodeBlock = !inCodeBlock
264-
}
265-
if inCodeBlock {
266-
foundDocstringMarker = true
267-
}
268-
}
269-
return inCodeBlock, foundDocstringMarker
270-
}
271-
272238
func stripTerminalCodes(msg string) string {
273239
// Regular expression to match ANSI escape sequences
274240
ansiRegex := regexp.MustCompile("\x1b\\[[0-9;]*[a-zA-Z]")

0 commit comments

Comments
 (0)