From c82275c4b840c5617e1c80458e73af9768f67781 Mon Sep 17 00:00:00 2001 From: giraud Date: Thu, 11 Jan 2018 09:13:55 +0100 Subject: [PATCH] #25 - Add minimal support for .ml4 files: use the same parser than for Ocaml --- resources/META-INF/plugin.xml | 6 ++ .../java/com/reason/ide/files/Ml4File.java | 23 ++++++ .../com/reason/ide/files/Ml4FileType.java | 41 +++++++++++ .../reason/ide/files/Ml4FileTypeFactory.java | 12 ++++ .../com/reason/lang/ocaml/OclP4Language.java | 11 +++ .../lang/ocaml/OclP4ParserDefinition.java | 70 +++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 src/main/java/com/reason/ide/files/Ml4File.java create mode 100644 src/main/java/com/reason/ide/files/Ml4FileType.java create mode 100644 src/main/java/com/reason/ide/files/Ml4FileTypeFactory.java create mode 100644 src/main/java/com/reason/lang/ocaml/OclP4Language.java create mode 100644 src/main/java/com/reason/lang/ocaml/OclP4ParserDefinition.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index c1f889232..14c209c64 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -91,6 +91,8 @@ + + diff --git a/src/main/java/com/reason/ide/files/Ml4File.java b/src/main/java/com/reason/ide/files/Ml4File.java new file mode 100644 index 000000000..c97de016b --- /dev/null +++ b/src/main/java/com/reason/ide/files/Ml4File.java @@ -0,0 +1,23 @@ +package com.reason.ide.files; + +import com.intellij.openapi.fileTypes.FileType; +import com.intellij.psi.FileViewProvider; +import com.reason.lang.ocaml.OclLanguage; +import org.jetbrains.annotations.NotNull; + +public class Ml4File extends FileBase { + public Ml4File(@NotNull FileViewProvider viewProvider) { + super(viewProvider, OclLanguage.INSTANCE); + } + + @NotNull + @Override + public FileType getFileType() { + return Ml4FileType.INSTANCE; + } + + @Override + public String toString() { + return "OcamlP4 File"; + } +} diff --git a/src/main/java/com/reason/ide/files/Ml4FileType.java b/src/main/java/com/reason/ide/files/Ml4FileType.java new file mode 100644 index 000000000..c827309a4 --- /dev/null +++ b/src/main/java/com/reason/ide/files/Ml4FileType.java @@ -0,0 +1,41 @@ +package com.reason.ide.files; + +import com.intellij.openapi.fileTypes.LanguageFileType; +import com.reason.icons.Icons; +import com.reason.lang.ocaml.OclLanguage; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class Ml4FileType extends LanguageFileType { + public static final Ml4FileType INSTANCE = new Ml4FileType(); + + private Ml4FileType() { + super(OclLanguage.INSTANCE); + } + + @NotNull + @Override + public String getName() { + return "OcamlP4 file"; + } + + @NotNull + @Override + public String getDescription() { + return "Ocaml preprocessor file"; + } + + @NotNull + @Override + public String getDefaultExtension() { + return "ml4"; + } + + @Nullable + @Override + public Icon getIcon() { + return Icons.OCL_FILE; + } +} diff --git a/src/main/java/com/reason/ide/files/Ml4FileTypeFactory.java b/src/main/java/com/reason/ide/files/Ml4FileTypeFactory.java new file mode 100644 index 000000000..43b681d00 --- /dev/null +++ b/src/main/java/com/reason/ide/files/Ml4FileTypeFactory.java @@ -0,0 +1,12 @@ +package com.reason.ide.files; + +import com.intellij.openapi.fileTypes.FileTypeConsumer; +import com.intellij.openapi.fileTypes.FileTypeFactory; +import org.jetbrains.annotations.NotNull; + +public class Ml4FileTypeFactory extends FileTypeFactory { + @Override + public void createFileTypes(@NotNull FileTypeConsumer fileTypeConsumer) { + fileTypeConsumer.consume(Ml4FileType.INSTANCE, "ml4"); + } +} diff --git a/src/main/java/com/reason/lang/ocaml/OclP4Language.java b/src/main/java/com/reason/lang/ocaml/OclP4Language.java new file mode 100644 index 000000000..6b5d1f42a --- /dev/null +++ b/src/main/java/com/reason/lang/ocaml/OclP4Language.java @@ -0,0 +1,11 @@ +package com.reason.lang.ocaml; + +import com.intellij.lang.Language; + +public class OclP4Language extends Language { + public static final OclP4Language INSTANCE = new OclP4Language(); + + private OclP4Language() { + super("Ml4"); + } +} diff --git a/src/main/java/com/reason/lang/ocaml/OclP4ParserDefinition.java b/src/main/java/com/reason/lang/ocaml/OclP4ParserDefinition.java new file mode 100644 index 000000000..f1ae84541 --- /dev/null +++ b/src/main/java/com/reason/lang/ocaml/OclP4ParserDefinition.java @@ -0,0 +1,70 @@ +package com.reason.lang.ocaml; + +import com.intellij.lang.ASTNode; +import com.intellij.lang.Language; +import com.intellij.lang.ParserDefinition; +import com.intellij.lang.PsiParser; +import com.intellij.lexer.Lexer; +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.TokenType; +import com.intellij.psi.tree.IFileElementType; +import com.intellij.psi.tree.TokenSet; +import com.reason.ide.files.Ml4File; +import com.reason.lang.LexerAdapter; +import com.reason.lang.PsiElementFactory; +import org.jetbrains.annotations.NotNull; + +public class OclP4ParserDefinition implements ParserDefinition { + private static final TokenSet WHITE_SPACES = TokenSet.create(TokenType.WHITE_SPACE); + private static final TokenSet COMMENTS = TokenSet.create(OclTypes.INSTANCE.COMMENT); + private static final TokenSet STRINGS = TokenSet.create(OclTypes.INSTANCE.STRING); + + private static final IFileElementType FILE = new IFileElementType(Language.findInstance(OclP4Language.class)); + + @NotNull + @Override + public Lexer createLexer(Project project) { + return new LexerAdapter(OclTypes.INSTANCE); + } + + @NotNull + public TokenSet getWhitespaceTokens() { + return WHITE_SPACES; + } + + @NotNull + public TokenSet getCommentTokens() { + return COMMENTS; + } + + @NotNull + public TokenSet getStringLiteralElements() { + return STRINGS; + } + + @NotNull + public PsiParser createParser(final Project project) { + return new OclParser(); + } + + @Override + public IFileElementType getFileNodeType() { + return FILE; + } + + public PsiFile createFile(FileViewProvider viewProvider) { + return new Ml4File(viewProvider); + } + + public ParserDefinition.SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode left, ASTNode right) { + return ParserDefinition.SpaceRequirements.MAY; + } + + @NotNull + public PsiElement createElement(ASTNode node) { + return PsiElementFactory.createElement(OclTypes.INSTANCE, node); + } +}