Skip to content

Commit

Permalink
refactor: undo changes in PcCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Oct 27, 2023
1 parent b24b676 commit 4929fb0
Show file tree
Hide file tree
Showing 25 changed files with 379 additions and 527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ case class DecorationOptions(
)

object DecorationOptions {
def apply(range: Range, text: String) =
def apply(text: String, range: Range) =
new DecorationOptions(
range,
renderOptions = ThemableDecorationInstanceRenderOptions(
Expand Down
66 changes: 34 additions & 32 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -566,42 +566,44 @@ class Compilers(
path: AbsolutePath,
token: CancelToken,
): Future[ju.List[DecorationOptions]] = {
val anyEnabled =
userConfig().showInferredType.contains("true") ||
userConfig().showInferredType.contains("minimal") ||
userConfig().showImplicitArguments ||
userConfig().showImplicitConversionsAndClasses
if (!anyEnabled) Future.successful(Nil.asJava)
else
loadCompiler(path)
.map { compiler =>
val (input, _, adjust) =
sourceAdjustments(
path.toNIO.toUri().toString(),
compiler.scalaVersion(),
)
val vFile =
CompilerVirtualFileParams(path.toNIO.toUri(), input.text, token)

val pcParams = CompilerSyntheticDecorationsParams(
vFile,
typeParameters = userConfig().showInferredType.contains("true"),
inferredTypes = userConfig().showInferredType.contains("minimal") ||
userConfig().showInferredType.contains("true"),
implicitParameters = userConfig().showImplicitArguments,
implicitConversions = userConfig().showImplicitConversionsAndClasses,
loadCompiler(path)
.map { compiler =>
val (input, _, adjust) =
sourceAdjustments(
path.toNIO.toUri().toString(),
compiler.scalaVersion(),
)
compiler
.syntheticDecorations(pcParams)
.asScala
.map(_.map { decoration =>
val vFile =
CompilerVirtualFileParams(path.toNIO.toUri(), input.text, token)

val pcParams = CompilerSyntheticDecorationsParams(
vFile,
typeParameters = userConfig().showInferredType.contains("true"),
inferredTypes = userConfig().showInferredType.contains("minimal") ||
userConfig().showInferredType.contains("true"),
implicitParameters = userConfig().showImplicitArguments,
implicitConversions = userConfig().showImplicitConversionsAndClasses,
)
compiler
.syntheticDecorations(pcParams)
.asScala
.map { decorations =>
val decorationOptions = decorations.map { decoration =>
DecorationOptions(
adjust.adjustRange(decoration.range()),
decoration.label(),
adjust.adjustRange(decoration.range()),
)
})
}
.getOrElse(Future.successful(Nil.asJava))
}
val isScala2 = compiler.scalaVersion().startsWith("2.")
if (isScala2 && path.isWorksheet && !decorationOptions.isEmpty()) {
// In scala 2 worksheet, last decoration is for synthetic `main` method
decorationOptions.remove(decorationOptions.size() - 1)
decorationOptions
} else decorationOptions
}

}
.getOrElse(Future.successful(Nil.asJava))

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,14 @@ class MetalsLspService(
for {
_ <- buildServerPromise.future
_ <- focusedDocument()
.map(publishSynthetics)
.map { path =>
Future.sequence(
List(
publishSynthetics(path, force = true),
worksheetProvider.onDidFocus(path),
)
)
}
.getOrElse(Future.successful(()))
} yield ()
} else {
Expand Down Expand Up @@ -1097,16 +1104,27 @@ class MetalsLspService(
}
}

def publishSynthetics(path: AbsolutePath): Future[Unit] = {
def publishSynthetics(
path: AbsolutePath,
force: Boolean = false,
): Future[Unit] = {
CancelTokens.future { token =>
compilers.syntheticDecorations(path, token).map { decorations =>
val params = new PublishDecorationsParams(
path.toURI.toString(),
decorations.asScala.toArray,
if (clientConfig.isInlineDecorationProvider()) true else null,
)
languageClient.metalsPublishDecorations(params)
}
val shouldShow =
force ||
userConfig.showInferredType.contains("true") ||
userConfig.showInferredType.contains("minimal") ||
userConfig.showImplicitArguments ||
userConfig.showImplicitConversionsAndClasses
if (shouldShow) {
compilers.syntheticDecorations(path, token).map { decorations =>
val params = new PublishDecorationsParams(
path.toURI.toString(),
decorations.asScala.toArray,
if (clientConfig.isInlineDecorationProvider()) true else null,
)
languageClient.metalsPublishDecorations(params)
}
} else Future.successful(())
}.asScala
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ trait MtagsEnrichments extends ScalametaCommonEnrichments {

def encloses(other: RangeParams): Boolean =
pos.start <= other.offset() && pos.end >= other.endOffset()

def adjust(
text: Array[Char],
forRename: Boolean = false
): (Position, Boolean) = {
val isBackticked = text(pos.start) == '`' &&
text(pos.end - 1) == '`' &&
pos.start != (pos.end - 1) // for one character names, e.g. `c`
// start-^^-end
val isOldNameBackticked = text(pos.start) == '`' &&
(text(pos.end - 1) != '`' || pos.start == (pos.end - 1)) &&
text(pos.end + 1) == '`'
if (isBackticked && forRename)
(pos.withStart(pos.start + 1).withEnd(pos.end - 1), true)
else if (isOldNameBackticked) // pos
(pos.withEnd(pos.end + 2), false)
else (pos, false)
}
}

implicit class XtensionRangeParameters(pos: RangeParams) {
Expand Down
13 changes: 13 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 @@ -1025,6 +1025,19 @@ class MetalsGlobal(
}
}

private val forCompMethods =
Set(nme.map, nme.flatMap, nme.withFilter, nme.foreach)

// We don't want to collect synthethic `map`, `withFilter`, `foreach` and `flatMap` in for-comprenhensions
def isForComprehensionMethod(sel: Select): Boolean = {
val syntheticName = sel.name match {
case name: TermName => forCompMethods(name)
case _ => false
}
val wrongSpan = sel.qualifier.pos.includes(sel.namePosition.focusStart)
syntheticName && wrongSpan
}

// 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
Loading

0 comments on commit 4929fb0

Please sign in to comment.