Skip to content

Commit

Permalink
feat: add xliff file type with schema
Browse files Browse the repository at this point in the history
  • Loading branch information
cvette committed Jul 25, 2023
1 parent 95f4714 commit 5fc32c3
Show file tree
Hide file tree
Showing 8 changed files with 2,374 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Unreleased

### Added
- Add XLIFF file type
- Allow to exclude symlinked packages automatically
- Improved detection of Neos projects by using the composer.json
- Compatibility with 2023.2 (eap)
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/de/vette/idea/neos/lang/xliff/XliffFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.vette.idea.neos.lang.xliff;

import com.intellij.psi.FileViewProvider;
import com.intellij.psi.impl.source.xml.XmlFileImpl;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.xml.XmlFile;

public class XliffFile extends XmlFileImpl implements XmlFile {
static final IFileElementType XLIFF_FILE_ELEMENT_TYPE = new IFileElementType("XLIFF_FILE_ELEMENT_TYPE", XliffLanguage.INSTANCE);
public XliffFile(FileViewProvider viewProvider) {
super(viewProvider, XLIFF_FILE_ELEMENT_TYPE);
}
}
48 changes: 48 additions & 0 deletions src/main/java/de/vette/idea/neos/lang/xliff/XliffFileType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package de.vette.idea.neos.lang.xliff;

import com.intellij.ide.highlighter.XmlLikeFileType;
import com.intellij.lang.Language;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.NlsSafe;
import de.vette.idea.neos.NeosIcons;
import de.vette.idea.neos.lang.eel.EelFileType;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class XliffFileType extends XmlLikeFileType {
@NonNls public static final String DOT_DEFAULT_EXTENSION = ".xlf";

public static final XliffFileType INSTANCE = new XliffFileType();

private XliffFileType() {
super(XliffLanguage.INSTANCE);
}

protected XliffFileType(Language language) {
super(language);
}

@Override
public @NonNls @NotNull String getName() {
return "Neos XLIFF";
}

@Override
public @NlsContexts.Label @NotNull String getDescription() {
return "Neos XLIFF";
}

@Override
public @NlsSafe @NotNull String getDefaultExtension() {
return "xlf";
}

@Override
public Icon getIcon() {
return NeosIcons.NODE_TYPE;
}
}
19 changes: 19 additions & 0 deletions src/main/java/de/vette/idea/neos/lang/xliff/XliffLanguage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.vette.idea.neos.lang.xliff;

import com.intellij.lang.Language;
import com.intellij.lang.html.HTMLLanguage;
import com.intellij.lang.xml.XMLLanguage;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

public class XliffLanguage extends XMLLanguage {
public static final XliffLanguage INSTANCE = new XliffLanguage();

private XliffLanguage() {
super(XMLLanguage.INSTANCE, "XLIFF", "application/xliff+xml", "application/x-xliff+xml");
}

protected XliffLanguage(@NotNull Language baseLanguage, @NonNls @NotNull String name, @NonNls @NotNull String @NotNull ... mime) {
super(baseLanguage, name, mime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package de.vette.idea.neos.lang.xliff;

import com.intellij.lang.*;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.lexer.Lexer;
import com.intellij.lexer.XmlLexer;
import com.intellij.openapi.project.Project;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.parsing.xml.XmlParser;
import com.intellij.psi.tree.IFileElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;

public class XliffParserDefinition implements ParserDefinition {
@Override
public @NotNull Lexer createLexer(Project project) {
return new XmlLexer();
}

@Override
public @NotNull PsiParser createParser(Project project) {
return LanguageParserDefinitions.INSTANCE.forLanguage(Language.findInstance(XMLLanguage.class)).createParser(project);
}

@Override
public @NotNull IFileElementType getFileNodeType() {
return XliffFile.XLIFF_FILE_ELEMENT_TYPE;
}

@Override
public @NotNull TokenSet getCommentTokens() {
return LanguageParserDefinitions.INSTANCE.forLanguage(Language.findInstance(XMLLanguage.class)).getCommentTokens();
}

@Override
public @NotNull TokenSet getStringLiteralElements() {
return TokenSet.EMPTY;
}

@Override
public @NotNull TokenSet getWhitespaceTokens() {
return LanguageParserDefinitions.INSTANCE.forLanguage(Language.findInstance(XMLLanguage.class)).getWhitespaceTokens();
}

@Override
public @NotNull PsiElement createElement(ASTNode node) {
throw new IllegalArgumentException("Unknown element: "+node);
}

@Override
public @NotNull PsiFile createFile(@NotNull FileViewProvider viewProvider) {
return new XliffFile(viewProvider);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.vette.idea.neos.lang.xliff;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.xml.XmlSchemaProvider;
import org.iso_relax.dispatcher.IslandSchema;
import org.iso_relax.dispatcher.IslandVerifier;
import org.iso_relax.dispatcher.SchemaProvider;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.net.URL;
import java.util.Iterator;

public class XliffSchemaProvider extends XmlSchemaProvider {

@Override
public @Nullable XmlFile getSchema(@NotNull @NonNls String url, @Nullable Module module, @NotNull PsiFile baseFile) {
final URL resource = XliffSchemaProvider.class.getResource("/de/vette/idea/neos/xsd/xliff/xliff12.xsd");
final VirtualFile fileByURL = VfsUtil.findFileByURL(resource);
PsiFile result = baseFile.getManager().findFile(fileByURL);
if (result instanceof XmlFile) {
return (XmlFile)result.copy();
}
return null;
}

@Override
public boolean isAvailable(final @NotNull XmlFile file) {
return FileUtilRt.extensionEquals(file.getName(), XliffFileType.INSTANCE.getDefaultExtension());
}
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<idea-version since-build="203.3645.34"/>

<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.modules.xml</depends>
<depends>com.jetbrains.php</depends>
<depends>org.intellij.intelliLang</depends>
<depends>org.jetbrains.plugins.yaml</depends>
Expand Down Expand Up @@ -41,6 +42,11 @@

<errorHandler implementation="de.vette.idea.neos.errorReporting.SentryErrorReporter" />

<!-- XLIFF -->
<fileType name="Neos XLIFF" language="XLIFF" implementationClass="de.vette.idea.neos.lang.xliff.XliffFileType" extensions="xlf;xliff" fieldName="INSTANCE"/>
<lang.parserDefinition implementationClass="de.vette.idea.neos.lang.xliff.XliffParserDefinition" language="XLIFF" />
<xml.schemaProvider implementation="de.vette.idea.neos.lang.xliff.XliffSchemaProvider"/>

<!-- EEL -->
<fileType name="Neos Eel" language="NeosEel" implementationClass="de.vette.idea.neos.lang.eel.EelFileType" extensions="eel" fieldName="INSTANCE"/>
<lang.parserDefinition language="NeosEel" implementationClass="de.vette.idea.neos.lang.eel.EelParserDefinition"/>
Expand Down
Loading

0 comments on commit 5fc32c3

Please sign in to comment.