-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add unsafe RawPresentationCompiler implementation #24133
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
Open
rochala
wants to merge
2
commits into
scala:main
Choose a base branch
from
rochala:raw-presentation-compiler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
presentation-compiler/src/main/dotty/tools/pc/DiagnosticProvider.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dotty.tools.pc | ||
|
||
import org.eclipse.lsp4j | ||
import org.eclipse.lsp4j.DiagnosticSeverity | ||
import dotty.tools.dotc.interactive.InteractiveDriver | ||
import dotty.tools.dotc.interfaces.Diagnostic as DiagnosticInterfaces | ||
import dotty.tools.dotc.reporting.Diagnostic | ||
import dotty.tools.pc.utils.InteractiveEnrichments.toLsp | ||
|
||
import scala.meta.pc.VirtualFileParams | ||
import ch.epfl.scala.bsp4j | ||
import dotty.tools.dotc.reporting.CodeAction | ||
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch | ||
import scala.jdk.CollectionConverters.* | ||
import dotty.tools.dotc.core.Contexts.Context | ||
import dotty.tools.dotc.reporting.ErrorMessageID | ||
import org.eclipse.lsp4j.DiagnosticTag | ||
|
||
class DiagnosticProvider(driver: InteractiveDriver, params: VirtualFileParams): | ||
|
||
def diagnostics(): List[lsp4j.Diagnostic] = | ||
val diags = driver.run(params.uri().nn, params.text().nn) | ||
given Context = driver.currentCtx | ||
diags.flatMap(toLsp) | ||
|
||
private def toLsp(diag: Diagnostic)(using Context): Option[lsp4j.Diagnostic] = | ||
Option.when(diag.pos.exists): | ||
val lspDiag = lsp4j.Diagnostic( | ||
diag.pos.toLsp, | ||
diag.msg.message, | ||
toDiagnosticSeverity(diag.level), | ||
"presentation compiler", | ||
) | ||
lspDiag.setCode(diag.msg.errorId.errorNumber) | ||
|
||
val scalaDiagnostic = new bsp4j.ScalaDiagnostic() | ||
val actions = diag.msg.actions.map(toBspScalaAction).asJava | ||
scalaDiagnostic.setActions(actions) | ||
// lspDiag.setRelatedInformation(???) Currently not emitted by the compiler | ||
lspDiag.setData(scalaDiagnostic) | ||
if diag.msg.errorId == ErrorMessageID.UnusedSymbolID then | ||
lspDiag.setTags(List(DiagnosticTag.Unnecessary).asJava) | ||
|
||
lspDiag | ||
|
||
private def toBspScalaAction(action: CodeAction): bsp4j.ScalaAction = | ||
val bspAction = bsp4j.ScalaAction(action.title) | ||
action.description.foreach(bspAction.setDescription) | ||
val workspaceEdit = bsp4j.ScalaWorkspaceEdit(action.patches.map(toBspTextEdit).asJava) | ||
bspAction.setEdit(workspaceEdit) | ||
bspAction | ||
|
||
private def toBspTextEdit(actionPatch: ActionPatch): bsp4j.ScalaTextEdit = | ||
val startPos = bsp4j.Position(actionPatch.srcPos.startLine, actionPatch.srcPos.startColumn) | ||
val endPos = bsp4j.Position(actionPatch.srcPos.endLine, actionPatch.srcPos.endColumn) | ||
val range = bsp4j.Range(startPos, endPos) | ||
bsp4j.ScalaTextEdit(range, actionPatch.replacement) | ||
|
||
|
||
private def toDiagnosticSeverity(severity: Int): DiagnosticSeverity = | ||
severity match | ||
case DiagnosticInterfaces.ERROR => DiagnosticSeverity.Error | ||
case DiagnosticInterfaces.WARNING => DiagnosticSeverity.Warning | ||
case DiagnosticInterfaces.INFO => DiagnosticSeverity.Information | ||
case _ => DiagnosticSeverity.Information |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import dotty.tools.dotc.util.Spans.Span | |
import org.eclipse.lsp4j.InlayHint | ||
import org.eclipse.lsp4j.InlayHintKind | ||
import org.eclipse.{lsp4j as l} | ||
import scala.meta.internal.pc.InlayHintOrigin | ||
|
||
class PcInlayHintsProvider( | ||
driver: InteractiveDriver, | ||
|
@@ -80,7 +81,7 @@ class PcInlayHintsProvider( | |
) | ||
case _ => inlayHints | ||
} | ||
|
||
tree match | ||
case ImplicitConversion(symbol, range) => | ||
val adjusted = adjustPos(range) | ||
|
@@ -89,11 +90,13 @@ class PcInlayHintsProvider( | |
adjusted.startPos.toLsp, | ||
labelPart(symbol, symbol.decodedName) :: LabelPart("(") :: Nil, | ||
InlayHintKind.Parameter, | ||
InlayHintOrigin.ImplicitConversion | ||
) | ||
.add( | ||
adjusted.endPos.toLsp, | ||
LabelPart(")") :: Nil, | ||
InlayHintKind.Parameter, | ||
InlayHintOrigin.ImplicitConversion | ||
) | ||
case ImplicitParameters(trees, pos) => | ||
firstPassHints.add( | ||
|
@@ -104,12 +107,14 @@ class PcInlayHintsProvider( | |
case None => LabelPart(label) | ||
), | ||
InlayHintKind.Parameter, | ||
InlayHintOrigin.ImplicitParameters | ||
) | ||
case ValueOf(label, pos) => | ||
firstPassHints.add( | ||
adjustPos(pos).toLsp, | ||
LabelPart("(") :: LabelPart(label) :: List(LabelPart(")")), | ||
InlayHintKind.Parameter, | ||
InlayHintOrigin.ImplicitParameters | ||
) | ||
case TypeParameters(tpes, pos, sel) | ||
if !syntheticTupleApply(sel) => | ||
|
@@ -118,19 +123,18 @@ class PcInlayHintsProvider( | |
adjustPos(pos).endPos.toLsp, | ||
label, | ||
InlayHintKind.Type, | ||
InlayHintOrigin.TypeParameters | ||
) | ||
case InferredType(tpe, pos, defTree) | ||
if !isErrorTpe(tpe) => | ||
val adjustedPos = adjustPos(pos).endPos | ||
if firstPassHints.containsDef(adjustedPos.start) then firstPassHints | ||
else | ||
firstPassHints | ||
.add( | ||
adjustedPos.toLsp, | ||
LabelPart(": ") :: toLabelParts(tpe, pos), | ||
InlayHintKind.Type, | ||
) | ||
.addDefinition(adjustedPos.start) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was removed out of sudden in Scala 2 presentation compiler, I believe it is no longer necessary as we're deduplicating inlay hints during |
||
firstPassHints | ||
.add( | ||
adjustedPos.toLsp, | ||
LabelPart(": ") :: toLabelParts(tpe, pos), | ||
InlayHintKind.Type, | ||
InlayHintOrigin.InferredType | ||
) | ||
case Parameters(isInfixFun, args) => | ||
def isNamedParam(pos: SourcePosition): Boolean = | ||
val start = text.indexWhere(!_.isWhitespace, pos.start) | ||
|
@@ -167,6 +171,7 @@ class PcInlayHintsProvider( | |
hintPos.startPos.toLsp, | ||
List(LabelPart(labelStr)), | ||
InlayHintKind.Parameter, | ||
if params.byNameParameters then InlayHintOrigin.ByNameParameters else InlayHintOrigin.NamedParameters | ||
) | ||
else ih | ||
} | ||
|
@@ -515,7 +520,7 @@ object XRayModeHint: | |
case Some(sel: Select) if sel.isForComprehensionMethod => false | ||
case Some(par) if par.sourcePos.exists && par.sourcePos.line == tree.sourcePos.line => true | ||
case _ => false | ||
|
||
tree match | ||
/* | ||
anotherTree | ||
|
@@ -530,7 +535,7 @@ object XRayModeHint: | |
.select | ||
*/ | ||
case select @ Select(innerTree, _) | ||
if innerTree.sourcePos.exists && !isParentOnSameLine && !isParentApply && | ||
if innerTree.sourcePos.exists && !isParentOnSameLine && !isParentApply && | ||
isEndOfLine(tree.sourcePos) => | ||
Some((select.tpe.widen.deepDealiasAndSimplify, tree.sourcePos)) | ||
case _ => None | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TLDR: I think that it's fine to patch only the necessary changes for now, but this should probably be refactored (similarly as Scala 2 code) soon-ish.
The refactor in the Scala 2 PC was meant to allow for more than one "case" to be executed here. This isn't a problem yet, since scalameta/metals#7815 hasn't been ported yet. Though one could make an argument that this problem appeared before and was "hacked" my merging implementations for two inlay hint types.
The idea behind the refactor is:
foldLeft
through all the cases.I think that a similar refactor should be done here as well, though it maybe shouldn't be part of this PR.