Code formatting in code mirror

Unable to find any function in codemirror documentation to format code? Any approach how to do this?

The library doesn’t do this.

Any other approach to acheive this functionality?

I use Prettier to do this on CodeMirror content. While mainly Node JS based, there’s a browser component in JS that will format quite a few languages:

Anything for C/C++/Java/Python? Prettier will work this Javascript only.

Please have you solved this problem?

How to format C/C++/Java/Python in nodejs environment

Hope to answer, thanks

You will definitely not get an answer by adding a new reply to this thread every day. As I said, the library doesn’t do formatting. You’ll have to build your own solution or give up on this.

Thanks for your reply, I have found the solution.

I tried several options:

  1. Implemented through LSP, just call the textDocument/formatting event
          textDocument: { uri: `file://${rootUri}/${openedPath}` },
          options: {
            tabSize: view.state.tabSize,
            insertSpaces: true,
        .then((result) => {
          if (result && result.result?.length) {
            const items = result.result;
            const transaction: Array<TransactionSpec> = [];
            items.forEach((item) => {
              const from = posToOffset(state.doc, item.range.start);
              const to = posToOffset(state.doc, item.range.end);
              if (from !== undefined && to !== undefined) {
                const trans = insertCompletionText(
  1. In the node middle tier, prettier and some additional packages (eg sql-format…) are called for code formatting. For c language reference C#在线格式化 (Executing autopep8 in node to achieve python code formatting is also an idea)
  2. Use prettier’s browser, unfortunately this doesn’t support all features very well

Hope it helps others

1 Like