Skip to content

Commit

Permalink
Add spell checking support.
Browse files Browse the repository at this point in the history
  • Loading branch information
iancaffey committed Apr 26, 2024
1 parent a16ca31 commit 6325217
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package software.amazon.smithy.intellij

import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiDocCommentBase
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.spellchecker.inspections.CommentSplitter
import com.intellij.spellchecker.inspections.PlainTextSplitter
import com.intellij.spellchecker.inspections.Splitter
import com.intellij.spellchecker.tokenizer.PsiIdentifierOwnerTokenizer
import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy
import com.intellij.spellchecker.tokenizer.TokenConsumer
import com.intellij.spellchecker.tokenizer.Tokenizer
import software.amazon.smithy.intellij.psi.SmithyString
import software.amazon.smithy.intellij.psi.SmithyTextBlock

/**
* A [SpellcheckingStrategy] for [Smithy](https://awslabs.github.io/smithy) IDL model files.
*
* @author Ian Caffey
* @since 1.0
*/
class SmithySpellcheckingStrategy : SpellcheckingStrategy() {
override fun getTokenizer(element: PsiElement): Tokenizer<out PsiElement> = when (element) {
is PsiComment -> CommentTokenizer
is PsiNameIdentifierOwner -> PsiIdentifierOwnerTokenizer()
is SmithyString -> StringTokenizer
is SmithyTextBlock -> TextBlockTokenizer
else -> EMPTY_TOKENIZER
}
}

abstract class SimpleTokenizer<T : PsiElement> : Tokenizer<T>() {
abstract val splitter: Splitter
abstract fun textRange(t: T): TextRange
override fun tokenize(t: T, consumer: TokenConsumer) {
consumer.consumeToken(t, t.text, false, 0, textRange(t), splitter)
}
}

object CommentTokenizer : SimpleTokenizer<PsiComment>() {
override val splitter = CommentSplitter.getInstance()
override fun textRange(comment: PsiComment) =
TextRange.create(if (comment is PsiDocCommentBase) 3 else 2, comment.textLength)
}

object StringTokenizer : SimpleTokenizer<SmithyString>() {
override val splitter = PlainTextSplitter.getInstance()
override fun textRange(string: SmithyString) = TextRange.create(1, string.textLength - 1)
}

object TextBlockTokenizer : SimpleTokenizer<SmithyTextBlock>() {
override val splitter = PlainTextSplitter.getInstance()
override fun textRange(text: SmithyTextBlock) = TextRange.create(3, text.textLength - 3)
}
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@
<iconProvider implementation="software.amazon.smithy.intellij.SmithyIconProvider"/>
<liveTemplateContext implementation="software.amazon.smithy.intellij.SmithyContextType"/>
<navbar implementation="software.amazon.smithy.intellij.SmithyNavBar"/>
<spellchecker.support language="Smithy"
implementationClass="software.amazon.smithy.intellij.SmithySpellcheckingStrategy"/>
</extensions>
</idea-plugin>
</idea-plugin>

0 comments on commit 6325217

Please sign in to comment.