Search in sources :

Example 6 with ElixirFile

use of org.elixir_lang.psi.ElixirFile in project intellij-elixir by KronicDeth.

the class TypedHandler method charTyped.

/*
     * Instance Methods
     */
/**
     * Called after the specified character typed by the user has been inserted in the editor.
     *
     * @param charTyped the character that was typed
     * @param project the project in which the {@code file} exists
     * @param editor the editor that has the {@code file} open
     * @param file the file into which the {@code charTyped} was typed
     */
@Override
public Result charTyped(char charTyped, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    Result result = Result.CONTINUE;
    if (file instanceof ElixirFile) {
        if (charTyped == ' ') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "(do|fn)<space><caret>"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 2);
                IElementType tokenType = iterator.getTokenType();
                if (tokenType == ElixirTypes.DO || tokenType == ElixirTypes.FN) {
                    editor.getDocument().insertString(caret, " end");
                    result = Result.STOP;
                }
            }
        } else if (charTyped == '<') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "~<sigil_name><"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 1);
                IElementType tokenType = iterator.getTokenType();
                if (SIGIL_PROMOTERS.contains(tokenType)) {
                    editor.getDocument().insertString(caret, ">");
                    result = Result.STOP;
                }
            }
            if (result == Result.CONTINUE && caret > 1) {
                // "<<"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 2);
                if (iterator.getTokenType() == ElixirTypes.OPENING_BIT) {
                    editor.getDocument().insertString(caret, ">>");
                    result = Result.STOP;
                }
            }
        } else if (charTyped == '/' || charTyped == '|') {
            int caret = editor.getCaretModel().getOffset();
            if (caret > 2) {
                // "~<sigil_name>(/|\|)"
                final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
                HighlighterIterator iterator = highlighter.createIterator(caret - 1);
                IElementType tokenType = iterator.getTokenType();
                if (SIGIL_PROMOTERS.contains(tokenType)) {
                    editor.getDocument().insertString(caret, String.valueOf(charTyped));
                    result = Result.STOP;
                }
            }
        }
    }
    return result;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) ElixirFile(org.elixir_lang.psi.ElixirFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 7 with ElixirFile

use of org.elixir_lang.psi.ElixirFile in project intellij-elixir by KronicDeth.

the class CreateElixirModuleAction method createModuleFromTemplate.

/**
     * @link com.intellij.ide.acitons.CreateTemplateInPackageAction#doCreate
     * @link com.intellij.ide.actions.CreateClassAction
     * @link com.intellij.psi.impl.file.JavaDirectoryServiceImpl.createClassFromTemplate
     */
@Nullable
private static ElixirFile createModuleFromTemplate(@NotNull PsiDirectory directory, @NotNull String basename, @NotNull String moduleName, @NotNull String templateName) {
    FileTemplateManager fileTemplateManager = FileTemplateManager.getDefaultInstance();
    FileTemplate template = fileTemplateManager.getInternalTemplate(templateName);
    Properties defaultProperties = fileTemplateManager.getDefaultProperties();
    Properties properties = new Properties(defaultProperties);
    properties.setProperty(FileTemplate.ATTRIBUTE_NAME, moduleName);
    PsiElement element;
    try {
        element = FileTemplateUtil.createFromTemplate(template, basename, properties, directory);
    } catch (Exception exception) {
        LOG.error(exception);
        return null;
    }
    if (element == null) {
        return null;
    }
    return (ElixirFile) element;
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) Properties(java.util.Properties) ElixirFile(org.elixir_lang.psi.ElixirFile) PsiElement(com.intellij.psi.PsiElement) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ElixirFile

use of org.elixir_lang.psi.ElixirFile in project intellij-elixir by KronicDeth.

the class CreateElixirModuleActionTest method checkModuleFile.

private void checkModuleFile(final String moduleName, String path) throws IOException {
    ActionManager actionManager = ActionManager.getInstance();
    final CreateElixirModuleAction elixirNewFileAction = (CreateElixirModuleAction) actionManager.getAction("Elixir.NewFile");
    // @see https://devnet.jetbrains.com/message/5539349#5539349
    VirtualFile directoryVirtualFile = myFixture.getTempDirFixture().findOrCreateDir("");
    final PsiDirectory directory = myFixture.getPsiManager().findDirectory(directoryVirtualFile);
    Application application = ApplicationManager.getApplication();
    application.runWriteAction(new Runnable() {

        @Override
        public void run() {
            ElixirFile file = elixirNewFileAction.createFile(moduleName, TEMPLATE_NAME, directory);
            assertNotNull("Expected CreateElixirModuleAction.createFile to create an ElixirFile", file);
        }
    });
    boolean ignoreTrailingWhitespaces = true;
    myFixture.checkResultByFile(path, path, ignoreTrailingWhitespaces);
}
Also used : ActionManager(com.intellij.openapi.actionSystem.ActionManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) Application(com.intellij.openapi.application.Application) ElixirFile(org.elixir_lang.psi.ElixirFile)

Example 9 with ElixirFile

use of org.elixir_lang.psi.ElixirFile in project intellij-elixir by KronicDeth.

the class BeamFileImpl method setMirror.

@Override
public void setMirror(@NotNull TreeElement element) throws InvalidMirrorException {
    PsiElement mirrorElement = SourceTreeToPsiMap.treeToPsiNotNull(element);
    if (!(mirrorElement instanceof ElixirFile)) {
        throw new InvalidMirrorException("Unexpected mirror file: " + mirrorElement);
    }
    ElixirFile mirrorFile = (ElixirFile) mirrorElement;
    setMirrors(modulars(), mirrorFile.modulars());
}
Also used : ElixirFile(org.elixir_lang.psi.ElixirFile)

Aggregations

ElixirFile (org.elixir_lang.psi.ElixirFile)9 Nullable (org.jetbrains.annotations.Nullable)5 PsiElement (com.intellij.psi.PsiElement)3 StructureViewBuilder (com.intellij.ide.structureView.StructureViewBuilder)2 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileTemplateManager (com.intellij.ide.fileTemplates.FileTemplateManager)1 StructureViewModel (com.intellij.ide.structureView.StructureViewModel)1 TreeBasedStructureViewBuilder (com.intellij.ide.structureView.TreeBasedStructureViewBuilder)1 ActionManager (com.intellij.openapi.actionSystem.ActionManager)1 Application (com.intellij.openapi.application.Application)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)1 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)1 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 SdkTypeId (com.intellij.openapi.projectRoots.SdkTypeId)1 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)1 ProjectRootManager (com.intellij.openapi.roots.ProjectRootManager)1