Skip to content

Update the formatter to support tabs in addition to spaces #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
adamdehaven opened this issue Jan 10, 2025 · 0 comments
Open

Update the formatter to support tabs in addition to spaces #10

adamdehaven opened this issue Jan 10, 2025 · 0 comments

Comments

@adamdehaven
Copy link
Contributor

adamdehaven commented Jan 10, 2025

In #9 we added the MDC formatter, but it requires the downstream Monaco Editor to utilize insertSpaces: true and detectIndentation: false.

We should update the formatter to include an additional FormatterOptions property of insertSpaces: boolean, and refactor the implementation to allow inserting space or tab character(s) \t for indentation based on the value of the insertSpaces option.

The result would then look like this when registering the document format providers:

// formatter implementation
export const formatter = (content: string, { 
  tabSize = 2, 
  isFormatOnType = false,
+ insertSpaces = true,
}: FormatterOptions): string => {
  // ... formatter code
})
// Register formatter
monaco.languages.registerDocumentFormattingEditProvider('mdc', {
  provideDocumentFormattingEdits: model => [{
    range: model.getFullModelRange(),
    text: mdcFormatter(model.getValue(), {
      tabSize: TAB_SIZE,
+     insertSpaces: false,
    }),
  }],
})

// Register format on type provider
monaco.languages.registerOnTypeFormattingEditProvider('mdc', {
  // Auto-format when the user types a newline character.
  autoFormatTriggerCharacters: ['\n'],
  provideOnTypeFormattingEdits: model => [{
    range: model.getFullModelRange(),
    // We pass `true` to `isFormatOnType` to indicate formatOnType is being called.
    text: mdcFormatter(model.getValue(), {
      tabSize: TAB_SIZE,
      isFormatOnType: true,
+     insertSpaces: false,
    }),
  }],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant