Skip to content

Commit

Permalink
Merge pull request #60 from abrookins/deprecation-fixes
Browse files Browse the repository at this point in the history
Add deprecation fixes, refactors
  • Loading branch information
abrookins authored Aug 26, 2024
2 parents 8996026 + c93201b commit 78f7a54
Show file tree
Hide file tree
Showing 16 changed files with 687 additions and 582 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ Anyway, I recommend that you use a monospaced font if you can.

* Bug fixes

## Developing

This project uses gradle.

To run tests, use the `tests` gradle command.

To build the plugin zip, use the `buildPlugin` gradle command.


## License

Expand Down
120 changes: 0 additions & 120 deletions build.gradle

This file was deleted.

51 changes: 51 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id("java")
id("org.jetbrains.kotlin.jvm") version "1.9.21"
id("org.jetbrains.intellij") version "1.17.3"
}

group = "com.andrewbrookins.idea.wrap"
version = "1.9.0"

repositories {
mavenCentral()
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
implementation(kotlin("stdlib-jdk8"))
}

// Configure Gradle IntelliJ Plugin
// Read more: https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html
intellij {
version.set("2022.2.5")
type.set("IC")
updateSinceUntilBuild = false
}

tasks {
// Set the JVM compatibility versions
withType<JavaCompile> {
sourceCompatibility = "17"
targetCompatibility = "17"
}
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}

patchPluginXml {
sinceBuild.set("222")
untilBuild.set("232.*")
}

signPlugin {
certificateChain.set(System.getenv("CERTIFICATE_CHAIN"))
privateKey.set(System.getenv("PRIVATE_KEY"))
password.set(System.getenv("PRIVATE_KEY_PASSWORD"))
}

publishPlugin {
token.set(System.getenv("PUBLISH_TOKEN"))
}
}
12 changes: 6 additions & 6 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jan 16 10:04:33 EET 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
#Tue Apr 16 10:26:26 PDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Empty file modified gradlew
100644 → 100755
Empty file.
5 changes: 5 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
pluginManagement {
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.9.23'
}
}
rootProject.name = 'WrapToColumn'

63 changes: 34 additions & 29 deletions src/main/java/com/andrewbrookins/idea/wrap/CodeWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.andrewbrookins.idea.wrap
import org.apache.commons.lang.StringUtils
import java.util.regex.Pattern


/**
* Code-aware text wrapper.
*
Expand All @@ -12,15 +11,16 @@ import java.util.regex.Pattern
* This code was inspired by Nir Soffer's codewrap library: * https://pypi.python.org/pypi/codewrap/
*/
class CodeWrapper(
private val commentRegex: Regex = "(/\\*+|\\*/|\\*|\\.|#+|//+|;+|--)?".toRegex(),
private val commentRegex: Regex = "(/\\*+|\\*/|\\*|\\.|#+|//+|;+|--|'''|\"\"\")?".toRegex(),

private val newlineRegex: Regex = "(\\r?\\n)".toRegex(),

private val htmlSeparatorsRegex: Regex = "<[pP]>|<[bB][rR] ?/?>".toRegex(),

// A string that contains only two new lines demarcates a paragraph.
private val paragraphSeparatorPattern: Pattern = Pattern.compile(
"($newlineRegex)\\s*$commentRegex\\s*($htmlSeparatorsRegex)?$newlineRegex"),
"($newlineRegex)\\s*$commentRegex\\s*($htmlSeparatorsRegex)?$newlineRegex"
),

private val tabPlaceholder: String = "",

Expand Down Expand Up @@ -51,7 +51,8 @@ class CodeWrapper(
// Meaningful non-comment symbols, like Markdown lists, etc. Used if
// preserveLeadingSymbolSpacing is true.
private val meaningfulSymbolRegex: String = "^(\\s+)?(\\*|-|(\\d+\\.))(\\s+)",
private val meaningfulSymbolPattern: Pattern = Pattern.compile(meaningfulSymbolRegex)) {
private val meaningfulSymbolPattern: Pattern = Pattern.compile(meaningfulSymbolRegex)
) {

/**
* Data about a line that has been split into two pieces: the indent portion
Expand All @@ -70,12 +71,12 @@ class CodeWrapper(
}

/**
* Wrap ``text`` to the chosen width.
* Wrap `text` to the chosen width.
*
* Preserve the amount of white space between paragraphs after wrapping
* them. A paragraph is defined as text separated by empty lines. A line is
* considered empty if contains only start of comment characters and a
* single ``<p>`` or ``<br>`` HTML tag (this is common in Javadoc).
* single `<p>` or `<br>` HTML tag (this is common in Javadoc).
*
* @param text the text to wrap, which may contain multiple paragraphs.
*
Expand Down Expand Up @@ -115,12 +116,11 @@ class CodeWrapper(

/**
* Wrap a single paragraph of text.
* Breaks ``paragraph`` into an array of lines of the chosen width, then
*
* Breaks `paragraph` into an array of lines of the chosen width, then
* joins them back into a single string.
*
* @param paragraph the paragraph to wrap
* *
* @return text reflowed to chosen width
*/
private fun wrapParagraph(paragraph: String): String {
Expand Down Expand Up @@ -182,22 +182,31 @@ class CodeWrapper(
*
* Note: C-style multi-line comments are always reflowed to the chosen
* column width. This means that the first line might stick out because
* it's indent is longer ("&#47;** " instead of " * " on continuation lines).
* its indent is longer (" ** " instead of "* " on continuation lines).
*
* @param text single paragraph of text
*
* @param text single paragraph of text
* @return array of lines
*/
private fun breakToLinesOfChosenWidth(text:String):MutableList<String> {
private fun breakToLinesOfChosenWidth(text: String): MutableList<String> {
val firstLineIndent = splitOnIndent(text).indent
val firstLineIsDocstring = text.trimStart().matches("^\"\"\"|^'''".toRegex())
val firstLineIsCommentOpener = firstLineIndent.matches("\\s*(/\\*+|\"\"\"|''')\\s*".toRegex())
var unwrappedText = unwrap(text)
val lines: Array<String>
var leadingSymbolWidth = 0
var leadingSymbol = ""
var width = width
var correctedText = text

if (preserveLeadingSymbolSpacing) {
// Remove docstring symbols from start and end of string
if (firstLineIsDocstring) {
correctedText = text.replaceFirst("^\"\"\"|^'''".toRegex(), "")
correctedText = correctedText.replaceFirst("\"\"\"\$|'''\$".toRegex(), "")
}

var unwrappedText = unwrap(correctedText)

if (preserveLeadingSymbolSpacing) {
val symbolMatcher = meaningfulSymbolPattern.matcher(text)
if (symbolMatcher.find()) {
leadingSymbol = symbolMatcher.group()
Expand All @@ -211,12 +220,11 @@ class CodeWrapper(

if (useMinimumRaggedness) {
lines = wrapMinimumRaggedness(unwrappedText, width).dropLastWhile(String::isEmpty).toTypedArray()
}
else {
} else {
lines = wrapGreedy(unwrappedText, width, lineSeparator)
.split(lineSeparator.toRegex())
.dropLastWhile(String::isEmpty)
.toTypedArray()
.split(lineSeparator.toRegex())
.dropLastWhile(String::isEmpty)
.toTypedArray()
}
val result = mutableListOf<String>()
val length = lines.size
Expand Down Expand Up @@ -253,12 +261,11 @@ class CodeWrapper(
}

/**
* Convert a hard wrapped paragraph to one line.
* Convert a hard-wrapped paragraph to one line.
*
* Indent and comment characters are striped.
* Indent and comment characters are stripped.
*
* @param text one paragraph of text, possibly hard-wrapped
*
* @return one line of text
*/
private fun unwrap(text: String): String {
Expand All @@ -276,7 +283,6 @@ class CodeWrapper(
val unindentedLine = splitOnIndent(line).rest.trim { it <= ' ' }

if (line.isEmpty()) {
// Ignore a line that was just a carriage return/new line.
lastLineWasCarriageReturn = true
continue
}
Expand All @@ -295,13 +301,12 @@ class CodeWrapper(
}

/**
* Split text on indent, including comment characters
* Split text on indent, including comment characters.
*
* Example (parsed from left margin):
* // Comment -> ' // ', 'Comment'
*
* @param text text to remove indents from
* *
* @return indent string, rest
*/
fun splitOnIndent(text: String): LineData {
Expand All @@ -313,18 +318,18 @@ class CodeWrapper(
// weirdness with comments-embedded-in-comments.
if (indentMatcher.find()) {
lineData.indent = indentMatcher.group()
lineData.rest = text.substring(indentMatcher.end(), text.length).trim({ it <= ' ' })
lineData.rest = text.substring(indentMatcher.end(), text.length).trim { it <= ' ' }
// We might get "/*\n", so strip the newline if so.
lineData.indent = lineData.indent.replace("[\\r\\n]+".toRegex(), "")
}

// If we suspect a line begins with a "meaningful symbol," save that.
// This is important for file types that use comment-like symbols for
// things like lists, e.g. Markdown, AsciiDoc.
// things like lists, e.g., Markdown, AsciiDoc.
if (symbolMatcher.find()) {
lineData.meaningfulSymbol = symbolMatcher.group()
}

return lineData
}
}
}
Loading

0 comments on commit 78f7a54

Please sign in to comment.