Skip to content

Commit 6f41f6d

Browse files
committed
fix: use isExistingFormatterOverrideable to know which language server
must be used to format. Signed-off-by: azerr <azerr@redhat.com>
1 parent b1f0d4d commit 6f41f6d

5 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPFormattingFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public boolean isSupported(@NotNull PsiFile file) {
7171
* @return true to use the language server for code formatting and false to use plugin-provided/built-in formatters.
7272
* @apiNote This method will only be called with files that contain a language supported by this language server.
7373
*/
74-
protected boolean isExistingFormatterOverrideable(@NotNull PsiFile file) {
74+
public boolean isExistingFormatterOverrideable(@NotNull PsiFile file) {
7575
return false;
7676
}
7777

src/main/java/com/redhat/devtools/lsp4ij/features/formatting/LSPFormattingSupport.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,20 @@ protected CompletableFuture<List<? extends TextEdit>> doLoad(LSPFormattingParams
141141
boolean isRangeFormatting = textRange != null;
142142
Ref<LanguageServerItem> server = Ref.create(null);
143143
LanguageServiceAccessor.getInstance(file.getProject()).processLanguageServers(file, ls -> {
144-
if (!isRangeFormatting) {
145-
if (server.isNull() && ls.getClientFeatures().getFormattingFeature().isFormattingSupported(file)) {
146-
server.set(new LanguageServerItem(ls.getLanguageServer(), ls));
147-
}
148-
} else {
149-
if (ls.getClientFeatures().getFormattingFeature().isFormattingSupported(file) ||
150-
ls.getClientFeatures().getFormattingFeature().isRangeFormattingSupported(file)) {
151-
if (server.isNull() || !server.get().isDocumentRangeFormattingSupported()) {
144+
var formattingFeature = ls.getClientFeatures().getFormattingFeature();
145+
if (formattingFeature.isEnabled(file)) {
146+
boolean overridable = formattingFeature.isExistingFormatterOverrideable(file);
147+
if (!isRangeFormatting) {
148+
if ((overridable || server.isNull()) && formattingFeature.isFormattingSupported(file)) {
152149
server.set(new LanguageServerItem(ls.getLanguageServer(), ls));
153150
}
151+
} else {
152+
if (formattingFeature.isFormattingSupported(file) ||
153+
formattingFeature.isRangeFormattingSupported(file)) {
154+
if (overridable || server.isNull() || !server.get().isDocumentRangeFormattingSupported()) {
155+
server.set(new LanguageServerItem(ls.getLanguageServer(), ls));
156+
}
157+
}
154158
}
155159
}
156160
});

src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/ClientConfigurationSettings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ public static class OnTypeFormattingSettings {
171171
*/
172172
public static class ClientConfigurationFormatSettings {
173173

174+
public @Nullable Boolean enabled;
175+
174176
public @Nullable Integer tabSize;
175177

176178
public @Nullable Boolean insertSpaces;

src/main/java/com/redhat/devtools/lsp4ij/server/definition/launching/UserDefinedFormattingFeature.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ private ClientConfigurationSettings.ClientConfigurationFormatSettings getClientC
3535
return clientConfiguration != null ? clientConfiguration.format : null;
3636
}
3737

38+
@Override
39+
public boolean isEnabled(@NotNull PsiFile file) {
40+
var format = getClientConfigurationFormatSettings();
41+
return format != null && format.enabled != null ? format.enabled : super.isEnabled(file);
42+
}
43+
3844
@Override
3945
public @Nullable Integer getTabSize(@NotNull PsiFile file, @Nullable Editor editor) {
4046
var format = getClientConfigurationFormatSettings();
@@ -48,7 +54,7 @@ private ClientConfigurationSettings.ClientConfigurationFormatSettings getClientC
4854
}
4955

5056
@Override
51-
protected boolean isExistingFormatterOverrideable(@NotNull PsiFile file) {
57+
public boolean isExistingFormatterOverrideable(@NotNull PsiFile file) {
5258
var format = getClientConfigurationFormatSettings();
5359
return format != null && format.existingFormatterOverrideable != null ? format.existingFormatterOverrideable : super.isExistingFormatterOverrideable(file);
5460
}

src/main/resources/jsonSchema/clientSettings.schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@
122122
"title": "Client-side formatter configuration",
123123
"additionalProperties": false,
124124
"properties": {
125+
"enabled": {
126+
"type": "boolean",
127+
"title": "Server-side formatting enabled",
128+
"description": "Server-side formatting enabled"
129+
},
125130
"tabSize": {
126131
"type": "integer",
127132
"title": "Size of a tab in spaces.",
@@ -135,7 +140,7 @@
135140
"existingFormatterOverrideable": {
136141
"type": "boolean",
137142
"title": "Returns true to use the language server for code formatting and false to use plugin-provided/built-in formatters.",
138-
"description": "Prefer spaces over tabs."
143+
"description": "Returns true to use the language server for code formatting and false to use plugin-provided/built-in formatters."
139144
},
140145
"onTypeFormatting": {
141146
"type": "object",

0 commit comments

Comments
 (0)