Search in sources :

Example 1 with TemplateToolkitFileViewProvider

use of com.perl5.lang.tt2.TemplateToolkitFileViewProvider in project Perl5-IDEA by Camelcade.

the class TemplateToolkitTypedHandler method processChar.

protected Result processChar(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
    FileViewProvider viewProvider = file.getViewProvider();
    if (!(viewProvider instanceof TemplateToolkitFileViewProvider)) {
        return null;
    }
    TemplateToolkitSettings settings = TemplateToolkitSettings.getInstance(project);
    String openTag = settings.START_TAG;
    if (StringUtil.isEmpty(openTag) || c != openTag.charAt(openTag.length() - 1)) {
        return null;
    }
    int offset = editor.getCaretModel().getOffset();
    if (offset == 0) {
        return null;
    }
    PsiElement element = viewProvider.findElementAt(offset - 1, TemplateToolkitLanguage.INSTANCE);
    if (element == null) {
        return null;
    }
    IElementType tokenType = element.getNode().getElementType();
    if (tokenType != TT2_HTML) {
        return null;
    }
    String openTagPrefix = openTag.substring(0, openTag.length() - 1);
    if (openTagPrefix.length() == 0) {
        return null;
    }
    int startOffset = offset - openTagPrefix.length();
    if (startOffset < 0) {
        return null;
    }
    if (StringUtil.equals(file.getText().subSequence(startOffset, offset), openTagPrefix)) {
        EditorModificationUtil.insertStringAtCaret(editor, c + "  " + settings.END_TAG, false, true, 2);
        return Result.STOP;
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TemplateToolkitSettings(com.perl5.lang.tt2.idea.settings.TemplateToolkitSettings) TemplateToolkitFileViewProvider(com.perl5.lang.tt2.TemplateToolkitFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) TemplateToolkitFileViewProvider(com.perl5.lang.tt2.TemplateToolkitFileViewProvider) PsiElement(com.intellij.psi.PsiElement)

Aggregations

FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 TemplateToolkitFileViewProvider (com.perl5.lang.tt2.TemplateToolkitFileViewProvider)1 TemplateToolkitSettings (com.perl5.lang.tt2.idea.settings.TemplateToolkitSettings)1