Skip to content

Commit 9807dc6

Browse files
committed
fix: fix custom folding markers to work with strings
1 parent 86f819a commit 9807dc6

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/hacks.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11

2-
import * as monaco from 'monaco-editor'
3-
42
/* Add custom blocks parsing (for tech.io):
53
// { [autofold]
64
// custom block content
75
// }
86
*/
97
const CUSTOM_BLOCK_BEGIN_REGEX = '^\\s*(?:\\/\\/|#).*(?:\\{|#region)'
108
const CUSTOM_BLOCK_END_REGEX = '^\\s*(?:\\/\\/|#).*(?:\\}|#endregion)'
11-
export function addCustomFoldingMarkers (configuration: monaco.extra.ILanguageConfiguration): monaco.extra.ILanguageConfiguration {
9+
10+
interface IRegExp {
11+
pattern: string
12+
flags?: string
13+
}
14+
export interface ILanguageConfiguration {
15+
folding?: {
16+
offSide?: boolean
17+
markers?: {
18+
start?: string | IRegExp
19+
end?: string | IRegExp
20+
}
21+
}
22+
}
23+
24+
function regexOr (regex: string, add: string): string
25+
function regexOr (regex: undefined | string | IRegExp, add: string): string | IRegExp
26+
function regexOr (regex: undefined | string | IRegExp, add: string): string | IRegExp {
27+
if (regex == null) {
28+
return add
29+
} else if (typeof regex === 'string') {
30+
return `(?:${regex})|(?:${add})`
31+
} else {
32+
return {
33+
flags: regex.flags,
34+
pattern: regexOr(regex.pattern, add)
35+
}
36+
}
37+
}
38+
39+
export function addCustomFoldingMarkers<T extends ILanguageConfiguration> (configuration: T): T {
1240
const markers = configuration.folding?.markers
1341
return {
1442
...configuration,
1543
folding: {
1644
...configuration.folding ?? {},
1745
markers: {
18-
start: markers != null ? new RegExp(markers.start.source + '|' + CUSTOM_BLOCK_BEGIN_REGEX) : new RegExp(CUSTOM_BLOCK_BEGIN_REGEX),
19-
end: markers != null ? new RegExp(markers.end.source + '|' + CUSTOM_BLOCK_END_REGEX) : new RegExp(CUSTOM_BLOCK_END_REGEX)
46+
start: regexOr(markers?.start, CUSTOM_BLOCK_BEGIN_REGEX),
47+
end: regexOr(markers?.end, CUSTOM_BLOCK_END_REGEX)
2048
}
2149
}
2250
}

0 commit comments

Comments
 (0)