Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Simplify access to client config information (fixes #912) #925

Merged
merged 9 commits into from
Mar 24, 2025

Conversation

SCWells72
Copy link
Contributor

This implements the optimization suggested in #912 and updates client-side on-type formatting to use it.

…client config aspects in a single call/iteration of the language servers.
* Reference to a boolean value that simplifies mutation of that value in lambdas/inner classes.
*/
@ApiStatus.Internal
public class BooleanRef extends MutableRef<Boolean> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several mutable reference types to provide indirect access to a mutable value of the respective simple types in lambdas. For boolean values we want to be able to perform logical ANDs and ORs; for string values we want to be able to append; for enumerated constant values we want to be able to retain the highest/lowest-ordered value.

* @param file the file
* @param processor the processor
*/
public void processLanguageServers(@NotNull PsiFile file,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows the caller to apply some logic to all started language servers.

*
* @param <T> the value type
*/
class MutableRef<T> extends Ref<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base class for the above mutable ref types.

* @return the value
*/
@Nullable
public T getValue() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this so that subclasses can change from @Nullable to @NotNull as appropriate. Unfortunately get() is final.

closeBraceChar
);
// Gather all of the relevant client configuration
BooleanRef rangeFormattingSupportedRef = BooleanRef.create();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create mutable refs for all the info we need.

// Statement terminators
if (formattingFeature.isFormatOnStatementTerminator(file) &&
);
boolean rangeFormattingSupported = rangeFormattingSupportedRef.getValue();
Copy link
Contributor Author

@SCWells72 SCWells72 Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dereference all of the mutable references into variables with simplified names.

String formatOnCompletionTriggerCharacters = formatOnCompletionTriggerCharactersRef.getValue();

// Close braces
if (formatOnCloseBrace &&
Copy link
Contributor Author

@SCWells72 SCWells72 Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the rest of the updates are just to use those collected settings. For the best diff, hide whitespace-only differences.

project,
editor,
file,
formattingFeature,
charTyped
formatOnCloseBraceScope,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were only passing formattingFeature to get to formatOnCloseBraceScope, so just pass that scope.

project,
editor,
file,
formatOnStatementTerminatorScope,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were only passing formattingFeature to get to formatOnStatementTerminatorScope, so just pass that scope.

}
}

return super.charTyped(charTyped, project, editor, file);
}

@Nullable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we can remove this.

@@ -330,14 +330,4 @@ private static void format(@NotNull Project project,
}
}

private static boolean hasLanguageServerSupportingOnlyFormatting(@NotNull PsiFile file) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And we can remove this.

@SCWells72 SCWells72 marked this pull request as ready for review March 24, 2025 20:47
// Statement terminators
if (formattingFeature.isFormatOnStatementTerminator(file) &&
// Close braces
if (onTypeFormattingSettings.formatOnCloseBrace &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And everything below here is just updating to use that collected info. I recommend hiding whitespace-only changes for the best diff.

project,
editor,
file,
formattingFeature,
charTyped
onTypeFormattingSettings.formatOnCloseBraceScope,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were previously passing formattingFeature here just to get the formatting scope, so now we just pass the formatting scope directly.

project,
editor,
file,
onTypeFormattingSettings.formatOnStatementTerminatorScope,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the above change to pass the scope instead of the formatting feature.

@@ -103,7 +103,7 @@ public static class ClientSideOnTypeFormattingSettings {
* The specific close brace characters that should trigger client-side on-type formatting. Defaults to the
* language's close brace characters.
*/
public String formatOnCloseBraceCharacters = null;
public String formatOnCloseBraceCharacters = "";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed these to default to the empty string instead of null. Downstream consumers are just checking for an empty or null string, so it's a no-op other than that we don't have to check for null when collecting these from language server definitions before appending.

LSPClientFeatures clientFeatures = ls.getClientFeatures();
LSPFormattingFeature formattingFeature = clientFeatures.getFormattingFeature();
LSPOnTypeFormattingFeature onTypeFormattingFeature = clientFeatures.getOnTypeFormattingFeature();
if (formattingFeature.isEnabled(file) && formattingFeature.isSupported(file) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these feature flags default to false/disabled, so if we don't find a language server that supports formatting but not server-side on-type formatting, everything below will be an effective no-op.

@angelozerr angelozerr added this to the 0.12.0 milestone Mar 24, 2025
@angelozerr angelozerr merged commit 86c9757 into redhat-developer:main Mar 24, 2025
6 checks passed
@angelozerr angelozerr removed this from the 0.12.0 milestone Mar 24, 2025
@angelozerr
Copy link
Contributor

Thanks @SCWells72 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants