HTML: extract utility method to define quote style to use in content

This commit is contained in:
Alexander Zolotov
2017-05-15 16:34:33 +03:00
parent 18592664b7
commit 78cc055fdd
7 changed files with 59 additions and 29 deletions
@@ -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<LookupElement> {
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<LookupElement> {
}
}
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();
@@ -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<LookupElement> {
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<LookupElement> {
}
}
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<LookupElement> {
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) {
@@ -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 : "\"";
}
}
@@ -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);
@@ -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<String, String> attributes,
@@ -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.
+1
View File
@@ -18,5 +18,6 @@
<orderEntry type="library" name="Xerces" level="project" />
<orderEntry type="library" name="XmlBeans" level="project" />
<orderEntry type="module" module-name="RegExpSupport" exported="" />
<orderEntry type="module" module-name="lang-api" />
</component>
</module>