From 80387b503327a85c716898100eb996bcd14da4df Mon Sep 17 00:00:00 2001 From: Ruby Iris Juric Date: Thu, 28 Nov 2024 20:20:16 -0500 Subject: [PATCH] improvement: Make including detail in completion label configurable Some LSP clients (such as Emacs using Eglot and Corfu) use the label field to insert a completion if completionItem/resolve has not returned by the time the completion is selected. Including the contents of the detail property in the label causes it to be included in the file itself in these clients. To remedy this, we define a isDetailIncludedInLabel option under compilerOptions to make this behaviour toggleable. We also disable this behaviour by default in Emacs, since this is where the undesirable behaviour was located. Fixes #6849. --- .../scala/meta/internal/metals/InitializationOptions.scala | 4 +++- .../scala/meta/internal/metals/MetalsServerConfig.scala | 3 ++- .../java/scala/meta/pc/PresentationCompilerConfig.java | 7 +++++++ .../meta/internal/pc/CompilerInitializationOptions.scala | 4 ++++ .../meta/internal/pc/PresentationCompilerConfigImpl.scala | 4 ++++ .../scala/meta/internal/pc/CompletionProvider.scala | 4 +++- .../meta/internal/pc/completions/CompletionProvider.scala | 6 +++++- 7 files changed, 28 insertions(+), 4 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/InitializationOptions.scala b/metals/src/main/scala/scala/meta/internal/metals/InitializationOptions.scala index 3b6cea590d5..a5acb3d0b82 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/InitializationOptions.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/InitializationOptions.scala @@ -200,6 +200,9 @@ object InitializationOptions { isSignatureHelpDocumentationEnabled = compilerObj.flatMap( _.getBooleanOption("isSignatureHelpDocumentationEnabled") ), + isDetailIncludedInLabel = compilerObj.flatMap( + _.getBooleanOption("isDetailIncludedInLabel") + ), overrideDefFormat = compilerObj.flatMap( _.getStringOption("overrideDefFormat") ), @@ -210,7 +213,6 @@ object InitializationOptions { compilerObj.flatMap(_.getBooleanOption("snippetAutoIndent")), ) } - } sealed trait CommandHTMLFormat { diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsServerConfig.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsServerConfig.scala index ad8b2e2396a..eb5d14565f8 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsServerConfig.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsServerConfig.scala @@ -225,7 +225,8 @@ object MetalsServerConfig { base.copy( executeClientCommand = ExecuteClientCommandConfig.on, compilers = base.compilers.copy( - snippetAutoIndent = false + snippetAutoIndent = false, + isDetailIncludedInLabel = false, ), ) case _ => diff --git a/mtags-interfaces/src/main/java/scala/meta/pc/PresentationCompilerConfig.java b/mtags-interfaces/src/main/java/scala/meta/pc/PresentationCompilerConfig.java index afcd8bb3616..8ebdf8e5fbe 100644 --- a/mtags-interfaces/src/main/java/scala/meta/pc/PresentationCompilerConfig.java +++ b/mtags-interfaces/src/main/java/scala/meta/pc/PresentationCompilerConfig.java @@ -96,6 +96,13 @@ enum OverrideDefFormat { */ boolean isCompletionSnippetsEnabled(); + /** + * Returns true if the completion's description should be included in the label. + */ + default boolean isDetailIncludedInLabel() { + return true; + } + /** * The maximum delay for requests to respond. * diff --git a/mtags-shared/src/main/scala/scala/meta/internal/pc/CompilerInitializationOptions.scala b/mtags-shared/src/main/scala/scala/meta/internal/pc/CompilerInitializationOptions.scala index 57b5e583277..274b49f6012 100644 --- a/mtags-shared/src/main/scala/scala/meta/internal/pc/CompilerInitializationOptions.scala +++ b/mtags-shared/src/main/scala/scala/meta/internal/pc/CompilerInitializationOptions.scala @@ -16,6 +16,8 @@ package scala.meta.internal.pc * @param isHoverDocumentationEnabled whether to include docstrings in a `textDocument/hover`. * @param isSignatureHelpDocumentationEnabled whether the `SignatureHelp.documentation` field * should be populated. + * @param isDetailIncludedInLabel whether the completion's description should be included in the + * label. * @param overrideDefFormat whether the override should include a unicode icon or only ascii. * @param parameterHintsCommand command identifier to trigger parameter hints. * @param snippetAutoIndent whether the client defaults to adding the indentation of the reference @@ -28,6 +30,7 @@ case class CompilerInitializationOptions( isCompletionItemResolve: Option[Boolean], isHoverDocumentationEnabled: Option[Boolean], isSignatureHelpDocumentationEnabled: Option[Boolean], + isDetailIncludedInLabel: Option[Boolean], overrideDefFormat: Option[String], parameterHintsCommand: Option[String], snippetAutoIndent: Option[Boolean] @@ -44,6 +47,7 @@ object CompilerInitializationOptions { None, None, None, + None, None ) } diff --git a/mtags-shared/src/main/scala/scala/meta/internal/pc/PresentationCompilerConfigImpl.scala b/mtags-shared/src/main/scala/scala/meta/internal/pc/PresentationCompilerConfigImpl.scala index 12db83917cf..18b72bb5248 100644 --- a/mtags-shared/src/main/scala/scala/meta/internal/pc/PresentationCompilerConfigImpl.scala +++ b/mtags-shared/src/main/scala/scala/meta/internal/pc/PresentationCompilerConfigImpl.scala @@ -22,6 +22,7 @@ case class PresentationCompilerConfigImpl( snippetAutoIndent: Boolean = true, isSignatureHelpDocumentationEnabled: Boolean = true, isCompletionSnippetsEnabled: Boolean = true, + override val isDetailIncludedInLabel: Boolean = true, isCompletionItemResolve: Boolean = true, _isStripMarginOnTypeFormattingEnabled: () => Boolean = () => true, timeoutDelay: Long = 20, @@ -72,6 +73,9 @@ case class PresentationCompilerConfigImpl( isCompletionItemResolve = options.isCompletionItemResolve.getOrElse( this.isCompletionItemResolve ), + isDetailIncludedInLabel = options.isDetailIncludedInLabel.getOrElse( + this.isDetailIncludedInLabel + ), _parameterHintsCommand = options.parameterHintsCommand.orElse( this._parameterHintsCommand ), diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/CompletionProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/CompletionProvider.scala index b6e6c930994..c08a8cef4c4 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/CompletionProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/CompletionProvider.scala @@ -111,7 +111,9 @@ class CompletionProvider( } def labelWithSig = - if (member.sym.isMethod || member.sym.isValue) { + if ( + compiler.metalsConfig.isDetailIncludedInLabel && (member.sym.isMethod || member.sym.isValue) + ) { ident + detail } else { ident diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/CompletionProvider.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/CompletionProvider.scala index 19a93ae188f..d591d509b1e 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/CompletionProvider.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/CompletionProvider.scala @@ -162,7 +162,11 @@ class CompletionProvider( // related issue https://github.com/lampepfl/dotty/issues/11941 lazy val kind: CompletionItemKind = completion.completionItemKind val description = completion.description(printer) - val label = completion.labelWithDescription(printer) + val label = + if config.isDetailIncludedInLabel then + completion.labelWithDescription(printer) + else + completion.label val ident = completion.insertText.getOrElse(completion.label) def mkItem(