Skip to content

Commit

Permalink
updates!
Browse files Browse the repository at this point in the history
  • Loading branch information
eminarican committed Jan 29, 2023
1 parent 397e4c1 commit 2243ad9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ tasks {
}

patchPluginXml {
//sinceBuild.set("213")
//untilBuild.set("223.*")
sinceBuild.set("213")
untilBuild.set("231.*")
}

signPlugin {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/eminarican/gopherformat/FormatData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ object FormatData {
const val TAG_START_SIZE = "<>".length
const val TAG_END_SIZE = "</>".length

const val FORMAT_FUNCTION = "Colourf"

val HolderPattern: Pattern = Pattern.compile("%[bcdeEfFgGopqstTUvVxX]")
val TagPattern: Pattern = Pattern.compile("<([\\w-]+)>(.*?)</\\1>")

Expand Down
22 changes: 15 additions & 7 deletions src/main/kotlin/com/eminarican/gopherformat/FormatHelper.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eminarican.gopherformat

import com.goide.highlighting.GoSyntaxHighlightingColors
import com.goide.psi.GoCallExpr
import com.intellij.lang.annotation.AnnotationBuilder
import com.intellij.lang.annotation.AnnotationHolder
Expand Down Expand Up @@ -34,7 +35,7 @@ object FormatHelper {
}
}

fun iteratePlaceholders(
fun iterateSymbols(
text: String, offset: Int,
callback: (range: TextRange) -> Unit
) {
Expand All @@ -43,7 +44,7 @@ object FormatHelper {
}
}

fun iterateSymbols(
fun iterateFunctionSymbols(
expression: GoCallExpr,
callback: (ok: Boolean, range: TextRange, index: Int, count: Int) -> Unit
) {
Expand All @@ -52,13 +53,12 @@ object FormatHelper {

first.value?.string?.let { value ->
val verbCount = value.split(FormatData.HolderPattern).size - 1
if (verbCount < 1) return
val argCount = expressions.size - 1

val argCount = expressions.size - 2
if (verbCount == argCount) return
if (verbCount < 1) return

var count = 0
iteratePlaceholders(value, first.textRange.startOffset.inc()) { range ->
var count = 1
iterateSymbols(value, first.textRange.startOffset.inc()) { range ->
callback(count <= argCount, range, count, argCount)
count++
}
Expand All @@ -72,6 +72,10 @@ object FormatHelper {
}
}

fun isFormatFunction(element: GoCallExpr): Boolean {
return element.expression.text.contains(FormatData.FORMAT_FUNCTION)
}

fun findStringLiterals(element: PsiElement): Collection<PsiElement> {
object : PsiElementProcessor.CollectElements<PsiElement>() {
override fun execute(each: PsiElement): Boolean {
Expand All @@ -96,4 +100,8 @@ object FormatHelper {
fun createWarning(holder: AnnotationHolder, message: String, range: TextRange) {
return holder.newAnnotation(HighlightSeverity.WEAK_WARNING, message).range(range).create()
}

fun highlightPlaceholder(holder: AnnotationHolder, range: TextRange) {
createAnnotation(holder, range).textAttributes(GoSyntaxHighlightingColors.VALID_STRING_ESCAPE).create()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class FunctionAnnotator : Annotator {

override fun annotate(element: PsiElement, holder: AnnotationHolder) {
if (element !is GoCallExpr) return
if (!FormatHelper.isFormatFunction(element)) return

FormatHelper.iterateSymbols(element) { ok, range, index, count ->
FormatHelper.iterateFunctionSymbols(element) { ok, range, index, count ->
if (!ok) addVerbWarning(holder, range, index, count)
FormatHelper.highlightPlaceholder(holder, range)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.eminarican.gopherformat.annotator

import com.eminarican.gopherformat.FormatData
import com.eminarican.gopherformat.FormatHelper
import com.goide.GoLanguage
import com.goide.highlighting.GoSyntaxHighlightingColors
import com.goide.psi.GoCallExpr
import com.goide.psi.GoStringLiteral
import com.intellij.lang.annotation.AnnotationHolder
import com.intellij.lang.annotation.Annotator
import com.intellij.openapi.util.TextRange
Expand All @@ -23,8 +26,10 @@ class LiteralAnnotator : Annotator {
}
}

FormatHelper.iteratePlaceholders(element.text, element.textRange.startOffset) { range ->
highlightPlaceholder(holder, range)
if (element.language is GoLanguage) return

FormatHelper.iterateSymbols(element.text, element.textRange.startOffset) { range ->
FormatHelper.highlightPlaceholder(holder, range)
}
}

Expand All @@ -47,8 +52,4 @@ class LiteralAnnotator : Annotator {
}
return false
}

private fun highlightPlaceholder(holder: AnnotationHolder, range: TextRange) {
FormatHelper.createAnnotation(holder, range).textAttributes(GoSyntaxHighlightingColors.VALID_STRING_ESCAPE).create()
}
}

0 comments on commit 2243ad9

Please sign in to comment.