Skip to content

Commit

Permalink
#27 Initial commit for comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
izhangzhihao committed Aug 8, 2020
1 parent 090cd57 commit 9bc2daf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.izhangzhihao.rainbow.fart

import com.github.izhangzhihao.rainbow.fart.RainbowFartTypedHandler.FartTypedHandler.releaseFart
import com.github.izhangzhihao.rainbow.fart.settings.RainbowFartSettings
import com.intellij.codeInsight.lookup.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand All @@ -10,6 +11,9 @@ import java.beans.PropertyChangeListener
class RainbowFartLookupHandler : LookupListener {
override fun itemSelected(event: LookupEvent) {
try {
if (!RainbowFartSettings.instance.isRainbowFartEnabled) {
return
}
val currentItem: LookupElement = event.lookup.currentItem ?: return
val lookupString: String = currentItem.lookupString
if (BuildInContributes.buildInContributes.containsKey(lookupString)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package com.github.izhangzhihao.rainbow.fart

import com.github.izhangzhihao.rainbow.fart.RainbowFartTypedHandler.FartTypedHandler.isTypingInsideComment
import com.github.izhangzhihao.rainbow.fart.RainbowFartTypedHandler.FartTypedHandler.releaseFart
import com.github.izhangzhihao.rainbow.fart.settings.RainbowFartSettings
import com.intellij.codeInsight.template.impl.editorActions.TypedActionHandlerBase
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.TypedActionHandler
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.elementType
import javazoom.jl.player.Player
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File


class RainbowFartTypedHandler(originalHandler: TypedActionHandler) : TypedActionHandlerBase(originalHandler) {

private var candidates: MutableList<Char> = mutableListOf()

override fun execute(editor: Editor, charTyped: Char, dataContext: DataContext) {
if (!RainbowFartSettings.instance.isRainbowFartEnabled) {
this.myOriginalHandler?.execute(editor, charTyped, dataContext)
return
}

try {
if (!RainbowFartSettings.instance.isRainbowFartEnabled) {
return
}
val project = editor.project

if (project != null) {
//val language = PsiUtilBase.getLanguageInEditor(editor, project)
val virtualFile = FileDocumentManager.getInstance().getFile(editor.document)
if (virtualFile != null) {
val file = PsiManager.getInstance(project).findFile(virtualFile)
if (file != null && isTypingInsideComment(editor, file)) {
return
}
}
}

candidates.add(charTyped)
val str = candidates.joinToString("")
BuildInContributes.buildInContributesSeq
Expand Down Expand Up @@ -66,5 +85,27 @@ class RainbowFartTypedHandler(originalHandler: TypedActionHandler) : TypedAction
player.play()
player.close()
}

fun isTypingInsideComment(editor: Editor, file: PsiFile): Boolean {
val provider = file.viewProvider
val offset = editor.caretModel.offset
val elementAtCaret: PsiElement?
elementAtCaret = if (offset < editor.document.textLength) {
provider.findElementAt(offset)
} else {
provider.findElementAt(editor.document.textLength - 1)
}
var element = elementAtCaret
while (element is PsiWhiteSpace) {
element = element.getPrevSibling()
}

return element.elementType.toString().contains("comment", true)
// if (element == null) {
// return false
// }
// val node: ASTNode? = element.node
// return node != null //&& JavaDocTokenType.ALL_JAVADOC_TOKENS.contains(node.getElementType())
}
}
}

0 comments on commit 9bc2daf

Please sign in to comment.