diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguage.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguage.kt deleted file mode 100644 index 89c70c440d..0000000000 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguage.kt +++ /dev/null @@ -1,166 +0,0 @@ -package org.hyperskill.app.android.code.presentation.model - -import android.content.Context -import android.os.Parcel -import android.os.Parcelable -import org.hyperskill.app.android.R - -enum class ProgrammingLanguage(val serverPrintableName: String) : Parcelable { - PYTHON("python3"), - CPP11("c++11"), - CPP("c++"), - C("c"), - HASKELL("haskell"), - HASKELL7("haskell 7.10"), - HASKELL8("haskell 8.0"), - JAVA("java"), - JAVA8("java8"), - OCTAVE("octave"), - ASM32("asm32"), - ASM64("asm64"), - SHELL("shell"), - RUST("rust"), - R("r"), - RUBY("ruby"), - CLOJURE("clojure"), - CS("mono c#"), - JAVASCRIPT("javascript"), - SCALA("scala"), - KOTLIN("kotlin"), - GO("go"), - PASCAL("pascalabc"), - PERL("perl"), - SQL("sql"); - - override fun describeContents(): Int = 0 - - override fun writeToParcel(parcel: Parcel, flags: Int) { - parcel.writeInt(ordinal) - } - - companion object CREATOR : Parcelable.Creator { - - override fun createFromParcel(parcel: Parcel): ProgrammingLanguage = - values()[parcel.readInt()] - - override fun newArray(size: Int): Array = - arrayOfNulls(size) - - // make it public and resolve highlighting - @Suppress("unused") - private fun highlighting(serverName: String) { - when (serverNameToLanguage(serverName)) { - PYTHON -> TODO() - CPP11 -> TODO() - CPP -> TODO() - C -> TODO() - HASKELL -> TODO() - HASKELL7 -> TODO() - HASKELL8 -> TODO() - JAVA -> TODO() - JAVA8 -> TODO() - OCTAVE -> TODO() - ASM32 -> TODO() - ASM64 -> TODO() - SHELL -> TODO() - RUST -> TODO() - R -> TODO() - RUBY -> TODO() - CLOJURE -> TODO() - CS -> TODO() - JAVASCRIPT -> TODO() - SCALA -> TODO() - KOTLIN -> TODO() - GO -> TODO() - PASCAL -> TODO() - PERL -> TODO() - SQL -> TODO() - null -> TODO() - } - } - } -} - -private fun serverNameToLanguage(serverName: String): ProgrammingLanguage? = - ProgrammingLanguage.values() - .find { - it.serverPrintableName.equals(serverName, ignoreCase = true) - } - -fun symbolsForLanguage(lang: String, context: Context): Array { - val programmingLanguage = serverNameToLanguage(lang) - with(context.resources) { - return when (programmingLanguage) { - ProgrammingLanguage.PYTHON -> - getStringArray(R.array.frequent_symbols_py) - ProgrammingLanguage.CPP11, ProgrammingLanguage.CPP, ProgrammingLanguage.C -> - getStringArray(R.array.frequent_symbols_cpp) - ProgrammingLanguage.HASKELL, ProgrammingLanguage.HASKELL7, ProgrammingLanguage.HASKELL8 -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.JAVA, ProgrammingLanguage.JAVA8 -> - getStringArray(R.array.frequent_symbols_java) - ProgrammingLanguage.OCTAVE -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.ASM32, ProgrammingLanguage.ASM64 -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.SHELL -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.RUST -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.R -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.RUBY -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.CLOJURE -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.CS -> - getStringArray(R.array.frequent_symbols_cs) - ProgrammingLanguage.JAVASCRIPT -> - getStringArray(R.array.frequent_symbols_js) - ProgrammingLanguage.SCALA -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.KOTLIN -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.GO -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.PASCAL -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.PERL -> - getStringArray(R.array.frequent_symbols_default) - ProgrammingLanguage.SQL -> - getStringArray(R.array.frequent_symbols_sql) - null -> - getStringArray(R.array.frequent_symbols_default) - } - } -} - -fun extensionForLanguage(lang: String): String = - when (serverNameToLanguage(lang)) { - ProgrammingLanguage.PYTHON -> "py" - ProgrammingLanguage.CPP11, - ProgrammingLanguage.CPP, - ProgrammingLanguage.C -> "cpp" - ProgrammingLanguage.HASKELL, - ProgrammingLanguage.HASKELL7, - ProgrammingLanguage.HASKELL8 -> "hs" - ProgrammingLanguage.JAVA, - ProgrammingLanguage.JAVA8 -> "java" - ProgrammingLanguage.OCTAVE -> "matlab" - ProgrammingLanguage.ASM32, - ProgrammingLanguage.ASM64 -> "asm" - ProgrammingLanguage.SHELL -> "sh" - ProgrammingLanguage.RUST -> "rust" - ProgrammingLanguage.R -> "r" - ProgrammingLanguage.RUBY -> "rb" - ProgrammingLanguage.CLOJURE -> "clj" - ProgrammingLanguage.CS -> "cs" - ProgrammingLanguage.JAVASCRIPT -> "js" - ProgrammingLanguage.SCALA -> "scala" - ProgrammingLanguage.KOTLIN -> "kt" - ProgrammingLanguage.GO -> "go" - ProgrammingLanguage.PASCAL -> "pascal" - ProgrammingLanguage.PERL -> "perl" - ProgrammingLanguage.SQL -> "sql" - null -> "" - } \ No newline at end of file diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguageExtension.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguageExtension.kt new file mode 100644 index 0000000000..576c10d1f1 --- /dev/null +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/code/presentation/model/ProgrammingLanguageExtension.kt @@ -0,0 +1,46 @@ +package org.hyperskill.app.android.code.presentation.model + +import android.content.Context +import org.hyperskill.app.android.R +import org.hyperskill.app.code.domain.model.ProgrammingLanguage +/* ktlint-disable */ +import org.hyperskill.app.code.domain.model.ProgrammingLanguage.* + +fun ProgrammingLanguage?.getSymbols(context: Context): Array = + when (this) { + PYTHON, PYTHON3, PYTHON3_1, PYTHON3_11 -> + R.array.frequent_symbols_py + CPP, CPP11, CPP20, C, C_VALGRIND -> + R.array.frequent_symbols_cpp + JAVA, JAVA8, JAVA9, JAVA11, JAVA17 -> + R.array.frequent_symbols_java + CS, CS_MONO -> + R.array.frequent_symbols_cs + JAVASCRIPT, TYPESCRIPT -> + R.array.frequent_symbols_js + SQL -> + R.array.frequent_symbols_sql + PHP -> + R.array.frequent_symbols_php + + HASKELL, HASKELL7, HASKELL8, HASKELL8_8, + OCTAVE, + ASM32, ASM64, + SHELL, + RUST, + R, + RUBY, + CLOJURE, + SCALA, SCALA3, + KOTLIN, + GO, + PASCAL, + PERL, + SWIFT, + JULIA, + DART, + null -> R.array.frequent_symbols_default + }.let(context.resources::getStringArray) + +fun symbolsForLanguage(lang: String, context: Context): Array = + ProgrammingLanguage.of(lang).getSymbols(context) \ No newline at end of file diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeLayoutDelegate.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeLayoutDelegate.kt index 3efa36b0ab..5d70ff4dcc 100644 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeLayoutDelegate.kt +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeLayoutDelegate.kt @@ -1,9 +1,9 @@ package org.hyperskill.app.android.step_quiz_code.view.delegate -import org.hyperskill.app.android.code.presentation.model.extensionForLanguage import org.hyperskill.app.android.code.view.adapter.CodeToolbarAdapter import org.hyperskill.app.android.code.view.widget.CodeEditorLayout import org.hyperskill.app.android.step_quiz_code.view.model.config.CodeStepQuizConfig +import org.hyperskill.app.code.domain.model.fileExtensionForLanguage class CodeLayoutDelegate( private val codeLayout: CodeEditorLayout, @@ -16,7 +16,7 @@ class CodeLayoutDelegate( * if [code] is null then [CodeStepQuizConfig.initialCode] will be used */ fun setLanguage(lang: String, code: String? = null) { - codeLayout.langExtension = extensionForLanguage(lang) + codeLayout.langExtension = fileExtensionForLanguage(lang) codeLayout.setTextIfChanged(code ?: config.initialCode) codeToolbarAdapter?.setLanguage(lang) } diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeQuizInstructionDelegate.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeQuizInstructionDelegate.kt index c34768ff83..36d7de4ff1 100644 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeQuizInstructionDelegate.kt +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/delegate/CodeQuizInstructionDelegate.kt @@ -9,11 +9,11 @@ import androidx.recyclerview.widget.RecyclerView import com.google.android.material.card.MaterialCardView import com.google.android.material.divider.MaterialDividerItemDecoration import org.hyperskill.app.android.R -import org.hyperskill.app.android.code.presentation.model.ProgrammingLanguage import org.hyperskill.app.android.step.view.delegate.CollapsibleStepBlockDelegate import org.hyperskill.app.android.step_quiz_code.view.adapter.CodeDetailSampleAdapterDelegate import org.hyperskill.app.android.step_quiz_code.view.model.CodeDetail import org.hyperskill.app.android.ui.custom.ArrowImageView +import org.hyperskill.app.code.domain.model.ProgrammingLanguage import ru.nobird.android.ui.adapters.DefaultDelegateAdapter class CodeQuizInstructionDelegate( @@ -62,7 +62,7 @@ class CodeQuizInstructionDelegate( } fun setCodeDetailsData(details: List, lang: String?) { - if (lang == ProgrammingLanguage.SQL.serverPrintableName) { + if (lang == ProgrammingLanguage.SQL.languageName) { detailsContainerView.isVisible = false } else { stepQuizCodeDetailsAdapter.items = details diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/model/config/SqlCodeStepConfig.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/model/config/SqlCodeStepConfig.kt index 7fc44f5baf..ce7217cf5e 100644 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/model/config/SqlCodeStepConfig.kt +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_code/view/model/config/SqlCodeStepConfig.kt @@ -1,7 +1,7 @@ package org.hyperskill.app.android.step_quiz_code.view.model.config -import org.hyperskill.app.android.code.presentation.model.ProgrammingLanguage import org.hyperskill.app.android.step_quiz_code.view.model.CodeDetail +import org.hyperskill.app.code.domain.model.ProgrammingLanguage import org.hyperskill.app.step.domain.model.Step import org.hyperskill.app.step_quiz.domain.model.submissions.Reply import org.hyperskill.app.step_quiz.domain.model.submissions.Submission @@ -9,7 +9,7 @@ import org.hyperskill.app.step_quiz.domain.model.submissions.Submission class SqlCodeStepConfig(private val step: Step) : CodeStepQuizConfig { override val langName: String - get() = ProgrammingLanguage.SQL.serverPrintableName + get() = ProgrammingLanguage.SQL.languageName override val displayedLangName: String? get() = null diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_fullscreen_code/dialog/CodeStepQuizFullScreenDialogFragment.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_fullscreen_code/dialog/CodeStepQuizFullScreenDialogFragment.kt index c2eb92659c..dfae9f8224 100644 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_fullscreen_code/dialog/CodeStepQuizFullScreenDialogFragment.kt +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_fullscreen_code/dialog/CodeStepQuizFullScreenDialogFragment.kt @@ -17,7 +17,6 @@ import by.kirich1409.viewbindingdelegate.viewBinding import com.google.android.material.button.MaterialButton import org.hyperskill.app.android.HyperskillApp import org.hyperskill.app.android.R -import org.hyperskill.app.android.code.presentation.model.ProgrammingLanguage import org.hyperskill.app.android.code.util.CodeEditorKeyboardExtensionUtil import org.hyperskill.app.android.code.view.adapter.CodeToolbarAdapter import org.hyperskill.app.android.code.view.widget.CodeEditorLayout @@ -32,6 +31,7 @@ import org.hyperskill.app.android.step_quiz_code.view.delegate.CodeQuizInstructi import org.hyperskill.app.android.step_quiz_code.view.model.CodeStepQuizConfigFactory import org.hyperskill.app.android.step_quiz_code.view.model.config.CodeStepQuizConfig import org.hyperskill.app.android.step_quiz_fullscreen_code.adapter.CodeStepQuizFullScreenPagerAdapter +import org.hyperskill.app.code.domain.model.ProgrammingLanguage import org.hyperskill.app.step.domain.model.Step import org.hyperskill.app.step_quiz.view.mapper.StepQuizStatsTextMapper import ru.nobird.android.view.base.ui.extension.argument @@ -342,7 +342,7 @@ class CodeStepQuizFullScreenDialogFragment : DialogFragment() { val isShowRetryButton: Boolean ) { val titleRes: Int - get() = if (lang == ProgrammingLanguage.SQL.serverPrintableName) { + get() = if (lang == ProgrammingLanguage.SQL.languageName) { org.hyperskill.app.R.string.step_quiz_sql_title } else { org.hyperskill.app.R.string.step_quiz_code_title diff --git a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_parsons/view/mapper/ParsonsLinesMapper.kt b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_parsons/view/mapper/ParsonsLinesMapper.kt index fc00b6aec8..b6b73b6841 100644 --- a/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_parsons/view/mapper/ParsonsLinesMapper.kt +++ b/androidHyperskillApp/src/main/java/org/hyperskill/app/android/step_quiz_parsons/view/mapper/ParsonsLinesMapper.kt @@ -3,10 +3,10 @@ package org.hyperskill.app.android.step_quiz_parsons.view.mapper import android.text.Spannable import androidx.core.text.HtmlCompat import org.hyperskill.app.android.code.presentation.highlight.prettify.PrettifyParser -import org.hyperskill.app.android.code.presentation.model.extensionForLanguage import org.hyperskill.app.android.code.view.applyPrettifyParseResults import org.hyperskill.app.android.code.view.model.themes.CodeTheme import org.hyperskill.app.android.step_quiz_parsons.view.model.UiParsonsLine +import org.hyperskill.app.code.domain.model.fileExtensionForLanguage import org.hyperskill.app.step.domain.model.Step import org.hyperskill.app.step_quiz.domain.model.attempts.Attempt import org.hyperskill.app.step_quiz.domain.model.submissions.Submission @@ -93,7 +93,7 @@ class ParsonsLinesMapper( val htmlSpannable = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_COMPACT) as Spannable val parsedHtmlString = htmlSpannable.toString() val prettifyParseResult = prettifyParser.parse( - /* fileExtension = */ extensionForLanguage(langName), + /* fileExtension = */ fileExtensionForLanguage(langName), /* content = */ parsedHtmlString ) return htmlSpannable.applyPrettifyParseResults( diff --git a/shared/src/commonMain/kotlin/org/hyperskill/app/code/domain/model/ProgrammingLanguage.kt b/shared/src/commonMain/kotlin/org/hyperskill/app/code/domain/model/ProgrammingLanguage.kt new file mode 100644 index 0000000000..2d9b29a462 --- /dev/null +++ b/shared/src/commonMain/kotlin/org/hyperskill/app/code/domain/model/ProgrammingLanguage.kt @@ -0,0 +1,91 @@ +package org.hyperskill.app.code.domain.model + +enum class ProgrammingLanguage(val languageName: String) { + PYTHON("python"), + PYTHON3("python3"), + PYTHON3_1("python3.10"), + PYTHON3_11("python3.11"), + CPP("c++"), + CPP11("c++11"), + CPP20("c++20"), + C("c"), + C_VALGRIND("c_valgrind"), + HASKELL("haskell"), + HASKELL7("haskell 7.10"), + HASKELL8("haskell 8.0"), + HASKELL8_8("haskell 8.8"), + JAVA("java"), + JAVA8("java8"), + JAVA9("java9"), + JAVA11("java11"), + JAVA17("java17"), + OCTAVE("octave"), + ASM32("asm32"), + ASM64("asm64"), + SHELL("shell"), + RUST("rust"), + R("r"), + RUBY("ruby"), + CLOJURE("clojure"), + CS("c#"), + CS_MONO("mono c#"), + JAVASCRIPT("javascript"), + TYPESCRIPT("typescript"), + SCALA("scala"), + SCALA3("scala3"), + KOTLIN("kotlin"), + GO("go"), + PASCAL("pascalabc"), + PERL("perl"), + SQL("sql"), + SWIFT("swift"), + PHP("php"), + JULIA("julia"), + DART("dart"); + + val fileExtension: String + get() = when (this) { + PYTHON, PYTHON3, PYTHON3_1, PYTHON3_11 -> "py" + CPP, CPP11, CPP20, C, C_VALGRIND -> "cpp" + HASKELL, HASKELL7, HASKELL8, HASKELL8_8 -> "hs" + JAVA, JAVA8, JAVA9, JAVA11, JAVA17 -> "java" + OCTAVE -> "matlab" + ASM32, ASM64 -> "asm" + SHELL -> "sh" + RUST -> "rust" + R -> "r" + RUBY -> "rb" + CLOJURE -> "clj" + CS, CS_MONO -> "cs" + JAVASCRIPT -> "js" + TYPESCRIPT -> "ts" + SCALA, SCALA3 -> "scala" + KOTLIN -> "kt" + GO -> "go" + PASCAL -> "pascal" + PERL -> "perl" + SQL -> "sql" + SWIFT -> "swift" + PHP -> "php" + JULIA -> "julia" + DART -> "dart" + } + + companion object { + fun of(languageName: String): ProgrammingLanguage? = + ProgrammingLanguage.values() + .find { + it.languageName.equals(languageName, ignoreCase = true) + } + } +} + +/** + * Returns the file extension for a given programming language. + * + * @param lang The name of the programming language. + * @return The file extension for the given programming language, or an empty string if the language is not found. + * Empty string for not found language is used for compatibility with Android code. + */ +fun fileExtensionForLanguage(lang: String): String = + ProgrammingLanguage.of(lang)?.fileExtension ?: "" \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/org/hyperskill/app/step/domain/model/Block.kt b/shared/src/commonMain/kotlin/org/hyperskill/app/step/domain/model/Block.kt index 09708f1bd5..c90128ae12 100644 --- a/shared/src/commonMain/kotlin/org/hyperskill/app/step/domain/model/Block.kt +++ b/shared/src/commonMain/kotlin/org/hyperskill/app/step/domain/model/Block.kt @@ -2,6 +2,7 @@ package org.hyperskill.app.step.domain.model import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import org.hyperskill.app.code.domain.model.ProgrammingLanguage @Serializable data class Block( @@ -39,4 +40,7 @@ data class Block( val text: String ) } -} \ No newline at end of file +} + +val Block.Options.programmingLanguage: ProgrammingLanguage? + get() = language?.let(ProgrammingLanguage::of) \ No newline at end of file