From b6f41b4779765d2a1a5d1ac08aa00dade7bad336 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Tue, 6 May 2014 13:56:26 +0400 Subject: [PATCH] Emmet: parse incomplete more/climb abbreviations --- .../template/emmet/EmmetParser.java | 44 +++++++++---------- .../template/emmet/XmlEmmetParser.java | 9 ++-- .../emmet/nodes/AddOperationNode.java | 4 +- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/EmmetParser.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/EmmetParser.java index c5224aee4b96..b9fb54d3b31a 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/EmmetParser.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/EmmetParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator; import com.intellij.codeInsight.template.emmet.nodes.*; import com.intellij.codeInsight.template.emmet.tokens.*; import com.intellij.codeInsight.template.impl.TemplateImpl; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; @@ -90,18 +89,15 @@ public abstract class EmmetParser { if (sign == '^') { return parseClimbUpOperation(mul); } - if (mul == null) { - return null; - } if (sign == '+') { advance(); ZenCodingNode add2 = parseAddOrMore(); if (add2 == null) { - return null; + return mul; } - return new AddOperationNode(mul, add2); + return new AddOperationNode(notNullNode(mul), add2); } - else if (sign == '>') { + if (sign == '>') { return parseMoreOperation(mul); } return null; @@ -109,23 +105,28 @@ public abstract class EmmetParser { protected ZenCodingNode parseClimbUpOperation(@Nullable ZenCodingNode leftPart) { advance(); - leftPart = leftPart != null ? leftPart : ZenEmptyNode.INSTANCE; - ZenCodingNode rigthPart = parseAddOrMore(); - if (rigthPart == null) { - return null; - } - return new ClimbUpOperationNode(leftPart, rigthPart); - } - - protected ZenCodingNode parseMoreOperation(@NotNull ZenCodingNode leftPart) { - advance(); + leftPart = notNullNode(leftPart); ZenCodingNode rightPart = parseAddOrMore(); if (rightPart == null) { - return null; + return leftPart; + } + return new ClimbUpOperationNode(leftPart, rightPart); + } + + protected ZenCodingNode parseMoreOperation(@Nullable ZenCodingNode leftPart) { + advance(); + leftPart = notNullNode(leftPart); + ZenCodingNode rightPart = parseAddOrMore(); + if (rightPart == null) { + return leftPart; } return new MoreOperationNode(leftPart, rightPart); } + private static ZenCodingNode notNullNode(ZenCodingNode node) { + return node != null ? node : ZenEmptyNode.INSTANCE; + } + protected int advance() { return myIndex++; } @@ -207,11 +208,6 @@ public abstract class EmmetParser { return new TemplateNode(templateToken); } - @Nullable - protected String getDefaultTemplateKey() { - return null; - } - protected boolean setTemplate(final TemplateToken token, TemplateImpl template) { if (template == null) { template = myGenerator.createTemplateByKey(token.getKey()); diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/XmlEmmetParser.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/XmlEmmetParser.java index 69350db5ea8e..6e2d2a12fe67 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/XmlEmmetParser.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/XmlEmmetParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,7 +58,7 @@ public class XmlEmmetParser extends EmmetParser { private boolean hasTagContext = false; private final Stack tagLevel = new Stack(); - private static Map parentChildTagMapping = new HashMap() {{ + private static final Map parentChildTagMapping = new HashMap() {{ put("p", "span"); put("ul", "li"); put("ol", "li"); @@ -159,7 +159,7 @@ public class XmlEmmetParser extends EmmetParser { } @Override - protected ZenCodingNode parseMoreOperation(@NotNull ZenCodingNode leftPart) { + protected ZenCodingNode parseMoreOperation(@Nullable ZenCodingNode leftPart) { String parentTag = getParentTag(leftPart); boolean hasParent = false; if (!Strings.isNullOrEmpty(parentTag)) { @@ -177,8 +177,7 @@ public class XmlEmmetParser extends EmmetParser { } @Nullable - @Override - protected String getDefaultTemplateKey() { + private String getDefaultTemplateKey() { return ZenCodingUtil.isHtml(myCallback) ? suggestTagName() : null; } diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/nodes/AddOperationNode.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/nodes/AddOperationNode.java index 60ab7cf8cf0b..c63375ef6808 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/nodes/AddOperationNode.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/nodes/AddOperationNode.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2010 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ public class AddOperationNode extends ZenCodingNode { private final ZenCodingNode myLeftOperand; private final ZenCodingNode myRightOperand; - public AddOperationNode(ZenCodingNode leftOperand, ZenCodingNode rightOperand) { + public AddOperationNode(@NotNull ZenCodingNode leftOperand, @NotNull ZenCodingNode rightOperand) { myLeftOperand = leftOperand; myRightOperand = rightOperand; }