11package com.wakaztahir.common
22
3- import androidx.compose.foundation.layout.fillMaxSize
3+ import androidx.compose.foundation.background
4+ import androidx.compose.foundation.layout.*
5+ import androidx.compose.foundation.text.BasicTextField
6+ import androidx.compose.material.MaterialTheme
47import androidx.compose.material.OutlinedTextField
8+ import androidx.compose.material.Text
9+ import androidx.compose.material.primarySurface
510import androidx.compose.runtime.*
611import androidx.compose.ui.Modifier
12+ import androidx.compose.ui.geometry.Offset
13+ import androidx.compose.ui.platform.LocalDensity
14+ import androidx.compose.ui.text.AnnotatedString
715import androidx.compose.ui.text.input.TextFieldValue
16+ import androidx.compose.ui.unit.DpOffset
17+ import androidx.compose.ui.unit.IntOffset
18+ import androidx.compose.ui.unit.dp
819import com.wakaztahir.codeeditor.model.CodeLang
920import com.wakaztahir.codeeditor.prettify.PrettifyParser
1021import com.wakaztahir.codeeditor.theme.CodeThemeType
1122import com.wakaztahir.codeeditor.utils.parseCodeAsAnnotatedString
23+ import kotlin.math.roundToInt
1224
1325@Composable
1426fun DisplayCodeEditor () {
@@ -24,31 +36,41 @@ fun DisplayCodeEditor() {
2436 val parser = remember { PrettifyParser () }
2537 val themeState by remember { mutableStateOf(CodeThemeType .Default ) }
2638 val theme = remember(themeState) { themeState.theme }
27- var textFieldValue by remember {
28- mutableStateOf(
29- TextFieldValue (
30- annotatedString = parseCodeAsAnnotatedString(
31- parser = parser,
32- theme = theme,
33- lang = language,
34- code = code
35- )
36- )
39+
40+ fun parse (code : String ): AnnotatedString {
41+ return parseCodeAsAnnotatedString(
42+ parser = parser,
43+ theme = theme,
44+ lang = language,
45+ code = code
3746 )
3847 }
3948
40- OutlinedTextField (
41- modifier = Modifier .fillMaxSize(),
42- value = textFieldValue,
43- onValueChange = {
44- textFieldValue = it.copy(
45- annotatedString = parseCodeAsAnnotatedString(
46- parser = parser,
47- theme = theme,
48- lang = language,
49- code = it.text
50- )
51- )
49+ var textFieldValue by remember { mutableStateOf(TextFieldValue (parse(code))) }
50+ var lineTops by remember { mutableStateOf(emptyArray<Float >()) }
51+ val density = LocalDensity .current
52+
53+ Row {
54+ if (lineTops.isNotEmpty()) {
55+ Box (modifier = Modifier .padding(horizontal = 4 .dp)) {
56+ lineTops.forEachIndexed { index, top ->
57+ Text (
58+ modifier = Modifier .offset(y = with (density) { top.toDp() }),
59+ text = index.toString(),
60+ color = MaterialTheme .colors.onBackground.copy(.3f )
61+ )
62+ }
63+ }
5264 }
53- )
65+ BasicTextField (
66+ modifier = Modifier .fillMaxSize(),
67+ value = textFieldValue,
68+ onValueChange = {
69+ textFieldValue = it.copy(annotatedString = parse(it.text))
70+ },
71+ onTextLayout = { result ->
72+ lineTops = Array (result.lineCount) { result.getLineTop(it) }
73+ }
74+ )
75+ }
5476}
0 commit comments