Skip to content

Commit

Permalink
Add support for code folding
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Sep 6, 2023
1 parent a3f5b3c commit f2cb635
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ files:
- Automatically load remote schema files (by enabling DiagnosticsOptions.enableSchemaRequest)
- Links from JSON references.
- Links and hover effects from YAML anchors.
- Code folding.

Schemas can also be provided by configuration. See
[here](https://github.com/remcohaszing/monaco-yaml/blob/main/index.d.ts) for the API that the plugin
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createDefinitionProvider,
createDocumentFormattingEditProvider,
createDocumentSymbolProvider,
createFoldingRangeProvider,
createHoverProvider,
createLinkProvider,
createMarkerDataProvider
Expand Down Expand Up @@ -76,6 +77,11 @@ export function configureMonacoYaml(monaco: MonacoEditor, options: MonacoYamlOpt

monaco.languages.registerCodeActionProvider('yaml', createCodeActionProvider(worker.getWorker)),

monaco.languages.registerFoldingRangeProvider(
'yaml',
createFoldingRangeProvider(worker.getWorker)
),

monaco.languages.setLanguageConfiguration('yaml', {
comments: {
lineComment: '#'
Expand Down
18 changes: 18 additions & 0 deletions src/languageFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { type languages } from 'monaco-editor/esm/vs/editor/editor.api.js'
import {
fromFoldingRangeContext,

Check failure on line 3 in src/languageFeatures.ts

View workflow job for this annotation

GitHub Actions / eslint

'fromFoldingRangeContext' is defined but never used

Check failure on line 3 in src/languageFeatures.ts

View workflow job for this annotation

GitHub Actions / tsc

'"monaco-languageserver-types"' has no exported member named 'fromFoldingRangeContext'. Did you mean 'fromFoldingRange'?
fromMarkerData,
fromPosition,
fromRange,
toCodeAction,
toCompletionList,
toDocumentSymbol,
toFoldingRange,
toHover,
toLink,
toLocationLink,
Expand Down Expand Up @@ -168,3 +170,19 @@ export function createCodeActionProvider(getWorker: WorkerAccessor): languages.C
}
}
}

export function createFoldingRangeProvider(
getWorker: WorkerAccessor
): languages.FoldingRangeProvider {
return {
async provideFoldingRanges(model) {
const resource = model.uri

const worker = await getWorker(resource)
const foldingRanges = await worker.getFoldingRanges(String(resource))
console.log(foldingRanges)

Check failure on line 183 in src/languageFeatures.ts

View workflow job for this annotation

GitHub Actions / eslint

Unexpected console statement

return foldingRanges?.map(toFoldingRange)
}
}
}
7 changes: 7 additions & 0 deletions src/yaml.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type Diagnostic,
type DocumentLink,
type DocumentSymbol,
type FoldingRange,
type Hover,
type LocationLink,
type Position,
Expand Down Expand Up @@ -45,6 +46,8 @@ export interface YAMLWorker {
findLinks: (uri: string) => DocumentLink[] | undefined

getCodeAction: (uri: string, range: Range, diagnostics: Diagnostic[]) => CodeAction[] | undefined

getFoldingRanges: (uri: string) => FoldingRange[] | null | undefined
}

const telemetry: Telemetry = {
Expand Down Expand Up @@ -116,6 +119,10 @@ initialize<YAMLWorker, MonacoYamlOptions>((ctx, { enableSchemaRequest, ...langua
textDocument: document,
context: { diagnostics }
})
),

getFoldingRanges: withDocument((document) =>
ls.getFoldingRanges(document, { lineFoldingOnly: true })
)
}
})

0 comments on commit f2cb635

Please sign in to comment.