Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Nov 6, 2023
1 parent 4929fb0 commit 8ef3139
Show file tree
Hide file tree
Showing 21 changed files with 228 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package scala.meta.internal.decorations

import java.nio.charset.Charset
import java.util.concurrent.atomic.AtomicReference

import scala.util.Try

Expand Down Expand Up @@ -35,22 +34,15 @@ final class SyntheticHoverProvider(
userConfig: () => UserConfiguration,
trees: Trees,
) {
private object Document {
/* We update it with each compilation in order not read the same file on
* each change. When typing documents stay the same most of the time.
*/
private val document = new AtomicReference[TextDocument]

def currentDocument: Option[TextDocument] = Option(document.get())

def set(doc: TextDocument): Unit = document.set(doc)
}
@volatile private var document: Option[TextDocument] =
Option.empty[TextDocument]

def addSyntheticsHover(
params: HoverExtParams,
pcHover: Option[l.Hover],
): Option[l.Hover] =
if (areSyntheticsEnabled) {
if (userConfig().areSyntheticsEnabled()) {
val path = params.textDocument.getUri().toAbsolutePath
val position = params.getPosition
val line = position.getLine()
Expand Down Expand Up @@ -107,13 +99,6 @@ final class SyntheticHoverProvider(
pcHover
}

private def areSyntheticsEnabled: Boolean = {
val showInferredType = !userConfig().showInferredType.contains(
"false"
) && userConfig().showInferredType.nonEmpty
userConfig().showImplicitArguments || showInferredType || userConfig().showImplicitConversionsAndClasses
}

private def createHoverAtPoint(
syntheticsAtLine: Seq[(l.Range, String)],
pcHover: Option[l.Hover],
Expand Down Expand Up @@ -196,7 +181,7 @@ final class SyntheticHoverProvider(
}

private def currentDocument(path: AbsolutePath): Option[TextDocument] = {
Document.currentDocument match {
document match {
case Some(doc) if workspace.resolve(doc.uri) == path => Some(doc)
case _ =>
val textDocument = semanticdbs
Expand All @@ -214,7 +199,7 @@ final class SyntheticHoverProvider(
doc <- textDocument
source <- fingerprints.loadLastValid(path, doc.md5, charset)
docWithText = doc.withText(source)
_ = Document.set(docWithText)
_ = document = Some(docWithText)
} yield docWithText
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1111,10 +1111,7 @@ class MetalsLspService(
CancelTokens.future { token =>
val shouldShow =
force ||
userConfig.showInferredType.contains("true") ||
userConfig.showInferredType.contains("minimal") ||
userConfig.showImplicitArguments ||
userConfig.showImplicitConversionsAndClasses
userConfig.areSyntheticsEnabled()
if (shouldShow) {
compilers.syntheticDecorations(path, token).map { decorations =>
val params = new PublishDecorationsParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ case class UserConfiguration(
bloopVersion.getOrElse(BuildInfo.bloopVersion)

def usedJavaBinary(): Option[AbsolutePath] = JavaBinary.path(javaHome)

def areSyntheticsEnabled(): Boolean = {
val showInferredType = !this.showInferredType.contains(
"false"
) && this.showInferredType.nonEmpty
showImplicitArguments || showInferredType || showImplicitConversionsAndClasses
}
}

object UserConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ public abstract CompletableFuture<List<TextEdit>> convertToNamedArguments(Offset
public abstract CompletableFuture<List<Diagnostic>> didChange(VirtualFileParams params);

/**
*
* Returns decorations for missing type adnotations, inferred type parameters, implicit parameters and conversions.
*/
public abstract CompletableFuture<List<SyntheticDecoration>> syntheticDecorations(SyntheticDecorationsParams params);
public CompletableFuture<List<SyntheticDecoration>> syntheticDecorations(SyntheticDecorationsParams params) {
return CompletableFuture.completedFuture(Collections.emptyList());
}

/**
* File was closed.
Expand Down
12 changes: 0 additions & 12 deletions mtags-interfaces/src/main/java/scala/meta/pc/SyntheticOptions.java

This file was deleted.

16 changes: 16 additions & 0 deletions mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,22 @@ class MetalsGlobal(
syntheticName && wrongSpan
}

private val infixNames =
Set(nme.apply, nme.unapply, nme.unapplySeq)

def isInfix(tree: Tree, text: String): Boolean =
tree match {
case Select(New(_), _) => false
case Select(_, name: TermName) if infixNames(name) => false
case Select(This(_), _) => false
// is a select statement without a dot `qual.name`
case Select(qual, _) => {
val pos = qual.pos.end
pos < text.length() && text(pos) != '.'
}
case _ => false
}

// Extractor for both term and type applications like `foo(1)` and foo[T]`
object TreeApply {
def unapply(tree: Tree): Option[(Tree, List[Tree])] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ final class PcSyntheticDecorationsProvider(
cursor = None
)
lazy val text = unit.source.content
lazy val textStr = text.mkString

typeCheck(unit)

def tpdTree = unit.lastBody
Expand Down Expand Up @@ -104,7 +106,8 @@ final class PcSyntheticDecorationsProvider(
object ImplicitParameters {
def unapply(tree: Tree): Option[(List[String], Position, Boolean)] =
tree match {
case Apply(_, args) if args.exists(isSyntheticArg) =>
case Apply(_, args)
if args.exists(isSyntheticArg) && !tree.pos.isOffset =>
val (implicitArgs, providedArgs) = args.partition(isSyntheticArg)
val allImplicit = providedArgs.isEmpty
val pos = providedArgs.lastOption.fold(tree.pos)(_.pos)
Expand Down Expand Up @@ -133,7 +136,11 @@ final class PcSyntheticDecorationsProvider(
None
case TypeApply(fun, args)
if args.exists(_.pos.isOffset) && tree.pos.isRange =>
val pos = fun.pos
val pos = fun match {
case sel: Select if isInfix(sel, textStr) =>
sel.namePosition
case _ => fun.pos
}
Some(args.map(_.tpe.widen.finalResultType), pos)
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,6 @@ trait Completions { this: MetalsGlobal =>
ident: Ident,
apply: Apply
): CompletionPosition = {
def isInfix(apply: Apply, text: String) =
apply.fun match {
case Select(New(_), _) => false
case Select(_, name) if name.decoded == "apply" => false
case Select(This(_), _) => false
// is a select statement without a dot `qual.name`
case Select(qual, _) => {
val pos = qual.pos.end
pos < text.length() && text(pos) != '.'
}
case _ => false
}
if (hasLeadingBrace(ident, text)) {
if (isCasePrefix(ident.name)) {
val moveToNewLine = ident.pos.line == apply.pos.line
Expand All @@ -485,7 +473,7 @@ trait Completions { this: MetalsGlobal =>
NoneCompletion
}
} else {
if (!isInfix(apply, text)) {
if (!isInfix(apply.fun, text)) {
ArgCompletion(ident, apply, pos, text, completions)
} else NoneCompletion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import scala.meta.pc.SymbolSearch
import dotty.tools.dotc.ast.tpd.*
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Denotations.*
import dotty.tools.dotc.core.Flags
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.NameOps.*
import dotty.tools.dotc.core.Names.*
Expand Down Expand Up @@ -256,6 +257,8 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
end symbolDocumentation
end extension

private val infixNames =
Set(nme.apply, nme.unapply, nme.unapplySeq)
extension (tree: Tree)
def qual: Tree =
tree match
Expand All @@ -271,6 +274,19 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
val denot = sym.denot.asSeenFrom(pre.tpe.widenTermRefExpr)
(denot.info, sym.withUpdatedTpe(denot.info))
catch case NonFatal(e) => (sym.info, sym)

def isInfix(using ctx: Context) =
tree match
case Select(New(_), _) => false
case Select(_, name: TermName) if infixNames(name) => false
case Select(This(_), _) => false
// is a select statement without a dot `qual.name`
case sel @ Select(qual, _) if !sel.symbol.is(Flags.Synthetic) =>
val source = tree.source
!(qual.span.end until sel.nameSpan.start)
.map(source.apply)
.contains('.')
case _ => false
end extension

extension (imp: Import)
Expand All @@ -287,7 +303,6 @@ object MtagsEnrichments extends ScalametaCommonEnrichments:
case _ => false
val wrongSpan = sel.qualifier.span.contains(sel.nameSpan)
syntheticName && wrongSpan

extension (denot: Denotation)
def allSymbols: List[Symbol] =
denot match
Expand Down
18 changes: 0 additions & 18 deletions mtags/src/main/scala-3/scala/meta/internal/pc/PcCollector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -447,24 +447,6 @@ abstract class PcCollector[T](
)
else occurences
case _ => occurences
/**
* for synthetic decorations in:
* given Conversion[Int, String] = _.toString
* val x: String = <<>>123
*/
case sel: Select
if sel.span.isZeroExtent && sel.symbol.is(Flags.ExtensionMethod) =>
parent match
case Some(a: Apply) =>
val span = a.span.withStart(a.span.point)
val amendedSelect = sel.withSpan(span)
if filter(amendedSelect) then
occurences + collect(
amendedSelect,
pos.withSpan(span),
)
else occurences
case _ => occurences
/* all definitions:
* def <<foo>> = ???
* class <<Foo>> = ???
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,28 @@ object ImplicitConversion:
def unapply(tree: Tree)(using Context) =
tree match
case Apply(fun: Ident, args) if isSynthetic(fun) =>
val lastArgPos =
args.lastOption.map(_.sourcePos).getOrElse(fun.sourcePos)
Some(
fun.symbol.decodedName,
lastArgPos.withStart(fun.sourcePos.start),
)
implicitConversion(fun, args)
case Apply(Select(fun, name), args)
if name == nme.apply && isSynthetic(fun) =>
val lastArgPos =
args.lastOption.map(_.sourcePos).getOrElse(fun.sourcePos)
Some(
fun.symbol.decodedName,
lastArgPos.withStart(fun.sourcePos.start),
)
implicitConversion(fun, args)
case _ => None
private def isSynthetic(tree: Tree)(using Context) =
tree.span.isSynthetic && tree.symbol.isOneOf(Flags.GivenOrImplicit)

private def implicitConversion(fun: Tree, args: List[Tree])(using Context) =
val lastArgPos =
args.lastOption.map(_.sourcePos).getOrElse(fun.sourcePos)
Some(
fun.symbol.decodedName,
lastArgPos.withStart(fun.sourcePos.start),
)
end ImplicitConversion

object ImplicitParameters:
def unapply(tree: Tree)(using Context) =
tree match
case Apply(fun, args) if args.exists(isSyntheticArg) =>
case Apply(fun, args)
if args.exists(isSyntheticArg) && !tree.sourcePos.span.isZeroExtent =>
val (implicitArgs, providedArgs) = args.partition(isSyntheticArg)
val allImplicit = providedArgs.isEmpty
val pos = implicitArgs.head.sourcePos
Expand All @@ -204,8 +203,12 @@ object TypeParameters:
tree match
case TypeApply(sel: Select, _) if sel.isForComprehensionMethod => None
case TypeApply(fun, args) if inferredTypeArgs(args) =>
val pos = fun match
case sel: Select if sel.isInfix =>
sel.sourcePos.withEnd(sel.nameSpan.end)
case _ => fun.sourcePos
val tpes = args.map(_.tpe.stripTypeVar.widen.finalResultType)
Some((tpes, tree.endPos, fun))
Some((tpes, pos.endPos, fun))
case _ => None
private def inferredTypeArgs(args: List[Tree]): Boolean =
args.forall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object NamedArgCompletions:
)(using ctx: Context): List[CompletionValue] =
path match
case (ident: Ident) :: ValDef(_, _, _) :: Block(_, app: Apply) :: _
if !isInfix(pos, app) =>
if !app.fun.isInfix =>
contribute(
Some(ident),
app,
Expand All @@ -58,7 +58,7 @@ object NamedArgCompletions:
val contribution =
for
app <- getApplyForContextFunctionParam(rest)
if !isInfix(pos, app)
if !app.fun.isInfix
yield contribute(
Some(ident),
app,
Expand All @@ -71,18 +71,6 @@ object NamedArgCompletions:
end match
end contribute

private def isInfix(pos: SourcePosition, apply: Apply)(using ctx: Context) =
apply.fun match
case Select(New(_), _) => false
case Select(_, name) if name.decoded == "apply" => false
case Select(This(_), _) => false
// is a select statement without a dot `qual.name`
case sel @ Select(qual, _) if !sel.symbol.is(Flags.Synthetic) =>
!(qual.span.end until sel.nameSpan.start)
.map(pos.source.apply)
.contains('.')
case _ => false

private def contribute(
ident: Option[Ident],
apply: Apply,
Expand Down
Loading

0 comments on commit 8ef3139

Please sign in to comment.