use of com.dmarcotte.handlebars.HbLanguage in project intellij-plugins by JetBrains.
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 (file.getLanguage() instanceof HbLanguage) {
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, psiElement -> 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.dmarcotte.handlebars.HbLanguage in project intellij-plugins by JetBrains.
the class HbTypedHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
int offset = editor.getCaretModel().getOffset();
if (offset == 0 || offset > editor.getDocument().getTextLength()) {
return Result.CONTINUE;
}
String previousChar = editor.getDocument().getText(new TextRange(offset - 1, offset));
if (file.getLanguage() instanceof HbLanguage) {
PsiDocumentManager.getInstance(project).commitAllDocuments();
// we suppress the built-in "}" auto-complete when we see "{{"
if (c == '{' && previousChar.equals("{")) {
// since the "}" autocomplete is built in to IDEA, we need to hack around it a bit by
// intercepting it before it is inserted, doing the work of inserting for the user
// by inserting the '{' the user just typed...
editor.getDocument().insertString(offset, Character.toString(c));
// ... and position their caret after it as they'd expect...
editor.getCaretModel().moveToOffset(offset + 1);
// ... then finally telling subsequent responses to this charTyped to do nothing
return Result.STOP;
}
}
return Result.CONTINUE;
}
Aggregations