Skip to content

Commit

Permalink
chore(shirelang): add spike comment injection and run line markers #133
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 16, 2024
1 parent 100394e commit eb2fbc9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.phodal.shirelang

import com.intellij.lang.injection.MultiHostInjector
import com.intellij.lang.injection.MultiHostRegistrar
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiDocCommentBase
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiLanguageInjectionHost
import org.jetbrains.annotations.NotNull
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
* Comment injector for Shire language
*/
class ShireInCommentInjector : MultiHostInjector {
private val SHIRE_CODE_PATTERN: Pattern = Pattern.compile("```shire\\s*(.*?)\\s*```", Pattern.DOTALL)

override fun getLanguagesToInject(@NotNull registrar: MultiHostRegistrar, @NotNull host: PsiElement) {
if (host !is PsiDocCommentBase) {
return
}

val commentText = host.getText()
val matcher: Matcher = SHIRE_CODE_PATTERN.matcher(commentText)

if (host as? PsiLanguageInjectionHost == null) {
return
}

/// refs to : https://github.com/intellij-rust/intellij-rust/blob/c6657c02bb62075bf7b7ceb84d000f93dda34dc1/src/main/kotlin/org/rust/ide/injected/RsDoctestLanguageInjector.kt
// while (matcher.find()) {
// val start: Int = matcher.start(1)
// val end: Int = matcher.end(1)
//
// registrar.startInjecting(ShireLanguage.INSTANCE)
// .addPlace(null, null, host as PsiLanguageInjectionHost, TextRange.create(start, end))
// .doneInjecting()
// }
}

@NotNull
override fun elementsToInjectIn(): List<Class<out PsiElement>?> {
return listOf(PsiComment::class.java)
}

companion object {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.phodal.shirelang.run

import com.phodal.shirelang.actions.ShireRunFileAction
import com.intellij.execution.lineMarker.RunLineMarkerContributor
import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.project.DumbAware
import com.intellij.psi.PsiElement
import com.phodal.shirelang.ShireBundle
import com.intellij.psi.PsiDocCommentBase

class ShireCommentRunLineMarkersProvider : RunLineMarkerContributor(), DumbAware {
override fun getInfo(element: PsiElement): Info? {
if (element !is PsiDocCommentBase) return null
val commentText = element.text
if (!commentText.contains("```shire")) return null

val actions = arrayOf<AnAction>(ActionManager.getInstance().getAction(ShireRunFileAction.ID))

return Info(
AllIcons.RunConfigurations.TestState.Run,
{ ShireBundle.message("shire.line.marker.run.comment") },
*actions
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,4 @@ class ShireRunLineMarkersProvider : RunLineMarkerContributor(), DumbAware {
*actions
)
}

private fun getCommentInfo(element: PsiElement): Info? {
if (element !is PsiComment) return null
val commentText = element.text
if (!commentText.contains("```shire")) return null

val actions = arrayOf<AnAction>(ActionManager.getInstance().getAction(ShireRunFileAction.ID))

return Info(
AllIcons.RunConfigurations.TestState.Run,
{ ShireBundle.message("shire.line.marker.run.comment") },
*actions
)
}

fun extractShireCodeFromComment(comment: String): String {
val codeBlockStart = comment.indexOf("```shire")
if (codeBlockStart == -1) return ""

val codeBlockEnd = comment.indexOf("```", codeBlockStart + 7)
if (codeBlockEnd == -1) return ""

return comment.substring(codeBlockStart + 7, codeBlockEnd).trim()
}
}
3 changes: 3 additions & 0 deletions shirelang/src/main/resources/com.phodal.shirelang.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<!-- <lang.formatter language="Shire" implementationClass="com.phodal.shirelang.formatter.ShireFormattingModelBuilder"/>-->

<languageInjector implementation="com.phodal.shirelang.ShireLanguageInjector"/>
<!-- <multiHostInjector implementation="com.phodal.shirelang.ShireInCommentInjector"/>-->

<configurationType implementation="com.phodal.shirelang.run.ShireConfigurationType"/>
<programRunner implementation="com.phodal.shirelang.run.ShireProgramRunner"/>
Expand All @@ -55,6 +56,8 @@
<runConfigurationProducer implementation="com.phodal.shirelang.run.ShireRunConfigurationProducer"/>
<runLineMarkerContributor language="Shire"
implementationClass="com.phodal.shirelang.run.ShireRunLineMarkersProvider"/>
<!-- <runLineMarkerContributor language="Shire"-->
<!-- implementationClass="com.phodal.shirelang.run.ShireCommentRunLineMarkersProvider"/>-->

<codeInsight.lineMarkerProvider language="Shire"
implementationClass="com.phodal.shirelang.run.ShireSyntaxLineMarkerProvider"/>
Expand Down

0 comments on commit eb2fbc9

Please sign in to comment.