-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic support for include, closes #109
- Loading branch information
Jan Polák
committed
Oct 24, 2022
1 parent
d591227
commit f738c35
Showing
8 changed files
with
1,113 additions
and
963 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/glslplugin/lang/elements/preprocessor/GLSLPreprocessorDirective.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/glslplugin/lang/elements/preprocessor/GLSLPreprocessorInclude.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package glslplugin.lang.elements.preprocessor; | ||
|
||
import com.intellij.lang.ASTNode; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.PsiDirectory; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiReference; | ||
import com.intellij.psi.PsiReferenceBase; | ||
import com.intellij.psi.ResolveState; | ||
import com.intellij.psi.scope.PsiScopeProcessor; | ||
import com.intellij.util.IncorrectOperationException; | ||
import glslplugin.lang.elements.GLSLTokenTypes; | ||
import glslplugin.lang.elements.reference.GLSLReferencableDeclaration; | ||
import glslplugin.lang.elements.reference.GLSLReferenceUtil; | ||
import glslplugin.lang.parser.GLSLFile; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* When the directive is in the top-level and contains a string literal (surrounded by whatever), | ||
* and that string can be evaluated to a valid GLSL file, declarations from that file will be included. | ||
* | ||
* Preprocessor redefinitions can't be included in this way. | ||
* Syntactical correctness of the file inclusion is not checked. | ||
*/ | ||
public class GLSLPreprocessorInclude extends GLSLPreprocessorDirective { | ||
public GLSLPreprocessorInclude(@NotNull ASTNode astNode) { | ||
super(astNode); | ||
} | ||
|
||
public @Nullable GLSLFile includedFile() { | ||
final PsiElement stringElement = findChildByType(GLSLTokenTypes.PREPROCESSOR_STRING); | ||
final String pathStringRaw = stringElement == null ? null : stringElement.getText(); | ||
final String pathString = pathStringRaw == null | ||
|| !pathStringRaw.startsWith("\"") | ||
|| !pathStringRaw.endsWith("\"") | ||
|| pathStringRaw.length() <= 2 | ||
? null : pathStringRaw.substring(1, pathStringRaw.length() - 1); | ||
if (pathString == null) return null; | ||
|
||
final String[] parts = pathString.split("[\\\\/]"); | ||
|
||
PsiDirectory dir = this.getContainingFile().getContainingDirectory(); | ||
for (int i = 0; i < parts.length - 1; i++) { | ||
String s = parts[i]; | ||
if (s.isEmpty() || ".".equals(s)) { | ||
continue;// Nothing to do | ||
} | ||
if ("..".equals(s)) { | ||
dir = dir.getParentDirectory(); | ||
} else { | ||
dir = dir.findSubdirectory(s); | ||
} | ||
if (dir == null) { | ||
return null; | ||
} | ||
} | ||
|
||
final PsiFile includedFile = dir.findFile(parts[parts.length - 1]); | ||
if (!(includedFile instanceof GLSLFile glslFile)) { | ||
return null; | ||
} | ||
|
||
return glslFile; | ||
} | ||
|
||
@Override | ||
public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { | ||
final GLSLFile glslFile = includedFile(); | ||
if (glslFile == null) return true; | ||
|
||
return glslFile.processDeclarations(processor, state, lastParent, place); | ||
} | ||
|
||
@Override | ||
public PsiReference getReference() { | ||
final PsiElement fileString = findChildByType(GLSLTokenTypes.PREPROCESSOR_STRING); | ||
if (fileString == null) return null; | ||
final TextRange textRange = GLSLReferenceUtil.rangeOfIn(fileString, this); | ||
return new PsiReferenceBase<>(this, textRange, true) { | ||
|
||
@Override | ||
public @Nullable PsiElement resolve() { | ||
return includedFile(); | ||
} | ||
|
||
@Override | ||
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException { | ||
newElementName = "\"" + newElementName.replace("\"", "").replace("\n", "").replace("\r", "") + "\""; | ||
|
||
final GLSLPreprocessorInclude element = getElement(); | ||
final PsiElement string = element.findChildByType(GLSLTokenTypes.PREPROCESSOR_STRING); | ||
if (string == null) { | ||
throw new IncorrectOperationException(element+" can't be renamed"); | ||
} | ||
|
||
GLSLReferencableDeclaration.replacePreprocessorString(string, newElementName); | ||
setRangeInElement(GLSLReferenceUtil.rangeOfIn(element.findChildByType(GLSLTokenTypes.PREPROCESSOR_STRING), element)); | ||
return element; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.