-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrating the Util.kt
and KModifier
from JVM to common
#2006
Changes from all commits
14bcf44
81046c1
d7c57e4
0920d3e
523b012
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.kotlinpoet | ||
|
||
private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() | ||
|
||
internal actual val String.isIdentifier: Boolean get() = IDENTIFIER_REGEX.matches(this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.kotlinpoet | ||
|
||
import com.squareup.kotlinpoet.CodeBlock.Companion.isPlaceholder | ||
import java.util.Collections | ||
|
||
internal actual fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> = | ||
Collections.unmodifiableMap(LinkedHashMap(this)) | ||
|
||
internal actual fun <T> Collection<T>.toImmutableList(): List<T> = | ||
Collections.unmodifiableList(ArrayList(this)) | ||
|
||
internal actual fun <T> Collection<T>.toImmutableSet(): Set<T> = | ||
Collections.unmodifiableSet(LinkedHashSet(this)) | ||
|
||
// TODO Waiting for `CodeBlock` migration. | ||
internal fun CodeBlock.ensureEndsWithNewLine() = trimTrailingNewLine('\n') | ||
|
||
// TODO Waiting for `CodeBlock` migration. | ||
internal fun CodeBlock.trimTrailingNewLine(replaceWith: Char? = null) = if (isEmpty()) { | ||
this | ||
} else { | ||
with(toBuilder()) { | ||
val lastFormatPart = trim().formatParts.last() | ||
if (lastFormatPart.isPlaceholder && args.isNotEmpty()) { | ||
val lastArg = args.last() | ||
if (lastArg is String) { | ||
val trimmedArg = lastArg.trimEnd('\n') | ||
args[args.size - 1] = if (replaceWith != null) { | ||
trimmedArg + replaceWith | ||
} else { | ||
trimmedArg | ||
} | ||
} | ||
} else { | ||
formatParts[formatParts.lastIndexOf(lastFormatPart)] = lastFormatPart.trimEnd('\n') | ||
if (replaceWith != null) { | ||
formatParts += "$replaceWith" | ||
} | ||
} | ||
return@with build() | ||
} | ||
} | ||
|
||
private val IDENTIFIER_REGEX = IDENTIFIER_REGEX_VALUE.toRegex() | ||
|
||
internal actual val String.isIdentifier: Boolean | ||
get() = IDENTIFIER_REGEX.matches(this) | ||
|
||
internal actual fun Char.isJavaIdentifierStart(): Boolean = | ||
Character.isJavaIdentifierStart(this) | ||
|
||
internal actual fun Char.isJavaIdentifierPart(): Boolean = | ||
Character.isJavaIdentifierPart(this) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.kotlinpoet | ||
|
||
internal actual fun <K, V> Map<K, V>.toImmutableMap(): Map<K, V> = | ||
toMap() | ||
|
||
internal actual fun <T> Collection<T>.toImmutableList(): List<T> = | ||
toList() | ||
|
||
internal actual fun <T> Collection<T>.toImmutableSet(): Set<T> = | ||
toSet() | ||
|
||
internal actual fun Char.isJavaIdentifierStart(): Boolean { | ||
return isLetter() || | ||
this in CharCategory.LETTER_NUMBER || | ||
this == '$' || | ||
this == '_' | ||
} | ||
|
||
internal actual fun Char.isJavaIdentifierPart(): Boolean { | ||
// TODO | ||
// A character may be part of a Java identifier if any of the following conditions are true: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we maybe extract these checks into common to have consistent implementations on all platforms? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be done in later PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, I am dividing them because I am unsure if the current implementation of non-JVM is the same as the one in JVM. |
||
// - it is a letter | ||
// - it is a currency symbol (such as '$') | ||
// - it is a connecting punctuation character (such as '_') | ||
// - it is a digit | ||
// - it is a numeric letter (such as a Roman numeral character) | ||
// - it is a combining mark | ||
// - it is a non-spacing mark | ||
// isIdentifierIgnorable returns true for the character. | ||
// Also missing here: | ||
// - a combining mark | ||
return isLetter() || | ||
isDigit() || | ||
this in CharCategory.LETTER_NUMBER || | ||
this in CharCategory.NON_SPACING_MARK || | ||
this == '_' || | ||
this == '$' || | ||
isIdentifierIgnorable() | ||
// | ||
} | ||
|
||
internal fun Char.isIdentifierIgnorable(): Boolean { | ||
// The following Unicode characters are ignorable in a Java identifier or a Unicode identifier: | ||
// - ISO control characters that are not whitespace | ||
// - '\u0000' through '\u0008' | ||
// - '\u000E' through '\u001B' | ||
// - '\u007F' through '\u009F' | ||
// - all characters that have the FORMAT general category value | ||
return ( | ||
isISOControl() && ( | ||
this in '\u0000'..'\u0008' || | ||
this in '\u000E'..'\u001B' || | ||
this in '\u007F'..'\u009F' | ||
) | ||
) || this in CharCategory.FORMAT | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.squareup.kotlinpoet | ||
|
||
internal actual val String.isIdentifier: Boolean | ||
get() { | ||
val regExp = RegExp(IDENTIFIER_REGEX_VALUE, "gu") | ||
regExp.reset() | ||
|
||
val match = regExp.exec(this) ?: return false | ||
return match.index == 0 && regExp.lastIndex == length | ||
} | ||
|
||
internal external interface RegExpMatch { | ||
val index: Int | ||
val length: Int | ||
} | ||
|
||
internal external class RegExp(pattern: String, flags: String? = definedExternally) : JsAny { | ||
fun exec(str: String): RegExpMatch? | ||
override fun toString(): String | ||
|
||
var lastIndex: Int | ||
} | ||
|
||
internal fun RegExp.reset() { | ||
lastIndex = 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not going to put these in an unmodifiable wrapper on other targets, do we still need to do it on JVM? Are we really worried about Java callers mutating things? I don't think I am.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original implementation was copied when we forked from JavaPoet, for what it's worth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, you have a point. If we no longer care about calls that mutate things on the JVM, do we keep
toImmutable*
in common and have its result just return ato*
(liketoSet
), or do we simply remove them and useto*
instead where appropriate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's defending against something that is not a real problem in practice, and I would eliminate the concept entirely and use Kotlin's collection APIs directly everywhere. This should probably be done in its own PR rather than cluttering this one.