Skip to content

614: Provided file path references for JS:STRING_LITERAL psi elements in the js files #1045

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
/**
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.reference.js;

import static com.intellij.patterns.StandardPatterns.string;
import static com.magento.idea.magento2plugin.util.RegExUtil.JsRegex;

import com.intellij.lang.javascript.JSTokenTypes;
import com.intellij.lang.javascript.JavascriptLanguage;
import com.intellij.lang.javascript.patterns.JSPatterns;
import com.intellij.psi.*;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiReferenceContributor;
import com.intellij.psi.PsiReferenceRegistrar;
import com.magento.idea.magento2plugin.reference.provider.FilePathReferenceProvider;
import com.magento.idea.magento2plugin.reference.provider.ModuleNameReferenceProvider;
import com.magento.idea.magento2plugin.reference.provider.RequireJsPreferenceReferenceProvider;
import com.magento.idea.magento2plugin.util.RegExUtil;
import org.jetbrains.annotations.NotNull;

import static com.intellij.patterns.StandardPatterns.string;

public class JsReferenceContributor extends PsiReferenceContributor {

@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
public void registerReferenceProviders(final @NotNull PsiReferenceRegistrar registrar) {
registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression()
.withText(string().matches(".*" + RegExUtil.Magento.MODULE_NAME + ".*")),
new ModuleNameReferenceProvider()
);

// Targets property value -> {test: 'Sandbox_Test/js/test'}
registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression().withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
JSPatterns.jsLiteralExpression()
.withText(string().matches(JsRegex.FILE_PATH)),
new FilePathReferenceProvider()
);

// Targets property key (JS:STRING_LITERAL) -> {'Sandbox_Test/js/test': true}
registrar.registerReferenceProvider(
PlatformPatterns.psiElement(JSTokenTypes.STRING_LITERAL)
.withText(string().matches(JsRegex.FILE_PATH))
.withLanguage(JavascriptLanguage.INSTANCE),
new FilePathReferenceProvider()
);

registrar.registerReferenceProvider(
JSPatterns.jsLiteralExpression().withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
JSPatterns.jsLiteralExpression()
.withText(string().matches(".*\\W" + RegExUtil.FILE_PATH + ".*")),
new RequireJsPreferenceReferenceProvider()
);
}
Expand Down
7 changes: 7 additions & 0 deletions src/com/magento/idea/magento2plugin/util/RegExUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ public static class XmlRegex {
"\\\\?" + PhpRegex.FQN + "(" + CLASS_MEMBER_NAME + ")?.*";
}

public static class JsRegex {

// Targets paths like `'Sandbox_Test/js/test'`
public static final String FILE_PATH
= "(\\W{1}[A-Z][a-zA-Z0-9]+_[A-Z][a-zA-Z0-9]+[\\/\\w*-]{1,}\\W{1})";
}

public static class CustomTheme {
public static final String MODULE_NAME =
"app\\/design\\/(adminhtml|frontend)\\/\\w*\\/\\w*\\/\\w*";
Expand Down