This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 The Android Open Source Project | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Allow filetype-specific plugins. | |
filetype plugin on | |
" Update faster (mainly for git stuff). | |
set updatetime=100 | |
" Split navigation with tab and backspace. | |
nnoremap <Tab> <c-w>w | |
nnoremap <bs> <c-w>W |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **This text should be bold.** | |
val PATTERN_BOLD = Pattern.compile("^\\*\\*([\\s\\S]+?)\\*\\*(?!\\*)") | |
class BoldRule : Rule(PATTERN_BOLD) { | |
override fun parse(matcher: Matcher, parser: Parser): ParseSpec { | |
val boldNode = StyleNode(StyleSpan(Typeface.BOLD)) | |
val innerParseStartIndex = matcher.start(1) | |
val innerParseEndIndex = matcher.end(1) | |
// Don't actually recursively parse, just return a ParseSpec that informs the Parser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Matches: **This text should be bold.** | |
val PATTERN_BOLD = Pattern.compile("^\\*\\*([\\s\\S]+?)\\*\\*(?!\\*)") | |
class BoldRule : Parser.Rule(PATTERN_BOLD) { | |
override fun parse(matcher: Matcher, parser: Parser): Node { | |
val boldSpan = StyleSpan(Typeface.BOLD) | |
// Recursively parse inside the **delimiters** | |
val childNodes = parser.parse(matcher.group(1)) | |
return StyleNode(StyleSpan(Typeface.BOLD, childNodes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StyleNode(val styles: List<CharacterStyle>) : SpannableRenderableNode() { | |
override fun render(builder: SpannableStringBuilder, context: Context?) { | |
// No matter what happens in this world, never forget where you came from. | |
val startIndex = builder.length | |
// First render all child nodes, as these are the nodes we want to apply the styles to. | |
getChildren()?.forEach { (it as SpannableRenderableNode).render(builder, context) } | |
styles.forEach { builder.setSpan(it, startIndex, builder.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextNode(val content: String) : SpannableRenderableNode() { | |
override fun render(builder: SpannableStringBuilder, context: Context?) { | |
builder.append(content) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (rule in rules) { | |
val matcher = rule.matcher.reset(mutableSource) // Faster than creating a new Matcher | |
if (matcher.find()) { | |
// Do stuff with the rule that matched the text | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (rule in rules) { | |
val matcher = rule.pattern.matcher(mutableSource) | |
if (matcher.find()) { | |
// Do stuff with the rule that matched the text | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parser(private val rules: List<Rule>) { | |
fun parse(source: CharSequence): List<Node> { | |
val ast = ArrayList<Node>() | |
var mutableSource = source | |
while (mutableSource.isNotEmpty()) { | |
// Find a rule that matches the source. | |
for (rule in rules) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Rule(val pattern: Pattern) { | |
abstract fun parse(matcher: Matcher, parser: Parser): Node | |
} |
NewerOlder