Skip to content
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

feat(formatter): add monaco formatter for mdc #9

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

adamdehaven
Copy link

Adds a monaco-editor formatting provider, along with updated usage instructions and unit tests.

Adds a monaco-editor formatting provider, along with updated usage instructions and unit tests.
@adamdehaven
Copy link
Author

adamdehaven commented Jan 3, 2025

@farnabaz I have this working in my downstream project and have added it to the playground in this PR.

Let me know if I can make any updates or scenarios I didn't account for

@farnabaz
Copy link
Collaborator

farnabaz commented Jan 8, 2025

Thanks @adamdehaven, This is great.
I tested it in the playground, but formatted does not run automatically while typing. Am I missing something?

I tried with putting editor.getAction('editor.action.formatDocument').run() in the editor, but in this case formatted will run once on load.

@adamdehaven
Copy link
Author

I tested it in the playground, but formatted does not run automatically while typing. Am I missing something?

Ah yep @farnabaz I forgot to register the formatOnType provider 😅

I have updated the formatter, playground example, and the docs to configure formatOnType.

When typing in the playground, it will auto-format whenever you enter a newline \n character. Since this impacts the editor's default indentation (e.g. keeping you at the same level as you type) I added a parameter isFormatOnType: boolean to the formatter function so that the result respects this indentation rule when typing and formats as expected when formatting the entire document.

@adamdehaven adamdehaven marked this pull request as draft January 9, 2025 03:03
@adamdehaven
Copy link
Author

adamdehaven commented Jan 9, 2025

I'm doing a little more refactoring to improve the formatting in some use cases, e.g. multiline strings, etc. so I changed the PR back to draft until I push a few optimizations.

Should be ready for review - give it a try in the Playground and let me know

@adamdehaven adamdehaven force-pushed the feat/mdc-monaco-formatter branch from 47e7d11 to 6c191d9 Compare January 9, 2025 05:14
@farnabaz
Copy link
Collaborator

farnabaz commented Jan 9, 2025

Perfect 👍

One thing about the README.
Let's separate the use of highlighter and formatter in README to imply that they can be used separately.

@adamdehaven
Copy link
Author

adamdehaven commented Jan 9, 2025

One thing about the README. Let's separate the use of highlighter and formatter in README to imply that they can be used separately.

Done.

Also added this note:

Note

Since the format provider utilizes spaces for indention, we will also configure the editor to insert spaces for tabs.

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

Successfully merging this pull request may close these issues.

2 participants