Skip to content

Commit

Permalink
Add support for document on type formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Jun 7, 2024
1 parent 32bb27e commit 6f86e70
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/demo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ const ed = editor.create(document.getElementById('editor')!, {
other: true,
comments: false,
strings: true
}
},
formatOnType: true
})

const select = document.getElementById('schema-selection') as HTMLSelectElement
Expand Down
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type JSONSchema4, type JSONSchema6, type JSONSchema7 } from 'json-schema'
import {
fromCodeActionContext,
fromFormattingOptions,
fromPosition,
fromRange,
toCodeAction,
Expand Down Expand Up @@ -304,16 +305,25 @@ export function configureMonacoYaml(monaco: MonacoEditor, options: MonacoYamlOpt
{ open: '(', close: ')' },
{ open: '"', close: '"' },
{ open: "'", close: "'" }
],

onEnterRules: [
{
beforeText: /:\s*$/,
action: { indentAction: monaco.languages.IndentAction.Indent }
}
]
}),

monaco.languages.registerOnTypeFormattingEditProvider('yaml', {
autoFormatTriggerCharacters: ['\n'],

async provideOnTypeFormattingEdits(model, position, ch, formattingOptions) {
const worker = await workerManager.getWorker(model.uri)
const edits = await worker.doDocumentOnTypeFormatting(
String(model.uri),
fromPosition(position),
ch,
fromFormattingOptions(formattingOptions)
)

return edits?.map(toTextEdit)
}
}),

monaco.languages.registerSelectionRangeProvider('yaml', {
async provideSelectionRanges(model, positions) {
const worker = await workerManager.getWorker(model.uri)
Expand Down
15 changes: 15 additions & 0 deletions src/yaml.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type DocumentLink,
type DocumentSymbol,
type FoldingRange,
type FormattingOptions,
type Hover,
type LocationLink,
type Position,
Expand Down Expand Up @@ -63,6 +64,16 @@ export interface YAMLWorker {
*/
doHover: (uri: string, position: Position) => Hover | null | undefined

/**
* Get formatting edits when the user types in a YAML document.
*/
doDocumentOnTypeFormatting: (
uri: string,
position: Position,
ch: string,
options: FormattingOptions
) => TextEdit[] | undefined

/**
* Format a YAML document using Prettier.
*/
Expand Down Expand Up @@ -152,6 +163,10 @@ initialize<YAMLWorker, MonacoYamlOptions>((ctx, { enableSchemaRequest, ...langua
ls.doDefinition(document, { position, textDocument: document })
),

doDocumentOnTypeFormatting: withDocument((document, position, ch, options) =>
ls.doDocumentOnTypeFormatting(document, { ch, options, position, textDocument: document })
),

doHover: withDocument(ls.doHover),

format: withDocument(ls.doFormat),
Expand Down

0 comments on commit 6f86e70

Please sign in to comment.