Last active
August 29, 2015 14:26
-
-
Save clojj/b96e8556759036be290c to your computer and use it in GitHub Desktop.
Runs in LivePlugin for IntelliJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.openapi.editor.markup.EffectType | |
import com.intellij.openapi.editor.markup.HighlighterTargetArea | |
import com.intellij.openapi.editor.markup.TextAttributes | |
import com.intellij.openapi.actionSystem.AnActionEvent | |
import com.intellij.openapi.actionSystem.LangDataKeys | |
import com.intellij.psi.PsiElement | |
import com.intellij.psi.PsiFile | |
import com.intellij.psi.PsiRecursiveElementWalkingVisitor | |
import com.intellij.psi.PsiReferenceExpression | |
import com.intellij.psi.search.searches.ReferencesSearch | |
import com.intellij.psi.util.PsiTreeUtil | |
import com.intellij.openapi.util.Conditions | |
import java.awt.* | |
import static liveplugin.PluginUtil.* | |
registerAction("HighlightScopesAction", "ctrl shift alt E", TOOLS_MENU) { AnActionEvent event -> | |
// 1) analyze scopes | |
/* | |
TODO test these: ReferencesSearch.search() PsiTreeUtil.getChildren*() PsiTreeUtil.getParent*() PsiTreeUtil.find*() etc. | |
see: http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/psi_helper_and_utilities.html?search=util | |
also for actions on elements: e.getData(LangDataKeys.PSI_ELEMENT) | |
*/ | |
PsiFile psiFile = event.getData(LangDataKeys.PSI_FILE) | |
psiFile.accept(new PsiRecursiveElementWalkingVisitor() { | |
@Override | |
void visitElement(PsiElement element) { | |
show("" + element + " CLASS " + element.getClass() + " node " + element.getNode() + " " + element.hashCode()) | |
show(" reference " + element.getReference()?.resolve()?.hashCode()) | |
if (element != null && element.getReference() != null) { | |
def resolved = element.getReference().resolve() | |
if (resolved != null) { | |
show(" first reference by search " + ReferencesSearch.search(resolved).findFirst().resolve().hashCode()) | |
// show(" SEARCH references " + ReferencesSearch.search(resolved).findAll()) | |
} | |
} | |
def parentsElem = PsiTreeUtil.getParents(element, null) | |
show("parents of element " + parentsElem) | |
def ref = element.getReference()?.resolve() | |
// def parent = PsiTreeUtil.getParentOfType(ref, Class.forName("cursive.psi.impl.list.ClListImpl.class")) | |
if (ref != null) { | |
def parents = PsiTreeUtil.getParents(ref, null) | |
show("parents of reference " + parents) | |
} | |
super.visitElement(element) | |
} | |
}) | |
// 2) colorize ! | |
runDocumentWriteAction(event.project) { | |
def editor = currentEditorIn(event.project) | |
// TODO | |
def from = editor.document.text.length() - 8 | |
def to = editor.document.text.length() | |
def textAttributes = new TextAttributes(Color.BLACK, Color.YELLOW, Color.YELLOW, EffectType.SEARCH_MATCH, Font.PLAIN) | |
editor.markupModel.addRangeHighlighter(from, to, 1, textAttributes, HighlighterTargetArea.EXACT_RANGE) | |
} | |
} | |
show("Loaded 'HighlightScopesAction'<br/>Use ctrl+alt+shift+E to highlight scopes") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment