/
ExtractMethodProvider.scala
172 lines (159 loc) Β· 6.17 KB
/
ExtractMethodProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package scala.meta.internal.pc
import java.nio.file.Paths
import scala.meta as m
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.printer.MetalsPrinter
import scala.meta.internal.pc.printer.MetalsPrinter.IncludeDefaultParam
import scala.meta.pc.OffsetParams
import scala.meta.pc.RangeParams
import scala.meta.pc.SymbolSearch
import dotty.tools.dotc.ast.Trees.*
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.tpd.DeepFolder
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Symbols.Symbol
import dotty.tools.dotc.core.Types.MethodType
import dotty.tools.dotc.core.Types.PolyType
import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.interactive.Interactive
import dotty.tools.dotc.interactive.InteractiveDriver
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
import org.eclipse.lsp4j as l
import org.eclipse.lsp4j.TextEdit
final class ExtractMethodProvider(
range: RangeParams,
extractionPos: OffsetParams,
driver: InteractiveDriver,
search: SymbolSearch,
noIndent: Boolean,
)(using ReportContext)
extends ExtractMethodUtils:
def extractMethod(): List[TextEdit] =
val text = range.text()
val uri = range.uri
val filePath = Paths.get(uri)
val source = SourceFile.virtual(filePath.toString, text)
driver.run(uri, source)
val unit = driver.currentCtx.run.units.head
val pos = driver.sourcePosition(range).startPos
val path =
Interactive.pathTo(driver.openedTrees(uri), pos)(using driver.currentCtx)
given locatedCtx: Context =
val newctx = driver.currentCtx.fresh.setCompilationUnit(unit)
Interactive.contextOfPath(path)(using newctx)
val indexedCtx = IndexedContext(locatedCtx)
val printer =
MetalsPrinter.standard(indexedCtx, search, IncludeDefaultParam.Never)
def prettyPrint(tpe: Type) =
def prettyPrintReturnType(tpe: Type): String =
tpe match
case mt: (MethodType | PolyType) =>
prettyPrintReturnType(tpe.resultType)
case tpe => printer.tpe(tpe)
def printParams(params: List[Type]) =
params match
case p :: Nil => prettyPrintReturnType(p)
case _ => s"(${params.map(prettyPrintReturnType).mkString(", ")})"
if tpe.paramInfoss.isEmpty
then prettyPrintReturnType(tpe)
else
val params = tpe.paramInfoss.map(printParams).mkString(" => ")
s"$params => ${prettyPrintReturnType(tpe)}"
end prettyPrint
def extractFromBlock(t: tpd.Tree): List[tpd.Tree] =
t match
case Block(stats, expr) =>
(stats :+ expr).filter(stat => range.encloses(stat.sourcePos))
case temp: Template[?] =>
temp.body.filter(stat => range.encloses(stat.sourcePos))
case other => List(other)
def localRefs(
ts: List[tpd.Tree],
defnPos: SourcePosition,
extractedPos: SourcePosition,
): (List[Symbol], List[Symbol]) =
def nonAvailable(sym: Symbol): Boolean =
val symPos = sym.sourcePos
symPos.exists && defnPos.contains(symPos) && !extractedPos
.contains(symPos)
def collectNames(symbols: Set[Symbol], tree: tpd.Tree): Set[Symbol] =
tree match
case id @ Ident(_) =>
val sym = id.symbol
if nonAvailable(sym) && (sym.isTerm || sym.isTypeParam)
then symbols + sym
else symbols
case _ => symbols
val traverser = new DeepFolder[Set[Symbol]](collectNames)
val allSymbols = ts
.foldLeft(Set.empty[Symbol])(traverser(_, _))
val methodParams = allSymbols.toList.filter(_.isTerm)
val methodParamTypes = methodParams
.flatMap(p => p :: p.paramSymss.flatten)
.map(_.info.typeSymbol)
.filter(tp => nonAvailable(tp) && tp.isTypeParam)
.distinct
// Type parameter can be a type of one of the parameters or a type parameter in extracted code
val typeParams =
allSymbols.filter(_.isTypeParam) ++ methodParamTypes
(
methodParams.sortBy(_.decodedName),
typeParams.toList.sortBy(_.decodedName),
)
end localRefs
val edits =
for
enclosing <- path.find(src => src.sourcePos.encloses(range))
extracted = extractFromBlock(enclosing)
head <- extracted.headOption
expr <- extracted.lastOption
shortenedPath =
path.takeWhile(src => extractionPos.offset() <= src.sourcePos.start)
stat = shortenedPath.lastOption.getOrElse(head)
yield
val defnPos = stat.sourcePos
val extractedPos = head.sourcePos.withEnd(expr.sourcePos.end)
val exprType = prettyPrint(expr.tpe.widen)
val name =
genName(indexedCtx.scopeSymbols.map(_.decodedName).toSet, "newMethod")
val (methodParams, typeParams) =
localRefs(extracted, stat.sourcePos, extractedPos)
val methodParamsText = methodParams
.map(sym => s"${sym.decodedName}: ${prettyPrint(sym.info)}")
.mkString(", ")
val typeParamsText = typeParams
.map(_.decodedName) match
case Nil => ""
case params => params.mkString("[", ", ", "]")
val exprParamsText = methodParams.map(_.decodedName).mkString(", ")
val newIndent = stat.startPos.startColumnPadding
val oldIndentLen = head.startPos.startColumnPadding.length()
val toExtract =
textToExtract(
range.text(),
head.startPos.start,
expr.endPos.end,
newIndent,
oldIndentLen,
)
val (obracket, cbracket) =
if noIndent && extracted.length > 1 then (" {", s"$newIndent}")
else ("", "")
val defText =
s"def $name$typeParamsText($methodParamsText): $exprType =$obracket\n${toExtract}\n$cbracket\n$newIndent"
val replacedText = s"$name($exprParamsText)"
List(
new l.TextEdit(
extractedPos.toLsp,
replacedText,
),
new l.TextEdit(
defnPos.startPos.toLsp,
defText,
),
)
edits.getOrElse(Nil)
end extractMethod
end ExtractMethodProvider