@@ -28,6 +28,7 @@ import com.google.googlejavaformat.Input
28
28
import com.google.googlejavaformat.Newlines
29
29
import com.google.googlejavaformat.java.FormatterException
30
30
import com.google.googlejavaformat.java.JavaOutput
31
+ import java.util.LinkedHashMap
31
32
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil
32
33
import org.jetbrains.kotlin.lexer.KtTokens
33
34
import org.jetbrains.kotlin.psi.KtFile
@@ -107,7 +108,6 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
107
108
*/
108
109
@Throws(FormatterException ::class )
109
110
internal fun characterRangeToTokenRange (offset : Int , length : Int ): Range <Int > {
110
- var length = length
111
111
val requiredLength = offset + length
112
112
if (requiredLength > text.length) {
113
113
throw FormatterException (
@@ -116,14 +116,15 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
116
116
length,
117
117
requiredLength))
118
118
}
119
- when {
120
- length < 0 -> return EMPTY_RANGE
121
- length == 0 -> // 0 stands for "format the line under the cursor"
122
- length = 1
123
- }
119
+ val expandedLength =
120
+ when {
121
+ length < 0 -> return EMPTY_RANGE
122
+ length == 0 -> 1 // 0 stands for "format the line under the cursor"
123
+ else -> length
124
+ }
124
125
val enclosed =
125
126
getPositionTokenMap()
126
- .subRangeMap(Range .closedOpen(offset, offset + length ))
127
+ .subRangeMap(Range .closedOpen(offset, offset + expandedLength ))
127
128
.asMapOfRanges()
128
129
.values
129
130
return if (enclosed.isEmpty()) {
@@ -134,11 +135,11 @@ class KotlinInput(private val text: String, file: KtFile) : Input() {
134
135
}
135
136
136
137
private fun makePositionToColumnMap (toks : List <KotlinTok >): ImmutableMap <Int , Int > {
137
- val builder = ImmutableMap .builder <Int , Int >()
138
+ val builder = LinkedHashMap <Int , Int >()
138
139
for (tok in toks) {
139
140
builder.put(tok.position, tok.column)
140
141
}
141
- return builder.build( )
142
+ return ImmutableMap .copyOf(builder )
142
143
}
143
144
144
145
private fun buildToks (file : KtFile , fileText : String ): ImmutableList <KotlinTok > {
0 commit comments