use of ool.intellij.plugin.file.OxyTemplateFileViewProvider in project oxy-template-support-plugin by mutant-industries.
the class BlockMarkerHandler method charTyped.
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
PsiElement elementAt;
FileViewProvider provider = file.getViewProvider();
if (!(provider instanceof OxyTemplateFileViewProvider)) {
return Result.CONTINUE;
}
int offset = editor.getCaretModel().getOffset();
// <% _%> -> <% _ %>
if (c == ' ' && (elementAt = file.getViewProvider().findElementAt(offset, OxyTemplate.INSTANCE)) != null && elementAt.getNode().getElementType() == OxyTemplateTypes.T_CLOSE_BLOCK_MARKER && OxyTemplateParserDefinition.OPEN_BLOCK_MARKERS.contains(elementAt.getParent().getPrevSibling().getNode().getElementType())) {
editor.getDocument().insertString(offset, " ");
editor.getCaretModel().moveToOffset(offset);
return Result.CONTINUE;
}
Pair delimiters = Pair.create("<%", "%>");
int openBraceLength = ((String) delimiters.first).length();
if (offset < openBraceLength) {
return Result.CONTINUE;
}
String previousChars = editor.getDocument().getText(new TextRange(offset - openBraceLength, offset));
// <%_ -> <%_%>
if (((delimiters.first).equals(previousChars)) && ((elementAt = provider.findElementAt(offset, OxyTemplate.INSTANCE)) == null || !OxyTemplateParserDefinition.INNER_JS.contains(elementAt.getNode().getElementType()))) {
if (elementAt != null && elementAt.getText().contains("%>")) {
return Result.CONTINUE;
}
editor.getDocument().insertString(offset, delimiters.second.toString());
}
return Result.CONTINUE;
}
use of ool.intellij.plugin.file.OxyTemplateFileViewProvider in project oxy-template-support-plugin by mutant-industries.
the class TagCloseHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file, FileType fileType) {
FileViewProvider provider = file.getViewProvider();
int offset;
PsiElement elementAt;
if (!(provider instanceof OxyTemplateFileViewProvider) || (offset = editor.getCaretModel().getOffset()) < 1) {
return Result.CONTINUE;
}
if (c == '>' && isOpenMacroEnd(provider.findElementAt(offset - 1, OxyTemplate.INSTANCE))) {
// <m:foo.bar> -> <m:foo.bar></m:foo.bar>
String macroTagToBeClosedName;
editor.getDocument().insertString(offset, ">");
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
elementAt = provider.findElementAt(offset, OxyTemplate.INSTANCE);
if ((macroTagToBeClosedName = OxyTemplateHelper.getPreviousUnclosedMacroTagName(elementAt)) != null) {
String closeTag = "</m:" + macroTagToBeClosedName + ">";
editor.getDocument().insertString(offset + 1, closeTag);
}
editor.getCaretModel().moveToOffset(offset + 1);
return Result.STOP;
} else if (c == '/') {
// <m:foo.bar / -> <m:foo.bar />
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
if (isOpenMacroEnd(provider.findElementAt(offset - 1, OxyTemplate.INSTANCE))) {
editor.getDocument().insertString(offset, "/>");
editor.getCaretModel().moveToOffset(offset + 2);
return Result.STOP;
}
}
return Result.CONTINUE;
}
use of ool.intellij.plugin.file.OxyTemplateFileViewProvider in project oxy-template-support-plugin by mutant-industries.
the class ParamQuoteHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file, FileType fileType) {
FileViewProvider provider = file.getViewProvider();
int offset;
if (!(provider instanceof OxyTemplateFileViewProvider) || (offset = editor.getCaretModel().getOffset()) < 1) {
return Result.CONTINUE;
}
PsiElement elementAt;
if (c == '"' || c == '\'') {
// <%@ layout "_ %> -> <%@ layout "_" %>, <m:foo.bar param="_ -> <m:foo.bar param="_"
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
if ((elementAt = provider.findElementAt(offset - 1, OxyTemplate.INSTANCE)) == null) {
return Result.CONTINUE;
}
if (OxyTemplateParserDefinition.WHITE_SPACES.contains(elementAt.getNode().getElementType())) {
elementAt = elementAt.getPrevSibling();
if (elementAt instanceof MacroEmptyTag) {
elementAt = elementAt.getLastChild();
}
if (elementAt instanceof MacroAttribute) {
elementAt = elementAt.getLastChild();
if (elementAt instanceof PsiErrorElement) {
elementAt = elementAt.getPrevSibling();
}
} else if (elementAt instanceof DirectiveStatement) {
if (c == '"' && ((DirectiveStatement) elementAt).getDirectiveParamWrapperList().size() == 0) {
editor.getDocument().insertString(offset, String.valueOf(c));
}
} else if (c == '"' && elementAt instanceof PsiErrorElement && elementAt.getPrevSibling().getNode().getElementType() == OxyTemplateTypes.T_DIRECTIVE) {
editor.getDocument().insertString(offset, String.valueOf(c));
}
}
if (elementAt.getNode().getElementType() == OxyTemplateTypes.T_MACRO_PARAM_ASSIGNMENT) {
editor.getDocument().insertString(offset, String.valueOf(c));
} else if (c == '"' && elementAt.getNode().getElementType() == OxyTemplateTypes.T_DIRECTIVE && elementAt.getNextSibling().getNode().getStartOffset() == offset) {
editor.getDocument().insertString(offset, String.valueOf(c));
}
} else if (c == '=') {
// <m:foo.bar param=_ -> <m:foo.bar param="_"
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
if ((elementAt = provider.findElementAt(offset - 1, OxyTemplate.INSTANCE)) == null) {
return Result.CONTINUE;
}
if (elementAt.getNode().getElementType() == OxyTemplateTypes.T_MACRO_PARAM_NAME && elementAt.getNextSibling() == null && (elementAt.getNode().getStartOffset() + elementAt.getTextLength()) == offset) {
editor.getDocument().insertString(offset, "=\"\"");
editor.getCaretModel().moveToOffset(offset + 2);
return Result.STOP;
}
}
return Result.CONTINUE;
}
Aggregations