-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework and simplify OnTypeFormatting and RangeFormatting classes
I mostly removed a couple of types, which were not adding much and increasing complexity. I also removed the `fn` functions and just inlined everything in the methods, which is overall shorter and simpler to understand.
- Loading branch information
Showing
8 changed files
with
189 additions
and
222 deletions.
There are no files selected for viewing
This file contains 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 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
142 changes: 0 additions & 142 deletions
142
metals/src/main/scala/scala/meta/internal/metals/OnTypeRangeFormattingProvider.scala
This file was deleted.
Oops, something went wrong.
This file contains 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 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
74 changes: 74 additions & 0 deletions
74
metals/src/main/scala/scala/meta/internal/metals/formatting/OnTypeFormattingProvider.scala
This file contains 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,74 @@ | ||
package scala.meta.internal.metals.formatting | ||
|
||
import scala.meta.inputs.Input | ||
import scala.meta.internal.metals.Buffers | ||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.internal.metals.UserConfiguration | ||
import scala.meta.internal.parsing.Trees | ||
import scala.meta.tokens.Tokens | ||
|
||
import org.eclipse.lsp4j.DocumentOnTypeFormattingParams | ||
import org.eclipse.lsp4j.Position | ||
import org.eclipse.lsp4j.Range | ||
import org.eclipse.lsp4j.TextEdit | ||
|
||
class OnTypeFormatter { | ||
def contribute( | ||
onTypeformatterParams: OnTypeFormatterParams | ||
): Option[List[TextEdit]] = None | ||
|
||
} | ||
|
||
case class OnTypeFormatterParams( | ||
sourceText: String, | ||
position: Position, | ||
triggerChar: String, | ||
startPos: meta.Position, | ||
endPos: meta.Position, | ||
tokens: Option[Tokens] | ||
) { | ||
lazy val splitLines: Array[String] = sourceText.split("\\r?\\n") | ||
val range = new Range(position, position) | ||
} | ||
|
||
class OnTypeFormattingProvider( | ||
buffers: Buffers, | ||
trees: Trees, | ||
userConfig: () => UserConfiguration | ||
) { | ||
|
||
// The order of which this is important to know which will first return the Edits | ||
val formatters: List[OnTypeFormatter] = List( | ||
MultilineString(userConfig) | ||
) | ||
|
||
def format( | ||
params: DocumentOnTypeFormattingParams | ||
): List[TextEdit] = { | ||
val uri = params.getTextDocument.getUri.toAbsolutePath | ||
val range = new Range(params.getPosition, params.getPosition) | ||
val triggerChar = params.getCh | ||
val position = params.getPosition | ||
buffers | ||
.get(uri) | ||
.map { sourceText => | ||
val virtualFile = Input.VirtualFile(sourceText.toString(), sourceText) | ||
val startPos = range.getStart.toMeta(virtualFile) | ||
val endPos = range.getEnd.toMeta(virtualFile) | ||
val tokensOpt = trees.tokenized(virtualFile).toOption | ||
val onTypeformatterParams = | ||
OnTypeFormatterParams( | ||
sourceText, | ||
position, | ||
triggerChar, | ||
startPos, | ||
endPos, | ||
tokensOpt | ||
) | ||
formatters.acceptFirst(formater => | ||
formater.contribute(onTypeformatterParams) | ||
) | ||
} | ||
.getOrElse(Nil) | ||
} | ||
} |
Oops, something went wrong.