Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update highlights to 1.0.0 and use new async API #33

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

Merged
merged 8 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
.externalNativeBuild
.cxx
local.properties
/.kotlin/
2 changes: 1 addition & 1 deletion 2 .run/iosExample.run.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="iosExample" type="KmmRunConfiguration" factoryName="iOS Application" CONFIG_VERSION="1" EXEC_TARGET_ID="645C2113-7E25-482E-9F32-B612B7A046E0" XCODE_PROJECT="$PROJECT_DIR$/iosExample/iosExample.xcodeproj" XCODE_CONFIGURATION="Debug" XCODE_SCHEME="iosExample">
<configuration default="false" name="iosExample" type="KmmRunConfiguration" factoryName="iOS Application" CONFIG_VERSION="1" EXEC_TARGET_ID="726A8064-1CB8-4F0E-A381-CABB326B938F" XCODE_PROJECT="$PROJECT_DIR$/iosExample/iosExample.xcodeproj" XCODE_CONFIGURATION="Debug" XCODE_SCHEME="iosExample">
<method v="2">
<option name="com.jetbrains.kmm.ios.BuildIOSAppTask" enabled="true" />
</method>
Expand Down
2 changes: 1 addition & 1 deletion 2 androidExample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ dependencies {
implementation(compose.material3)
implementation(compose.ui)
implementation(compose.materialIconsExtended)
implementation(libs.kodeview)
implementation(project(":kodeview"))
}
4 changes: 2 additions & 2 deletions 4 desktopExample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
plugins {
alias(libs.plugins.jvm)
alias(libs.plugins.compose)
alias(libs.plugins.compose.compiler)
}

dependencies {
Expand All @@ -14,8 +15,7 @@ dependencies {
@OptIn(org.jetbrains.compose.ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
implementation(compose.desktop.currentOs)
implementation(libs.kodeview)
alias(libs.plugins.compose.compiler)
implementation(project(":kodeview"))
}

compose.desktop {
Expand Down
72 changes: 34 additions & 38 deletions 72 desktopExample/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ package dev.snipme.desktopexample

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.VerticalDivider
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
Expand All @@ -31,10 +31,8 @@ import dev.snipme.highlights.model.SyntaxLanguage
import dev.snipme.highlights.model.SyntaxTheme
import dev.snipme.highlights.model.SyntaxThemes
import dev.snipme.highlights.model.SyntaxThemes.useDark
import dev.snipme.kodeview.view.material3.CodeEditText
import dev.snipme.kodeview.view.CodeTextView

private val windowSize = 600.dp
import dev.snipme.kodeview.view.material3.CodeEditText

private val sampleCode =
"""
Expand Down Expand Up @@ -72,18 +70,14 @@ fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "KodeView example",
state = rememberWindowState(
width = windowSize,
height = windowSize,
)
state = rememberWindowState()
) {
Surface {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.SpaceBetween
verticalArrangement = Arrangement.Center,
) {
Spacer(Modifier.height(8.dp))

Expand All @@ -106,36 +100,38 @@ fun main() = application {

Spacer(modifier = Modifier.size(16.dp))

CodeTextView(highlights = highlights)

Spacer(modifier = Modifier.size(16.dp))

Divider()

Spacer(modifier = Modifier.size(16.dp))

Text("Edit this...")
CodeEditText(
highlights = highlights,
onValueChange = { textValue ->
highlightsState.value = highlights.getBuilder()
.code(textValue)
.build()
},
colors = TextFieldDefaults.colors(
unfocusedContainerColor = Color.Transparent,
focusedContainerColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
errorIndicatorColor = Color.Transparent,
),
)
Row(
modifier = Modifier
.weight(1f)
.fillMaxWidth(),
) {
CodeTextView(
modifier = Modifier.weight(1f),
highlights = highlights,
)
VerticalDivider(Modifier.padding(8.dp))
CodeEditText(
modifier = Modifier.weight(1f),
label = { Text("Edit code") },
highlights = highlights,
onValueChange = { textValue ->
highlightsState.value = highlights.getBuilder()
.code(textValue)
.build()
},
colors = TextFieldDefaults.colors(
unfocusedContainerColor = Color.Transparent,
focusedContainerColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
disabledIndicatorColor = Color.Transparent,
errorIndicatorColor = Color.Transparent,
),
)
}

Spacer(modifier = Modifier.size(16.dp))

Spacer(modifier = Modifier.weight(1f))

Dropdown(
options = SyntaxThemes.getNames(),
selected = SyntaxThemes.themes().keys.indexOf(highlights.getTheme().key),
Expand Down
2 changes: 1 addition & 1 deletion 2 gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ kotlinMultiplatform = "2.0.20"
compose = "1.6.11"
androidLibrary = "8.6.1"
kodeview = "0.8.0"
highlights = "0.7.1"
highlights = "1.0.0"
composeMaterial = "1.4.0"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import androidx.compose.material.TextField
import androidx.compose.material.TextFieldColors
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import calculateFieldPhraseUpdate
import copySpanStyles
import dev.snipme.highlights.DefaultHighlightsResultListener
import updateIndentations
import dev.snipme.highlights.Highlights
import dev.snipme.highlights.model.CodeHighlight
import generateAnnotatedString

@Composable
Expand All @@ -28,7 +32,7 @@ fun CodeEditText(
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
translateTabToSpaces: Boolean = true,
handleIndentations: Boolean = true,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
Expand All @@ -47,24 +51,37 @@ fun CodeEditText(
) {
val currentText = remember {
mutableStateOf(
TextFieldValue()
TextFieldValue(
AnnotatedString(highlights.getCode())
)
)
}

LaunchedEffect(highlights) {
highlights.getHighlightsAsync(object : DefaultHighlightsResultListener() {
override fun onSuccess(result: List<CodeHighlight>) {
currentText.value = currentText.value.copy(
annotatedString = result.generateAnnotatedString(currentText.value.text),
)
}
})
}

fun updateNewValue(change: TextFieldValue) {
val updated = change.updateIndentations(handleIndentations)
if (updated.text != currentText.value.text) {
onValueChange(updated.text)
}

currentText.value = updated.copySpanStyles(
currentText.value
)
}

TextField(
modifier = modifier.fillMaxWidth(),
onValueChange = {
val fieldUpdate = it.calculateFieldPhraseUpdate(translateTabToSpaces)
currentText.value = fieldUpdate
onValueChange(fieldUpdate.text)
},
value = TextFieldValue(
selection = currentText.value.selection,
composition = currentText.value.composition,
annotatedString = buildAnnotatedString {
generateAnnotatedString(highlights)
},
),
onValueChange = ::updateNewValue,
value = currentText.value,
enabled = enabled,
readOnly = readOnly,
textStyle = textStyle,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
package dev.snipme.kodeview.view

import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.AnnotatedString
import dev.snipme.highlights.Highlights
import dev.snipme.highlights.model.BoldHighlight
import dev.snipme.highlights.model.ColorHighlight
import generateAnnotatedString

@Composable
fun CodeTextView(
modifier: Modifier = Modifier.background(Color.Transparent),
highlights: Highlights
) {
Surface {
Text(
modifier = modifier,
text = buildAnnotatedString {
append(highlights.getCode())
var textState by remember {
mutableStateOf(AnnotatedString(highlights.getCode()))
}

highlights.getHighlights()
.filterIsInstance<ColorHighlight>()
.forEach {
addStyle(
SpanStyle(color = Color(it.rgb).copy(alpha = 1f)),
start = it.location.start,
end = it.location.end,
)
}
LaunchedEffect(highlights) {
textState = highlights
.getHighlights()
.generateAnnotatedString(highlights.getCode())
}

highlights.getHighlights()
.filterIsInstance<BoldHighlight>()
.forEach {
addStyle(
SpanStyle(fontWeight = FontWeight.Bold),
start = it.location.start,
end = it.location.end,
)
}
})
Surface(
modifier = modifier,
color = Color.Transparent
) {
Text(
modifier = modifier
.verticalScroll(rememberScrollState())
.horizontalScroll(rememberScrollState()),
text = textState
)
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.