@@ -30,7 +30,7 @@ allprojects {
3030
3131``` groovy
3232dependencies {
33- implementation 'com.github.timeline-notes :compose-code-editor:2.0.2 '
33+ implementation 'com.github.qawaz :compose-code-editor:2.0.3 '
3434}
3535```
3636
@@ -69,19 +69,19 @@ gpr.key=yourgithubpersonalaccesstoken
6969#### Step 3 : Add The Dependency
7070
7171``` kotlin
72- implementation(" com.wakaztahir:codeeditor:3.0.1 " )
72+ implementation(" com.wakaztahir:codeeditor:3.0.4 " )
7373```
7474
7575## Usage
7676
7777``` kotlin
7878// Step 1. Declare Language & Code
79- val language = CodeLang .Java
79+ val language = CodeLang .Kotlin
8080val code = """
8181 package com.wakaztahir.codeeditor
8282
83- public static void main(String[] args ){
84- System.out. println("Hello World")
83+ fun main(){
84+ println("Hello World");
8585 }
8686""" .trimIndent()
8787
@@ -111,20 +111,6 @@ Text(parsedCode)
111111### Using TextField Composable
112112
113113``` kotlin
114- val language = CodeLang .Java
115- val code = """
116- package com.wakaztahir.codeeditor
117-
118- public static void main(String[] args){
119- System.out.println("Hello World")
120- }
121- """ .trimIndent()
122-
123- val scope = rememberCoroutineScope()
124- var bringIntoViewRequester = remember { BringIntoViewRequester () }
125- val parser = remember { PrettifyParser () }
126- val themeState by remember { mutableStateOf(CodeThemeType .Default ) }
127- val theme = remember(themeState) { themeState.theme() }
128114var textFieldValue by remember {
129115 mutableStateOf(
130116 TextFieldValue (
@@ -139,7 +125,7 @@ var textFieldValue by remember {
139125}
140126
141127OutlinedTextField (
142- modifier = Modifier .fillMaxSize().bringIntoViewRequester(bringIntoViewRequester) ,
128+ modifier = Modifier .fillMaxSize(),
143129 value = textFieldValue,
144130 onValueChange = {
145131 textFieldValue = it.copy(
@@ -150,17 +136,74 @@ OutlinedTextField(
150136 code = it.text
151137 )
152138 )
153- scope.launch {
154- bringIntoViewRequester.bringIntoView()
155- }
156139 }
157140)
158141```
159142
143+ ### Displaying Line Numbers
144+
145+ To display line numbers in the text field we must use a ` BasicTextField ` since it has a parameter for ` onTextLayout `
146+
147+ A basic example can be setup like this , On every text layout a new array is created
148+ which contains top offsets of each line in the ` BasicTextField `
149+
150+ ``` kotlin
151+
152+ val language = CodeLang .Kotlin
153+ val code = """
154+ package com.wakaztahir.codeeditor
155+
156+ fun main(){
157+ println("Hello World");
158+ }
159+ """ .trimIndent()
160+
161+ val parser = remember { PrettifyParser () }
162+ val themeState by remember { mutableStateOf(CodeThemeType .Default ) }
163+ val theme = remember(themeState) { themeState.theme }
164+
165+ fun parse (code : String ): AnnotatedString {
166+ return parseCodeAsAnnotatedString(
167+ parser = parser,
168+ theme = theme,
169+ lang = language,
170+ code = code
171+ )
172+ }
173+
174+ var textFieldValue by remember { mutableStateOf(TextFieldValue (parse(code))) }
175+ var lineTops by remember { mutableStateOf(emptyArray<Float >()) }
176+ val density = LocalDensity .current
177+
178+ Row {
179+ if (lineTops.isNotEmpty()) {
180+ Box (modifier = Modifier .padding(horizontal = 4 .dp)) {
181+ lineTops.forEachIndexed { index, top ->
182+ Text (
183+ modifier = Modifier .offset(y = with (density) { top.toDp() }),
184+ text = index.toString(),
185+ color = MaterialTheme .colors.onBackground.copy(.3f )
186+ )
187+ }
188+ }
189+ }
190+ BasicTextField (
191+ modifier = Modifier .fillMaxSize(),
192+ value = textFieldValue,
193+ onValueChange = {
194+ textFieldValue = it.copy(annotatedString = parse(it.text))
195+ },
196+ onTextLayout = { result ->
197+ lineTops = Array (result.lineCount) { result.getLineTop(it) }
198+ }
199+ )
200+ }
201+ ```
202+
160203## List of available languages & their extensions
161204
162205Default (``` "default-code" ``` ), HTML (``` "default-markup" ``` ) , C/C++/Objective-C (``` "c" ``` , ``` "cc" ``` , ``` "cpp" ``` , ``` "cxx" ``` , ``` "cyc" ``` , ``` "m" ``` ),
163- C# (``` "cs" ``` ), Java (``` "java" ``` ), Bash (``` "bash" ``` , ``` "bsh" ``` , ``` "csh" ``` , ``` "sh" ``` ),
206+ C# (``` "cs" ``` ), Java (``` "java" ``` ),Kotlin ( ``` "kt" ``` ) , Bash (``` "bash" ``` , ``` "bsh" ``` , ``` "csh" ``` , ``` "sh" ``` ),
164207Python (``` "cv" ``` , ``` "py" ``` , ``` "python" ``` ), Perl (``` "perl" ``` , ``` "pl" ``` , ``` "pm" ``` ),
165208Ruby (``` "rb" ``` , ``` "ruby" ``` ), JavaScript (``` "javascript" ``` , ``` "js" ``` ),
166209CoffeeScript (``` "coffee" ``` ), Rust (``` "rc" ``` , ``` "rs" ``` , ``` "rust" ``` ), Appollo (``` "apollo" ```
@@ -186,9 +229,4 @@ releases.
186229
187230## Issues
188231
189- * Does not support kotlin yet , but basic syntax highlighting can be achieved by using another
190- language
191232* Lack of themes
192- * Everytime user types code in a text field , all the code is parsed again rather than only the
193- changed lines which makes it a little inefficient , This is due to compose not supporting
194- multiline text editing yet , so it will be fixed in future
0 commit comments