@@ -3,23 +3,20 @@ package scala.meta.internal.metals.codeactions
3
3
import scala .concurrent .ExecutionContext
4
4
import scala .concurrent .Future
5
5
6
- import scala .meta .internal .metals .BuildTargets
7
6
import scala .meta .internal .metals .Compilers
8
7
import scala .meta .internal .metals .MetalsEnrichments ._
9
- import scala .meta .internal .metals .ScalaVersions
10
8
import scala .meta .internal .metals .ScalacDiagnostic
11
9
import scala .meta .internal .metals .ServerCommands
12
10
import scala .meta .internal .metals .clients .language .MetalsLanguageClient
13
11
import scala .meta .internal .metals .codeactions .CodeAction
14
12
import scala .meta .internal .metals .logging
15
13
import scala .meta .pc .CancelToken
14
+ import scala .meta .pc .CodeActionId
16
15
17
16
import org .eclipse .{lsp4j => l }
18
- import scala .meta .pc .CodeActionId
19
17
20
18
class CreateNewSymbol (
21
19
compilers : Compilers ,
22
- buildTargets : BuildTargets ,
23
20
languageClient : MetalsLanguageClient ,
24
21
) extends CodeAction {
25
22
override def kind : String = l.CodeActionKind .QuickFix
@@ -29,8 +26,6 @@ class CreateNewSymbol(
29
26
ServerCommands .InsertInferredMethod
30
27
)
31
28
32
- override def maybeCodeActionId : Option [String ] = Some (CodeActionId .InsertInferredMethod )
33
-
34
29
override def handleCommand (
35
30
textDocumentParams : l.TextDocumentPositionParams ,
36
31
token : CancelToken ,
@@ -40,13 +35,15 @@ class CreateNewSymbol(
40
35
textDocumentParams,
41
36
token,
42
37
CodeActionId .InsertInferredMethod ,
43
- None
38
+ None ,
44
39
)
45
40
_ = logging.logErrorWhen(
46
41
edits.isEmpty(),
47
42
s " Could not infer method at ${textDocumentParams}" ,
48
43
)
49
- workspaceEdit = new l.WorkspaceEdit (Map (textDocumentParams.getTextDocument().getUri() -> edits).asJava)
44
+ workspaceEdit = new l.WorkspaceEdit (
45
+ Map (textDocumentParams.getTextDocument().getUri() -> edits).asJava
46
+ )
50
47
_ <- languageClient
51
48
.applyEdit(new l.ApplyWorkspaceEditParams (workspaceEdit))
52
49
.asScala
@@ -63,14 +60,9 @@ class CreateNewSymbol(
63
60
64
61
val uri = params.getTextDocument().getUri()
65
62
val file = uri.toAbsolutePath
66
- lazy val isScala3 =
67
- (for {
68
- buildId <- buildTargets.inverseSources(file)
69
- target <- buildTargets.scalaTarget(buildId)
70
- isScala3 = ScalaVersions .isScala3Version(
71
- target.scalaVersion
72
- )
73
- } yield isScala3).getOrElse(false )
63
+ lazy val isSupportedInferredType = compilers
64
+ .supportedCodeActions(file)
65
+ .contains(CodeActionId .InsertInferredMethod )
74
66
75
67
def createNewSymbol (
76
68
diagnostic : l.Diagnostic ,
@@ -107,26 +99,29 @@ class CreateNewSymbol(
107
99
)
108
100
}
109
101
102
+ def overalaps (diags : Seq [l.Diagnostic ]) = {
103
+ params.getRange().overlapsWith(diags.head.getRange())
104
+ }
105
+
110
106
val codeActions = params
111
107
.getContext()
112
108
.getDiagnostics()
113
109
.asScala
110
+ .toSeq
114
111
.groupBy {
115
112
case ScalacDiagnostic .SymbolNotFound (name) if name.nonEmpty =>
116
113
Some (name)
117
- case ScalacDiagnostic .NotAMember (name) if name.nonEmpty =>
114
+ case ScalacDiagnostic .NotAMember (name)
115
+ if name.nonEmpty && isSupportedInferredType =>
118
116
Some (name)
119
117
case _ => None
120
118
}
121
119
.collect {
122
120
case (Some (name), diags)
123
- if ! isScala3 // scala 3 not supported yet
124
- && name.head.isLower && params
125
- .getRange()
126
- .overlapsWith(diags.head.getRange()) =>
121
+ if overalaps(diags) && name.head.isLower &&
122
+ isSupportedInferredType =>
127
123
createNewMethod(diags.head, name)
128
- case (Some (name), diags)
129
- if params.getRange().overlapsWith(diags.head.getRange()) =>
124
+ case (Some (name), diags) if overalaps(diags) =>
130
125
createNewSymbol(diags.head, name)
131
126
}
132
127
.toSeq
0 commit comments