1515import java .util .concurrent .ExecutionException ;
1616import java .util .concurrent .TimeUnit ;
1717import java .util .concurrent .TimeoutException ;
18+ import java .util .regex .Pattern ;
1819
1920import org .eclipse .core .commands .operations .IOperationHistory ;
2021import org .eclipse .core .commands .operations .IOperationHistoryListener ;
@@ -157,23 +158,26 @@ void doFormatOnType(@Nullable ITextViewer viewer, @Nullable IDocument document,
157158 if (document == null || event == null || event .fText .isEmpty () || viewer == null ) {
158159 return ;
159160 }
160- final String [] triggerChar = new String [] { "" }; //$NON-NLS-1$
161+ final String [] triggerCharArr = new String [] { "" }; //$NON-NLS-1$
161162 var executor = LanguageServers .forDocument (document )
162163 .withCapability (capabilities -> {
163164 var provider = capabilities .getDocumentOnTypeFormattingProvider ();
164165 if (provider != null ) {
165- if (event .fText .contains (provider .getFirstTriggerCharacter ())) {
166- triggerChar [0 ] = provider .getFirstTriggerCharacter ();
166+ String firstTriggerChar = provider .getFirstTriggerCharacter ();
167+ var spaceRemovalRegex = "[\\ s&&[^" + Pattern .quote (firstTriggerChar ) + "]]+" ; //$NON-NLS-1$ //$NON-NLS-2$
168+ var filteredText = event .fText .replaceAll (spaceRemovalRegex , "" ); //$NON-NLS-1$
169+ if (filteredText .equals (firstTriggerChar )) {
170+ triggerCharArr [0 ] = firstTriggerChar ;
167171 } else if (provider .getMoreTriggerCharacter () != null ) {
168172 for (String c : provider .getMoreTriggerCharacter ()) {
169173 if (event .fText .equals (c )) {
170- triggerChar [0 ] = c ;
174+ triggerCharArr [0 ] = c ;
171175 break ;
172176 }
173177 }
174178 }
175179 }
176- return Either .forLeft (provider != null && !triggerChar [0 ].isEmpty ());
180+ return Either .forLeft (provider != null && !triggerCharArr [0 ].isEmpty ());
177181 });
178182 if (!executor .anyMatching ()) {
179183 return ;
@@ -186,7 +190,7 @@ void doFormatOnType(@Nullable ITextViewer viewer, @Nullable IDocument document,
186190 try {
187191 long modificationStamp = DocumentUtil .getDocumentModificationStamp (document );
188192 int position = event .fOffset + event .fText .length ();
189- DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams (textDocumentIdentifier , formattingOptions , LSPEclipseUtils .toPosition (position , document ), triggerChar [0 ]);
193+ DocumentOnTypeFormattingParams params = new DocumentOnTypeFormattingParams (textDocumentIdentifier , formattingOptions , LSPEclipseUtils .toPosition (position , document ), triggerCharArr [0 ]);
190194 var edits = executor .computeFirst (ls -> ls .getTextDocumentService ().onTypeFormatting (params )).get (1 , TimeUnit .SECONDS );
191195 if (!edits .isEmpty ()) {
192196 int caretOffset = new VersionedEdits (modificationStamp , edits .get (), document ).apply (position );
@@ -198,4 +202,4 @@ void doFormatOnType(@Nullable ITextViewer viewer, @Nullable IDocument document,
198202 }
199203 }
200204
201- }
205+ }
0 commit comments