Skip to content

Commit 3d49b66

Browse files
committed
support absolute path in Twig templates; optimize path resolving
1 parent 4c6e314 commit 3d49b66

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

src/fr/adrienbrault/idea/symfony2plugin/templating/path/TwigNamespaceSetting.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,12 @@ public boolean isEnabled() {
6464
}
6565

6666
public boolean equals(Project project, TwigPath twigPath) {
67-
6867
if(!twigPath.getNamespaceType().equals(this.getNamespaceType()) || !twigPath.getNamespace().equals(this.getNamespace())) {
6968
return false;
7069
}
7170

7271
String relativePath = twigPath.getRelativePath(project);
73-
if(relativePath == null || !relativePath.equals(this.getPath())) {
74-
return false;
75-
}
76-
77-
return true;
78-
72+
return relativePath != null && relativePath.equals(this.getPath());
7973
}
8074

8175
public TwigNamespaceSetting setEnabled(boolean disabled) {

src/fr/adrienbrault/idea/symfony2plugin/templating/path/TwigPath.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.adrienbrault.idea.symfony2plugin.templating.path;
22

33
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.util.io.FileUtil;
45
import com.intellij.openapi.vfs.VfsUtil;
56
import com.intellij.openapi.vfs.VirtualFile;
67
import org.jetbrains.annotations.NotNull;
@@ -53,6 +54,7 @@ public TwigPath clone() {
5354

5455
TwigPath twigPath = new TwigPath(this.getPath(), this.getNamespace(), this.getNamespaceType(), this.isCustomPath());
5556
twigPath.setEnabled(this.isEnabled());
57+
5658
return twigPath;
5759
}
5860

@@ -72,11 +74,11 @@ public boolean isGlobalNamespace() {
7274

7375
@Nullable
7476
public String getRelativePath(@NotNull Project project) {
75-
if(this.isCustomPath()) {
76-
return this.getPath();
77+
if(!FileUtil.isAbsolute(path)) {
78+
return path;
7779
}
7880

79-
VirtualFile virtualFile = this.getDirectory();
81+
VirtualFile virtualFile = getDirectory();
8082
if(virtualFile == null) {
8183
return null;
8284
}
@@ -86,12 +88,16 @@ public String getRelativePath(@NotNull Project project) {
8688

8789
@Nullable
8890
public VirtualFile getDirectory(@NotNull Project project) {
89-
String relativePath = this.getRelativePath(project);
90-
if(relativePath == null) {
91-
return null;
91+
if(!FileUtil.isAbsolute(path)) {
92+
return VfsUtil.findRelativeFile(path, project.getBaseDir());
93+
} else {
94+
VirtualFile fileByIoFile = VfsUtil.findFileByIoFile(new File(path), true);
95+
if(fileByIoFile != null) {
96+
return fileByIoFile;
97+
}
9298
}
9399

94-
return VfsUtil.findRelativeFile(relativePath, project.getBaseDir());
100+
return null;
95101
}
96102

97103
@NotNull
@@ -103,6 +109,7 @@ public boolean isEnabled() {
103109
return enabled;
104110
}
105111

112+
@Deprecated
106113
public TwigPath setEnabled(boolean enabled) {
107114
this.enabled = enabled;
108115
return this;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.templating.path;
2+
3+
import com.intellij.util.SystemIndependent;
4+
import fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPath;
5+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyTempCodeInsightFixtureTestCase;
6+
7+
/**
8+
* @author Daniel Espendiller <daniel@espendiller.net>
9+
* @see fr.adrienbrault.idea.symfony2plugin.templating.path.TwigPath#TwigPath
10+
*/
11+
public class TwigPathTempTest extends SymfonyTempCodeInsightFixtureTestCase {
12+
public void testRelativePathResolving() {
13+
createFile("app/views");
14+
15+
TwigPath twigPath = new TwigPath("app", "namespace");
16+
assertEquals("app", twigPath.getDirectory(getProject()).getName());
17+
18+
assertEquals("app", twigPath.getPath());
19+
assertEquals("namespace", twigPath.getNamespace());
20+
21+
assertEquals("app", twigPath.getRelativePath(getProject()));
22+
}
23+
24+
public void testAbsolutePathResolving() {
25+
createFile("app/views");
26+
27+
@SystemIndependent String basePath = getProject().getBasePath();
28+
TwigPath twigPath = new TwigPath(basePath + "/app", "namespace");
29+
assertEquals("app", twigPath.getDirectory(getProject()).getName());
30+
31+
assertTrue(twigPath.getPath().endsWith("app"));
32+
assertEquals("namespace", twigPath.getNamespace());
33+
34+
assertEquals("app", twigPath.getRelativePath(getProject()));
35+
}
36+
}

0 commit comments

Comments
 (0)