Skip to content

Commit fc97cdf

Browse files
committed
Consolidated the language server capability checks into the processLanguageServers() call.
1 parent aabee1e commit fc97cdf

File tree

1 file changed

+65
-71
lines changed

1 file changed

+65
-71
lines changed

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

+65-71
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.intellij.util.containers.ContainerUtil;
2424
import com.redhat.devtools.lsp4ij.LSPIJEditorUtils;
2525
import com.redhat.devtools.lsp4ij.LanguageServiceAccessor;
26+
import com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures;
2627
import com.redhat.devtools.lsp4ij.client.features.LSPFormattingFeature;
2728
import com.redhat.devtools.lsp4ij.client.features.LSPFormattingFeature.FormattingScope;
29+
import com.redhat.devtools.lsp4ij.client.features.LSPOnTypeFormattingFeature;
2830
import com.redhat.devtools.lsp4ij.features.codeBlockProvider.LSPCodeBlockProvider;
2931
import com.redhat.devtools.lsp4ij.features.completion.LSPCompletionTriggerTypedHandler;
3032
import com.redhat.devtools.lsp4ij.features.selectionRange.LSPSelectionRangeSupport;
@@ -50,15 +52,18 @@ public Result charTyped(char charTyped,
5052
@NotNull Project project,
5153
@NotNull Editor editor,
5254
@NotNull PsiFile file) {
53-
// Only if there's a server for the file that supports formatting and doesn't support server-side on-type formatting
54-
if (hasLanguageServerSupportingOnlyFormatting(file)) {
55-
// Gather all of the relevant client configuration
56-
Ref<Boolean> rangeFormattingSupportedRef = Ref.create(false);
57-
ClientSideOnTypeFormattingSettings onTypeFormattingSettings = new ClientSideOnTypeFormattingSettings();
58-
LanguageServiceAccessor.getInstance(project).processLanguageServers(
59-
file,
60-
ls -> {
61-
LSPFormattingFeature formattingFeature = ls.getClientFeatures().getFormattingFeature();
55+
// Gather all of the relevant client configuration
56+
Ref<Boolean> rangeFormattingSupportedRef = Ref.create(false);
57+
ClientSideOnTypeFormattingSettings onTypeFormattingSettings = new ClientSideOnTypeFormattingSettings();
58+
LanguageServiceAccessor.getInstance(project).processLanguageServers(
59+
file,
60+
ls -> {
61+
// Only include servers that support formatting and don't support server-side on-type formatting
62+
LSPClientFeatures clientFeatures = ls.getClientFeatures();
63+
LSPFormattingFeature formattingFeature = clientFeatures.getFormattingFeature();
64+
LSPOnTypeFormattingFeature onTypeFormattingFeature = clientFeatures.getOnTypeFormattingFeature();
65+
if (formattingFeature.isEnabled(file) && formattingFeature.isSupported(file) &&
66+
(!onTypeFormattingFeature.isEnabled(file) || !onTypeFormattingFeature.isSupported(file))) {
6267
rangeFormattingSupportedRef.set(rangeFormattingSupportedRef.get() || formattingFeature.isRangeFormattingSupported(file));
6368

6469
onTypeFormattingSettings.formatOnCloseBrace |= formattingFeature.isFormatOnCloseBrace(file);
@@ -78,65 +83,65 @@ public Result charTyped(char charTyped,
7883
onTypeFormattingSettings.formatOnCompletionTrigger |= formattingFeature.isFormatOnCompletionTrigger(file);
7984
onTypeFormattingSettings.formatOnCompletionTriggerCharacters += formattingFeature.getFormatOnCompletionTriggerCharacters(file);
8085
}
81-
);
82-
boolean rangeFormattingSupported = rangeFormattingSupportedRef.get();
83-
84-
// Close braces
85-
if (onTypeFormattingSettings.formatOnCloseBrace &&
86-
// Make sure the formatter supports formatting of the configured scope
87-
((onTypeFormattingSettings.formatOnCloseBraceScope == FormattingScope.FILE) || rangeFormattingSupported)) {
88-
Map.Entry<Character, Character> bracePair = ContainerUtil.find(
89-
LSPIJEditorUtils.getBracePairs(file).entrySet(),
90-
entry -> entry.getValue() == charTyped
91-
);
92-
if (bracePair != null) {
93-
Character openBraceChar = bracePair.getKey();
94-
Character closeBraceChar = bracePair.getValue();
95-
if (StringUtil.isEmpty(onTypeFormattingSettings.formatOnCloseBraceCharacters) ||
96-
(onTypeFormattingSettings.formatOnCloseBraceCharacters.indexOf(closeBraceChar) > -1)) {
97-
return handleCloseBraceTyped(
98-
project,
99-
editor,
100-
file,
101-
onTypeFormattingSettings.formatOnCloseBraceScope,
102-
openBraceChar,
103-
closeBraceChar
104-
);
105-
}
10686
}
107-
}
108-
109-
// Statement terminators
110-
if (onTypeFormattingSettings.formatOnStatementTerminator &&
111-
// Make sure the formatter supports formatting of the configured scope
112-
((onTypeFormattingSettings.formatOnStatementTerminatorScope == FormattingScope.FILE) || rangeFormattingSupported)) {
113-
if (StringUtil.isNotEmpty(onTypeFormattingSettings.formatOnStatementTerminatorCharacters) &&
114-
(onTypeFormattingSettings.formatOnStatementTerminatorCharacters.indexOf(charTyped) > -1)) {
115-
return handleStatementTerminatorTyped(
87+
);
88+
boolean rangeFormattingSupported = rangeFormattingSupportedRef.get();
89+
90+
// Close braces
91+
if (onTypeFormattingSettings.formatOnCloseBrace &&
92+
// Make sure the formatter supports formatting of the configured scope
93+
((onTypeFormattingSettings.formatOnCloseBraceScope == FormattingScope.FILE) || rangeFormattingSupported)) {
94+
Map.Entry<Character, Character> bracePair = ContainerUtil.find(
95+
LSPIJEditorUtils.getBracePairs(file).entrySet(),
96+
entry -> entry.getValue() == charTyped
97+
);
98+
if (bracePair != null) {
99+
Character openBraceChar = bracePair.getKey();
100+
Character closeBraceChar = bracePair.getValue();
101+
if (StringUtil.isEmpty(onTypeFormattingSettings.formatOnCloseBraceCharacters) ||
102+
(onTypeFormattingSettings.formatOnCloseBraceCharacters.indexOf(closeBraceChar) > -1)) {
103+
return handleCloseBraceTyped(
116104
project,
117105
editor,
118106
file,
119-
onTypeFormattingSettings.formatOnStatementTerminatorScope,
120-
charTyped
107+
onTypeFormattingSettings.formatOnCloseBraceScope,
108+
openBraceChar,
109+
closeBraceChar
121110
);
122111
}
123112
}
113+
}
124114

125-
// Completion triggers
126-
if (onTypeFormattingSettings.formatOnCompletionTrigger &&
127-
// Make sure the formatter supports range formatting
128-
rangeFormattingSupported &&
129-
// It must be a completion trigger character for the language no matter what
130-
LSPCompletionTriggerTypedHandler.hasLanguageServerSupportingCompletionTriggerCharacters(charTyped, project, file)) {
131-
// But the subset that should trigger completion can be configured
132-
if (StringUtil.isEmpty(onTypeFormattingSettings.formatOnCompletionTriggerCharacters) ||
133-
(onTypeFormattingSettings.formatOnCompletionTriggerCharacters.indexOf(charTyped) > -1)) {
134-
return handleCompletionTriggerTyped(
135-
project,
136-
editor,
137-
file
138-
);
139-
}
115+
// Statement terminators
116+
if (onTypeFormattingSettings.formatOnStatementTerminator &&
117+
// Make sure the formatter supports formatting of the configured scope
118+
((onTypeFormattingSettings.formatOnStatementTerminatorScope == FormattingScope.FILE) || rangeFormattingSupported)) {
119+
if (StringUtil.isNotEmpty(onTypeFormattingSettings.formatOnStatementTerminatorCharacters) &&
120+
(onTypeFormattingSettings.formatOnStatementTerminatorCharacters.indexOf(charTyped) > -1)) {
121+
return handleStatementTerminatorTyped(
122+
project,
123+
editor,
124+
file,
125+
onTypeFormattingSettings.formatOnStatementTerminatorScope,
126+
charTyped
127+
);
128+
}
129+
}
130+
131+
// Completion triggers
132+
if (onTypeFormattingSettings.formatOnCompletionTrigger &&
133+
// Make sure the formatter supports range formatting
134+
rangeFormattingSupported &&
135+
// It must be a completion trigger character for the language no matter what
136+
LSPCompletionTriggerTypedHandler.hasLanguageServerSupportingCompletionTriggerCharacters(charTyped, project, file)) {
137+
// But the subset that should trigger completion can be configured
138+
if (StringUtil.isEmpty(onTypeFormattingSettings.formatOnCompletionTriggerCharacters) ||
139+
(onTypeFormattingSettings.formatOnCompletionTriggerCharacters.indexOf(charTyped) > -1)) {
140+
return handleCompletionTriggerTyped(
141+
project,
142+
editor,
143+
file
144+
);
140145
}
141146
}
142147

@@ -317,15 +322,4 @@ private static void format(@NotNull Project project,
317322
CodeStyleManager.getInstance(project).reformatText(file, Collections.singletonList(textRange));
318323
}
319324
}
320-
321-
private static boolean hasLanguageServerSupportingOnlyFormatting(@NotNull PsiFile file) {
322-
return LanguageServiceAccessor.getInstance(file.getProject())
323-
.hasAny(file, ls -> {
324-
var clientFeatures = ls.getClientFeatures();
325-
return clientFeatures.getFormattingFeature().isEnabled(file) &&
326-
clientFeatures.getFormattingFeature().isSupported(file) &&
327-
(!clientFeatures.getOnTypeFormattingFeature().isEnabled(file) ||
328-
!clientFeatures.getOnTypeFormattingFeature().isSupported(file));
329-
});
330-
}
331325
}

0 commit comments

Comments
 (0)