mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-106488 Separate highlighting of namespace prefix and tag name
This commit is contained in:
+4
-3
@@ -69,10 +69,11 @@ public class SimpleEditorPreview implements PreviewPanel{
|
||||
String text = page.getDemoText();
|
||||
|
||||
HighlightsExtractor extractant2 = new HighlightsExtractor(page.getAdditionalHighlightingTagToDescriptorMap());
|
||||
myHighlightData = extractant2.extractHighlights(text);
|
||||
|
||||
List<HighlightData> highlights = new ArrayList<HighlightData>();
|
||||
String stripped = extractant2.extractHighlights(text, highlights);
|
||||
myHighlightData = highlights.toArray(new HighlightData[highlights.size()]);
|
||||
int selectedLine = -1;
|
||||
myEditor = (EditorEx)FontEditorPreview.createPreviewEditor(extractant2.cutDefinedTags(text), 10, 3, selectedLine, myOptions, false);
|
||||
myEditor = (EditorEx)FontEditorPreview.createPreviewEditor(stripped, 10, 3, selectedLine, myOptions, false);
|
||||
|
||||
FontEditorPreview.installTrafficLights(myEditor);
|
||||
myBlinkingAlarm = new Alarm().setActivationComponent(myEditor.getComponent());
|
||||
|
||||
+27
-48
@@ -20,6 +20,7 @@
|
||||
package com.intellij.application.options.colors.highlighting;
|
||||
|
||||
import com.intellij.openapi.editor.colors.TextAttributesKey;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.util.containers.Stack;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -36,20 +37,21 @@ public class HighlightsExtractor {
|
||||
private int mySkippedLen;
|
||||
private int myIndex;
|
||||
private boolean myIsOpeningTag;
|
||||
private static final HighlightData[] EMPTY_DATA = new HighlightData[0];
|
||||
|
||||
private List<TextRange> mySkipped = new ArrayList<TextRange>();
|
||||
|
||||
public HighlightsExtractor(@Nullable Map<String, TextAttributesKey> tags) {
|
||||
myTags = tags;
|
||||
}
|
||||
|
||||
public HighlightData[] extractHighlights(String text) {
|
||||
if (myTags == null || myTags.isEmpty()) return EMPTY_DATA;
|
||||
public String extractHighlights(String text, List<HighlightData> highlights) {
|
||||
mySkipped.clear();
|
||||
if (myTags == null || myTags.isEmpty()) return text;
|
||||
resetIndices();
|
||||
List<HighlightData> highlights = new ArrayList<HighlightData>();
|
||||
Stack<HighlightData> highlightsStack = new Stack<HighlightData>();
|
||||
while (true) {
|
||||
String tagName = findTagName(text);
|
||||
if (tagName == null) break;
|
||||
if (tagName == null || myIndex < 0) break;
|
||||
if (myTags.containsKey(tagName)) {
|
||||
if (myIsOpeningTag) {
|
||||
mySkippedLen += tagName.length() + 2;
|
||||
@@ -64,7 +66,7 @@ public class HighlightsExtractor {
|
||||
}
|
||||
}
|
||||
|
||||
return highlights.toArray(new HighlightData[highlights.size()]);
|
||||
return cutDefinedTags(text);
|
||||
}
|
||||
|
||||
private String findTagName(String text) {
|
||||
@@ -84,15 +86,26 @@ public class HighlightsExtractor {
|
||||
myIndex = openTag + 1;
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
int closeTag = text.indexOf('>', openTag + 1);
|
||||
if (closeTag == -1) return null;
|
||||
if (closeTag == -1) return null;
|
||||
int i = text.indexOf('<', openTag + 1);
|
||||
if (i != -1 && i < closeTag) {
|
||||
myIndex = i;
|
||||
return "";
|
||||
}
|
||||
final String tagName = text.substring(openTag + 1, closeTag);
|
||||
|
||||
if (myIsOpeningTag) {
|
||||
myStartOffset = openTag + tagName.length() + 2;
|
||||
if (myTags.containsKey(tagName)) {
|
||||
mySkipped.add(TextRange.from(openTag, tagName.length() + 2));
|
||||
}
|
||||
} else {
|
||||
myEndOffset = openTag - 1;
|
||||
if (myTags.containsKey(tagName)) {
|
||||
mySkipped.add(TextRange.from(openTag - 1, tagName.length() + 3));
|
||||
}
|
||||
}
|
||||
myIndex = Math.max(myStartOffset, myEndOffset + 1);
|
||||
return tagName;
|
||||
@@ -102,47 +115,13 @@ public class HighlightsExtractor {
|
||||
return Character.isLetter(c) || c == '_';
|
||||
}
|
||||
|
||||
public String cutDefinedTags(String text) {
|
||||
if (myTags == null || myTags.isEmpty()) return text;
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int index = 0;
|
||||
while (true) {
|
||||
int from = text.indexOf('<', index);
|
||||
if (from == -1) {
|
||||
sb.append(text.substring(index, text.length()));
|
||||
break;
|
||||
}
|
||||
while (text.charAt(from+1) == '<') {
|
||||
from++;
|
||||
}
|
||||
int to = text.indexOf('>', from+1);
|
||||
if (to == -1) {
|
||||
sb.append(text.substring(index, text.length()));
|
||||
break;
|
||||
}
|
||||
int tagNameStart = from + 1;
|
||||
if (text.charAt(tagNameStart) == '/') {
|
||||
tagNameStart ++;
|
||||
}
|
||||
|
||||
if (isValidTagFirstChar(text.charAt(tagNameStart))) {
|
||||
String tag;
|
||||
tag = text.substring(tagNameStart, to);
|
||||
if (myTags.containsKey(tag)) {
|
||||
sb.append(text.substring(index, from));
|
||||
index = to + 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
to = from;
|
||||
}
|
||||
sb.append(text.substring(index, to + 1));
|
||||
index = to + 1;
|
||||
private String cutDefinedTags(String text) {
|
||||
StringBuilder builder = new StringBuilder(text);
|
||||
for (int i = mySkipped.size() - 1; i >= 0; i--) {
|
||||
TextRange range = mySkipped.get(i);
|
||||
builder.delete(range.getStartOffset(), range.getEndOffset());
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void resetIndices() {
|
||||
|
||||
@@ -109,6 +109,7 @@ options.xml.attribute.descriptor.prologue=Prologue
|
||||
options.xml.attribute.descriptor.comment=Comment
|
||||
options.xml.attribute.descriptor.tag=Tag
|
||||
options.xml.attribute.descriptor.tag.name=Tag name
|
||||
options.xml.attribute.descriptor.namespace.prefix=Namespace prefix
|
||||
options.xml.attribute.descriptor.attribute.name=Attribute name
|
||||
options.xml.attribute.descriptor.attribute.value=Attribute value
|
||||
options.xml.attribute.descriptor.tag.data=Tag data
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
<lang.syntaxHighlighterFactory key="DTD" implementationClass="com.intellij.lang.dtd.DtdSyntaxHighlighterFactory"/>
|
||||
|
||||
<annotator language="XML" implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlNamespaceAnnotator"/>
|
||||
<annotator language="XML" implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlNsPrefixAnnotator"/>
|
||||
|
||||
<renameHandler id="xmlTagRenameHandler" implementation="com.intellij.xml.refactoring.XmlTagRenameHandler"/>
|
||||
<renameHandler implementation="com.intellij.xml.refactoring.SchemaPrefixRenameHandler" order="before xmlTagRenameHandler"/>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2000-2013 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.daemon.impl.analysis;
|
||||
|
||||
import com.intellij.lang.annotation.AnnotationHolder;
|
||||
import com.intellij.lang.annotation.Annotator;
|
||||
import com.intellij.openapi.editor.XmlHighlighterColors;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.impl.source.xml.SchemaPrefixReference;
|
||||
import com.intellij.psi.xml.XmlAttribute;
|
||||
import com.intellij.psi.xml.XmlTag;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.FilteringIterator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author Dmitry Avdeev
|
||||
* Date: 25.10.13
|
||||
*/
|
||||
public class XmlNsPrefixAnnotator implements Annotator {
|
||||
@Override
|
||||
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
|
||||
if (element instanceof XmlTag || element instanceof XmlAttribute) {
|
||||
SchemaPrefixReference reference =
|
||||
(SchemaPrefixReference)ContainerUtil.find(element.getReferences(), FilteringIterator.instanceOf(SchemaPrefixReference.class));
|
||||
if (reference != null) {
|
||||
TextRange rangeInElement = reference.getRangeInElement();
|
||||
if (!rangeInElement.isEmpty()) {
|
||||
TextRange range = rangeInElement.shiftRight(element.getTextRange().getStartOffset());
|
||||
holder.createInfoAnnotation(range, null).setTextAttributes(XmlHighlighterColors.XML_NS_PREFIX);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import com.intellij.openapi.options.OptionsBundle;
|
||||
import com.intellij.openapi.options.colors.AttributesDescriptor;
|
||||
import com.intellij.openapi.options.colors.ColorDescriptor;
|
||||
import com.intellij.openapi.options.colors.ColorSettingsPage;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -35,6 +37,7 @@ public class XMLColorsPage implements ColorSettingsPage {
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.comment"), XmlHighlighterColors.XML_COMMENT),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.tag"), XmlHighlighterColors.XML_TAG),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.tag.name"), XmlHighlighterColors.XML_TAG_NAME),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.namespace.prefix"), XmlHighlighterColors.XML_NS_PREFIX),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.attribute.name"), XmlHighlighterColors.XML_ATTRIBUTE_NAME),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.attribute.value"), XmlHighlighterColors.XML_ATTRIBUTE_VALUE),
|
||||
new AttributesDescriptor(OptionsBundle.message("options.xml.attribute.descriptor.tag.data"), XmlHighlighterColors.XML_TAG_DATA),
|
||||
@@ -70,17 +73,18 @@ public class XMLColorsPage implements ColorSettingsPage {
|
||||
return "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
|
||||
"<!DOCTYPE index>\n" +
|
||||
"<!-- Some xml example -->\n" +
|
||||
"<index version=\"1.0\">\n" +
|
||||
"<index version=\"1.0\" xmlns:<np>pf</np>=\"http://test\">\n" +
|
||||
" <name>Main Index</name>\n" +
|
||||
" <indexitem text=\"rename\" target=\"refactoring.rename\"/>\n" +
|
||||
" <indexitem text=\"move\" target=\"refactoring.move\"/>\n" +
|
||||
" <indexitem text=\"migrate\" target=\"refactoring.migrate\"/>\n" +
|
||||
" <indexitem text=\"usage search\" target=\"find.findUsages\"/>\n& ·" +
|
||||
" <indexitem text=\"project\" target=\"project.management\"/>\n" +
|
||||
" <indexitem text=\"project\" target=\"project.management\"/>" +
|
||||
" <<np>pf</np>:foo <np>pf</np>:bar=\"bar\"/>\n" +
|
||||
"</index>";
|
||||
}
|
||||
|
||||
public Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() {
|
||||
return null;
|
||||
return ContainerUtil.newHashMap(Pair.create("np", XmlHighlighterColors.XML_NS_PREFIX));
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,8 @@ public class XmlHighlighterColors {
|
||||
TextAttributesKey.createTextAttributesKey("XML_TAG", DefaultLanguageHighlighterColors.MARKUP_TAG);
|
||||
public static final TextAttributesKey XML_TAG_NAME =
|
||||
TextAttributesKey.createTextAttributesKey("XML_TAG_NAME", DefaultLanguageHighlighterColors.KEYWORD);
|
||||
public static final TextAttributesKey XML_NS_PREFIX =
|
||||
TextAttributesKey.createTextAttributesKey("XML_NS_PREFIX", DefaultLanguageHighlighterColors.IDENTIFIER);
|
||||
public static final TextAttributesKey XML_ATTRIBUTE_NAME =
|
||||
TextAttributesKey.createTextAttributesKey("XML_ATTRIBUTE_NAME", DefaultLanguageHighlighterColors.MARKUP_ATTRIBUTE);
|
||||
public static final TextAttributesKey XML_ATTRIBUTE_VALUE =
|
||||
|
||||
Reference in New Issue
Block a user