Last active
August 29, 2015 14:27
-
-
Save clojj/bc213a17fae4a07fa2c2 to your computer and use it in GitHub Desktop.
highlight Cursive code
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.actionSystem.LangDataKeys | |
import com.intellij.openapi.editor.Document | |
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.util.Condition | |
import com.intellij.psi.PsiDocumentManager | |
import com.intellij.psi.PsiElement | |
import com.intellij.psi.PsiFile | |
import com.intellij.psi.PsiRecursiveElementWalkingVisitor | |
import com.intellij.psi.codeStyle.CodeStyleManager | |
import com.intellij.psi.search.searches.ReferencesSearch | |
import com.intellij.psi.util.PsiTreeUtil | |
import java.awt.* | |
import java.util.List | |
import static liveplugin.PluginUtil.* | |
def String toHex(String arg) { | |
return arg.bytes.encodeHex() | |
} | |
def colorize(startOffset, length, editor) { | |
def textAttributes = new TextAttributes(Color.BLACK, Color.YELLOW, Color.YELLOW, EffectType.SEARCH_MATCH, Font.PLAIN) | |
editor.markupModel.addRangeHighlighter(startOffset, startOffset + length, 1, textAttributes, HighlighterTargetArea.EXACT_RANGE) | |
} | |
def int getLineNumber(Document document, PsiElement element) { | |
int lineNum = document.getLineNumber(element.getTextRange().getStartOffset()); | |
return lineNum + 1 // shown in editor as 1-based | |
} | |
registerAction("HighlightScopesAction", "ctrl alt shift E", TOOLS_MENU) { AnActionEvent event -> | |
def editor = currentEditorIn(event.project) | |
// 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) | |
Document document = PsiDocumentManager.getInstance(event.project).getDocument(psiFile) | |
def csm = CodeStyleManager.getInstance(event.project) | |
def Condition<PsiElement> condition_ClList = new Condition<PsiElement>() { | |
@Override | |
public boolean value(PsiElement psiElement) { | |
return psiElement.toString().equals("ClList"); | |
} | |
} | |
psiFile.accept(new PsiRecursiveElementWalkingVisitor() { | |
@Override | |
void visitElement(PsiElement element) { | |
def text = element.getText() | |
def offset = element.getTextOffset() | |
if (!element.toString().equals("PsiWhiteSpace")) { | |
if (element.toString().equals("ClList")) { | |
// if (true) { | |
show("" + element + " CLASS " + element.getClass() + " node " + element.getNode() + " " + element.hashCode()) | |
int lineNum = getLineNumber(document, element) | |
show("ELEMENT LINE: " + lineNum) | |
show(" reference " + element.getReference()?.resolve()?.hashCode()) | |
show(" line indent: " + toHex(csm.getLineIndent(psiFile, offset))) | |
def List<String> tokens = text.tokenize('\n') | |
show("tokens by \\n from element.getText(): " + tokens) | |
show(toHex(text)) | |
colorize(offset, element.getTextLength(), editor) | |
show(offset + " length: " + element.getTextLength() + " text: " + text) | |
if (element != null && element.getReference() != null) { | |
def resolved = element.getReference().resolve() | |
if (resolved != null) { | |
def resolvedRef = ReferencesSearch.search(resolved).findFirst().resolve() | |
show(" SEARCH references " + ReferencesSearch.search(resolved).findAll()) | |
show(" first reference class " + resolvedRef.getClass().toString()) | |
show(" first reference by search " + resolvedRef.hashCode()) | |
} | |
} | |
def parentsElem = PsiTreeUtil.findFirstParent(element, condition_ClList) | |
show("parents of element: " + parentsElem) | |
} | |
} | |
else { | |
show("Whitespace " + toHex(text) + " length: " + text.length()) | |
int lineNum = getLineNumber(document, element) | |
show("Whitespace LINE: " + lineNum) | |
} | |
super.visitElement(element) | |
} | |
}) | |
} | |
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