Skip to content

Commit

Permalink
💩 refactor: Improve the opening function and optimize the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilViolet committed Jun 13, 2024
1 parent f023adc commit 809aa84
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static synchronized EditorHighlightEditorEntity getInstance() {
/**
* add HighlightEditorCartListener
*
* @param key key
* @param key key
* @param value value
*/
public void putValue(Editor key, HighlightEditorCartListener value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.lang.Language;
import com.intellij.psi.tree.IElementType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -16,8 +17,13 @@
* @since v1.3.0
*/
public class CustomSupportedToken {
Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<Language,
List<Pair<IElementType, IElementType>>> languagePairsMap) {
return languagePairsMap;
/**
* Added support for customization
*
* @param languagePairsMap languagePairsMap
* @return List<Pair<IElementType, IElementType>>
*/
public List<Pair<IElementType, IElementType>> addSupported(Map<Language, List<Pair<IElementType, IElementType>>> languagePairsMap) {
return new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
* @since v1.3.0
*/
public class VueSupportedToken extends CustomSupportedToken {
public static final String VUE = "VUE";

@Override
public Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<Language,
List<Pair<IElementType, IElementType>>> languagePairsMap) {
public List<Pair<IElementType, IElementType>> addSupported(Map<Language, List<Pair<IElementType, IElementType>>> languagePairsMap) {
Language vue = Language.findLanguageByID("Vue");
if (vue == null) {
return languagePairsMap;
return new ArrayList<>();
}
List<Pair<IElementType, IElementType>> vueJsPairList = languagePairsMap.get(Language.findLanguageByID("VueJS"));
List<Pair<IElementType, IElementType>> xmlPairList = languagePairsMap.get(Language.findLanguageByID("XML"));
Expand All @@ -38,7 +39,7 @@ public Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<La
vuePairList.addAll(cssPairList);
}
languagePairsMap.put(vue, vuePairList);
return languagePairsMap;
return vuePairList;
}

public enum Singleton {
Expand All @@ -57,8 +58,16 @@ public VueSupportedToken getInstance() {
return vueSupportedToken;
}

public Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<Language, List<Pair<IElementType, IElementType>>> languagePairsMap) {
return vueSupportedToken.addSupported(languagePairsMap);
public void addSupported(Language language, Map<Language, List<Pair<IElementType, IElementType>>> map) {
if (VUE.equalsIgnoreCase(language.getID())) {
vueSupportedToken.addSupported(map);
}
}

public void removeSupported(Language language, Map<Language, List<Pair<IElementType, IElementType>>> map) {
if (VUE.equalsIgnoreCase(language.getID())) {
map.remove(language);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.xml.XmlTokenType;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* Customer Xml support Bracket
Expand All @@ -24,6 +21,35 @@
* @since v1.3.0
*/
public class XmlSupportedToken extends CustomSupportedToken {
public static final String XML = "XML";

@Override
public List<Pair<IElementType, IElementType>> addSupported(Map<Language, List<Pair<IElementType, IElementType>>> languagePairsMap) {
Language xml = Language.findLanguageByID("XML");
if (xml == null) {
return new ArrayList<>();
}

List<Pair<IElementType, IElementType>> pairList = languagePairsMap.getOrDefault(xml, new ArrayList<>());
if (CollectionUtils.isEmpty(pairList)) {
pairList.add(new Pair<>(XmlTokenType.XML_START_TAG_START, XmlTokenType.XML_TAG_END));
pairList.add(new Pair<>(XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER,
XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER));
pairList.add(new Pair<>(XmlTokenType.XML_START_TAG_START, XmlTokenType.XML_EMPTY_ELEMENT_END));
pairList.add(new Pair<>(BraceTokenTypes.TEXT_TOKEN, BraceTokenTypes.TEXT_TOKEN));
languagePairsMap.put(xml, pairList);
for (Language subLanguage : xml.getDialects()) {
List<Pair<IElementType, IElementType>> pairs = languagePairsMap.get(subLanguage);
if (pairs != null) {
pairs.addAll(pairList);
} else {
languagePairsMap.put(subLanguage, pairList);
}
}
}
return pairList;
}

public static String getLeftPart(int start, HighlighterIterator iterator) {
int tagNameEnd = 0;
Document document = iterator.getDocument();
Expand Down Expand Up @@ -82,35 +108,6 @@ public static String getRightPart(int end, HighlighterIterator iterator, int lef
return "";
}

@Override
public Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<Language, List<Pair<IElementType,
IElementType>>> languagePairsMap) {
Language xml = Language.findLanguageByID("XML");
if (xml == null) {
return languagePairsMap;
}

List<Pair<IElementType, IElementType>> pairList = languagePairsMap.get(xml);
if (pairList == null) {
pairList = new ArrayList<>();
pairList.add(new Pair<>(XmlTokenType.XML_START_TAG_START, XmlTokenType.XML_TAG_END));
pairList.add(new Pair<>(XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER,
XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER));
pairList.add(new Pair<>(XmlTokenType.XML_START_TAG_START, XmlTokenType.XML_EMPTY_ELEMENT_END));
pairList.add(new Pair<>(BraceTokenTypes.TEXT_TOKEN, BraceTokenTypes.TEXT_TOKEN));
languagePairsMap.put(xml, pairList);
for (Language subLanguage : xml.getDialects()) {
List<Pair<IElementType, IElementType>> pairs = languagePairsMap.get(subLanguage);
if (pairs != null) {
pairs.addAll(pairList);
} else {
languagePairsMap.put(subLanguage, pairList);
}
}
}
return languagePairsMap;
}

public enum Singleton {
/**
* Enumeration singleton
Expand All @@ -127,8 +124,16 @@ public XmlSupportedToken getInstance() {
return xmlSupportedToken;
}

public Map<Language, List<Pair<IElementType, IElementType>>> addSupported(Map<Language, List<Pair<IElementType, IElementType>>> map) {
return xmlSupportedToken.addSupported(map);
public void addSupported(Language language, Map<Language, List<Pair<IElementType, IElementType>>> map) {
if (XML.equalsIgnoreCase(language.getID())) {
xmlSupportedToken.addSupported(map);
}
}

public void removeSupported(Language language, Map<Language, List<Pair<IElementType, IElementType>>> map) {
if (XML.equalsIgnoreCase(language.getID())) {
map.remove(language);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public Pair<RangeHighlighter, RangeHighlighter> renderBracesInGutter(BracePair b
/**
* Render braces in gutter
*
* @param braceLine brace line
* @param braceText brace text
* @param braceLine brace line
* @param braceText brace text
* @param textAttributes text
* @return RangeHighlighter
*/
Expand Down Expand Up @@ -286,7 +286,7 @@ private BracePair findCloseBracePairInBraceTokens(int offset) {
.leftOffset(leftBraceOffset).rightOffset(rightBraceOffset).build();
}
return new BracePair.BracePairBuilder().leftType(braceTokenPair.getLeft()).rightType(braceTokenPair.getRight())
.leftIterator(leftTraverseIterator).rightIterator(rightTraverseIterator).build();
.leftIterator(leftTraverseIterator).rightIterator(rightTraverseIterator).build();
}
}
return EMPTY_BRACE_PAIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.intellij.lang.PairedBraceMatcher;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.tree.IElementType;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;

Expand Down Expand Up @@ -53,18 +54,23 @@ public DefaultAbstractBracketHighlighter(Editor editor) {
@Override
public List<Pair<IElementType, IElementType>> getSupportedBraceToken() {
Language language = this.psiFile.getLanguage();
List<Pair<IElementType, IElementType>> braceList = LANGUAGE_BRACE_PAIRS.get(language);
boolean emptyFlag = CollectionUtils.isEmpty(braceList);
if (Boolean.TRUE.equals(bracketPairSettings.getHighlightXmlFlag())) {
XmlSupportedToken.Singleton.INSTANCE.addSupported(LANGUAGE_BRACE_PAIRS);
if (emptyFlag) {
XmlSupportedToken.Singleton.INSTANCE.addSupported(language, LANGUAGE_BRACE_PAIRS);
}
} else {
LANGUAGE_BRACE_PAIRS.remove(language);
XmlSupportedToken.Singleton.INSTANCE.removeSupported(language, LANGUAGE_BRACE_PAIRS);
}
if (Boolean.TRUE.equals(bracketPairSettings.getHighlightVueFlag())) {
VueSupportedToken.Singleton.INSTANCE.addSupported(LANGUAGE_BRACE_PAIRS);
if (emptyFlag) {
VueSupportedToken.Singleton.INSTANCE.addSupported(language, LANGUAGE_BRACE_PAIRS);
}
} else {
LANGUAGE_BRACE_PAIRS.remove(language);
VueSupportedToken.Singleton.INSTANCE.removeSupported(language, LANGUAGE_BRACE_PAIRS);
}

List<Pair<IElementType, IElementType>> braceList = LANGUAGE_BRACE_PAIRS.get(language);
braceList = LANGUAGE_BRACE_PAIRS.get(language);
return braceList == null ? new ArrayList<>() : braceList;
}

Expand Down

0 comments on commit 809aa84

Please sign in to comment.