Skip to content

Commit 963be4b

Browse files
authored
Merge pull request #1439 from Haehnchen/feature/twig-paths-relative-support
provide relative path support for Twig paths configuration via yaml
2 parents 6351ec1 + 7a841fc commit 963be4b

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/main/java/fr/adrienbrault/idea/symfony2plugin/templating/util/TwigUtil.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,31 +1897,42 @@ public static Collection<Pair<String, String>> getTwigPathFromYamlConfigResolved
18971897
Collection<Pair<String, String>> paths = new ArrayList<>();
18981898

18991899
for (Pair<String, String> pair : getTwigPathFromYamlConfig(yamlFile)) {
1900-
String second = pair.getSecond();
1900+
// normalize path; just to be error prune
1901+
String namespacePath = pair.getSecond().replace("\\", "/").replaceAll("/+", "/");
1902+
String namespace = pair.getFirst();
19011903

1902-
if(second.startsWith("%kernel.root_dir%")) {
1904+
if(namespacePath.startsWith("%kernel.root_dir%")) {
19031905
// %kernel.root_dir%/../app
19041906
// %kernel.root_dir%/foo
19051907
for (VirtualFile appDir : FilesystemUtil.getAppDirectories(yamlFile.getProject())) {
1906-
String path = StringUtils.stripStart(second.substring("%kernel.root_dir%".length()), "/");
1908+
String path = StringUtils.stripStart(namespacePath.substring("%kernel.root_dir%".length()), "/");
19071909

19081910
VirtualFile relativeFile = VfsUtil.findRelativeFile(appDir, path.split("/"));
19091911
if(relativeFile != null) {
19101912
String relativePath = VfsUtil.getRelativePath(relativeFile, baseDir, '/');
19111913
if(relativePath != null) {
1912-
paths.add(Pair.create(pair.getFirst(), relativePath));
1914+
paths.add(Pair.create(namespace, relativePath));
19131915
}
19141916
}
19151917
}
1916-
} else if(second.startsWith("%kernel.project_dir%")) {
1918+
} else if(namespacePath.startsWith("%kernel.project_dir%")) {
19171919
// '%kernel.root_dir%/test'
1918-
String path = StringUtils.stripStart(second.substring("%kernel.project_dir%".length()), "/");
1920+
String path = StringUtils.stripStart(namespacePath.substring("%kernel.project_dir%".length()), "/");
19191921

1920-
VirtualFile relativeFile = VfsUtil.findRelativeFile(yamlFile.getProject().getBaseDir(), path.split("/"));
1922+
VirtualFile relativeFile = VfsUtil.findRelativeFile(baseDir, path.split("/"));
19211923
if(relativeFile != null) {
19221924
String relativePath = VfsUtil.getRelativePath(relativeFile, baseDir, '/');
19231925
if(relativePath != null) {
1924-
paths.add(Pair.create(pair.getFirst(), relativePath));
1926+
paths.add(Pair.create(namespace, relativePath));
1927+
}
1928+
}
1929+
} else if(!namespacePath.startsWith("/") && !namespacePath.startsWith("\\")) {
1930+
// 'test/foo'
1931+
VirtualFile relativeFile = VfsUtil.findRelativeFile(baseDir, namespacePath.split("/"));
1932+
if(relativeFile != null) {
1933+
String relativePath = VfsUtil.getRelativePath(relativeFile, baseDir, '/');
1934+
if(relativePath != null) {
1935+
paths.add(Pair.create(namespace, relativePath));
19251936
}
19261937
}
19271938
}

src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/templating/util/TwigUtilTempTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
import org.jetbrains.annotations.NotNull;
1515
import org.jetbrains.yaml.psi.YAMLFile;
1616

17-
import java.util.Arrays;
18-
import java.util.Collection;
19-
import java.util.Collections;
20-
import java.util.List;
17+
import java.util.*;
2118

2219
/**
2320
* @author Daniel Espendiller <daniel@espendiller.net>
@@ -183,29 +180,41 @@ public void testGetTemplateTargetOnOffset() {
183180
* @see TwigUtil#getTwigPathFromYamlConfigResolved
184181
*/
185182
public void testGetTwigPathFromYamlConfigResolved() {
186-
createFile("app/test/foo.yaml");
183+
createFile("app/test/foo.html.twig");
184+
createFile("app/template/foo.html.twig");
187185

188186
PsiFile dummyFile = YamlPsiElementFactory.createDummyFile(getProject(), "" +
189187
"twig:\n" +
190188
" paths:\n" +
191189
" '%kernel.root_dir%/test': foo\n" +
192190
" '%kernel.project_dir%/app/test': project\n" +
193-
" '%kernel.root_dir%/../app': app\n"
191+
" '%kernel.root_dir%/../app': app\n" +
192+
" 'app/template': MY_PREFIX\n" +
193+
" 'app///\\\\\\template': MY_PREFIX_1\n"
194194
);
195195

196196
Collection<Pair<String, String>> paths = TwigUtil.getTwigPathFromYamlConfigResolved((YAMLFile) dummyFile);
197197

198198
assertNotNull(
199-
paths.stream().filter(pair -> "foo".equals(pair.getFirst()) && "app/test".equals(pair.getSecond())).findFirst()
199+
paths.stream().filter(pair -> "foo".equals(pair.getFirst()) && "app/test".equals(pair.getSecond())).findFirst().get()
200200
);
201201

202202
assertNotNull(
203-
paths.stream().filter(pair -> "project".equals(pair.getFirst()) && "app/test".equals(pair.getSecond())).findFirst()
203+
paths.stream().filter(pair -> "project".equals(pair.getFirst()) && "app/test".equals(pair.getSecond())).findFirst().get()
204204
);
205205

206206
assertNotNull(
207-
paths.stream().filter(pair -> "app".equals(pair.getFirst()) && "app".equals(pair.getSecond())).findFirst()
207+
paths.stream().filter(pair -> "app".equals(pair.getFirst()) && "app".equals(pair.getSecond())).findFirst().get()
208208
);
209+
210+
assertNotNull(
211+
paths.stream().filter(pair -> "MY_PREFIX".equals(pair.getFirst()) && "app/template".equals(pair.getSecond())).findFirst().get()
212+
);
213+
214+
Pair<String, String> myPrefix1 = paths.stream().filter(pair -> "MY_PREFIX_1".equals(pair.getFirst())).findAny().get();
215+
216+
assertEquals("MY_PREFIX_1", myPrefix1.getFirst());
217+
assertEquals("app/template", myPrefix1.getSecond());
209218
}
210219

211220
private void assertIsDirectoryAtOffset(@NotNull String templateName, int offset, @NotNull String directory) {

0 commit comments

Comments
 (0)