Skip to content

Commit

Permalink
Merge pull request #578 from kjarrio/bugfix/GH-569
Browse files Browse the repository at this point in the history
Bugfix for issue: #569 Plugin doesn't reassign extension if it has already been associated with text
  • Loading branch information
parrt authored Aug 13, 2022
2 parents 7254ea7 + c25e023 commit f9753ad
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/org/antlr/intellij/plugin/TestUtils.java
Original file line number Diff line number Diff line change
@@ -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<Exception> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
});
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
});
}
Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/antlr/intellij/plugin/editor/Issue569Test.java
Original file line number Diff line number Diff line change
@@ -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();
});
}

}

0 comments on commit f9753ad

Please sign in to comment.