[CMI] Parse description of meta info and save it to ParsedCodeMetaInfo

This commit is contained in:
Dmitriy Novozhilov
2020-11-05 13:41:07 +03:00
parent 2bbab3170f
commit 87a6a66953
2 changed files with 22 additions and 11 deletions
@@ -8,11 +8,16 @@ package org.jetbrains.kotlin.codeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo
object CodeMetaInfoParser { object CodeMetaInfoParser {
private val openingRegex = "(<!([^>]+?)!>)".toRegex() private val openingRegex = """(<!([^>]+?)!>)""".toRegex()
private val closingRegex = "(<!>)".toRegex() private val closingRegex = """(<!>)""".toRegex()
private val descriptionRegex = "\\(\".*?\"\\)".toRegex() /*
private val platformRegex = "\\{(.+)}".toRegex() * ([\S&&[^,(){}]]+) -- tag, allowing all non-space characters except bracers and curly bracers
* ([{](.*?)[}])? -- list of platforms
* (\("(.*?)"\))? -- arguments of meta info
* (, )? -- possible separator between different infos
*/
private val tagRegex = """([\S&&[^,(){}]]+)([{](.*?)[}])?(\("(.*?)"\))?(, )?""".toRegex()
fun getCodeMetaInfoFromText(renderedText: String): List<ParsedCodeMetaInfo> { fun getCodeMetaInfoFromText(renderedText: String): List<ParsedCodeMetaInfo> {
var text = renderedText var text = renderedText
@@ -48,14 +53,19 @@ object CodeMetaInfoParser {
while (!openingMatchResults.isEmpty()) { while (!openingMatchResults.isEmpty()) {
val openingMatchResult = openingMatchResults.removeLast() val openingMatchResult = openingMatchResults.removeLast()
val closingMatchResult = closingMatchResults.removeLast() val closingMatchResult = closingMatchResults.removeLast()
val metaInfoWithoutParams = openingMatchResult.groups[2]!!.value.replace(descriptionRegex, "") val allMetaInfos = openingMatchResult.groups[2]!!.value
metaInfoWithoutParams.split(",").forEach { tagRegex.findAll(allMetaInfos).map { it.groups }.forEach {
val tag = platformRegex.replace(it, "").trim() val tag = it[1]!!.value
val platforms = val platforms = it[3]?.value?.split(";") ?: emptyList()
if (platformRegex.containsMatchIn(it)) platformRegex.find(it)!!.destructured.component1().split(";") else listOf() val description = it[5]?.value
result.add( result.add(
ParsedCodeMetaInfo( ParsedCodeMetaInfo(
openingMatchResult.range.first, closingMatchResult.range.first, platforms.toMutableList(), tag, openingMatchResult.range.first,
closingMatchResult.range.first,
platforms.toMutableList(),
tag,
description
) )
) )
} }
@@ -11,7 +11,8 @@ class ParsedCodeMetaInfo(
override val start: Int, override val start: Int,
override val end: Int, override val end: Int,
override val platforms: MutableList<String>, override val platforms: MutableList<String>,
private val tag: String private val tag: String,
val description: String?
) : CodeMetaInfo { ) : CodeMetaInfo {
override val renderConfiguration = object : AbstractCodeMetaInfoRenderConfiguration(false) {} override val renderConfiguration = object : AbstractCodeMetaInfoRenderConfiguration(false) {}