From f9d3c56b0c35d6e8beb8f5620ecd4f284defa83e Mon Sep 17 00:00:00 2001 From: Igor Demin Date: Wed, 28 Oct 2020 11:05:57 +0300 Subject: [PATCH] Multiplatform template in IDEA / Android Studio. Refactoring --- .../android/src/main/assets/data/EditorView.kt | 10 +++++----- .../main/java/org/jetbrains/codeviewer/MainActivity.kt | 1 - .../org/jetbrains/codeviewer/platform/JvmFile.kt | 6 +++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/codeviewer/android/src/main/assets/data/EditorView.kt b/examples/codeviewer/android/src/main/assets/data/EditorView.kt index 6f180d0ffc..68852cdbef 100644 --- a/examples/codeviewer/android/src/main/assets/data/EditorView.kt +++ b/examples/codeviewer/android/src/main/assets/data/EditorView.kt @@ -1,5 +1,5 @@ /** - * This file is example (we can open it in android application) + * This file is an example (we can open it in android application) */ package org.jetbrains.codeviewer.ui.editor @@ -73,7 +73,7 @@ fun EditorView(model: Editor, settings: Settings) = key(model) { @Composable private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient.current) { - val maxNum = remember(lines.lineNumberDigitCount) { + val maxNumber = remember(lines.lineNumberDigitCount) { (1..lines.lineNumberDigitCount).joinToString(separator = "") { "9" } } @@ -89,7 +89,7 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient val line: Editor.Line? by loadable { lines.get(index) } Box(Modifier.height(lineHeight)) { if (line != null) { - Line(Modifier.align(Alignment.CenterStart), maxNum, line!!, settings) + Line(Modifier.align(Alignment.CenterStart), maxNumber, line!!, settings) } } } @@ -108,11 +108,11 @@ private fun Lines(lines: Editor.Lines, settings: Settings) = with(DensityAmbient // دعم اللغة العربية // 中文支持 @Composable -private fun Line(modifier: Modifier, maxNum: String, line: Editor.Line, settings: Settings) { +private fun Line(modifier: Modifier, maxNumber: String, line: Editor.Line, settings: Settings) { Row(modifier = modifier) { WithoutSelection { Box { - LineNumber(maxNum, Modifier.drawOpacity(0f), settings) + LineNumber(maxNumber, Modifier.drawOpacity(0f), settings) LineNumber(line.number.toString(), Modifier.align(Alignment.CenterEnd), settings) } } diff --git a/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt b/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt index eb7a470595..0957a85d61 100644 --- a/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt +++ b/examples/codeviewer/android/src/main/java/org/jetbrains/codeviewer/MainActivity.kt @@ -8,7 +8,6 @@ import org.jetbrains.codeviewer.ui.MainView import java.io.File import java.io.FileOutputStream - class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt b/examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt index 49c83db708..df4ad90b10 100644 --- a/examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt +++ b/examples/codeviewer/common/src/jvmMain/kotlin/org/jetbrains/codeviewer/platform/JvmFile.kt @@ -25,6 +25,7 @@ fun java.io.File.toProjectFile(): File = object : File { get() = isDirectory && listFiles()?.size ?: 0 > 0 override suspend fun readLines(backgroundScope: CoroutineScope): TextLines { + // linePositions can be very big, so we are using IntList instead of List val linePositions = IntList() var size by mutableStateOf(0) @@ -52,6 +53,7 @@ fun java.io.File.toProjectFile(): File = object : File { try { RandomAccessFile(this@toProjectFile, "rws").use { it.seek(position.toLong()) + // NOTE: it isn't efficient, but simple String( it.readLine() .toCharArray() @@ -77,7 +79,6 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext( } val averageLineLength = 200 - // linePositions can be very big, so we are using IntArray instead of List list.clear(length().toInt() / averageLineLength) var isBeginOfLine = true @@ -107,6 +108,9 @@ private suspend fun java.io.File.readLinePositions(list: IntList) = withContext( list.compact() } +/** + * Compact version of List (without unboxing Int and using IntArray under the hood) + */ private class IntList(initialCapacity: Int = 16) { @Volatile private var array = IntArray(initialCapacity)