Skip to content

Commit

Permalink
feat: adds config file #region folding support (#1528)
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <[email protected]>
  • Loading branch information
pedrolamas authored Nov 13, 2024
1 parent db4e43b commit e0ecc8c
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/components/widgets/filesystem/setupMonaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ type ReduceState<T> = {
result: T[]
}

type StackReduceState<U, T> = {
stack: U[],
result: T[]
}

type CodeLensSupportedService = 'klipper' | 'moonraker' | 'moonraker-telegram-bot' | 'crowsnest'

const isCodeLensSupportedService = (service: string) : service is CodeLensSupportedService => [
const isCodeLensSupportedService = (service: string): service is CodeLensSupportedService => [
'klipper',
'moonraker',
'moonraker-telegram-bot',
Expand Down Expand Up @@ -49,7 +54,7 @@ const getDocsSection = (service: CodeLensSupportedService, sectionName: string)
return sectionName
}

async function setupMonaco () {
async function setupMonaco() {
await Promise.all([
loadWASM(onigasmWasm),
import('./setupMonaco.features')
Expand Down Expand Up @@ -149,17 +154,16 @@ async function setupMonaco () {
.result

return {
lenses: sectionBlocks.map((section, index) =>
({
lenses: sectionBlocks
.map((section, index) => ({
range: section.range,
id: `docs${index}`,
command: {
id: 'fluidd_open_docs',
title: app.$t('app.file_system.label.view_section_documentation', { section: section.referenceSection }).toString(),
arguments: [service, section.referenceSection]
}
})
),
})),
dispose: () => undefined
}
},
Expand Down Expand Up @@ -192,12 +196,38 @@ async function setupMonaco () {
}, { result: [] } as ReduceState<monaco.languages.FoldingRange>)
.result

const regionBlocks = linesContent
.reduce((state, lineContent, index) => {
lineContent = lineContent.trim()

if (lineContent.length > 0) {
const isRegion = /^#region\b/.test(lineContent)

if (isRegion) {
state.stack.push(index + 1)
} else {
const isEndRegion = /^#endregion\b/.test(lineContent)

if (isEndRegion && state.stack.length > 0) {
state.result.push({
kind: monaco.languages.FoldingRangeKind.Region,
start: state.stack.pop() ?? 0,
end: index + 1
})
}
}
}

return state
}, { stack: [], result: [] } as StackReduceState<number, monaco.languages.FoldingRange>)
.result

const commentBlocks = linesContent
.reduce((state, lineContent, index) => {
lineContent = lineContent.trim()

if (lineContent.length > 0) {
const isComment = ['#', ';'].includes(lineContent[0])
const isComment = /^;|#(?!(region|endregion)\b)/.test(lineContent)

if (isComment) {
if (state.current) {
Expand All @@ -220,6 +250,7 @@ async function setupMonaco () {

return [
...sectionBlocks,
...regionBlocks,
...commentBlocks
]
}
Expand Down

0 comments on commit e0ecc8c

Please sign in to comment.