Repository navigation
feat: format embedded languages in tagged templates in the Wasm plugin - #838
Merged
Merged
Conversation
The Wasm plugin now formats the text of tagged templates (ex. html`...` and css`...`) with the plugin the dprint host uses for the language's file extension, which the Rust API could already do with an external formatter. - `taggedTemplate.formatEmbedded` (default `true`) turns it off. - `taggedTemplate.embeddedLanguages` maps a tag to the file extension of the language its text is formatted as, which replaces the defaults (`css` as `less`, `gql` and `graphql` as `graphql`, and `html`, `sql`, `svg`, and `xml` as themselves). The styled-components patterns use the `css` entry. The text is dedented and trimmed before it's formatted, and CSS is wrapped in a rule so declarations are valid. A template is left as is when the host says the text didn't change, which is also what it says when no plugin formats the extension. A template on a single line stays on a single line when its formatted text does too. BREAKING CHANGE: The external formatter of the Rust API is now called with the file extension of the language (ex. `"less"` for css`...`) instead of the tag, and `ExternalFormatter` has a lifetime parameter. Closes #9
The rustfmt command in dprint.json didn't pass the edition, so it formatted with the 2015 edition and couldn't format files that use let chains (ex. generate.rs). It now passes --edition 2024 like cargo fmt does, and this formats the code with it.
This was referenced Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #9
The Wasm plugin now formats the text of tagged templates with the plugin the dprint host uses for the language's file extension. The Rust API could already do this with an external formatter, but the Wasm plugin passed none. For example, with the markup_fmt and malva plugins:
Configuration
taggedTemplate.formatEmbedded(defaulttrue) turns it off.taggedTemplate.embeddedLanguagesmaps a tag to the file extension of the language its text is formatted as, and replaces the defaults when it's specified:cssis formatted asless, because the placeholders of its expressions are Less variables (ex.@dpr1nt_00_d), which plain CSS doesn't allow in places like values;gqlandgraphqlasgraphql;html,sql,svg, andxmlas themselves.The styled-components patterns (ex. styled.div
...and styled(Button)...) use thecssentry.Both are in the schema.
How it works
lineWidth,indentWidth,useTabs, andnewLineKind: lf, so it lines up with the code when it's put back in the template.a{ ... }) so declarations are valid, and unwrapped after.<b>text</b>in a conditional), instead of always being put on its own lines.Breaking changes to the Rust API
"less"for css...by default) instead of the tag, and only for the tags intaggedTemplate.embeddedLanguages.ExternalFormatterhas a lifetime parameter, which lets the Wasm plugin's formatter borrow the host's callback. Code that writes&ExternalFormatterstill compiles.Rust 2024 edition formatting
The second commit fixes the rustfmt command in
dprint.json, which didn't pass the edition, so it formatted with the 2015 edition and couldn't format files that use let chains (ex.generate.rs). It now passes--edition 2024likecargo fmtdoes, and reformats the code with it, sodprint checkandcargo fmt --checkagree.Testing