From 78cc055fddffc9487f5fe5577c64a8f583d01d27 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Mon, 15 May 2017 13:09:30 +0300 Subject: [PATCH] HTML: extract utility method to define quote style to use in content --- .../completion/XmlAttributeInsertHandler.java | 10 ++--- .../completion/XmlTagInsertHandler.java | 15 +++---- .../editorActions/XmlEditUtil.java | 42 +++++++++++++++++++ .../editorActions/XmlEqTypedHandler.java | 5 +-- .../generators/XmlZenCodingGenerator.java | 13 +----- .../src/com/intellij/xml/util/HtmlUtil.java | 2 +- xml/xml-psi-impl/xml-psi-impl.iml | 1 + 7 files changed, 59 insertions(+), 29 deletions(-) create mode 100644 xml/impl/src/com/intellij/codeInsight/editorActions/XmlEditUtil.java diff --git a/xml/impl/src/com/intellij/codeInsight/completion/XmlAttributeInsertHandler.java b/xml/impl/src/com/intellij/codeInsight/completion/XmlAttributeInsertHandler.java index 38167f930812..100914889c7b 100644 --- a/xml/impl/src/com/intellij/codeInsight/completion/XmlAttributeInsertHandler.java +++ b/xml/impl/src/com/intellij/codeInsight/completion/XmlAttributeInsertHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -17,6 +17,7 @@ package com.intellij.codeInsight.completion; import com.intellij.application.options.editor.WebEditorOptions; import com.intellij.codeInsight.AutoPopupController; +import com.intellij.codeInsight.editorActions.XmlEditUtil; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; @@ -27,7 +28,6 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; -import com.intellij.psi.codeStyle.CodeStyleSchemes; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.xml.XmlAttribute; import com.intellij.psi.xml.XmlFile; @@ -68,7 +68,7 @@ public class XmlAttributeInsertHandler implements InsertHandler { final PsiFile file = context.getFile(); final CharSequence chars = document.getCharsSequence(); - final String quote = getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file)); + final String quote = XmlEditUtil.getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file)); final boolean insertQuotes = WebEditorOptions.getInstance().isInsertQuotesForAttributeValue() && StringUtil.isNotEmpty(quote); final boolean hasQuotes = CharArrayUtil.regionMatches(chars, caretOffset, "=\"") || CharArrayUtil.regionMatches(chars, caretOffset, "='"); @@ -126,10 +126,6 @@ public class XmlAttributeInsertHandler implements InsertHandler { } } - public static String getAttributeQuote(boolean html) { - return html ? CodeStyleSchemes.getInstance().getCurrentScheme().getCodeStyleSettings().HTML_QUOTE_STYLE.quote : "\""; - } - private static void qualifyWithPrefix(@NotNull String namespacePrefix, @NotNull PsiElement context) { final PsiElement parent = context.getParent(); diff --git a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java index 912a92ce505a..669c17990022 100644 --- a/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java +++ b/xml/impl/src/com/intellij/codeInsight/completion/XmlTagInsertHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -17,6 +17,7 @@ package com.intellij.codeInsight.completion; import com.intellij.application.options.editor.WebEditorOptions; import com.intellij.codeInsight.TailType; +import com.intellij.codeInsight.editorActions.XmlEditUtil; import com.intellij.codeInsight.lookup.Lookup; import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupItem; @@ -230,9 +231,9 @@ public class XmlTagInsertHandler implements InsertHandler { if (shouldBeInserted && (tag == null || tag.getAttributeValue(attributeName) == null)) { if (!notRequiredAttributes.contains(attributeName)) { if (!extension.isIndirectSyntax(attributeDecl)) { - template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(htmlCode)); + template.addTextSegment(" " + attributeName + "=" + XmlEditUtil.getAttributeQuote(htmlCode)); template.addVariable(new MacroCallNode(new CompleteMacro()), true); - template.addTextSegment(XmlAttributeInsertHandler.getAttributeQuote(htmlCode)); + template.addTextSegment(XmlEditUtil.getAttributeQuote(htmlCode)); } else { if (indirectRequiredAttrs == null) indirectRequiredAttrs = new StringBuilder(); @@ -241,8 +242,8 @@ public class XmlTagInsertHandler implements InsertHandler { } } else if (shouldBeInserted && attributeDecl.isFixed() && attributeDecl.getDefaultValue() != null && !htmlCode) { - template.addTextSegment(" " + attributeName + "=" + XmlAttributeInsertHandler.getAttributeQuote(false) + - attributeDecl.getDefaultValue() + XmlAttributeInsertHandler.getAttributeQuote(false)); + template.addTextSegment(" " + attributeName + "=" + XmlEditUtil.getAttributeQuote(false) + + attributeDecl.getDefaultValue() + XmlEditUtil.getAttributeQuote(false)); } } } @@ -318,9 +319,9 @@ public class XmlTagInsertHandler implements InsertHandler { private static void completeAttribute(Template template, boolean htmlCode) { template.addTextSegment(" "); template.addVariable(new MacroCallNode(new CompleteMacro()), true); - template.addTextSegment("=" + XmlAttributeInsertHandler.getAttributeQuote(htmlCode)); + template.addTextSegment("=" + XmlEditUtil.getAttributeQuote(htmlCode)); template.addEndVariable(); - template.addTextSegment(XmlAttributeInsertHandler.getAttributeQuote(htmlCode)); + template.addTextSegment(XmlEditUtil.getAttributeQuote(htmlCode)); } private static boolean needAlLeastOneAttribute(XmlTag tag) { diff --git a/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEditUtil.java b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEditUtil.java new file mode 100644 index 000000000000..face145d4dcd --- /dev/null +++ b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEditUtil.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.editorActions; + +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.codeStyle.CodeStyleSchemes; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import org.jetbrains.annotations.NotNull; + +public class XmlEditUtil { + /** + * Calculates quote style to use in a particular file depends on user's settings and injections + */ + public static CodeStyleSettings.QuoteStyle quoteStyle(@NotNull PsiFile file) { + PsiElement context = file.getContext(); + CodeStyleSettings.QuoteStyle style = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings().HTML_QUOTE_STYLE; + if (context != null && !style.quote.isEmpty() && context.getText().startsWith(style.quote)) { + return style == CodeStyleSettings.QuoteStyle.Double ? CodeStyleSettings.QuoteStyle.Single : CodeStyleSettings.QuoteStyle.Double; + } + return style; + } + + @NotNull + public static String getAttributeQuote(boolean html) { + return html ? CodeStyleSchemes.getInstance().getCurrentScheme().getCodeStyleSettings().HTML_QUOTE_STYLE.quote : "\""; + } +} diff --git a/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEqTypedHandler.java b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEqTypedHandler.java index 3e52928978d7..c93b0b943d30 100644 --- a/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEqTypedHandler.java +++ b/xml/impl/src/com/intellij/codeInsight/editorActions/XmlEqTypedHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -17,7 +17,6 @@ package com.intellij.codeInsight.editorActions; import com.intellij.application.options.editor.WebEditorOptions; import com.intellij.codeInsight.AutoPopupController; -import com.intellij.codeInsight.completion.XmlAttributeInsertHandler; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; @@ -60,7 +59,7 @@ public class XmlEqTypedHandler extends TypedHandlerDelegate { if (fileContext.getText().startsWith("\'")) toInsert = "\"\""; } if (toInsert == null) { - final String quote = XmlAttributeInsertHandler.getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file)); + final String quote = XmlEditUtil.getAttributeQuote(HtmlUtil.hasHtml(file) || HtmlUtil.supportsXmlTypedHandlers(file)); toInsert = quote + quote; } editor.getDocument().insertString(offset, toInsert); diff --git a/xml/impl/src/com/intellij/codeInsight/template/emmet/generators/XmlZenCodingGenerator.java b/xml/impl/src/com/intellij/codeInsight/template/emmet/generators/XmlZenCodingGenerator.java index 01bff9b7a684..14437b872ad1 100644 --- a/xml/impl/src/com/intellij/codeInsight/template/emmet/generators/XmlZenCodingGenerator.java +++ b/xml/impl/src/com/intellij/codeInsight/template/emmet/generators/XmlZenCodingGenerator.java @@ -16,6 +16,7 @@ package com.intellij.codeInsight.template.emmet.generators; import com.intellij.application.options.emmet.EmmetOptions; +import com.intellij.codeInsight.editorActions.XmlEditUtil; import com.intellij.codeInsight.template.CustomTemplateCallback; import com.intellij.codeInsight.template.emmet.ZenCodingTemplate; import com.intellij.codeInsight.template.emmet.ZenCodingUtil; @@ -30,7 +31,6 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettings; -import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.xml.XmlTag; @@ -64,7 +64,7 @@ public abstract class XmlZenCodingGenerator extends ZenCodingGenerator { @NotNull private String toString(@NotNull TemplateToken token, boolean hasChildren, @NotNull PsiElement context) { - CodeStyleSettings.QuoteStyle quoteStyle = quoteStyle(context.getContainingFile()); + CodeStyleSettings.QuoteStyle quoteStyle = XmlEditUtil.quoteStyle(context.getContainingFile()); XmlTag tag = token.getXmlTag(); if (tag != null) { if (quoteStyle != CodeStyleSettings.QuoteStyle.None) { @@ -93,15 +93,6 @@ public abstract class XmlZenCodingGenerator extends ZenCodingGenerator { } return text; } - - private static CodeStyleSettings.QuoteStyle quoteStyle(@NotNull PsiFile file) { - PsiElement context = file.getContext(); - CodeStyleSettings.QuoteStyle style = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings().HTML_QUOTE_STYLE; - if (context != null && !style.quote.isEmpty() && context.getText().startsWith(style.quote)) { - return style == CodeStyleSettings.QuoteStyle.Double ? CodeStyleSettings.QuoteStyle.Single : CodeStyleSettings.QuoteStyle.Double; - } - return style; - } public abstract String toString(@NotNull XmlTag tag, @NotNull Map attributes, diff --git a/xml/xml-psi-impl/src/com/intellij/xml/util/HtmlUtil.java b/xml/xml-psi-impl/src/com/intellij/xml/util/HtmlUtil.java index 1bbc060a0c0b..af10e82968a9 100644 --- a/xml/xml-psi-impl/src/com/intellij/xml/util/HtmlUtil.java +++ b/xml/xml-psi-impl/src/com/intellij/xml/util/HtmlUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. diff --git a/xml/xml-psi-impl/xml-psi-impl.iml b/xml/xml-psi-impl/xml-psi-impl.iml index a11dcfa5948b..bd0b8a0ba022 100644 --- a/xml/xml-psi-impl/xml-psi-impl.iml +++ b/xml/xml-psi-impl/xml-psi-impl.iml @@ -18,5 +18,6 @@ + \ No newline at end of file