├── settings.gradle ├── src ├── test │ ├── resources │ │ └── data │ │ │ ├── Widget.html │ │ │ ├── App.html │ │ │ ├── Sample1.svelte │ │ │ ├── sample01.html │ │ │ └── Sample1.txt │ └── java │ │ └── technology │ │ └── svelte │ │ └── lexer │ │ ├── LexerToken.java │ │ ├── SvelteParsingTest.java │ │ ├── AbstractLexerTest.java │ │ ├── SvelteLexerTest.java │ │ └── SvelteTopLexerTest.java └── main │ ├── resources │ ├── icons │ │ └── svelte.png │ └── META-INF │ │ └── plugin.xml │ ├── java │ └── technology │ │ └── svelte │ │ ├── lexer │ │ ├── LexerState.java │ │ ├── SvelteElementType.java │ │ ├── SvelteSubLexer.java │ │ ├── SvelteLexer.java │ │ ├── SvelteTopLexer.java │ │ ├── LayeredLexer.java │ │ └── SvelteTokenTypes.java │ │ ├── Svelte.java │ │ ├── SvelteIcons.java │ │ ├── psi │ │ ├── SvelteFile.java │ │ └── impl │ │ │ ├── SveltePsiElement.java │ │ │ ├── MacroAttrImpl.java │ │ │ ├── SvelteFileImpl.java │ │ │ └── MacroNodeImpl.java │ │ ├── file │ │ ├── SvelteFileViewProviderFactory.java │ │ ├── SvelteFileTypeFactory.java │ │ ├── SvelteFileType.java │ │ └── SvelteFileViewProvider.java │ │ ├── completion │ │ ├── SvelteCompletionContributor.java │ │ └── KeywordCompletionProvider.java │ │ ├── SvelteLanguage.java │ │ ├── suppressions │ │ └── XmlUnboundNsPrefixSuppressionProvider.java │ │ ├── codeInsight │ │ └── attributes │ │ │ ├── SvelteAttributeDescriptorsProvider.java │ │ │ └── SvelteAttributeDescriptor.java │ │ ├── editor │ │ ├── SvelteTemplateHighlighter.java │ │ ├── SvelteSyntaxHighlighter.java │ │ └── SvelteColorsPage.java │ │ └── parser │ │ ├── SvelteParser.java │ │ └── SvelteParserDefinition.java │ └── jflex │ ├── technology │ └── svelte │ │ └── lexer │ │ └── svelte.flex │ └── idea-flex.skeleton ├── gradle └── wrapper │ └── gradle-wrapper.properties ├── .gitignore ├── sveltejs-idea-plugin.iml ├── README.adoc ├── LICENSE ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'sveltejs-idea-plugin' 2 | 3 | -------------------------------------------------------------------------------- /src/test/resources/data/Widget.html: -------------------------------------------------------------------------------- 1 |

And this is a nested component with {{foo}} where bar is {{bar}} and dynamic is {{dynamic}}

-------------------------------------------------------------------------------- /src/main/resources/icons/svelte.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dschulten/sveltejs-idea-plugin/HEAD/src/main/resources/icons/svelte.png -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/LexerState.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | /** 4 | * State of the lexer, to be pushed onto stack 5 | */ 6 | public class LexerState { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/Svelte.java: -------------------------------------------------------------------------------- 1 | package technology.svelte; 2 | 3 | 4 | public class Svelte { 5 | public static final String languageName = "Svelte"; 6 | public static final String languageDescription = "Svelte html components"; 7 | } 8 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sun Sep 10 19:16:06 CEST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | 24 | .idea/ 25 | .gradle 26 | /out 27 | /gradle.properties 28 | /build 29 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/SvelteIcons.java: -------------------------------------------------------------------------------- 1 | package technology.svelte; 2 | 3 | import com.intellij.openapi.util.IconLoader; 4 | import javax.swing.*; 5 | 6 | /** 7 | * Created by IntelliJ IDEA. 8 | * User: juzna 9 | * Date: 1/11/12 10 | * Time: 3:13 AM 11 | * To change this template use File | Settings | File Templates. 12 | */ 13 | 14 | public class SvelteIcons { 15 | public static Icon FILETYPE_ICON = IconLoader.getIcon("/icons/svelte.png", SvelteIcons.class); 16 | } -------------------------------------------------------------------------------- /src/test/java/technology/svelte/lexer/LexerToken.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.psi.tree.IElementType; 4 | 5 | public class LexerToken { 6 | IElementType type; 7 | int start, end; 8 | String content; 9 | 10 | public LexerToken(IElementType type, int start, int end, String content) { 11 | this.type = type; 12 | this.start = start; 13 | this.end = end; 14 | this.content = content; 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/main/java/technology/svelte/psi/SvelteFile.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.psi; 2 | 3 | import com.intellij.openapi.fileTypes.FileType; 4 | import com.intellij.psi.impl.PsiFileEx; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | /** 8 | * Created by IntelliJ IDEA. 9 | * User: juzna 10 | * Date: 1/26/12 11 | * Time: 1:35 AM 12 | * To change this template use File | Settings | File Templates. 13 | */ 14 | public interface SvelteFile extends PsiFileEx { 15 | @NotNull 16 | @Override 17 | FileType getFileType(); 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/SvelteElementType.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.psi.tree.IElementType; 4 | import technology.svelte.SvelteLanguage; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | 8 | public class SvelteElementType extends IElementType { 9 | public SvelteElementType(@NotNull String debugName) { 10 | super(debugName, SvelteLanguage.SVELTE_LANGUAGE); 11 | } 12 | 13 | public String toString() { 14 | return "[Svelte] " + super.toString(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/psi/impl/SveltePsiElement.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.psi.impl; 2 | 3 | import com.intellij.extapi.psi.ASTDelegatePsiElement; 4 | import com.intellij.extapi.psi.ASTWrapperPsiElement; 5 | import com.intellij.lang.ASTNode; 6 | import com.intellij.psi.PsiElement; 7 | import com.intellij.psi.impl.source.tree.SharedImplUtil; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * Base of all PsiElements 12 | */ 13 | public class SveltePsiElement extends ASTWrapperPsiElement { 14 | public SveltePsiElement(@NotNull ASTNode astNode) { 15 | super(astNode); 16 | } 17 | 18 | // some common logic should come here 19 | } 20 | -------------------------------------------------------------------------------- /sveltejs-idea-plugin.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/file/SvelteFileViewProviderFactory.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.file; 2 | 3 | import com.intellij.lang.Language; 4 | import com.intellij.openapi.vfs.VirtualFile; 5 | import com.intellij.psi.FileViewProvider; 6 | import com.intellij.psi.FileViewProviderFactory; 7 | import com.intellij.psi.PsiManager; 8 | 9 | 10 | public class SvelteFileViewProviderFactory implements FileViewProviderFactory { 11 | @Override 12 | public FileViewProvider createFileViewProvider(VirtualFile virtualFile, Language language, PsiManager psiManager, boolean physical) { 13 | return new SvelteFileViewProvider(psiManager, virtualFile, physical); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/SvelteSubLexer.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.lexer.FlexAdapter; 4 | import com.intellij.lexer.Lexer; 5 | import com.intellij.lexer.MergingLexerAdapter; 6 | import com.intellij.psi.tree.TokenSet; 7 | 8 | import java.io.Reader; 9 | 10 | 11 | public class SvelteSubLexer extends MergingLexerAdapter { 12 | // To be merged 13 | private static final TokenSet TOKENS_TO_MERGE = TokenSet.create( 14 | SvelteTokenTypes.WHITE_SPACE 15 | ); 16 | 17 | public SvelteSubLexer() { 18 | super(null, TOKENS_TO_MERGE); 19 | // super(new FlexAdapter(new _SvelteSubLexer((Reader) null)), TOKENS_TO_MERGE); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/SvelteLexer.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | 4 | import com.intellij.lexer.FlexAdapter; 5 | import com.intellij.lexer.MergingLexerAdapter; 6 | import com.intellij.psi.tree.IElementType; 7 | import com.intellij.psi.tree.TokenSet; 8 | 9 | import java.io.Reader; 10 | 11 | 12 | public class SvelteLexer extends MergingLexerAdapter { 13 | // To be merged 14 | private static final TokenSet TOKENS_TO_MERGE = TokenSet.create( 15 | SvelteTokenTypes.WHITE_SPACE, 16 | SvelteTokenTypes.TEMPLATE_HTML_TEXT 17 | ); 18 | 19 | public SvelteLexer() { 20 | super(new FlexAdapter(new technology.svelte.lexer._SvelteLexer((Reader) null)), TOKENS_TO_MERGE); 21 | } 22 | } -------------------------------------------------------------------------------- /src/test/resources/data/App.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | {{#if alive}} 5 |
Cat is alive
6 | {{elseif alive === undefined}} 7 | Cat is both alive and not alive 8 | {{else}} 9 | Cat is dead 10 | {{/if}} 11 |
12 | 13 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/psi/impl/MacroAttrImpl.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.psi.impl; 2 | 3 | import com.intellij.lang.ASTNode; 4 | import com.intellij.psi.PsiElement; 5 | import technology.svelte.lexer.SvelteTokenTypes; 6 | 7 | /** 8 | * Curly brackets macro 9 | */ 10 | public class MacroAttrImpl extends SveltePsiElement { 11 | 12 | public MacroAttrImpl(ASTNode node) { 13 | super(node); 14 | } 15 | 16 | public String getMacroName() { 17 | for(PsiElement el: getChildren()) { 18 | if(el.getNode().getElementType() == SvelteTokenTypes.BLOCK_STMT_NAME) return el.getText(); 19 | } 20 | return null; 21 | } 22 | 23 | public PsiElement getParams() { 24 | for(PsiElement el: getChildren()) { 25 | if(el.getNode().getElementType() == SvelteTokenTypes.SVELTE_PARAMS) return el; 26 | } 27 | return null; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | # sveltejs-idea-plugin 2 | Idea plugin for sveltejs components 3 | 4 | ## Usage 5 | Install in Idea via Settings -> Plugins -> Browse repositories -> (search for) Svelte 6 | 7 | Or download it from here and install it manually: https://plugins.jetbrains.com/plugin/10021-svelte 8 | 9 | ## For Plugin Developers 10 | The plugin requires Intellij Ultimate for development, since it depends on the Javascript plugin. 11 | 12 | ### Run the Plugin 13 | To start the plugin from Intellij, execute the runIde task from the list of intellij gradle tasks. It downloads a 2016.2 runtime and executes that version of intellij with the svelte plugin included. 14 | 15 | ### Build Distribution 16 | Run the buildPlugin task. The distribution will be created in build/distributions. Upload to https://plugins.jetbrains.com/plugin/edit?pluginId=10021[Plugin Repository]. 17 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/completion/SvelteCompletionContributor.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.completion; 2 | 3 | import com.intellij.codeInsight.completion.CompletionContributor; 4 | import com.intellij.codeInsight.completion.CompletionType; 5 | import com.intellij.openapi.diagnostic.Logger; 6 | import com.intellij.patterns.StandardPatterns; 7 | import com.intellij.psi.PsiElement; 8 | 9 | /** 10 | * @author Dietrich Schulten - ds@escalon.de 11 | */ 12 | public class SvelteCompletionContributor extends CompletionContributor { 13 | private static final Logger log = Logger.getInstance("SvelteCompletionContributor"); 14 | 15 | 16 | public SvelteCompletionContributor() { 17 | log.info("Created Svelte completion contributor"); 18 | 19 | extend(CompletionType.BASIC, StandardPatterns.instanceOf(PsiElement.class), new KeywordCompletionProvider()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/technology/svelte/lexer/SvelteParsingTest.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.lang.LanguageASTFactory; 4 | import com.intellij.openapi.application.PluginPathManager; 5 | import com.intellij.psi.PsiFile; 6 | import com.intellij.testFramework.ParsingTestCase; 7 | import com.intellij.testFramework.PlatformTestCase; 8 | import technology.svelte.parser.SvelteParserDefinition; 9 | 10 | public class SvelteParsingTest extends ParsingTestCase { 11 | public SvelteParsingTest() { 12 | super("", "svelte", new SvelteParserDefinition()); 13 | } 14 | 15 | @Override 16 | protected void setUp() throws Exception { 17 | super.setUp(); 18 | } 19 | 20 | @Override 21 | protected String getTestDataPath() { 22 | return "src/test/resources/data"; 23 | } 24 | 25 | public void testSample1() throws Exception { doTest(true); } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/psi/impl/SvelteFileImpl.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.psi.impl; 2 | 3 | import com.intellij.extapi.psi.PsiFileBase; 4 | import com.intellij.openapi.fileTypes.FileType; 5 | import com.intellij.psi.FileViewProvider; 6 | import technology.svelte.SvelteLanguage; 7 | import technology.svelte.file.SvelteFileType; 8 | import technology.svelte.psi.SvelteFile; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | public class SvelteFileImpl extends PsiFileBase implements SvelteFile { 12 | public SvelteFileImpl(FileViewProvider viewProvider) { 13 | super(viewProvider, SvelteLanguage.SVELTE_LANGUAGE); 14 | } 15 | 16 | @NotNull 17 | @Override 18 | public FileType getFileType() { 19 | return SvelteFileType.SVELTE_FILE_TYPE; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "SvelteFile:" + getName(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/SvelteLanguage.java: -------------------------------------------------------------------------------- 1 | package technology.svelte; 2 | 3 | 4 | import com.intellij.lang.Language; 5 | import com.intellij.openapi.editor.colors.EditorColorsScheme; 6 | import com.intellij.openapi.editor.highlighter.EditorHighlighter; 7 | import com.intellij.openapi.fileTypes.*; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.openapi.vfs.VirtualFile; 10 | import com.intellij.psi.templateLanguages.TemplateLanguage; 11 | import technology.svelte.editor.SvelteSyntaxHighlighter; 12 | import technology.svelte.editor.SvelteTemplateHighlighter; 13 | import org.jetbrains.annotations.NotNull; 14 | import org.jetbrains.annotations.Nullable; 15 | 16 | 17 | public class SvelteLanguage extends Language implements TemplateLanguage { 18 | // singleton 19 | public static final SvelteLanguage SVELTE_LANGUAGE = new SvelteLanguage(); 20 | 21 | public SvelteLanguage() { 22 | super("Svelte", "application/x-svelte-template"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/psi/impl/MacroNodeImpl.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.psi.impl; 2 | 3 | import com.intellij.extapi.psi.ASTDelegatePsiElement; 4 | import com.intellij.lang.ASTNode; 5 | import com.intellij.psi.PsiElement; 6 | import com.intellij.psi.util.PsiTreeUtil; 7 | import technology.svelte.lexer.SvelteTokenTypes; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | /** 11 | * Curly brackets macro 12 | */ 13 | public class MacroNodeImpl extends SveltePsiElement { 14 | 15 | public MacroNodeImpl(ASTNode node) { 16 | super(node); 17 | } 18 | 19 | public String getMacroName() { 20 | for(PsiElement el: getChildren()) { 21 | if(el.getNode().getElementType() == SvelteTokenTypes.BLOCK_STMT_NAME) return el.getText(); 22 | } 23 | return null; 24 | } 25 | 26 | public PsiElement getParams() { 27 | for(PsiElement el: getChildren()) { 28 | if(el.getNode().getElementType() == SvelteTokenTypes.SVELTE_PARAMS) return el; 29 | } 30 | return null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Dietrich Schulten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/test/resources/data/Sample1.svelte: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | {{#if alive}} 5 |
Cat is alive
6 | {{elseif alive === undefined}} 7 | Cat is both alive and not alive 8 | {{else}} 9 | Cat is dead 10 | {{/if}} 11 |
12 | {{#each rows as row, y}} 13 |
14 | {{#each columns as column, x}} 15 | 16 | {{x + 1}},{{y + 1}}: 17 | {{row[column]}} 18 | 19 | {{/each}} 20 |
21 | {{/each}} 22 |
23 |
24 | 25 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/suppressions/XmlUnboundNsPrefixSuppressionProvider.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.suppressions; 2 | 3 | import com.intellij.codeInspection.DefaultXmlSuppressionProvider; 4 | import com.intellij.lang.Language; 5 | import com.intellij.psi.PsiElement; 6 | import com.intellij.psi.PsiFile; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | public class XmlUnboundNsPrefixSuppressionProvider extends DefaultXmlSuppressionProvider { 10 | 11 | @Override 12 | public boolean isProviderAvailable(@NotNull PsiFile file) { 13 | boolean ret; 14 | Language language = file.getLanguage(); 15 | String languageID = language.getID(); 16 | if ("HTML".equals(languageID)) { 17 | ret = true; 18 | } else { 19 | ret = false; 20 | } 21 | return ret; 22 | } 23 | 24 | @Override 25 | public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String inspectionId) { 26 | boolean ret; 27 | if ("XmlUnboundNsPrefix".equals(inspectionId)) { 28 | ret = true; 29 | } else { 30 | ret = super.isSuppressedFor(element, inspectionId); 31 | } 32 | return ret; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/test/resources/data/sample01.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Application Skeleton 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |

Sample page with only HTML code

33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/test/java/technology/svelte/lexer/AbstractLexerTest.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.ide.plugins.PluginManager; 4 | import com.intellij.lexer.Lexer; 5 | import com.intellij.openapi.application.PluginPathManager; 6 | import com.intellij.openapi.application.ex.PathManagerEx; 7 | import com.intellij.psi.tree.IElementType; 8 | import com.intellij.testFramework.PlatformLiteFixture; 9 | 10 | import java.util.ArrayList; 11 | 12 | 13 | public abstract class AbstractLexerTest extends PlatformLiteFixture { 14 | Lexer lexer; 15 | 16 | protected String getTestDataPath() { 17 | return "src/test/resources/data"; 18 | } 19 | 20 | protected void assertToken(LexerToken token, IElementType type, String content) { 21 | if(type != null) assertEquals(type, token.type); 22 | if(content != null) assertEquals(content, token.content); 23 | } 24 | 25 | protected LexerToken[] lex(String str) { 26 | ArrayList tokens = new ArrayList(); 27 | IElementType el; 28 | 29 | lexer.start(str); 30 | 31 | while((el = lexer.getTokenType()) != null) { 32 | tokens.add(new LexerToken(el, lexer.getTokenStart(), lexer.getTokenEnd(), lexer.getTokenText())); 33 | lexer.advance(); 34 | } 35 | 36 | return tokens.toArray(new LexerToken[tokens.size()]); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/technology/svelte/lexer/SvelteLexerTest.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | 4 | public class SvelteLexerTest extends AbstractLexerTest { 5 | @Override 6 | public void setUp() throws Exception { 7 | super.setUp(); 8 | lexer = new SvelteLexer(); 9 | } 10 | 11 | public void testIfElseEndif() throws Exception { 12 | LexerToken[] tokens = lex("Now you {{#if visible}}see me{{else}}don't{{/if}}"); 13 | 14 | assertToken(tokens[0], SvelteTokenTypes.TEMPLATE_HTML_TEXT, "Now you "); 15 | assertToken(tokens[1], SvelteTokenTypes.OPENING, "{{"); 16 | assertToken(tokens[2], SvelteTokenTypes.OPEN_BLOCK, "#"); 17 | assertToken(tokens[3], SvelteTokenTypes.BLOCK_STMT_NAME, "if"); 18 | assertToken(tokens[4], SvelteTokenTypes.SVELTE_PARAMS, " visible"); 19 | assertToken(tokens[5], SvelteTokenTypes.CLOSING, "}}"); 20 | assertToken(tokens[6], SvelteTokenTypes.TEMPLATE_HTML_TEXT, "see me"); 21 | assertToken(tokens[7], SvelteTokenTypes.OPENING, "{{"); 22 | assertToken(tokens[8], SvelteTokenTypes.BLOCK_STMT_NAME, "else"); 23 | assertToken(tokens[9], SvelteTokenTypes.CLOSING, "}}"); 24 | assertToken(tokens[10], SvelteTokenTypes.TEMPLATE_HTML_TEXT, "don't"); 25 | assertToken(tokens[11], SvelteTokenTypes.OPENING, "{{"); 26 | assertToken(tokens[12], SvelteTokenTypes.CLOSE_BLOCK, "/"); 27 | assertToken(tokens[13], SvelteTokenTypes.BLOCK_STMT_NAME, "if"); 28 | assertToken(tokens[14], SvelteTokenTypes.CLOSING, "}}"); 29 | assertEquals(15, tokens.length); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/SvelteTopLexer.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | 4 | import com.intellij.lexer.FlexAdapter; 5 | import com.intellij.lexer.Lexer; 6 | import com.intellij.lexer.LookAheadLexer; 7 | import com.intellij.lexer.MergingLexerAdapter; 8 | import com.intellij.psi.tree.IElementType; 9 | import com.intellij.psi.tree.TokenSet; 10 | import java.io.Reader; 11 | 12 | 13 | public class SvelteTopLexer extends MergingLexerAdapter { 14 | // To be merged 15 | private static final TokenSet TOKENS_TO_MERGE = TokenSet.create( 16 | SvelteTokenTypes.WHITE_SPACE, 17 | SvelteTokenTypes.TEMPLATE_HTML_TEXT 18 | ); 19 | 20 | public SvelteTopLexer() { 21 | super(null, TOKENS_TO_MERGE); 22 | // super(new LookAheadLexer(new FlexAdapter(new _SvelteTopLexer((Reader) null))) { 23 | // 24 | // @Override 25 | // protected void lookAhead(Lexer lex) { 26 | // IElementType type = lex.getTokenType(); 27 | // if(type == SvelteTokenTypes.N_ATTR) { // n: attr - keep all interesting tokens as they are 28 | // addToken(type); 29 | // lex.advance(); 30 | // 31 | // while (SvelteTokenTypes.nAttrSet.contains(type = lex.getTokenType())) { 32 | // addToken(type); 33 | // lex.advance(); 34 | // } 35 | // } 36 | // 37 | // if(SvelteTokenTypes.nAttrSet.contains(type)) addToken(SvelteTokenTypes.TEMPLATE_HTML_TEXT); // otherwise replace attributes by generic HTML 38 | // else addToken(type); 39 | // 40 | // lex.advance(); 41 | // } 42 | // }, TOKENS_TO_MERGE); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/technology/svelte/codeInsight/attributes/SvelteAttributeDescriptorsProvider.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.codeInsight.attributes; 2 | 3 | import com.intellij.psi.xml.XmlAttribute; 4 | import com.intellij.psi.xml.XmlTag; 5 | import com.intellij.xml.XmlAttributeDescriptor; 6 | import com.intellij.xml.XmlAttributeDescriptorsProvider; 7 | import org.jetbrains.annotations.Nullable; 8 | 9 | import java.util.Arrays; 10 | import java.util.LinkedHashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | public class SvelteAttributeDescriptorsProvider implements XmlAttributeDescriptorsProvider { 15 | 16 | private final List svelteDirectives = Arrays.asList("on:", "bind:", "ref:"); 17 | 18 | @Override 19 | public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag context) { 20 | final Map result = new LinkedHashMap<>(); 21 | XmlAttribute[] attributes = context.getAttributes(); 22 | for (XmlAttribute attribute : attributes) { 23 | String attributeName = attribute.getName(); 24 | if(attributeName 25 | .contains(":")) { 26 | result.put(attributeName, new SvelteAttributeDescriptor(context.getProject(), attributeName)); 27 | } 28 | } 29 | return result.values() 30 | .toArray(new XmlAttributeDescriptor[result.size()]); 31 | } 32 | 33 | @Nullable 34 | @Override 35 | public XmlAttributeDescriptor getAttributeDescriptor(String attributeName, XmlTag context) { 36 | XmlAttributeDescriptor ret; 37 | if (attributeName.contains(":")) { 38 | ret = new SvelteAttributeDescriptor(context.getProject(), attributeName); 39 | } else { 40 | ret = null; 41 | } 42 | return ret; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/LayeredLexer.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.lexer.Lexer; 4 | import com.intellij.lexer.LookAheadLexer; 5 | import com.intellij.psi.tree.IElementType; 6 | import com.intellij.psi.tree.TokenSet; 7 | 8 | /** 9 | * Uses base-lexer to get big chunks, and several of them (typesToLex) are then lexed into smaller tokens using sub-lexer. 10 | * 11 | * Useful in some templating languages, where you lex-out whole chunks of your language, which is then lexed by another lexer. 12 | * 13 | * @author Jan Dolecek 14 | */ 15 | public class LayeredLexer extends LookAheadLexer { 16 | protected Lexer subLexer; 17 | protected TokenSet typesToLex; 18 | 19 | /** 20 | * @param baseLexer Lexer which creates big chunks 21 | * @param subLexer Lexer which is applied to chunks from previous lexer 22 | * @param typesToLex Only these chunks from base-lexer are processed 23 | */ 24 | public LayeredLexer(Lexer baseLexer, Lexer subLexer, TokenSet typesToLex) { 25 | super(baseLexer); 26 | this.subLexer = subLexer; 27 | this.typesToLex = typesToLex; 28 | } 29 | 30 | @Override 31 | protected void lookAhead(Lexer baseLexer) { 32 | if(typesToLex.contains(baseLexer.getTokenType())) { 33 | // Parse all sub tokens 34 | IElementType subToken; 35 | subLexer.start(baseLexer.getBufferSequence(), baseLexer.getTokenStart(), baseLexer.getTokenEnd()); 36 | while((subToken = subLexer.getTokenType()) != null) { 37 | addToken(subLexer.getTokenEnd(), subToken); 38 | subLexer.advance(); 39 | } 40 | 41 | baseLexer.advance(); 42 | 43 | } else { 44 | addToken(baseLexer.getTokenType()); 45 | baseLexer.advance(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/editor/SvelteTemplateHighlighter.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.editor; 2 | 3 | import com.intellij.lang.Language; 4 | import com.intellij.openapi.editor.colors.EditorColorsScheme; 5 | import com.intellij.openapi.editor.ex.util.LayerDescriptor; 6 | import com.intellij.openapi.editor.ex.util.LayeredLexerEditorHighlighter; 7 | import com.intellij.openapi.fileTypes.FileType; 8 | import com.intellij.openapi.fileTypes.StdFileTypes; 9 | import com.intellij.openapi.fileTypes.SyntaxHighlighter; 10 | import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory; 11 | import com.intellij.openapi.project.Project; 12 | import com.intellij.openapi.vfs.VirtualFile; 13 | import com.intellij.psi.templateLanguages.TemplateDataLanguageMappings; 14 | import technology.svelte.lexer.SvelteTokenTypes; 15 | import org.jetbrains.annotations.NotNull; 16 | import org.jetbrains.annotations.Nullable; 17 | 18 | /** 19 | * Layered highlighter - uses SvelteSyntaxHighlighter as main one, and second for outer text (HTML) 20 | */ 21 | public class SvelteTemplateHighlighter extends LayeredLexerEditorHighlighter { 22 | public SvelteTemplateHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile, @NotNull EditorColorsScheme colors) { 23 | // create main highlighter 24 | super(new SvelteSyntaxHighlighter(), colors); 25 | 26 | // highlighter for outer lang 27 | FileType type = null; 28 | if(project == null || virtualFile == null) { 29 | type = StdFileTypes.PLAIN_TEXT; 30 | } else { 31 | Language language = TemplateDataLanguageMappings.getInstance(project).getMapping(virtualFile); 32 | if(language != null) type = language.getAssociatedFileType(); 33 | if(type == null) type = StdFileTypes.HTML; 34 | } 35 | 36 | SyntaxHighlighter outerHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter(type, project, virtualFile); 37 | 38 | registerLayer(SvelteTokenTypes.TEMPLATE_HTML_TEXT, new LayerDescriptor(outerHighlighter, "")); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/file/SvelteFileTypeFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 Joachim Ansorg, mail@ansorg-it.com 3 | * File: BashFileTypeLoader.java, Class: BashFileTypeLoader 4 | * Last modified: 2010-03-24 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package technology.svelte.file; 20 | 21 | import com.intellij.framework.FrameworkAvailabilityCondition; 22 | import com.intellij.ide.DataManager; 23 | import com.intellij.openapi.actionSystem.DataContext; 24 | import com.intellij.openapi.actionSystem.DataKeys; 25 | import com.intellij.openapi.fileTypes.FileTypeConsumer; 26 | import com.intellij.openapi.fileTypes.FileTypeFactory; 27 | import com.intellij.openapi.project.Project; 28 | import com.intellij.openapi.roots.ModuleRootManager; 29 | import com.intellij.util.Consumer; 30 | import org.jetbrains.annotations.NotNull; 31 | 32 | 33 | public class SvelteFileTypeFactory extends FileTypeFactory { 34 | public void createFileTypes(@NotNull FileTypeConsumer consumer) { 35 | consumer.consume(SvelteFileType.SVELTE_FILE_TYPE, SvelteFileType.DEFAULT_EXTENSION+";html"); 36 | // DataManager.getInstance() 37 | // .getDataContextFromFocus() 38 | // .doWhenDone((Consumer) dataContext -> { 39 | // Project project = DataKeys.PROJECT.getData(dataContext); 40 | // consumer.consume(SvelteFileType.SVELTE_FILE_TYPE, "html"); 41 | // }) 42 | // .getResult(); 43 | // Project project = (Project) dataContext.getData(DataConstants.PROJECT); 44 | // 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/editor/SvelteSyntaxHighlighter.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.editor; 2 | 3 | 4 | import com.intellij.lexer.Lexer; 5 | import com.intellij.openapi.editor.DefaultLanguageHighlighterColors; 6 | import com.intellij.openapi.editor.HighlighterColors; 7 | import com.intellij.openapi.editor.colors.TextAttributesKey; 8 | import com.intellij.openapi.fileTypes.SyntaxHighlighterBase; 9 | import com.intellij.psi.tree.IElementType; 10 | import com.intellij.psi.tree.TokenSet; 11 | import technology.svelte.lexer.SvelteLexer; 12 | import technology.svelte.lexer.SvelteTokenTypes; 13 | import org.jetbrains.annotations.NotNull; 14 | 15 | import java.util.HashMap; 16 | import java.util.Map; 17 | 18 | 19 | public class SvelteSyntaxHighlighter extends SyntaxHighlighterBase { 20 | 21 | public static final String BAD_CHARACTER_ID = "Bad character"; 22 | public static final TextAttributesKey BAD_CHARACTER = TextAttributesKey.createTextAttributesKey(BAD_CHARACTER_ID, HighlighterColors.BAD_CHARACTER.getDefaultAttributes() 23 | .clone()); 24 | 25 | public static final String TEMPLATE_ID = "Template"; 26 | public static final TextAttributesKey TEMPLATE = TextAttributesKey.createTextAttributesKey(TEMPLATE_ID, DefaultLanguageHighlighterColors.TEMPLATE_LANGUAGE_COLOR); 27 | 28 | 29 | // Groups of IElementType's 30 | public static final TokenSet sBAD = TokenSet.create(SvelteTokenTypes.BAD_CHARACTER); 31 | public static final TokenSet sTEMPLATE = TokenSet.create( 32 | SvelteTokenTypes.OPENING, 33 | SvelteTokenTypes.CLOSING, 34 | SvelteTokenTypes.BLOCK_STMT_NAME, 35 | SvelteTokenTypes.OPEN_BLOCK, 36 | SvelteTokenTypes.CLOSE_BLOCK); 37 | 38 | // Static container 39 | private static final Map ATTRIBUTES = new HashMap(); 40 | 41 | 42 | // Fill in the map 43 | static { 44 | fillMap(ATTRIBUTES, sBAD, BAD_CHARACTER); 45 | fillMap(ATTRIBUTES, sTEMPLATE, TEMPLATE); 46 | } 47 | 48 | 49 | @NotNull 50 | @Override 51 | public Lexer getHighlightingLexer() { 52 | return new SvelteLexer(); 53 | } 54 | 55 | @NotNull 56 | @Override 57 | public TextAttributesKey[] getTokenHighlights(IElementType type) { 58 | TextAttributesKey[] x = pack(ATTRIBUTES.get(type)); 59 | return x; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/codeInsight/attributes/SvelteAttributeDescriptor.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.codeInsight.attributes; 2 | 3 | import com.intellij.lang.javascript.psi.*; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.psi.PsiElement; 6 | import com.intellij.psi.stubs.StubIndexKey; 7 | import com.intellij.util.ArrayUtil; 8 | import com.intellij.xml.impl.BasicXmlAttributeDescriptor; 9 | import com.intellij.xml.impl.XmlAttributeDescriptorEx; 10 | import org.jetbrains.annotations.NonNls; 11 | import org.jetbrains.annotations.NotNull; 12 | import org.jetbrains.annotations.Nullable; 13 | 14 | public class SvelteAttributeDescriptor extends BasicXmlAttributeDescriptor implements XmlAttributeDescriptorEx { 15 | 16 | private final Project project; 17 | private final String attributeName; 18 | private final StubIndexKey index; 19 | 20 | public SvelteAttributeDescriptor(final Project project, String attributeName) { 21 | this.project = project; 22 | this.attributeName = attributeName; 23 | this.index = null; 24 | } 25 | 26 | @Nullable 27 | @Override 28 | public String handleTargetRename(@NotNull @NonNls String newTargetName) { 29 | return newTargetName; 30 | } 31 | 32 | @Override 33 | public boolean isRequired() { 34 | return false; 35 | } 36 | 37 | @Override 38 | public boolean hasIdType() { 39 | return false; 40 | } 41 | 42 | @Override 43 | public boolean hasIdRefType() { 44 | return false; 45 | } 46 | 47 | @Override 48 | public boolean isEnumerated() { 49 | return index != null; 50 | } 51 | 52 | @Override 53 | public PsiElement getDeclaration() { 54 | return null; 55 | } 56 | 57 | @Override 58 | public String getName() { 59 | return attributeName; 60 | } 61 | 62 | @Override 63 | public void init(PsiElement element) { 64 | } 65 | 66 | @Override 67 | public Object[] getDependences() { 68 | return ArrayUtil.EMPTY_OBJECT_ARRAY; 69 | } 70 | 71 | @Override 72 | public boolean isFixed() { 73 | return false; 74 | } 75 | 76 | @Override 77 | public String getDefaultValue() { 78 | return null; 79 | } 80 | 81 | @Override 82 | public String[] getEnumeratedValues() { 83 | return ArrayUtil.EMPTY_STRING_ARRAY; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/lexer/SvelteTokenTypes.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.psi.TokenType; 4 | import com.intellij.psi.templateLanguages.TemplateDataElementType; 5 | import com.intellij.psi.tree.IElementType; 6 | import com.intellij.psi.tree.IFileElementType; 7 | import com.intellij.psi.tree.TokenSet; 8 | import technology.svelte.SvelteLanguage; 9 | 10 | /** 11 | * @author Dietrich Schulten - ds@escalon.de 12 | */ 13 | public interface SvelteTokenTypes { 14 | // large grain parsing 15 | public static final IElementType TEMPLATE_HTML_TEXT = 16 | new SvelteElementType("SVELTE_TEMPLATE_HTML_TEXT"); // produced by lexer for all HTML code 17 | 18 | 19 | IElementType BAD_CHARACTER = TokenType.BAD_CHARACTER; 20 | IElementType WHITE_SPACE = TokenType.WHITE_SPACE; 21 | 22 | IElementType OPENING = new SvelteElementType("OPENING"); 23 | IElementType CLOSING = new SvelteElementType("CLOSING"); 24 | 25 | IElementType OPEN_BLOCK = new SvelteElementType("OPEN-BLOCK"); 26 | IElementType CLOSE_BLOCK = new SvelteElementType("CLOSE-BLOCK"); 27 | 28 | 29 | IElementType SVELTE_PARAMS = new SvelteElementType("PARAMS"); 30 | IElementType BLOCK_STMT_NAME = new SvelteElementType("BLOCK_STMT_NAME"); 31 | IElementType MACRO = new SvelteElementType("MACRO"); 32 | 33 | IFileElementType FILE = new IFileElementType("FILE", SvelteLanguage.SVELTE_LANGUAGE); 34 | 35 | public static final IElementType OUTER_ELEMENT_TYPE = new SvelteElementType("SVELTE_FRAGMENT"); 36 | public static final TemplateDataElementType TEMPLATE_DATA = 37 | new TemplateDataElementType("SVELTE_TEMPLATE_DATA", SvelteLanguage.SVELTE_LANGUAGE, TEMPLATE_HTML_TEXT, OUTER_ELEMENT_TYPE); 38 | // IElementType PARAMS = new SvelteElementType("PARAMS"); 39 | 40 | // IElementType TAG_START = new SvelteElementType("TAG-START"); 41 | // IElementType TAG_CLOSING = new SvelteElementType("TAG-CLOSING"); 42 | // IElementType END_TAG = new SvelteElementType("END_TAG"); 43 | // IElementType TAG_NAME = new SvelteElementType("TAG-NAME"); 44 | // IElementType OUTER_TEXT = new SvelteElementType("OUTER-TEST"); 45 | // IElementType SVELTE_STRING = new SvelteElementType("SVELTE-STRING"); 46 | // 47 | // IElementType N_ATTR = new SvelteElementType("N-ATTR"); 48 | // IElementType ATTR = new SvelteElementType("ATTR"); 49 | // IElementType ATTR_VALUE = new SvelteElementType("ATTR-VALUE"); 50 | 51 | 52 | 53 | 54 | TokenSet WHITESPACES = TokenSet.create(WHITE_SPACE); 55 | TokenSet STRING_LITERALS = TokenSet.create(); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | de.escalon.idea.plugin.svelte 3 | Svelte 4 | from.build.gradle 5 | Escalon Systementwicklung, 6 | Dietrich Schulten 7 | 8 | 9 | Svelte components. 11 | ]]> 12 | 13 | 14 | 16 |
17 |
0.0.1
18 |
Svelte directives not considered elements with unbound namespace 19 |
0.0.2
20 |
Compatibility extended to 2017.1 21 |
0.0.3
22 |
Compatibility extended to 2017.2 23 |
0.0.4
24 |
Minimal parsing, syntax-highlighting and code completion for Svelte templates 25 |
26 | 27 | ]]> 28 |
29 | 30 | 31 | 32 | 33 | 35 | 38 | 39 | JavaScript 40 | com.intellij.modules.lang 41 | 42 | 43 | 46 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
60 | -------------------------------------------------------------------------------- /src/main/jflex/technology/svelte/lexer/svelte.flex: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | import com.intellij.lexer.FlexLexer; 4 | import com.intellij.psi.tree.IElementType; 5 | import static technology.svelte.lexer.SvelteTokenTypes.*; 6 | 7 | %% 8 | 9 | %class _SvelteLexer 10 | %implements FlexLexer 11 | %unicode 12 | %function advance 13 | %type IElementType 14 | %eof{ return; 15 | %eof} 16 | 17 | STRING = \'(\\.|[^\'\\])*\'|\"(\\.|[^\"\\])*\" 18 | CRLF = \n | \r | \r\n 19 | WHITE_SPACE_CHAR = [\ \n\r\t\f] 20 | OPENING = "{{" 21 | CLOSING = "}}" 22 | BLOCK_STMT_NAME = [^\'\"{} ]+ 23 | 24 | 25 | %state AFTER_LD 26 | %state IN_SVELTE 27 | %state IN_TAG 28 | %state TAG_STARTED 29 | %state ATTR_NAME 30 | %state IN_ATTR_VALUE 31 | %state IN_BLOCK_STMT 32 | 33 | %% 34 | 35 | 36 | 37 | 38 | { 39 | {OPENING} { yybegin(AFTER_LD); return OPENING; } 40 | // "=\"'] | [:letter:] | [:digit:] { return TEMPLATE_HTML_TEXT; } 45 | } 46 | 47 | { 48 | "#" { yybegin(IN_BLOCK_STMT); return OPEN_BLOCK; } 49 | "else" { yybegin(IN_SVELTE); return BLOCK_STMT_NAME; } 50 | "elseif" { yybegin(IN_SVELTE); return BLOCK_STMT_NAME; } 51 | "/" { yybegin(IN_BLOCK_STMT); return CLOSE_BLOCK; } 52 | [^#] { yybegin(IN_SVELTE); } 53 | } 54 | 55 | { 56 | {BLOCK_STMT_NAME} { yybegin(IN_SVELTE); return BLOCK_STMT_NAME; } 57 | {CLOSING} { yybegin(YYINITIAL); return CLOSING; } 58 | } 59 | 60 | { 61 | [^\}]+ { return SVELTE_PARAMS; } 62 | {CLOSING} { yybegin(YYINITIAL); return CLOSING; } 63 | } 64 | 65 | // { 66 | // [a-z0-9:]+ { yybegin(IN_TAG); return TEMPLATE_HTML_TEXT; /* TAG_NAME; */ } 67 | //} 68 | // 69 | // { 70 | // [a-z0-9]+(={STRING})? { return TEMPLATE_HTML_TEXT; } 71 | // 72 | // ">" { yybegin(YYINITIAL); return TEMPLATE_HTML_TEXT; /* END_TAG;*/ } 73 | //} 74 | // 75 | // { 76 | // "=" { yybegin(IN_ATTR_VALUE); return TEMPLATE_HTML_TEXT; } 77 | //} 78 | // 79 | // { 80 | // {STRING} { yybegin(IN_TAG); return ATTR_VALUE; } 81 | //} 82 | 83 | 84 | 85 | //{WHITE_SPACE_CHAR}+ { return WHITE_SPACE; } 86 | . { return BAD_CHARACTER; } -------------------------------------------------------------------------------- /src/main/java/technology/svelte/editor/SvelteColorsPage.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.editor; 2 | 3 | import com.intellij.openapi.editor.colors.TextAttributesKey; 4 | import com.intellij.openapi.fileTypes.SyntaxHighlighter; 5 | import com.intellij.openapi.options.colors.AttributesDescriptor; 6 | import com.intellij.openapi.options.colors.ColorDescriptor; 7 | import com.intellij.openapi.options.colors.ColorSettingsPage; 8 | import org.jetbrains.annotations.NotNull; 9 | import technology.svelte.SvelteIcons; 10 | 11 | import javax.swing.*; 12 | import java.util.Map; 13 | 14 | 15 | /** 16 | * Settings dialog for colors 17 | */ 18 | public class SvelteColorsPage implements ColorSettingsPage { 19 | public static final AttributesDescriptor[] ATTRS = { 20 | new AttributesDescriptor("colors.bad.character", SvelteSyntaxHighlighter.BAD_CHARACTER), 21 | // new AttributesDescriptor("colors.comment", SvelteSyntaxHighlighter.COMMENT), 22 | new AttributesDescriptor("colors.brackets", SvelteSyntaxHighlighter.TEMPLATE), 23 | // new AttributesDescriptor("colors.identifier", SvelteSyntaxHighlighter.IDENTIFIER), 24 | // new AttributesDescriptor("colors.attribute", SvelteSyntaxHighlighter.ATTR), 25 | // new AttributesDescriptor("colors.keyword", SvelteSyntaxHighlighter.KEYWORD), 26 | // new AttributesDescriptor("colors.variable", SvelteSyntaxHighlighter.VARIABLE), 27 | // new AttributesDescriptor("colors.string", SvelteSyntaxHighlighter.STRING), 28 | // new AttributesDescriptor("colors.punctuation", SvelteSyntaxHighlighter.PUNCTUATION), 29 | }; 30 | 31 | @NotNull 32 | @Override 33 | public String getDisplayName() { 34 | return "Svelte"; 35 | } 36 | 37 | @Override 38 | public Icon getIcon() { 39 | return SvelteIcons.FILETYPE_ICON; 40 | } 41 | 42 | @NotNull 43 | @Override 44 | public AttributesDescriptor[] getAttributeDescriptors() { 45 | return ATTRS; 46 | } 47 | 48 | @NotNull 49 | @Override 50 | public ColorDescriptor[] getColorDescriptors() { 51 | return new ColorDescriptor[0]; 52 | } 53 | 54 | @NotNull 55 | @Override 56 | public SyntaxHighlighter getHighlighter() { 57 | return new SvelteSyntaxHighlighter(); 58 | } 59 | 60 | @NotNull 61 | @Override 62 | public String getDemoText() { 63 | return "
\n" + 64 | " \n" + 65 | " \n" + 66 | " {{#if alive}}\n" + 67 | "
Cat is alive
\n" + 68 | " {{elseif alive === undefined}}\n" + 69 | " Cat is both alive and not alive\n" + 70 | " {{else}}\n" + 71 | " Cat is dead\n" + 72 | " {{/if}}\n" + 73 | "
"; 74 | } 75 | 76 | @Override 77 | public Map getAdditionalHighlightingTagToDescriptorMap() { 78 | return null; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/file/SvelteFileType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010 Joachim Ansorg, mail@ansorg-it.com 3 | * File: BashFileType.java, Class: BashFileType 4 | * Last modified: 2010-06-30 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | package technology.svelte.file; 20 | 21 | 22 | import com.intellij.icons.AllIcons; 23 | import com.intellij.openapi.diagnostic.Logger; 24 | import com.intellij.openapi.editor.colors.EditorColorsScheme; 25 | import com.intellij.openapi.editor.highlighter.EditorHighlighter; 26 | import com.intellij.openapi.fileTypes.*; 27 | import com.intellij.openapi.project.Project; 28 | import com.intellij.openapi.vfs.VirtualFile; 29 | import technology.svelte.Svelte; 30 | import technology.svelte.SvelteIcons; 31 | import technology.svelte.SvelteLanguage; 32 | import technology.svelte.editor.SvelteTemplateHighlighter; 33 | import org.jetbrains.annotations.NotNull; 34 | import org.jetbrains.annotations.Nullable; 35 | 36 | import javax.swing.*; 37 | import java.util.Arrays; 38 | import java.util.List; 39 | 40 | 41 | 42 | public class SvelteFileType extends LanguageFileType { 43 | private static final Logger LOG = Logger.getInstance("#SvelteFileType"); 44 | public static final SvelteFileType SVELTE_FILE_TYPE = new SvelteFileType(); 45 | 46 | 47 | public static final String DEFAULT_EXTENSION = "svelte"; 48 | 49 | 50 | /** 51 | * All extensions which are associated with this plugin. 52 | */ 53 | public static final String[] extensions = {DEFAULT_EXTENSION}; 54 | 55 | 56 | protected SvelteFileType() { 57 | super(SvelteLanguage.SVELTE_LANGUAGE); 58 | 59 | // register highlighter - lazy singleton factory 60 | FileTypeEditorHighlighterProviders.INSTANCE.addExplicitExtension(this, new EditorHighlighterProvider() { 61 | public EditorHighlighter getEditorHighlighter(@Nullable Project project, 62 | @NotNull FileType fileType, 63 | @Nullable VirtualFile virtualFile, 64 | @NotNull EditorColorsScheme editorColorsScheme) { 65 | return new SvelteTemplateHighlighter(project, virtualFile, editorColorsScheme); 66 | } 67 | }); 68 | } 69 | 70 | @NotNull 71 | public String getName() { 72 | return Svelte.languageName; 73 | } 74 | 75 | @NotNull 76 | public String getDescription() { 77 | return Svelte.languageDescription; 78 | } 79 | 80 | @NotNull 81 | public String getDefaultExtension() { 82 | return DEFAULT_EXTENSION; 83 | } 84 | 85 | public Icon getIcon() { 86 | return AllIcons.FileTypes.Html; 87 | } 88 | 89 | @Override 90 | public boolean isJVMDebuggingSupported() { 91 | return false; 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/file/SvelteFileViewProvider.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.file; 2 | 3 | import com.intellij.lang.Language; 4 | import com.intellij.lang.LanguageParserDefinitions; 5 | import com.intellij.openapi.fileTypes.FileType; 6 | import com.intellij.openapi.fileTypes.PlainTextLanguage; 7 | import com.intellij.openapi.fileTypes.StdFileTypes; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.openapi.vfs.VirtualFile; 10 | import com.intellij.psi.LanguageSubstitutors; 11 | import com.intellij.psi.MultiplePsiFilesPerDocumentFileViewProvider; 12 | import com.intellij.psi.PsiFile; 13 | import com.intellij.psi.PsiManager; 14 | import com.intellij.psi.impl.source.PsiFileImpl; 15 | import com.intellij.psi.templateLanguages.TemplateDataLanguageMappings; 16 | import com.intellij.psi.templateLanguages.TemplateLanguage; 17 | import com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider; 18 | import technology.svelte.SvelteLanguage; 19 | import technology.svelte.lexer.SvelteTokenTypes; 20 | import gnu.trove.THashSet; 21 | import org.jetbrains.annotations.NotNull; 22 | 23 | import java.util.Arrays; 24 | import java.util.EnumSet; 25 | import java.util.HashSet; 26 | import java.util.Set; 27 | 28 | 29 | public class SvelteFileViewProvider extends MultiplePsiFilesPerDocumentFileViewProvider implements TemplateLanguageFileViewProvider { 30 | // main language of the file (HTML) 31 | private final Language myTemplateDataLanguage; 32 | 33 | 34 | // default constructor from parent 35 | public SvelteFileViewProvider(PsiManager manager, VirtualFile file, boolean physical) { 36 | super(manager, file, physical); 37 | 38 | // get the main language of the file 39 | Language dataLang = TemplateDataLanguageMappings.getInstance(manager.getProject()).getMapping(file); 40 | if(dataLang == null) dataLang = StdFileTypes.HTML.getLanguage(); 41 | 42 | // some magic? 43 | if(dataLang instanceof TemplateLanguage) { 44 | myTemplateDataLanguage = PlainTextLanguage.INSTANCE; 45 | } else { 46 | myTemplateDataLanguage = LanguageSubstitutors.INSTANCE.substituteLanguage(dataLang, file, manager.getProject()); 47 | } 48 | } 49 | 50 | // constructor to be used by self 51 | public SvelteFileViewProvider(PsiManager psiManager, VirtualFile virtualFile, boolean physical, Language myTemplateDataLanguage) { 52 | super(psiManager, virtualFile, physical); 53 | this.myTemplateDataLanguage = myTemplateDataLanguage; 54 | } 55 | 56 | 57 | @NotNull 58 | @Override 59 | public Language getBaseLanguage() { 60 | return SvelteLanguage.SVELTE_LANGUAGE; 61 | } 62 | 63 | @NotNull 64 | @Override 65 | public Language getTemplateDataLanguage() { 66 | return myTemplateDataLanguage; 67 | } 68 | 69 | @NotNull 70 | @Override 71 | public Set getLanguages() { 72 | return new THashSet(Arrays.asList(new Language[]{ SvelteLanguage.SVELTE_LANGUAGE, myTemplateDataLanguage })); 73 | } 74 | 75 | @Override 76 | protected MultiplePsiFilesPerDocumentFileViewProvider cloneInner(VirtualFile virtualFile) { 77 | return new SvelteFileViewProvider(getManager(), virtualFile, false, myTemplateDataLanguage); 78 | } 79 | 80 | 81 | @Override 82 | protected PsiFile createFile(Language lang) { 83 | // creating file for main lang (HTML) 84 | if(lang == myTemplateDataLanguage) { 85 | PsiFileImpl file = (PsiFileImpl) LanguageParserDefinitions.INSTANCE.forLanguage(lang).createFile(this); 86 | file.setContentElementType(SvelteTokenTypes.TEMPLATE_DATA); 87 | return file; 88 | } else if(lang == SvelteLanguage.SVELTE_LANGUAGE) { 89 | return LanguageParserDefinitions.INSTANCE.forLanguage(lang).createFile(this); 90 | } else { 91 | return null; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/parser/SvelteParser.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.parser; 2 | 3 | import com.intellij.lang.ASTNode; 4 | import com.intellij.lang.PsiBuilder; 5 | import com.intellij.lang.PsiBuilder.Marker; 6 | import com.intellij.lang.PsiParser; 7 | import com.intellij.psi.tree.IElementType; 8 | import technology.svelte.lexer.SvelteTokenTypes; 9 | import org.jetbrains.annotations.NotNull; 10 | 11 | public class SvelteParser implements PsiParser { 12 | @NotNull 13 | @Override 14 | public ASTNode parse(IElementType root, PsiBuilder builder) { 15 | Marker marker = builder.mark(); 16 | 17 | // Process all tokens 18 | while(!builder.eof()) { 19 | IElementType type = builder.getTokenType(); 20 | 21 | if(type == SvelteTokenTypes.OPENING) parseTemplate(builder); 22 | // else if(type == SvelteTokenTypes.N_ATTR) parseNAttr(builder); 23 | 24 | builder.advanceLexer(); // move to next token 25 | } 26 | 27 | marker.done(root); 28 | return builder.getTreeBuilt(); 29 | } 30 | 31 | // {macro ...} 32 | private void parseTemplate(PsiBuilder builder) { 33 | Marker templateStart = builder.mark(); 34 | builder.advanceLexer(); 35 | 36 | // is there a name? 37 | String tagName = null; 38 | if(builder.getTokenType() == SvelteTokenTypes.BLOCK_STMT_NAME) { 39 | Marker macroNameMark = builder.mark(); 40 | tagName = builder.getTokenText(); 41 | builder.advanceLexer(); 42 | macroNameMark.done(SvelteTokenTypes.BLOCK_STMT_NAME); 43 | } 44 | 45 | // params 46 | Marker paramsMark = builder.mark(); 47 | // parseParams(tagName, builder, SvelteTokenTypes.CLOSING); 48 | while((builder.getTokenType() != SvelteTokenTypes.CLOSING) && !builder.eof()) { 49 | builder.advanceLexer(); 50 | } 51 | paramsMark.done(SvelteTokenTypes.SVELTE_PARAMS); 52 | 53 | // finish him 54 | if(builder.getTokenType() == SvelteTokenTypes.CLOSING) { 55 | builder.advanceLexer(); 56 | } 57 | templateStart.done(SvelteTokenTypes.MACRO); 58 | } 59 | 60 | // n:link="something" 61 | // n:link=something 62 | // private void parseNAttr(PsiBuilder builder) { 63 | // Marker start = builder.mark(); 64 | // builder.advanceLexer(); 65 | // 66 | // // Process name 67 | // String attrName = null; 68 | // if(builder.getTokenType() == SvelteTokenTypes.ATTR_NAME) { 69 | // Marker macroName = builder.mark(); 70 | // attrName = "@" + builder.getTokenText(); 71 | // builder.advanceLexer(); 72 | // macroName.done(SvelteTokenTypes.MACRO_NAME); 73 | // } 74 | // 75 | // if(builder.getTokenType() == SvelteTokenTypes.N_ATTR_EQ) builder.advanceLexer(); 76 | // 77 | // boolean inQuotes; 78 | // if(builder.getTokenType() == SvelteTokenTypes.N_QUOTE) { 79 | // inQuotes = true; 80 | // builder.advanceLexer(); 81 | // } else inQuotes = false; 82 | // 83 | // 84 | // // Process value 85 | // Marker value = builder.mark(); 86 | // parseParams(attrName, builder, inQuotes ? SvelteTokenTypes.N_QUOTE : SvelteTokenTypes.TEMPLATE_HTML_TEXT); 87 | // value.done(SvelteTokenTypes.PARAMS); 88 | // 89 | // if(inQuotes && builder.getTokenType() == SvelteTokenTypes.N_QUOTE) { 90 | // builder.advanceLexer(); 91 | // } 92 | // 93 | // start.done(SvelteTokenTypes.MACRO_ATTR); 94 | // } 95 | // 96 | // // custom params 97 | // private void parseParams(String macroName, PsiBuilder builder, IElementType closing) { 98 | // // just process it atm 99 | // while(builder.getTokenType() != closing && !builder.eof()) { 100 | // builder.advanceLexer(); 101 | // } 102 | // 103 | // } 104 | } 105 | -------------------------------------------------------------------------------- /src/test/java/technology/svelte/lexer/SvelteTopLexerTest.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.lexer; 2 | 3 | 4 | import com.intellij.openapi.util.io.FileUtil; 5 | import com.intellij.psi.impl.DebugUtil; 6 | 7 | import java.io.File; 8 | 9 | public class SvelteTopLexerTest extends AbstractLexerTest { 10 | 11 | @Override 12 | protected void setUp() throws Exception { 13 | super.setUp(); 14 | lexer = new SvelteLexer(); 15 | } 16 | 17 | // public void testMacro01() throws Exception { 18 | // LexerToken[] tokens = lex("Ahoj {$jmeno}"); 19 | // 20 | // assertEquals(4, tokens.length); 21 | // assertToken(tokens[0], SvelteTokenTypes.TEMPLATE_HTML_TEXT, "Ahoj "); 22 | // assertToken(tokens[1], SvelteTokenTypes.OPENING, "{"); 23 | // assertToken(tokens[2], SvelteTokenTypes.BLOCK_STMT_NAME, "$jmeno"); 24 | // assertToken(tokens[3], SvelteTokenTypes.CLOSING, "}"); 25 | // } 26 | 27 | // public void testMacro02() throws Exception { 28 | // LexerToken[] tokens = lex("Ahoj {if $jmeno} something {else} list {/endif}"); 29 | // 30 | // assertToken(tokens[2], SvelteTokenTypes.BLOCK_STMT_NAME, "if"); 31 | // assertToken(tokens[3], SvelteTokenTypes.WHITE_SPACE, " "); 32 | // assertToken(tokens[4], SvelteTokenTypes.SVELTE_PARAMS, "$jmeno"); 33 | // } 34 | 35 | // public void testNAttr01() throws Exception { 36 | // String[] templates = new String[] { 37 | // "Ahoj ", 38 | // "Ahoj " 39 | // }; 40 | // 41 | // for(String tpl: templates) { 42 | // LexerToken[] tokens = lex(tpl); 43 | // 44 | // assertToken(tokens[0], SvelteTokenTypes.TEMPLATE_HTML_TEXT, null); 45 | // assertToken(tokens[1], SvelteTokenTypes.N_ATTR, "n:"); 46 | // assertToken(tokens[2], SvelteTokenTypes.ATTR_NAME, "href"); 47 | // assertToken(tokens[3], SvelteTokenTypes.N_ATTR_EQ, "="); 48 | // assertToken(tokens[4], SvelteTokenTypes.N_QUOTE, null); 49 | // assertToken(tokens[5], SvelteTokenTypes.N_ATTR_VALUE, "Novak:detail!"); 50 | // assertToken(tokens[6], SvelteTokenTypes.N_QUOTE, null); 51 | // assertToken(tokens[7], SvelteTokenTypes.TEMPLATE_HTML_TEXT, null); 52 | // assertEquals(8, tokens.length); 53 | // } 54 | // } 55 | 56 | // public void testNAttr02() throws Exception { 57 | // String tpl = "Ahoj "; 58 | // 59 | // LexerToken[] tokens = lex(tpl); 60 | // 61 | // assertToken(tokens[0], SvelteTokenTypes.TEMPLATE_HTML_TEXT, null); 62 | // assertToken(tokens[1], SvelteTokenTypes.N_ATTR, "n:"); 63 | // assertToken(tokens[2], SvelteTokenTypes.ATTR_NAME, "href"); 64 | // assertToken(tokens[3], SvelteTokenTypes.N_ATTR_EQ, "="); 65 | // assertToken(tokens[4], SvelteTokenTypes.N_ATTR_VALUE, "Novak:detail!"); 66 | // assertToken(tokens[5], SvelteTokenTypes.TEMPLATE_HTML_TEXT, null); 67 | // assertEquals(6, tokens.length); 68 | // } 69 | 70 | public void testHtmlTemplate01() throws Exception { 71 | String testPath = getTestDataPath() + "/sample01.html"; // plain html, no Svelte at all 72 | String data = FileUtil.loadFile(new File(testPath)); 73 | 74 | LexerToken[] tokens = lex(data); 75 | assertEquals(1, tokens.length); 76 | } 77 | 78 | public void testHtml01() throws Exception { 79 | String[] htmlSnippets = new String[] { 80 | "", 81 | "\n\n", 82 | "", 83 | "", 84 | "
", 85 | "\t" 89 | }; 90 | 91 | for(String snippet: htmlSnippets) { 92 | LexerToken[] tokens = lex(snippet); 93 | assertEquals("HTML snippet lexed as more than one token: " + snippet, 1, tokens.length); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/parser/SvelteParserDefinition.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.parser; 2 | 3 | import com.intellij.extapi.psi.ASTWrapperPsiElement; 4 | import com.intellij.lang.ASTNode; 5 | import com.intellij.lang.ParserDefinition; 6 | import com.intellij.lang.PsiParser; 7 | import com.intellij.lexer.Lexer; 8 | import com.intellij.openapi.project.Project; 9 | import com.intellij.psi.FileViewProvider; 10 | import com.intellij.psi.PsiElement; 11 | import com.intellij.psi.PsiFile; 12 | import com.intellij.psi.tree.IElementType; 13 | import com.intellij.psi.tree.IFileElementType; 14 | import com.intellij.psi.tree.TokenSet; 15 | import technology.svelte.lexer.SvelteLexer; 16 | import technology.svelte.lexer.SvelteTokenTypes; 17 | import technology.svelte.psi.SvelteFile; 18 | import technology.svelte.psi.impl.SvelteFileImpl; 19 | import technology.svelte.psi.impl.SveltePsiElement; 20 | import technology.svelte.psi.impl.MacroAttrImpl; 21 | import technology.svelte.psi.impl.MacroNodeImpl; 22 | import org.jetbrains.annotations.NotNull; 23 | 24 | public class SvelteParserDefinition implements ParserDefinition { 25 | 26 | @NotNull 27 | @Override 28 | public Lexer createLexer(Project project) { 29 | return new SvelteLexer(); 30 | } 31 | 32 | @Override 33 | public PsiParser createParser(Project project) { 34 | return new SvelteParser(); 35 | } 36 | 37 | @Override 38 | public IFileElementType getFileNodeType() { 39 | return SvelteTokenTypes.FILE; 40 | } 41 | 42 | @NotNull 43 | @Override 44 | public TokenSet getWhitespaceTokens() { 45 | return SvelteTokenTypes.WHITESPACES; 46 | } 47 | 48 | @NotNull 49 | @Override 50 | public TokenSet getCommentTokens() { 51 | return TokenSet.EMPTY; 52 | } 53 | 54 | @NotNull 55 | @Override 56 | public TokenSet getStringLiteralElements() { 57 | return SvelteTokenTypes.STRING_LITERALS; 58 | } 59 | 60 | @NotNull 61 | @Override 62 | public PsiElement createElement(ASTNode astNode) { 63 | return new ASTWrapperPsiElement(astNode); 64 | } 65 | 66 | @Override 67 | public PsiFile createFile(FileViewProvider fileViewProvider) { 68 | return new SvelteFileImpl(fileViewProvider); 69 | } 70 | 71 | @Override 72 | public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode astNode, ASTNode astNode1) { 73 | return SpaceRequirements.MAY; 74 | } 75 | 76 | // @NotNull 77 | // @Override 78 | // public Lexer createLexer(Project project) { 79 | // return new SvelteLexer(); 80 | // } 81 | // 82 | // @Override 83 | // public PsiParser createParser(Project project) { 84 | // return new SvelteParser(); 85 | // } 86 | // 87 | // @Override 88 | // public IFileElementType getFileNodeType() { 89 | // return SvelteTokenTypes.FILE; 90 | // } 91 | // 92 | // @NotNull 93 | // @Override 94 | // public TokenSet getWhitespaceTokens() { 95 | // return SvelteTokenTypes.WHITESPACES; 96 | // } 97 | // 98 | // @NotNull 99 | // @Override 100 | // public TokenSet getCommentTokens() { 101 | // return SvelteTokenTypes.COMMENTS; 102 | // } 103 | // 104 | // @NotNull 105 | // @Override 106 | // public TokenSet getStringLiteralElements() { 107 | // return SvelteTokenTypes.STRING_LITERALS; 108 | // } 109 | // 110 | // @NotNull 111 | // @Override 112 | // public PsiElement createElement(ASTNode node) { 113 | // IElementType type = node.getElementType(); 114 | // 115 | // if(type == SvelteTokenTypes.MACRO_NODE) return new MacroNodeImpl(node); 116 | // else if(type == SvelteTokenTypes.MACRO_ATTR) return new MacroAttrImpl(node); 117 | // else return new SveltePsiElement(node); 118 | // } 119 | // 120 | // @Override 121 | // public PsiFile createFile(FileViewProvider fileViewProvider) { 122 | // return new SvelteFileImpl(fileViewProvider); 123 | // } 124 | // 125 | // @Override 126 | // public SpaceRequirements spaceExistanceTypeBetweenTokens(ASTNode astNode, ASTNode astNode1) { 127 | // return SpaceRequirements.MAY; 128 | // } 129 | } 130 | -------------------------------------------------------------------------------- /src/test/resources/data/Sample1.txt: -------------------------------------------------------------------------------- 1 | SvelteFile:Sample1.svelte 2 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('
\n \n \n ') 3 | ASTWrapperPsiElement([Svelte] MACRO) 4 | PsiElement([Svelte] OPENING)('{{') 5 | ASTWrapperPsiElement([Svelte] PARAMS) 6 | PsiElement([Svelte] OPEN-BLOCK)('#') 7 | PsiElement([Svelte] BLOCK_STMT_NAME)('if') 8 | PsiElement([Svelte] PARAMS)(' alive') 9 | PsiElement([Svelte] CLOSING)('}}') 10 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n
Cat is alive
\n ') 11 | ASTWrapperPsiElement([Svelte] MACRO) 12 | PsiElement([Svelte] OPENING)('{{') 13 | ASTWrapperPsiElement([Svelte] BLOCK_STMT_NAME) 14 | PsiElement([Svelte] BLOCK_STMT_NAME)('elseif') 15 | ASTWrapperPsiElement([Svelte] PARAMS) 16 | PsiElement([Svelte] PARAMS)(' alive === undefined') 17 | PsiElement([Svelte] CLOSING)('}}') 18 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n Cat is both alive and not alive\n ') 19 | ASTWrapperPsiElement([Svelte] MACRO) 20 | PsiElement([Svelte] OPENING)('{{') 21 | ASTWrapperPsiElement([Svelte] BLOCK_STMT_NAME) 22 | PsiElement([Svelte] BLOCK_STMT_NAME)('else') 23 | ASTWrapperPsiElement([Svelte] PARAMS) 24 | 25 | PsiElement([Svelte] CLOSING)('}}') 26 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n Cat is dead\n ') 27 | ASTWrapperPsiElement([Svelte] MACRO) 28 | PsiElement([Svelte] OPENING)('{{') 29 | ASTWrapperPsiElement([Svelte] PARAMS) 30 | PsiElement([Svelte] CLOSE-BLOCK)('/') 31 | PsiElement([Svelte] BLOCK_STMT_NAME)('if') 32 | PsiElement([Svelte] CLOSING)('}}') 33 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n
\n ') 34 | ASTWrapperPsiElement([Svelte] MACRO) 35 | PsiElement([Svelte] OPENING)('{{') 36 | ASTWrapperPsiElement([Svelte] PARAMS) 37 | PsiElement([Svelte] OPEN-BLOCK)('#') 38 | PsiElement([Svelte] BLOCK_STMT_NAME)('each') 39 | PsiElement([Svelte] PARAMS)(' rows as row, y') 40 | PsiElement([Svelte] CLOSING)('}}') 41 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n
\n ') 42 | ASTWrapperPsiElement([Svelte] MACRO) 43 | PsiElement([Svelte] OPENING)('{{') 44 | ASTWrapperPsiElement([Svelte] PARAMS) 45 | PsiElement([Svelte] OPEN-BLOCK)('#') 46 | PsiElement([Svelte] BLOCK_STMT_NAME)('each') 47 | PsiElement([Svelte] PARAMS)(' columns as column, x') 48 | PsiElement([Svelte] CLOSING)('}}') 49 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n \n ') 50 | ASTWrapperPsiElement([Svelte] MACRO) 51 | PsiElement([Svelte] OPENING)('{{') 52 | ASTWrapperPsiElement([Svelte] PARAMS) 53 | PsiElement([Svelte] PARAMS)('x + 1') 54 | PsiElement([Svelte] CLOSING)('}}') 55 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)(',') 56 | ASTWrapperPsiElement([Svelte] MACRO) 57 | PsiElement([Svelte] OPENING)('{{') 58 | ASTWrapperPsiElement([Svelte] PARAMS) 59 | PsiElement([Svelte] PARAMS)('y + 1') 60 | PsiElement([Svelte] CLOSING)('}}') 61 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)(':\n ') 62 | ASTWrapperPsiElement([Svelte] MACRO) 63 | PsiElement([Svelte] OPENING)('{{') 64 | ASTWrapperPsiElement([Svelte] PARAMS) 65 | PsiElement([Svelte] PARAMS)('row[column]') 66 | PsiElement([Svelte] CLOSING)('}}') 67 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n \n ') 68 | ASTWrapperPsiElement([Svelte] MACRO) 69 | PsiElement([Svelte] OPENING)('{{') 70 | ASTWrapperPsiElement([Svelte] PARAMS) 71 | PsiElement([Svelte] CLOSE-BLOCK)('/') 72 | PsiElement([Svelte] BLOCK_STMT_NAME)('each') 73 | PsiElement([Svelte] CLOSING)('}}') 74 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n
\n ') 75 | ASTWrapperPsiElement([Svelte] MACRO) 76 | PsiElement([Svelte] OPENING)('{{') 77 | ASTWrapperPsiElement([Svelte] PARAMS) 78 | PsiElement([Svelte] CLOSE-BLOCK)('/') 79 | PsiElement([Svelte] BLOCK_STMT_NAME)('each') 80 | PsiElement([Svelte] CLOSING)('}}') 81 | PsiElement([Svelte] SVELTE_TEMPLATE_HTML_TEXT)('\n
\n
\n\n') -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /src/main/jflex/idea-flex.skeleton: -------------------------------------------------------------------------------- 1 | 2 | /** This character denotes the end of file */ 3 | public static final int YYEOF = -1; 4 | 5 | /** initial size of the lookahead buffer */ 6 | --- private static final int ZZ_BUFFERSIZE = ...; 7 | 8 | /** lexical states */ 9 | --- lexical states, charmap 10 | 11 | /* error codes */ 12 | private static final int ZZ_UNKNOWN_ERROR = 0; 13 | private static final int ZZ_NO_MATCH = 1; 14 | private static final int ZZ_PUSHBACK_2BIG = 2; 15 | 16 | /* error messages for the codes above */ 17 | private static final String[] ZZ_ERROR_MSG = { 18 | "Unknown internal scanner error", 19 | "Error: could not match input", 20 | "Error: pushback value was too large" 21 | }; 22 | 23 | --- isFinal list 24 | /** the input device */ 25 | private java.io.Reader zzReader; 26 | 27 | /** the current state of the DFA */ 28 | private int zzState; 29 | 30 | /** the current lexical state */ 31 | private int zzLexicalState = YYINITIAL; 32 | 33 | /** this buffer contains the current text to be matched and is 34 | the source of the yytext() string */ 35 | private CharSequence zzBuffer = ""; 36 | 37 | /** the textposition at the last accepting state */ 38 | private int zzMarkedPos; 39 | 40 | /** the current text position in the buffer */ 41 | private int zzCurrentPos; 42 | 43 | /** startRead marks the beginning of the yytext() string in the buffer */ 44 | private int zzStartRead; 45 | 46 | /** endRead marks the last character in the buffer, that has been read 47 | from input */ 48 | private int zzEndRead; 49 | 50 | /** 51 | * zzAtBOL == true <=> the scanner is currently at the beginning of a line 52 | */ 53 | private boolean zzAtBOL = true; 54 | 55 | /** zzAtEOF == true <=> the scanner is at the EOF */ 56 | private boolean zzAtEOF; 57 | 58 | /** denotes if the user-EOF-code has already been executed */ 59 | private boolean zzEOFDone; 60 | 61 | --- user class code 62 | 63 | --- constructor declaration 64 | 65 | public final int getTokenStart() { 66 | return zzStartRead; 67 | } 68 | 69 | public final int getTokenEnd() { 70 | return getTokenStart() + yylength(); 71 | } 72 | 73 | public void reset(CharSequence buffer, int start, int end, int initialState) { 74 | zzBuffer = buffer; 75 | zzCurrentPos = zzMarkedPos = zzStartRead = start; 76 | zzAtEOF = false; 77 | zzAtBOL = true; 78 | zzEndRead = end; 79 | yybegin(initialState); 80 | } 81 | 82 | /** 83 | * Refills the input buffer. 84 | * 85 | * @return false, iff there was new input. 86 | * 87 | * @exception java.io.IOException if any I/O-Error occurs 88 | */ 89 | private boolean zzRefill() throws java.io.IOException { 90 | return true; 91 | } 92 | 93 | 94 | /** 95 | * Returns the current lexical state. 96 | */ 97 | public final int yystate() { 98 | return zzLexicalState; 99 | } 100 | 101 | 102 | /** 103 | * Enters a new lexical state 104 | * 105 | * @param newState the new lexical state 106 | */ 107 | public final void yybegin(int newState) { 108 | zzLexicalState = newState; 109 | } 110 | 111 | 112 | /** 113 | * Returns the text matched by the current regular expression. 114 | */ 115 | public final CharSequence yytext() { 116 | return zzBuffer.subSequence(zzStartRead, zzMarkedPos); 117 | } 118 | 119 | 120 | /** 121 | * Returns the character at position pos from the 122 | * matched text. 123 | * 124 | * It is equivalent to yytext().charAt(pos), but faster 125 | * 126 | * @param pos the position of the character to fetch. 127 | * A value from 0 to yylength()-1. 128 | * 129 | * @return the character at position pos 130 | */ 131 | public final char yycharat(int pos) { 132 | return zzBuffer.charAt(zzStartRead+pos); 133 | } 134 | 135 | 136 | /** 137 | * Returns the length of the matched text region. 138 | */ 139 | public final int yylength() { 140 | return zzMarkedPos-zzStartRead; 141 | } 142 | 143 | 144 | /** 145 | * Reports an error that occured while scanning. 146 | * 147 | * In a wellformed scanner (no or only correct usage of 148 | * yypushback(int) and a match-all fallback rule) this method 149 | * will only be called with things that "Can't Possibly Happen". 150 | * If this method is called, something is seriously wrong 151 | * (e.g. a JFlex bug producing a faulty scanner etc.). 152 | * 153 | * Usual syntax/scanner level error handling should be done 154 | * in error fallback rules. 155 | * 156 | * @param errorCode the code of the errormessage to display 157 | */ 158 | --- zzScanError declaration 159 | String message; 160 | try { 161 | message = ZZ_ERROR_MSG[errorCode]; 162 | } 163 | catch (ArrayIndexOutOfBoundsException e) { 164 | message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR]; 165 | } 166 | 167 | --- throws clause 168 | } 169 | 170 | 171 | /** 172 | * Pushes the specified amount of characters back into the input stream. 173 | * 174 | * They will be read again by then next call of the scanning method 175 | * 176 | * @param number the number of characters to be read again. 177 | * This number must not be greater than yylength()! 178 | */ 179 | --- yypushback decl (contains zzScanError exception) 180 | if ( number > yylength() ) 181 | zzScanError(ZZ_PUSHBACK_2BIG); 182 | 183 | zzMarkedPos -= number; 184 | } 185 | 186 | 187 | --- zzDoEOF 188 | /** 189 | * Resumes scanning until the next regular expression is matched, 190 | * the end of input is encountered or an I/O-Error occurs. 191 | * 192 | * @return the next token 193 | * @exception java.io.IOException if any I/O-Error occurs 194 | */ 195 | --- yylex declaration 196 | int zzInput; 197 | int zzAction; 198 | 199 | // cached fields: 200 | int zzCurrentPosL; 201 | int zzMarkedPosL; 202 | int zzEndReadL = zzEndRead; 203 | CharSequence zzBufferL = zzBuffer; 204 | 205 | --- local declarations 206 | 207 | while (true) { 208 | zzMarkedPosL = zzMarkedPos; 209 | 210 | --- start admin (line, char, col count) 211 | zzAction = -1; 212 | 213 | zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL; 214 | 215 | --- start admin (lexstate etc) 216 | 217 | zzForAction: { 218 | while (true) { 219 | 220 | --- next input, line, col, char count, next transition, isFinal action 221 | zzAction = zzState; 222 | zzMarkedPosL = zzCurrentPosL; 223 | --- line count update 224 | } 225 | 226 | } 227 | } 228 | 229 | // store back cached position 230 | zzMarkedPos = zzMarkedPosL; 231 | --- char count update 232 | 233 | if (zzInput == YYEOF && zzStartRead == zzCurrentPos) { 234 | zzAtEOF = true; 235 | --- eofvalue 236 | } 237 | else { 238 | --- actions 239 | default: 240 | --- no match 241 | } 242 | } 243 | } 244 | } 245 | 246 | --- main 247 | 248 | } 249 | -------------------------------------------------------------------------------- /src/main/java/technology/svelte/completion/KeywordCompletionProvider.java: -------------------------------------------------------------------------------- 1 | package technology.svelte.completion; 2 | 3 | import com.intellij.codeInsight.completion.CompletionParameters; 4 | import com.intellij.codeInsight.completion.CompletionProvider; 5 | import com.intellij.codeInsight.completion.CompletionResultSet; 6 | import com.intellij.codeInsight.lookup.LookupElementBuilder; 7 | import com.intellij.psi.PsiElement; 8 | import com.intellij.psi.PsiWhiteSpace; 9 | import com.intellij.psi.xml.XmlAttribute; 10 | import com.intellij.psi.xml.XmlElement; 11 | import com.intellij.util.ProcessingContext; 12 | import technology.svelte.lexer.SvelteTokenTypes; 13 | import technology.svelte.psi.impl.MacroNodeImpl; 14 | import org.jetbrains.annotations.NotNull; 15 | 16 | import java.util.*; 17 | 18 | /** 19 | * Provides keywords to be auto-completed 20 | */ 21 | public class KeywordCompletionProvider extends CompletionProvider { 22 | // constant lists 23 | private static final String[] KEYWORDS = {"#if", "else", "elseif", "/if", "#each", "/each"}; 24 | // private static final String[] FILTERS = { "date", "format", "replace", "url_encode", "json_encode", "title", "capitalize", "upper", "lower", "striptags", "join", "reverse", "length", "sort", "default", "keys", "escape", "raw", "merge" }; 25 | 26 | // CompletionResultSet wants list of LookupElements 27 | private List KEYWORD_LOOKUPS = new ArrayList(); 28 | // private List FILTER_LOOKUPS = new ArrayList(); 29 | 30 | 31 | public KeywordCompletionProvider() { 32 | super(); 33 | 34 | for (String keyword : KEYWORDS) { 35 | KEYWORD_LOOKUPS.add(LookupElementBuilder.create(keyword)); 36 | } 37 | // for(String filter: FILTERS) FILTER_LOOKUPS.add(LookupElementBuilder.create(filter)); 38 | } 39 | 40 | @Override 41 | protected void addCompletions(@NotNull CompletionParameters params, 42 | ProcessingContext ctx, 43 | @NotNull CompletionResultSet results) { 44 | 45 | PsiElement currElement = params.getPosition() 46 | .getOriginalElement(); 47 | if (currElement.getNode() 48 | .getElementType() == SvelteTokenTypes.SVELTE_PARAMS) { 49 | for (LookupElementBuilder x : KEYWORD_LOOKUPS) { 50 | results.addElement(x); 51 | } 52 | } 53 | } 54 | // // constant lists 55 | // private static final String[] KEYWORDS = { 56 | // // CoreMacros.php 57 | // "if", "ifset", "for", "foreach", "while", "first", "last", "sep", "class", "attr", "captute", 58 | // "var", "default", "dump", "debugbreak", "l", "r", 59 | // 60 | // // FormMacros.php 61 | // "form", "input", "label", "formContainer", 62 | // 63 | // // UIMacros.php 64 | // "include", "extends", "block", "define", "snippet", "widget", "control", "href", "link", "plink", "contentType", "status", 65 | // }; 66 | // 67 | // private static final List PAIR_MACROS = Arrays.asList( 68 | // "if", "ifset", "for", "foreach", "while", "first", "last", "sep" 69 | // ); 70 | // 71 | // private static final String[] FILTERS = { 72 | // // static 73 | // "normalize", "toascii", "webalize", "truncate", "lower", "upper", "firstupper", "capitalize", "trim", "padleft", "padright", 74 | // "replacere", "url", "striptags", "nl2br", "substr", "repeat", "implode", "number", 75 | // 76 | // // methods in Helpers.php 77 | // "espaceHtml", "escapeHtmlComment", "escapeXML", "escapeCss", "escapeJs", "strip", "indent", "date", "bytes", 78 | // "length", "replace", "dataStream", "null", 79 | // }; 80 | // 81 | // 82 | // // CompletionResultSet wants list of LookupElements 83 | // private List KEYWORD_LOOKUPS = new ArrayList(); 84 | // private List ATTR_LOOKUPS = new ArrayList(); 85 | // private List FILTER_LOOKUPS = new ArrayList(); 86 | // private HashMap CLOSING_LOOKUPS = new HashMap(); 87 | // 88 | // public KeywordCompletionProvider() { 89 | // super(); 90 | // 91 | // for(String keyword: KEYWORDS) { 92 | // KEYWORD_LOOKUPS.add(LookupElementBuilder.create(keyword)); 93 | // ATTR_LOOKUPS.add(LookupElementBuilder.create("n:" + keyword)); 94 | // } 95 | // for(String filter: FILTERS) FILTER_LOOKUPS.add(LookupElementBuilder.create(filter)); 96 | // } 97 | // 98 | // @Override 99 | // protected void addCompletions(@NotNull CompletionParameters params, 100 | // ProcessingContext ctx, 101 | // @NotNull CompletionResultSet results) { 102 | // 103 | // PsiElement curr = params.getPosition().getOriginalElement(); 104 | // 105 | // // n: attributes 106 | // if(curr.getParent() instanceof XmlAttribute) { 107 | // for(LookupElementBuilder x: ATTR_LOOKUPS) results.addElement(x); 108 | // return; 109 | // } 110 | // 111 | // // Keywords 112 | // if(curr.getNode().getElementType() == SvelteTokenTypes.MACRO_NAME) { 113 | // for(LookupElementBuilder x: KEYWORD_LOOKUPS) results.addElement(x); 114 | // 115 | // HashSet openedMacros = new HashSet(); // macros which are opened before (should complete closing) 116 | // HashSet closedMacros = new HashSet(); // which are closed (should not complete closing) 117 | // 118 | // // Go up and find open keywords (and offer them for closing) 119 | // PsiElement cursor = curr; 120 | // while(cursor != null && (cursor.getPrevSibling() != null || cursor.getParent() != null)) { 121 | // // macro found {xx} found 122 | // if(cursor instanceof MacroNodeImpl) { 123 | // MacroNodeImpl node = (MacroNodeImpl) cursor; 124 | // String name = node.getMacroName(); 125 | // 126 | // if(name.charAt(0) == '/') { // closing macro 127 | // closedMacros.add(name.substring(1)); 128 | // } else if(PAIR_MACROS.contains(name)) { 129 | // if(closedMacros.contains(name)) closedMacros.remove(name); // is closed later 130 | // else openedMacros.add(name); // not closed, let us close it 131 | // } 132 | // } 133 | // 134 | // 135 | // PsiElement tmp; 136 | // if((tmp = cursor.getPrevSibling()) != null) cursor = tmp; // Go prev if possible... 137 | // else cursor = cursor.getParent(); // ... or up 138 | // } 139 | // 140 | // for(String name: openedMacros) { 141 | // if(name.equals("if")) { 142 | // results.addElement(reuseClosingTag("else")); 143 | // results.addElement(reuseClosingTag("elseif")); 144 | // } 145 | // results.addElement(reuseClosingTag("/" + name)); 146 | // } 147 | // 148 | // results.stopHere(); 149 | // } 150 | // 151 | // // Modifiers (if after pipe) 152 | // PsiElement prev = curr.getPrevSibling(); 153 | // if(prev != null && prev instanceof PsiWhiteSpace) prev = prev.getPrevSibling(); 154 | // if(prev != null && prev.getNode().getElementType() == SvelteTokenTypes.MODIFIER) { 155 | // for(LookupElementBuilder x: FILTER_LOOKUPS) results.addElement(x); 156 | // results.stopHere(); 157 | // } 158 | // } 159 | // 160 | // // Get closing tag 161 | // private LookupElementBuilder reuseClosingTag(String name) { 162 | // if(!CLOSING_LOOKUPS.containsKey(name)) { 163 | // CLOSING_LOOKUPS.put(name, LookupElementBuilder.create(name)); 164 | // } 165 | // 166 | // return CLOSING_LOOKUPS.get(name); 167 | // } 168 | } 169 | --------------------------------------------------------------------------------