monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

Add an XML formatter

Open ramstheroyal opened this issue 5 years ago • 8 comments

I am trying to use Manaco Editor for XML language, but XML string is not getting formatted while editor being rendered, where as its working fine for JSON.

I either don't find "Format Document" option in Context Menu of Editor(if it is opened with XML language). Doesn't Monaco editor support XML formatting by default?

ramstheroyal avatar Jul 15 '19 11:07 ramstheroyal

Running into the same issue. "Format Document" doesn't exist in the command template, and the calling of formatting by JS doesn't do anything.

ThunderSizzle avatar Jul 17 '19 19:07 ThunderSizzle

Hello, Is there a new update regarding this issue? I am trying to use the XML format and highlighting as well but unfortunately, it doesn't work for me, however, it works perfectly with JSON format.

sefikrausz avatar Nov 02 '19 12:11 sefikrausz

Running into the same issue. "Format Document" doesn't exist in the command template, and the calling of formatting by JS doesn't do anything.

same issue using react-monaco-editor with version 0.34.0

BuggMaker avatar May 19 '20 02:05 BuggMaker

same problem here.

regisnew avatar Oct 12 '20 00:10 regisnew

+1

jgibson02 avatar Apr 14 '21 18:04 jgibson02

with version 0.25.2, i'm using workaround of registering xml in html language service.

monaco.languages.html.registerHTMLLanguageService('xml', {}, { documentFormattingEdits: true })

shubhendusaurabh avatar Jun 19 '21 17:06 shubhendusaurabh

+1

fcappell avatar Jan 14 '22 11:01 fcappell

Seems like monaco would ship with an xml formatter but for those landing here looking for an alternative to registering the html worker as suggested above (which absolutely works btw)

Register a format provider for xml

monaco.languages.registerDocumentFormattingEditProvider('xml', {
    async provideDocumentFormattingEdits(model, options, token) {
        return [
            {
                range: model.getFullModelRange(),
                text: formatXml(model.getValue()),
            },
        ];
    },
});

choose your own format function/lib but here is shared by a SO posts that works fine for me.

function formatXml(xml) {
    // https://stackoverflow.com/questions/57039218/doesnt-monaco-editor-support-xml-language-by-default
    const PADDING = ' '.repeat(2);
    const reg = /(>)(<)(\/*)/g;
    let pad = 0;

    xml = xml.replace(reg, '$1\r\n$2$3');

    return xml.split('\r\n').map((node, index) => {
        let indent = 0;
        if (node.match(/.+<\/\w[^>]*>$/)) {
            indent = 0;
        } else if (node.match(/^<\/\w/) && pad > 0) {
            pad -= 1;
        } else if (node.match(/^<\w[^>]*[^/]>.*$/)) {
            indent = 1;
        } else {
            indent = 0;
        }

        pad += indent;

        return PADDING.repeat(pad - indent) + node;
    }).join('\r\n');
}

RickeyWard avatar Jul 01 '22 18:07 RickeyWard

We closed this issue because we don't plan to address it in the foreseeable future. If you disagree and feel that this issue is crucial: we are happy to listen and to reconsider.

If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

Thanks for your understanding, and happy coding!

hediet avatar Mar 13 '23 11:03 hediet