-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Regex pattern definitions to a separate file in preparation for SN
- Loading branch information
Showing
4 changed files
with
104 additions
and
47 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
scalafmt-core/jvm/src/main/scala/org/scalafmt/internal/RegexCompat.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,94 @@ | ||
package org.scalafmt.internal | ||
|
||
import java.util.regex.Pattern | ||
|
||
private[scalafmt] object RegexCompat { | ||
|
||
@inline | ||
val trailingSpace = Pattern.compile("\\h++$", Pattern.MULTILINE) | ||
|
||
// "slc" stands for single-line comment | ||
@inline | ||
val slcLine = Pattern.compile("^/\\/\\/*+\\h*+(.*?)\\h*+$") | ||
|
||
@inline | ||
val slcDelim = Pattern.compile("\\h++") | ||
|
||
// "mlc" stands for multi-line comment | ||
@inline | ||
val mlcHeader = Pattern.compile("^/\\*\\h*+(?:\n\\h*+[*]*+\\h*+)?") | ||
|
||
@inline | ||
val mlcLineDelim = Pattern.compile("\\h*+\n\\h*+[*]*+\\h*+") | ||
|
||
@inline | ||
val mlcParagraphEnd = Pattern.compile("[.:!?=]$") | ||
|
||
@inline | ||
val mlcParagraphBeg = Pattern.compile("^(?:[-*@=]|\\d++[.:])") | ||
|
||
@inline | ||
private val leadingAsteriskSpace = Pattern.compile("(?<=\n)\\h*+(?=[*][^*])") | ||
|
||
@inline | ||
val docstringLine = Pattern | ||
.compile("^(?:\\h*+\\*)?(\\h*+)(.*?)\\h*+$", Pattern.MULTILINE) | ||
|
||
@inline | ||
private val emptyLines = "\\h*+(\n\\h*+\\*?\\h*+)*" | ||
|
||
@inline | ||
val emptyDocstring = Pattern.compile(s"^/\\*\\*$emptyLines\\*/$$") | ||
|
||
@inline | ||
val onelineDocstring = { | ||
val oneline = "[^*\n\\h](?:[^\n]*[^\n\\h])?" | ||
Pattern.compile(s"^/\\*\\*$emptyLines($oneline)$emptyLines\\*/$$") | ||
} | ||
|
||
@inline | ||
val docstringLeadingSpace = Pattern.compile("^\\h++") | ||
|
||
@inline | ||
def compileStripMarginPattern(pipe: Char) = Pattern | ||
.compile(s"(?<=\n)\\h*+(?=\\$pipe)") | ||
|
||
@inline | ||
def compileStripMarginPatternWithLineContent(pipe: Char) = Pattern | ||
.compile(s"\n(\\h*+\\$pipe)?([^\n]*+)") | ||
|
||
@inline | ||
val stripMarginPattern = compileStripMarginPattern('|') | ||
|
||
@inline | ||
val stripMarginPatternWithLineContent = | ||
compileStripMarginPatternWithLineContent('|') | ||
|
||
@inline | ||
private val leadingPipeSpace = compileStripMarginPattern('|') | ||
|
||
@inline | ||
private def getStripMarginPattern(pipe: Char) = | ||
if (pipe == '|') leadingPipeSpace else compileStripMarginPattern(pipe) | ||
|
||
@inline | ||
def replaceAllStripMargin( | ||
text: String, | ||
spaces: String, | ||
pipe: Char, | ||
): String = | ||
getStripMarginPattern(pipe).matcher(text).replaceAll(spaces) | ||
|
||
@inline | ||
def replaceAllLeadingAsterisk( | ||
trimmed: String, | ||
spaces: String, | ||
): String = leadingAsteriskSpace.matcher(trimmed).replaceAll(spaces) | ||
|
||
// Replaces baseText.split("(?={beforeText})") | ||
@inline | ||
def splitByBeforeTextMatching( | ||
baseText: String, | ||
beforeText: String, | ||
): Array[String] = baseText.split(s"(?=$beforeText)") | ||
} |
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
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