Set filetype association for LightJavaCodeInsightFixtureTestCase

Answered

I have succesfully created an extension that creates references from 'Angular HTML Template' elements to my DSL. When I want to unit-test these references the fixture always considers the file a Plain HTML file so the PsiTree is not as expected and I cannot test my references.

Is there anyway to set the file type association for a LightJavaCodeInsightFixtureTestCase so something with '*.component.html' is considered an Angular HTML Template FileType. 

15 comments
Comment actions Permalink

Did you define a dependency on Angular plugin in your plugin.xml?

0
Comment actions Permalink

Yes, as an optional dependency. Everything works fine when I use it in runIde or when actually deploying the plugin. The only situation where it doesn't work is in a LightJavaCodeInsightFixtureTestCase where the PsiFile is identified as a plain HTML file instead of Angular HTML template

0
Comment actions Permalink

Tim, the Angular HTML language replaces the regular HTML under certain circumstances. One of the easiest ways to reproduce the regular project setup is to add `package.json` file with `@angular/core` dependency in it. E.g.:

myFixture.configureByText("package.json","{\"dependencies\":{\"@angular/core\":\"*\"}}")

You can also register your own context provider (`Angular2ContextProvider` in 2020.3 and older or `WebFrameworkContext` in 2021.1 and newer) in `setUp()` method, which always returns `true` for the test duration. E.g.:

Angular2ContextProvider.ANGULAR_CONTEXT_PROVIDER_EP
.getPoint()
.registerExtension(psiDir -> CachedValueProvider.Result.create(true, ModificationTracker.NEVER_CHANGED),
myFixture.getTestRootDisposable());

Let me know if you have any questions!

0
Comment actions Permalink

Hi Piotr,

thank you for your answer. Unfortunately it didn't do the trick for me. I have upgraded to 2021.1 and to my surprise now my .ts files in my unit-tests are considered plain-text. Also, the icon for the Angular HTML file type is missing. Don't know if this might be a bug in the release or something went wrong during my upgrade.

I have tried your package.json example, that one didn't work. The WebFrameworkContext example I couldn't get to work, I'm not sure what I should register, the line in your example isn't working (probably also deprecated) and the interface for registerExtension shows little to no documentation. I do see a lot of references to a PlatformTestUtil, does this mean these tests should not setup using a LightJavaCodeInsightFixtureTestCase but rather a heavier one?

Kr, Tim

0
Comment actions Permalink

Tim, I am sorry, I haven't payed enough attention to the class you mentioned. Indeed, these tests should be based on `BasePlatformTestCase`. You can see Angular tests here and take the most appropriate as an example: https://github.com/JetBrains/intellij-plugins/tree/master/AngularJS/test/org/angular2 .

0
Comment actions Permalink

Hi Piotr,

thanks for your response. I have tried it quickly but so far nothing seems to get it working in a test environment. I have created a very simpel setup with 2 tests: 

import com.intellij.lang.javascript.TypeScriptFileType;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import org.angular2.lang.html.Angular2HtmlFileType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class Angular2TSReferenceTest extends BasePlatformTestCase {

@Override
@BeforeEach
protected void setUp() throws Exception {
super.setUp();
myFixture.configureByText("package.json","{\"dependencies\":{\"@angular/core\":\"*\"}}");
}

@Override
@AfterEach
protected void tearDown() throws Exception {
super.tearDown();
}

@Test
void testTSFile() {
String content = "@Component({\n" +
" templateUrl: './test.html',\n" +
"})";
final PsiFile psiFile = myFixture.configureByText("myFile.ts", content);
assertEquals(TypeScriptFileType.INSTANCE, psiFile.getFileType());
}

@Test
void testHTMLFile() {
String content = "<div (click)=\"domeSomeActionInTheTsComponent();\"></div>";
final PsiFile psiFile = myFixture.configureByText("test.html", content);
assertEquals(Angular2HtmlFileType.INSTANCE, psiFile.getFileType());
}
}

Both tests fail on their assertion:

testTSFile:

Expected :com.intellij.lang.javascript.TypeScriptFileType@658dcf73
Actual :com.intellij.openapi.fileTypes.PlainTextFileType@22ac8c64

Second

Expected :org.angular2.lang.html.Angular2HtmlFileType@1b2f727a
Actual :com.intellij.ide.highlighter.HtmlFileType@7d291106

In a few days I'll give it another go when I have some more time, perhaps you can have a quick look to see if you can reproduce the above to see if I'm missing something in my setup. The fact that my typescript file is recognized as plain text makes me think that it somehow is running against a community edition.

0
Comment actions Permalink

Tim,

I've run the code you've shared within the context of AngularJS plugin and both tests are green. I have used JUnit 3 though, so I had to remove all of the JUnit 4+ annotations. It seems that you must be missing dependencies in your test module classpath, or there are some problems with JUnit 4+. Are you using Gradle? Can you share configuration?

0
Comment actions Permalink

Hi Piotr,

this is my build.gradle

plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.7.2'
id 'jacoco'
}

group 'com.misset'
version '4.0.3'

sourceCompatibility = 11
sourceSets.main.java.srcDirs 'src/main/gen'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-params:5.3.1'
testCompile group: 'org.mockito', name: 'mockito-inline', version: '3.4.6'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.3.1'
compile group: 'com.atlassian.commonmark', name: 'commonmark', version: '0.2.0'
compile group: 'commons-io', name: 'commons-io', version: '2.8.0'
compile group: 'org.apache.jena', name: 'jena', version: '3.17.0', ext: 'pom' exclude group: 'org.slf4j'
compile group: 'org.apache.jena', name: 'jena-core', version: '3.17.0' exclude group: 'org.slf4j'
}
test {
useJUnitPlatform()
systemProperty "idea.home.path", "C:/Program Files/JetBrains/IntelliJ IDEA 2021.1"
finalizedBy jacocoTestReport
}
jacocoTestReport {
dependsOn test
}
buildSearchableOptions.enabled = false
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2021.1'
type 'IU'
plugins = [
'java',
'JavaScriptLanguage',
'AngularJS'
]
}
patchPluginXml {
sinceBuild '203.5981'

changeNotes """
...
"""
}
tasks{
runIde {
jvmArgs("-Xmx2000m")
}
runPluginVerifier {
ideVersions(["IC-2020.3.1", "IC-2021.1"])
}


}
publishPlugin {
token System.getenv().getOrDefault("ORG_GRADLE_PROJECT_intellijPublishToken", "")
}
0
Comment actions Permalink

And the plugin.xml

<idea-plugin>
<id>com.misset.OMT</id>
<name>OMT / ODT Language</name>
<vendor email="..." url="https://github.com/timmisset/omt">Misset</vendor>

<description>Plugin for the OPP specific languages OMT and ODT</description>
<depends>com.intellij.modules.java</depends>
<depends>com.intellij.modules.lang</depends>
<depends>com.intellij.java</depends>
<depends config-file="withAngularJS.xml" optional="true">AngularJS</depends>
<extensions defaultExtensionNs="com.intellij">
<!-- OMT -->
<fileType name="OMT File" implementationClass="com.misset.opp.omt.OMTFileType"
fieldName="INSTANCE" language="OMT" extensions="omt"/>
<lang.parserDefinition language="OMT"
implementationClass="com.misset.opp.omt.OMTParserDefinition"/>
<lang.syntaxHighlighterFactory language="OMT"
implementationClass="com.misset.opp.omt.style.OMTSyntaxHighlighterFactory"/>
<lang.braceMatcher language="OMT" implementationClass="com.misset.opp.omt.OMTBraceMatcher"/>
<editor.backspaceModeOverride language="OMT"
implementationClass="com.intellij.codeInsight.editorActions.SmartBackspaceDisabler"/>
<enterHandlerDelegate implementation="com.misset.opp.omt.formatter.OMTEnterTypedHandler"/>
<colorSettingsPage implementation="com.misset.opp.omt.style.OMTColorSettingsPage"/>
<annotator language="OMT" implementationClass="com.misset.opp.omt.annotations.OMTAnnotator"/>
<codeStyleSettingsProvider implementation="com.misset.opp.omt.settings.OMTCodeStyleSettingsProvider"/>
<lang.foldingBuilder language="OMT" implementationClass="com.misset.opp.omt.OMTFoldingBuilder"/>
<langCodeStyleSettingsProvider
implementation="com.misset.opp.omt.settings.OMTLanguageCodeStyleSettingsProvider"/>
<completion.contributor language="OMT"
order="first"
implementationClass="com.misset.opp.omt.completion.OMTCompletionContributor"/>
<lang.findUsagesProvider language="OMT"
implementationClass="com.misset.opp.omt.OMTFindUsageProvider"/>
<postStartupActivity implementation="com.misset.opp.omt.OMTStartupActivity"/>
<lang.commenter language="OMT" implementationClass="com.misset.opp.omt.OMTCommenter"/>
<lang.psiStructureViewFactory language="OMT"
implementationClass="com.misset.opp.omt.structure.OMTStructureViewFactory"/>
<lang.refactoringSupport language="OMT"
implementationClass="com.misset.opp.omt.OMTRefactoringSupportProvider"/>
<lang.formatter language="OMT"
implementationClass="com.misset.opp.omt.formatter.OMTFormattingModelBuilder"/>
<applicationConfigurable id="com.misset.opp.omt.settings.OMTConfigurable"
parentId="language"
displayName="OMT / ODT Settings"
instance="com.misset.opp.omt.settings.OMTConfigurable">
</applicationConfigurable>
<!-- Code Inspection -->
<localInspection
shortName="UnusedDeclarations" hasStaticDescription="true"
language="OMT" displayName="OMT unused declarations" groupPath="OMT"
groupName="Redundancy" enabledByDefault="true"
implementationClass="com.misset.opp.omt.inspection.OMTCodeInspectionUnused"/>
<applicationService serviceImplementation="com.misset.opp.omt.settings.OMTSettingsState"/>

<!-- TTL -->
<fileType name="Turtle File" implementationClass="com.misset.opp.ttl.TTLFileType"
fieldName="INSTANCE" language="Turtle" extensions="ttl"/>
<lang.parserDefinition language="Turtle"
implementationClass="com.misset.opp.ttl.TTLParserDefinition"/>
<findUsagesHandlerFactory implementation="com.misset.opp.ttl.TTLFindUsageHandlerFactory"/>
<lang.refactoringSupport language="Turtle"
implementationClass="com.misset.opp.ttl.TTLRefactoringSupportProvider"/>
<colorSettingsPage implementation="com.misset.opp.ttl.style.TTLColorSettingsPage"/>
<lang.syntaxHighlighterFactory language="Turtle"
implementationClass="com.misset.opp.ttl.style.TTLSyntaxHighlighterFactory"/>
<annotator language="Turtle" implementationClass="com.misset.opp.ttl.style.TTLColorAnnotator"/>
</extensions>

<actions>
<!-- Add your actions here -->
</actions>
</idea-plugin>
0
Comment actions Permalink

And finally the withAngularJS.xml

<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<!-- TypeScript extensions -->
<psi.referenceContributor language="TypeScript"
implementation="com.misset.opp.omt.psi.references.external.providers.javascript.TSReferenceProvider"/>

<!-- Angular 2 HTML or Plain HTML extensions -->
<psi.referenceContributor language="Angular2Html"
implementation="com.misset.opp.omt.psi.references.external.providers.angular2HTML.Angular2HTMLReferenceProvider"/>
<psi.referenceContributor language="HTML"
implementation="com.misset.opp.omt.psi.references.external.providers.angular2HTML.Angular2HTMLReferenceProvider"/>
<gotoDeclarationHandler
implementation="com.misset.opp.omt.psi.references.external.providers.angular2HTML.Angular2HTMLGotoDeclarationHandler"/>
<referencesSearch
implementation="com.misset.opp.omt.psi.references.external.search.ActionReferencesSearch"/>
</extensions>
</idea-plugin>
0
Comment actions Permalink

Tim,

It seems that there is some problem with JUnit 5 and classloading/extension point registration. I can reproduce the issue locally - it is not related to Angular. However, I wasn't able to find a solution yet.

0
Comment actions Permalink

Hi Piotr,

thanks a lot for taking the time to investigate this. I will for now defer to manually testing these extensions. Rewriting all my tests to JUnit4 is a bit too much effort :). Let me know when you find something.

Kr, Tim

0
Comment actions Permalink

Hi Tim!

Actually, I've investigated it further and it turns out that it is actually a problem with 2021.1. Please include "CSS" plugin on your list in build.gradle as a workaround. With that plugin present, the `Angular2TSReferenceTest` works for me. Let me know if it works for you :)

0
Comment actions Permalink

Hi Piotr,

Thanks a lot, that also works for me :) :)

 

0
Comment actions Permalink

Awesome, thanks for confirmation and sorry for the trouble!

0

Please sign in to leave a comment.