Repository navigation
fix: don't format a file which is excluded from formatting - #1817
Merged
angelozerr merged 1 commit intoNov 2, 2026
Merged
Conversation
"Reformat code" before a commit let a language server format a file which matches a "Do not format" pattern of Settings > Editor > Code Style > Formatter. IntelliJ checks these patterns with a LanguageFormattingRestriction, ExcludedFileFormattingRestriction, and asks the restrictions only in LanguageFormatting.forContext(), for its own formatter: the commit's ReformatCodeProcessor skips an excluded file only when the file's formatting service is the CoreFormattingService. Reformat Code in the editor checks the patterns itself, which is why it did not format the file. AbstractLSPFormattingService.canFormat() now returns false for a file which a LanguageFormattingRestriction does not allow to format, so IntelliJ's own formatter takes the file and leaves it as it is. ExcludedFileFormattingTest reformats a file with ReformatCodeProcessor: an excluded file is not formatted and the language server gets no formatting request; a file which is not excluded is formatted. Without the fix, the first test fails. Fixes redhat-developer#1647
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1817 +/- ##
=========================================
Coverage 37.62% 37.63%
=========================================
Files 786 786
Lines 30019 30023 +4
Branches 4546 6002 +1456
=========================================
+ Hits 11296 11300 +4
Misses 16772 16772
Partials 1951 1951 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Collaborator
Author
|
@coderabbitai review |
Action performed
|
Contributor
|
Thanks! |
angelozerr
pushed a commit
that referenced
this pull request
Nov 4, 2026
* docs: describe the 0.30.0 LSP and DAP changes that the docs miss Several changes merged for 0.30.0 changed behavior that the docs describe, or added behavior that they don't. LSPSupport.md still said that pull diagnostics don't support related documents, which #1723 changed. LSP: - LSPSupport.md, Pull Diagnostics: previousResultId, and the relatedDocuments of either kind of report (#1723, #1735), in place of "It doesn't support the related documents." - LSPSupport.md, Inline Completion: the trigger kind and the selected completion item (#1715). - LSPSupport.md, Signature Help: the retrigger, the active parameter of each signature, and signatures without parameters (#1712, #1734). - LSPSupport.md, Formatting: a file without an open editor (#1707). - LSPSupport.md, new Did Change Watched Files section: the directories outside the project that watchers name, and the watchers of a stopped server (#1742). The support list links to it. - UserGuide.md, Formatting: the Do not format patterns (#1817). - UserDefinedLanguageServer.md, Configuration tab: null properties (#1710, #1847) and invalid JSON (#1828). - DeveloperGuide.md, Workspace Configuration: null members of createSettings() (#1710, #1847). DAP, dap/UserGuide.md: - Exception breakpoint: the hint built from exceptionInfo (#1761). The screenshot of the stop at the exception moves back after the example it shows; the Smart Step Into section had come between them. - Large variables: a page with fewer children than asked for is the last one (#1851). Validation: each statement was checked against the code at d1a281d: DocumentContentSynchronizer.refreshPullDiagnostic, LSPInlineCompletionProvider.createParams, LSPParameterInfoHandler, ClientCapabilitiesFactory, AbstractLSPFormattingService.canFormat, LSPFormattingSupport, FileSystemWatchRoots, LanguageServerPanel .validateJsonFields, JsonSettingsTypeAdapterFactory, DAPClient.getExceptionHint and DAPVariableChildren. The other changes merged for 0.30.0 are already documented, by their own PRs, #1784 and #1810, or change nothing that the docs describe. Limits: docs only, no new screenshots, no build run. * docs: say that the JSON fields of a language server are read leniently CodeRabbit noted that "must be valid JSON" is stricter than what LSP4IJ accepts. The dialog's check (LanguageServerPanel.jsonError, JsonParser.parseString) and the readers at run time (SettingsHelper.parseJson, JsonParser.parseReader for the initialization options, experimental and installer, Gson.fromJson for the client configuration and workspace folders) all read leniently. The paragraph now says which JSON is accepted and which isn't. Validation: probed Gson 2.15.0's JsonParser.parseString and Gson.fromJson. Both accept // and /* */ and # comments, unquoted names and single-quoted strings, and both reject a trailing comma, a missing brace and an unescaped backslash, such as in C:\Users.
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.
When Reformat code runs before a commit, LSP4IJ lets a language server format a file which matches a Do not format pattern of Settings > Editor > Code Style > Formatter. In #1647 this is the
application.propertiesof a Quarkus project, which the Quarkus Tools language server formats although*.propertiesis excluded.IntelliJ checks these patterns with a
LanguageFormattingRestriction,ExcludedFileFormattingRestriction, and asks the restrictions only inLanguageFormatting.forContext(), for its own formatter. The commit'sReformatCodeProcessorskips an excluded file only when the file's formatting service is theCoreFormattingService(AbstractLayoutCodeProcessor.canBeFormatted()).AbstractLSPFormattingService.canFormat()doesn't ask the restrictions, so for a file of a language server, the LSP formatting service is chosen and formats it. Reformat Code on a directory in the Project view goes through the same check. Reformat Code in the editor checks the patterns itself, withCodeStyle.isFormattingEnabled()inFileInEditorProcessor, which is why it didn't format the file when tried in the issue.Fixes #1647.
Changes
AbstractLSPFormattingService.canFormat()returns false for a file which aLanguageFormattingRestrictiondoesn't allow to format (isFormatterAllowed), asLanguageFormatting.forContext()does for IntelliJ's own formatter. IntelliJ's formatter then takes the file and leaves it as it is. The check runs after the check that the file has a language server, so other files don't pay for it.isFormatterAllowed, notisAutoFormatAllowed: IntelliJ asksLanguageFormatting.isAutoFormatAllowed()itself before it formats on save.textDocument/onTypeFormatting) doesn't go through the formatting service and is not changed.Tests
New
ExcludedFileFormattingTestreformatstest.tswithReformatCodeProcessor, the processor that Reformat code before a commit runs, here for one file withrunWithoutProgress(). Each test sets a "Do not format" glob pattern in temporary code style settings:testExcludedFileIsNotFormatted: with the pattern*.ts, the file is left as it is, and the language server gets notextDocument/formatting.testFileWhichIsNotExcludedIsFormatted: with the pattern*.properties, the file is formatted, with onetextDocument/formatting.Without the
canFormat()change, the first fails (the file is formatted toconst a = 1;) and the second passes. With it, both pass. The formatting tests (com.redhat.devtools.lsp4ij.features.formatting.*andLanguageServerFileEnablementTest, 80 tests) pass.Locally, the full suite passes (1,005 tests, 3 skipped) except the 7 known macOS failures ("File accessed outside allowed roots"), which fail on
maintoo. Not checked in a running IDE.