diff --git a/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java b/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java index 03ee11bc..3e1a7464 100644 --- a/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java +++ b/src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java @@ -6,6 +6,7 @@ import com.intellij.ide.plugins.IdeaPluginDescriptor; import com.intellij.ide.plugins.PluginManager; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.components.ProjectComponent; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; @@ -20,6 +21,8 @@ import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.fileEditor.FileEditorManagerEvent; import com.intellij.openapi.fileEditor.FileEditorManagerListener; +import com.intellij.openapi.fileTypes.FileTypeManager; +import com.intellij.openapi.fileTypes.FileTypes; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.progress.Task; @@ -54,7 +57,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Objects; /** This object is the controller for the ANTLR plug-in. It receives * events and can send them on to its contained components. For example, @@ -113,6 +115,16 @@ public static ANTLRv4PluginController getInstance(Project project) { @Override public void initComponent() { + + final FileTypeManager fileTypeManager = FileTypeManager.getInstance(); + final String defaultExtension = ANTLRv4FileType.INSTANCE.getDefaultExtension(); + + WriteCommandAction.runWriteCommandAction(this.project, () -> + fileTypeManager.removeAssociatedExtension(FileTypes.PLAIN_TEXT, defaultExtension)); + + WriteCommandAction.runWriteCommandAction(this.project, () -> + fileTypeManager.associateExtension(ANTLRv4FileType.INSTANCE, defaultExtension)); + } @Override diff --git a/src/test/java/org/antlr/intellij/plugin/TestUtils.java b/src/test/java/org/antlr/intellij/plugin/TestUtils.java index 7e1b0bf1..7298b8f7 100644 --- a/src/test/java/org/antlr/intellij/plugin/TestUtils.java +++ b/src/test/java/org/antlr/intellij/plugin/TestUtils.java @@ -1,25 +1,33 @@ package org.antlr.intellij.plugin; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorFactory; import com.intellij.util.ThrowableRunnable; import com.intellij.util.lang.CompoundRuntimeException; - import java.io.PrintWriter; import java.io.StringWriter; public class TestUtils { + + public static void releaseEditorIfNotDisposed(Editor editor) { + if (!editor.isDisposed()) EditorFactory.getInstance().releaseEditor(editor); + } + public static void tearDownIgnoringObjectNotDisposedException(ThrowableRunnable delegate) throws Exception { try { delegate.run(); } catch (RuntimeException e) { // We don't want to release the editor in the Tool Output tool window, so we ignore // ObjectNotDisposedExceptions related to this particular editor - if ( e.getClass().getName().equals("com.intellij.openapi.util.TraceableDisposable$ObjectNotDisposedException") + if ( e.getClass().getName().startsWith("com.intellij.openapi.util.TraceableDisposable$") || e instanceof CompoundRuntimeException ) { StringWriter stringWriter = new StringWriter(); e.printStackTrace(new PrintWriter(stringWriter)); String stack = stringWriter.toString(); + System.out.println(e.getStackTrace()[0].getClassName()); if ( stack.contains("ANTLRv4PluginController.createToolWindows") || - stack.contains("Issue559Test") || stack.contains("Issue540Test") || + + stack.contains("Issue559") || stack.contains("Issue540") || stack.contains("Issue569") || stack.contains("org.antlr.intellij.plugin.preview.InputPanel.createPreviewEditor") ) { return; } diff --git a/src/test/java/org/antlr/intellij/plugin/editor/Issue540Test.java b/src/test/java/org/antlr/intellij/plugin/editor/Issue540Test.java index a6a483b5..cb3cbfd0 100644 --- a/src/test/java/org/antlr/intellij/plugin/editor/Issue540Test.java +++ b/src/test/java/org/antlr/intellij/plugin/editor/Issue540Test.java @@ -3,7 +3,6 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; @@ -91,8 +90,9 @@ protected void tearDown() throws Exception { FileUtils.forceDeleteOnExit(LEXER_FILE); FileUtils.forceDeleteOnExit(PARSER_FILE); FileUtils.forceDeleteOnExit(new File(getTestDataGenPath())); - EditorFactory.getInstance().releaseEditor(myFixture.getEditor()); - TestUtils.tearDownIgnoringObjectNotDisposedException(super::tearDown); + TestUtils.tearDownIgnoringObjectNotDisposedException(() -> { + super.tearDown(); + }); } } \ No newline at end of file diff --git a/src/test/java/org/antlr/intellij/plugin/editor/Issue559Test.java b/src/test/java/org/antlr/intellij/plugin/editor/Issue559Test.java index 8034e226..d6db1e9d 100644 --- a/src/test/java/org/antlr/intellij/plugin/editor/Issue559Test.java +++ b/src/test/java/org/antlr/intellij/plugin/editor/Issue559Test.java @@ -1,6 +1,5 @@ package org.antlr.intellij.plugin.editor; -import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; @@ -52,7 +51,7 @@ protected String getTestDataPath() { @Override protected void tearDown() throws Exception { TestUtils.tearDownIgnoringObjectNotDisposedException(() -> { - EditorFactory.getInstance().releaseEditor(myFixture.getEditor()); + TestUtils.releaseEditorIfNotDisposed(myFixture.getEditor()); super.tearDown(); }); } diff --git a/src/test/java/org/antlr/intellij/plugin/editor/Issue569Test.java b/src/test/java/org/antlr/intellij/plugin/editor/Issue569Test.java new file mode 100644 index 00000000..cf49bb62 --- /dev/null +++ b/src/test/java/org/antlr/intellij/plugin/editor/Issue569Test.java @@ -0,0 +1,50 @@ +package org.antlr.intellij.plugin.editor; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.EditorFactory; +import com.intellij.openapi.fileTypes.FileTypeManager; +import com.intellij.openapi.fileTypes.FileTypes; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.testFramework.fixtures.BasePlatformTestCase; +import org.antlr.intellij.plugin.ANTLRv4FileType; +import org.antlr.intellij.plugin.ANTLRv4PluginController; +import org.antlr.intellij.plugin.TestUtils; +import org.junit.Test; + +public class Issue569Test extends BasePlatformTestCase { + + @Test + public void test_shouldReassignExtensionType() { + + // Reassign extension temporarily + WriteCommandAction.runWriteCommandAction(getProject(), () -> + FileTypeManager.getInstance().associateExtension(FileTypes.PLAIN_TEXT, "g4")); + + // Test File + VirtualFile file = myFixture.configureByFile("FooParser.g4").getVirtualFile(); + assertEquals(file.getFileType(), FileTypes.PLAIN_TEXT); + + // Create controller and initialize it + ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(getProject()); + assertNotNull(controller); + controller.initComponent(); + + // Check if file type is reassigned + assertEquals(file.getFileType(), ANTLRv4FileType.INSTANCE); + + } + + @Override + protected String getTestDataPath() { + return "src/test/resources/references"; + } + + @Override + protected void tearDown() throws Exception { + TestUtils.tearDownIgnoringObjectNotDisposedException(() -> { + EditorFactory.getInstance().releaseEditor(myFixture.getEditor()); + super.tearDown(); + }); + } + +} \ No newline at end of file