23
23
import com .intellij .util .containers .ContainerUtil ;
24
24
import com .redhat .devtools .lsp4ij .LSPIJEditorUtils ;
25
25
import com .redhat .devtools .lsp4ij .LanguageServiceAccessor ;
26
+ import com .redhat .devtools .lsp4ij .client .features .LSPClientFeatures ;
26
27
import com .redhat .devtools .lsp4ij .client .features .LSPFormattingFeature ;
27
28
import com .redhat .devtools .lsp4ij .client .features .LSPFormattingFeature .FormattingScope ;
29
+ import com .redhat .devtools .lsp4ij .client .features .LSPOnTypeFormattingFeature ;
28
30
import com .redhat .devtools .lsp4ij .features .codeBlockProvider .LSPCodeBlockProvider ;
29
31
import com .redhat .devtools .lsp4ij .features .completion .LSPCompletionTriggerTypedHandler ;
30
32
import com .redhat .devtools .lsp4ij .features .selectionRange .LSPSelectionRangeSupport ;
@@ -50,15 +52,18 @@ public Result charTyped(char charTyped,
50
52
@ NotNull Project project ,
51
53
@ NotNull Editor editor ,
52
54
@ 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 ))) {
62
67
rangeFormattingSupportedRef .set (rangeFormattingSupportedRef .get () || formattingFeature .isRangeFormattingSupported (file ));
63
68
64
69
onTypeFormattingSettings .formatOnCloseBrace |= formattingFeature .isFormatOnCloseBrace (file );
@@ -78,65 +83,65 @@ public Result charTyped(char charTyped,
78
83
onTypeFormattingSettings .formatOnCompletionTrigger |= formattingFeature .isFormatOnCompletionTrigger (file );
79
84
onTypeFormattingSettings .formatOnCompletionTriggerCharacters += formattingFeature .getFormatOnCompletionTriggerCharacters (file );
80
85
}
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
- }
106
86
}
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 (
116
104
project ,
117
105
editor ,
118
106
file ,
119
- onTypeFormattingSettings .formatOnStatementTerminatorScope ,
120
- charTyped
107
+ onTypeFormattingSettings .formatOnCloseBraceScope ,
108
+ openBraceChar ,
109
+ closeBraceChar
121
110
);
122
111
}
123
112
}
113
+ }
124
114
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
+ );
140
145
}
141
146
}
142
147
@@ -317,15 +322,4 @@ private static void format(@NotNull Project project,
317
322
CodeStyleManager .getInstance (project ).reformatText (file , Collections .singletonList (textRange ));
318
323
}
319
324
}
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
- }
331
325
}
0 commit comments