Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject TypoScript Language in String arguments #179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cache:

matrix:
include:
- env: IDEA_VERSION="IU-182.2757.3" PHP_PLUGIN_VERSION="182.2574.13" DEPLOY=true
- env: IDEA_VERSION="IU-182.2949.4" PHP_PLUGIN_VERSION="182.2949.27" TYPOSCRIPT_PLUGIN_VERSION="1.9.0" DEPLOY=true
# - env: IDEA_VERSION="IU-173.4127.17" PHP_PLUGIN_VERSION="173.4127.13"
# allow_failures:
# - env: IDEA_VERSION="IU-173.4127.17" PHP_PLUGIN_VERSION="173.4127.13"
Expand All @@ -25,6 +25,7 @@ before_install:
- "export ORG_GRADLE_PROJECT_ideaVersion=${IDEA_VERSION}"
- "export ORG_GRADLE_PROJECT_ideaType=${IDEA_TYPE}"
- "export ORG_GRADLE_PROJECT_phpPluginVersion=${PHP_PLUGIN_VERSION}"
- "export ORG_GRADLE_PROJECT_typoScriptPluginVersion=${TYPOSCRIPT_PLUGIN_VERSION}"
- java -version

script:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ apply plugin: 'java'
intellij {
version ideaVersion
pluginName 'TYPO3 CMS Plugin'
plugins = ["com.jetbrains.php:${phpPluginVersion}", 'CSS', 'java-i18n', 'properties', 'yaml']
plugins = ["com.jetbrains.php:${phpPluginVersion}", "de.sgalinski.typoscript.plugin.id:${typoScriptPluginVersion}", 'CSS', 'java-i18n', 'properties', 'yaml', 'IntelliLang']
downloadSources !Boolean.valueOf(System.getenv('CI'))

publishPlugin {
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ideaVersion = IU-182.2757.3
phpPluginVersion = 182.2574.13
ideaVersion = IU-182.2949.4
phpPluginVersion = 182.2949.27
typoScriptPluginVersion = 1.9.0
#ideaVersion = IU-2018.1.4
#phpPluginVersion = 181.5087.24
#ideaVersion = IU-2017.3.3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.cedricziel.idea.typo3.injection;

import com.intellij.lang.Language;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.injection.PhpInjectorBase;
import com.jetbrains.php.lang.psi.elements.*;
import de.sgalinski.typoscript.language.TypoScriptLanguage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class TypoScriptInjector extends PhpInjectorBase {

public static final String[] TYPOSCRIPT_ARGUMENTS = new String[]{
"TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility::addPageTSConfig",
"TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility::addUserTSConfig",
"TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility::addTypoScriptSetup",
"TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility::addTypoScriptConstants",
};

private static boolean isTypoScriptAcceptingMethod(@NotNull PsiElement psiElement) {
for (String TYPOSCRIPT_ARGUMENT : TYPOSCRIPT_ARGUMENTS) {
String[] split = TYPOSCRIPT_ARGUMENT.split("::");
if (isStringArgumentInMethodReference(psiElement) && signatureMatches(psiElement, split[0], split[1])) {

return true;
}
}

return false;
}

private static boolean signatureMatches(@NotNull PsiElement psiElement, @NotNull String className, @NotNull String methodName) {
PsiElement parent = psiElement.getParent();
PsiElement superParent = parent.getParent();

MethodReference methodReference = (MethodReference) superParent;
PsiElement resolve = methodReference.resolve();
if (!(resolve instanceof Method)) {
return false;
}

Method method = (Method) resolve;
PhpClass containingClass = method.getContainingClass();

return methodReference.getName() != null && methodReference.getName().equals(methodName)
&& containingClass != null && containingClass.getFQN().equals(className);
}

private static boolean isStringArgumentInMethodReference(@NotNull PsiElement psiElement) {

return PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(
PlatformPatterns.psiElement(ParameterList.class).withParent(
PlatformPatterns.psiElement(MethodReference.class)
)
).accepts(psiElement);
}

@Nullable
@Override
public Language getInjectedLanguage(@NotNull PsiElement psiElement) {
if (isTypoScriptAcceptingMethod(psiElement)) {
return TypoScriptLanguage.INSTANCE;
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ It is a great inspiration for possible solutions and parts of the code.</p>
<depends>com.jetbrains.php</depends>
<depends>com.intellij.modules.xml</depends>
<depends>org.jetbrains.plugins.yaml</depends>
<depends>org.intellij.intelliLang</depends>
<depends optional="true" config-file="typoscript-plugin.xml">de.sgalinski.typoscript.plugin.id</depends>

<extensions defaultExtensionNs="com.jetbrains.php">
<typeProvider3 implementation="com.cedricziel.idea.typo3.provider.GeneralUtilityTypeProvider"/>
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/typoscript-plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="iso-8859-1"?>

<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<multiHostInjector implementation="com.cedricziel.idea.typo3.injection.TypoScriptInjector"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cedricziel.idea.typo3.injection;

import com.cedricziel.idea.typo3.AbstractTestCase;
import de.sgalinski.typoscript.language.TypoScriptLanguage;

public class TypoScriptInjectorTest extends AbstractTestCase {
public void testInjectsLanguagesInMethodArguments() {
myFixture.configureByText("foo.php", "<?php \n" +
"\\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility::addTypoScriptConstants(\"<caret>\">)");

assertInstanceOf(myFixture.getElementAtCaret().getLanguage(), TypoScriptLanguage.class);
}
}