use of com.intellij.psi.PsiElement in project idea-handlebars by dmarcotte.
the class HbEnterHandler method isBetweenHbTags.
/**
* Checks to see if {@code Enter} has been typed while the caret is between an open and close tag pair.
*
* @return true if between open and close tags, false otherwise
*/
private static boolean isBetweenHbTags(Editor editor, PsiFile file, int offset) {
if (offset == 0)
return false;
CharSequence chars = editor.getDocument().getCharsSequence();
if (chars.charAt(offset - 1) != '}')
return false;
EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
HighlighterIterator iterator = highlighter.createIterator(offset - 1);
final PsiElement openerElement = file.findElementAt(iterator.getStart());
PsiElement openTag = HbPsiUtil.findParentOpenTagElement(openerElement);
if (openTag == null) {
return false;
}
iterator.advance();
if (iterator.atEnd()) {
// no more tokens, so certainly no close tag
return false;
}
final PsiElement closerElement = file.findElementAt(iterator.getStart());
PsiElement closeTag = HbPsiUtil.findParentCloseTagElement(closerElement);
// if we got this far, we're between open and close tags iff this is a close tag
return closeTag != null;
}
use of com.intellij.psi.PsiElement in project idea-handlebars by dmarcotte.
the class HbTypedHandler method charTyped.
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
int offset = editor.getCaretModel().getOffset();
FileViewProvider provider = file.getViewProvider();
if (!provider.getBaseLanguage().isKindOf(HbLanguage.INSTANCE)) {
return Result.CONTINUE;
}
if (offset < 2 || offset > editor.getDocument().getTextLength()) {
return Result.CONTINUE;
}
String previousChar = editor.getDocument().getText(new TextRange(offset - 2, offset - 1));
boolean closeBraceCompleted = false;
if (provider instanceof HbFileViewProvider) {
if (HbConfig.isAutocompleteMustachesEnabled() && c == '}' && !previousChar.equals("}")) {
// we may be able to complete the second brace
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement elementAt = provider.findElementAt(offset - 1, provider.getBaseLanguage());
ASTNode node = elementAt != null ? elementAt.getNode() : null;
if (node != null && node.getElementType() == HbTokenTypes.INVALID) {
// we should be looking at the beginning of a close brace. Find its matching open brace and auto-complete based on its type
PsiElement mustache = PsiTreeUtil.findFirstParent(elementAt, new Condition<PsiElement>() {
@Override
public boolean value(PsiElement psiElement) {
return psiElement instanceof HbMustache;
}
});
if (mustache != null) {
String braceCompleter;
if (mustache.getFirstChild().getNode().getElementType() == HbTokenTypes.OPEN_UNESCAPED) {
// add "}}" to complete the CLOSE_UNESCAPED
braceCompleter = "}}";
} else {
// add "}" to complete the CLOSE
braceCompleter = "}";
}
editor.getDocument().insertString(offset, braceCompleter);
offset = offset + braceCompleter.length();
editor.getCaretModel().moveToOffset(offset);
closeBraceCompleted = true;
}
}
}
}
// if we just completed a close brace or the user just typed one, we may have some business to attend to
if (closeBraceCompleted || (c == '}' && previousChar.equals("}"))) {
autoInsertCloseTag(project, offset, editor, provider);
adjustMustacheFormatting(project, offset, editor, file, provider);
} else if (c == '/' && previousChar.equals("{")) {
finishClosingTag(offset, editor, provider);
}
return Result.CONTINUE;
}
use of com.intellij.psi.PsiElement in project idea-handlebars by dmarcotte.
the class HbTypedHandler method autoInsertCloseTag.
/**
* When appropriate, auto-inserts Handlebars close tags. i.e. When "{{#tagId}}" or "{{^tagId}} is typed,
* {{/tagId}} is automatically inserted
*/
private static void autoInsertCloseTag(Project project, int offset, Editor editor, FileViewProvider provider) {
if (!HbConfig.isAutoGenerateCloseTagEnabled()) {
return;
}
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiElement elementAtCaret = provider.findElementAt(offset - 1, HbLanguage.class);
if (elementAtCaret == null || elementAtCaret.getNode().getElementType() != HbTokenTypes.CLOSE) {
return;
}
HbOpenBlockMustache openTag = HbPsiUtil.findParentOpenTagElement(elementAtCaret);
if (openTag != null && openTag.getChildren().length > 1) {
HbMustacheName mustacheName = PsiTreeUtil.findChildOfType(openTag, HbMustacheName.class);
if (mustacheName != null) {
// insert the corresponding close tag
editor.getDocument().insertString(offset, "{{/" + mustacheName.getText() + "}}");
}
}
}
use of com.intellij.psi.PsiElement in project idea-handlebars by dmarcotte.
the class HbFoldingBuilder method appendDescriptors.
private void appendDescriptors(PsiElement psi, List<FoldingDescriptor> descriptors, Document document) {
if (isSingleLine(psi, document)) {
return;
}
if (HbTokenTypes.COMMENT == psi.getNode().getElementType()) {
ASTNode commentNode = psi.getNode();
String commentText = commentNode.getText();
// tags before we allow folding
if (commentText.length() > 6 && commentText.substring(0, 3).equals("{{!") && commentText.substring(commentText.length() - 2, commentText.length()).equals("}}")) {
TextRange range = new TextRange(commentNode.getTextRange().getStartOffset() + 3, commentNode.getTextRange().getEndOffset() - 2);
descriptors.add(new FoldingDescriptor(commentNode, range));
}
}
if (psi instanceof HbBlockWrapper) {
PsiElement endOpenBlockStache = getOpenBlockCloseStacheElement(psi.getFirstChild());
PsiElement endCloseBlockStache = getCloseBlockCloseStacheElement(psi.getLastChild());
// if we've got a well formed block with the open and close elems we need, define a region to fold
if (endOpenBlockStache != null && endCloseBlockStache != null) {
int endOfFirstOpenStacheLine = document.getLineEndOffset(document.getLineNumber(psi.getTextRange().getStartOffset()));
// we set the start of the text we'll fold to be just before the close braces of the open stache,
// or, if the open stache spans multiple lines, to the end of the first line
int foldingRangeStartOffset = Math.min(endOpenBlockStache.getTextRange().getStartOffset(), endOfFirstOpenStacheLine);
// we set the end of the text we'll fold to be just before the final close braces in this block
int foldingRangeEndOffset = endCloseBlockStache.getTextRange().getStartOffset();
TextRange range = new TextRange(foldingRangeStartOffset, foldingRangeEndOffset);
descriptors.add(new FoldingDescriptor(psi, range));
}
}
PsiElement child = psi.getFirstChild();
while (child != null) {
appendDescriptors(child, descriptors, document);
child = child.getNextSibling();
}
}
use of com.intellij.psi.PsiElement in project buck by facebook.
the class BuckBuildUtil method getPropertyValue.
/**
* Get the value of a property in a specific buck rule body.
* TODO(#7908675): We should use Buck's own classes for it.
*/
public static String getPropertyValue(BuckRuleBody body, String name) {
if (body == null) {
return null;
}
PsiElement[] children = body.getChildren();
for (PsiElement child : children) {
if (BuckPsiUtils.testType(child, BuckTypes.PROPERTY)) {
PsiElement lvalue = child.getFirstChild();
PsiElement propertyName = lvalue.getFirstChild();
if (propertyName != null && propertyName.getText().equals(name)) {
BuckExpression expression = (BuckExpression) BuckPsiUtils.findChildWithType(child, BuckTypes.EXPRESSION);
return expression != null ? BuckPsiUtils.getStringValueFromExpression(expression) : null;
}
}
}
return null;
}
Aggregations