Skip to content
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

Feature: Support code language highlighting #18

Merged
merged 8 commits into from
Nov 4, 2020
Merged

Conversation

lytefast
Copy link
Contributor

@lytefast lytefast commented Oct 24, 2020

Adds support to highlight code for Kotlin, Python, Rust, XML, ProtoBuff. Kotlin has some additional support, but it wouldn't be hard to add it for other languages too.

Feature

New CodeRule + CodeNode added to the library.

  • CodeNode does a bit of span-fu, see inlined documentation. Basically it makes sure that styles applied by the CodeNode apply first, then the child styles are applied. This prevents the CodeNode styles from overwriting the styles of the children.
  • Nodes are passed in a CodeStyleProviders which provides it a way of creating styled spannables for the different values. This is how we can configure the styling in the client to use whatever styles we want.

Eventually this should support Textmate language extensions

Adding new language:

Adding simple support for new languages can be done by defining 3 things:

  • line comment prefix
  • built in types
  • keywords

Example of adding a language:

val pythonRules = createGenericCodeRules<R, S>(
    codeStyleProviders,
    additionalRules = listOf(
        createSingleLineCommentPattern("#")
            .toFullMatchRule(codeStyleProviders.commentStyleProvider),
        Pattern.compile("""^@(\w+)""")
            .toFullMatchRule(codeStyleProviders.genericsStyleProvider)),
    definitions = arrayOf("class", "def", "lambda"),
    builtIns = arrayOf("True|False|None"),
    /* keywords */"from|import|global|nonlocal",
    "async|await|class|self|cls|def|lambda",
)

Additional changes

-New StyleNode.Text for a bit more optimized Node structure used for CodeNode

  • added a basic BlockBackgroundNode similar to what we have in discord to the sample app

Screens

image

@lytefast lytefast added the enhancement New feature or request label Oct 24, 2020
@lytefast lytefast requested a review from AndyG October 24, 2020 00:46
@lytefast lytefast self-assigned this Oct 24, 2020
@lytefast lytefast force-pushed the code-language-syntax branch 12 times, most recently from f723261 to cbd2474 Compare October 24, 2020 09:58
@lytefast lytefast changed the base branch from deps-upgrade to dev/deps-upgrade October 24, 2020 23:22
@lytefast lytefast force-pushed the code-language-syntax branch 2 times, most recently from 76f5ff9 to 9671e14 Compare October 25, 2020 00:18
@lytefast lytefast changed the base branch from dev/deps-upgrade to master October 25, 2020 00:19
@lytefast lytefast force-pushed the code-language-syntax branch 5 times, most recently from 5c87693 to 11691d8 Compare October 26, 2020 19:57
@@ -22,4 +20,18 @@ open class Node<R> {
}

open fun render(builder: SpannableStringBuilder, renderContext: R) {}
Copy link
Contributor Author

@lytefast lytefast Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to change the render function here to do what Parent does, but didn't know if that would conflict with any downstream types.

@lytefast lytefast force-pushed the code-language-syntax branch 3 times, most recently from 161e2b3 to 5918d34 Compare October 31, 2020 01:55
@lytefast lytefast force-pushed the code-language-syntax branch from 5918d34 to 99f6b20 Compare November 3, 2020 00:25
Copy link
Contributor Author

@lytefast lytefast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AndyG renamted StyleNode.Text -> StyleNode.TextStyledNode in 09caf3a

@lytefast lytefast force-pushed the code-language-syntax branch 3 times, most recently from 202ad11 to 09caf3a Compare November 3, 2020 02:57
@lytefast lytefast force-pushed the code-language-syntax branch from 09caf3a to b32638f Compare November 4, 2020 19:04
@lytefast lytefast merged commit b5f079c into master Nov 4, 2020
@lytefast lytefast deleted the code-language-syntax branch November 4, 2020 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants