Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render braces in gutter. #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added HighlightBracketPair-1.1.2.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
public class HighlightEditorComponent implements CaretListener {
private final Editor editor;
private List<RangeHighlighter> highlighterList = new ArrayList<>();
private List<RangeHighlighter> gutterHighlighterList = new ArrayList<>();
private ExtraHighlightTrigger extraHighlightTrigger;

public void dispose() {
Expand Down Expand Up @@ -98,13 +99,24 @@ public void highlightEditorCurrentPair(Editor editor) {
// clear the high lighter
highlighter.eraseHighlight(highlighterList);

// clear braces in gutter
highlighter.eraseHighlight(gutterHighlighterList);

// find the brace positions
BracePair bracePair = highlighter.findClosetBracePair(offset);

// high light the brace
Pair<RangeHighlighter, RangeHighlighter> highlighterEntry =
highlighter.highlightPair(bracePair);

// show braces in gutter
List<RangeHighlighter> showBracesInGutter =
highlighter.showBracesInGutter(bracePair);

if (showBracesInGutter!= null) {
gutterHighlighterList.addAll(showBracesInGutter);
}

// record the high lighter
if (highlighterEntry != null) {
highlighterList.add(highlighterEntry.getLeft());
Expand Down
95 changes: 88 additions & 7 deletions src/main/java/io/github/qeesung/highlighter/BraceHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.intellij.openapi.editor.ex.MarkupModelEx;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.editor.markup.*;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDocumentManager;
Expand All @@ -21,7 +18,11 @@
import io.github.qeesung.brace.BracePair;
import io.github.qeesung.setting.HighlightBracketPairSettingsPage;
import io.github.qeesung.util.Pair;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

Expand Down Expand Up @@ -57,7 +58,6 @@ public BraceHighlighter(Editor editor) {
}

/**
*
* @return
*/
public List<Pair<IElementType, IElementType>> getSupportedBraceToken() {
Expand All @@ -82,8 +82,6 @@ public BracePair findClosetBracePairInBraceTokens(int offset) {
rightType(braceTokenPair.getRight()).
leftIterator(leftTraverseIterator).
rightIterator(rightTraverseIterator).build();


}
}
return EMPTY_BRACE_PAIR;
Expand Down Expand Up @@ -125,6 +123,89 @@ public BracePair findClosetBracePair(int offset) {
}
}

public List<RangeHighlighter> showBracesInGutter(BracePair bracePair) {
final Brace leftBrace = bracePair.getLeftBrace();
final Brace rightBrace = bracePair.getRightBrace();
final int leftBraceOffset = leftBrace.getOffset();
final int rightBraceOffset = rightBrace.getOffset();
final String leftBraceText = leftBrace.getText();
final String rightBraceText = rightBrace.getText();

if (leftBraceOffset == NON_OFFSET ||
rightBraceOffset == NON_OFFSET)
return null;

// try to get the text attr by element type
TextAttributesKey textAttributesKey =
HighlightBracketPairSettingsPage.getTextAttributesKeyByToken(leftBrace.getElementType());
// if not found, get the text attr by brace text
if (textAttributesKey == null) {
textAttributesKey = HighlightBracketPairSettingsPage.getTextAttributesKeyByText(leftBraceText);
}
final TextAttributes textAttributes = editor.getColorsScheme().getAttributes(textAttributesKey);

int openBraceLine = document.getLineNumber(leftBraceOffset);
RangeHighlighter openBraceHighlighter = renderBraceInGutter(openBraceLine, leftBraceText, textAttributes);

int closeBraceLine = document.getLineNumber(rightBraceOffset);
RangeHighlighter closeBraceHighlighter = renderBraceInGutter(closeBraceLine, rightBraceText, textAttributes);

List<RangeHighlighter> highlighters = new ArrayList<RangeHighlighter>();
highlighters.add(openBraceHighlighter);
highlighters.add(closeBraceHighlighter);

return highlighters;
}

public RangeHighlighter renderBraceInGutter(int braceLine, String braceText, TextAttributes textAttributes) {
RangeHighlighter braceHighlighter = editor.getMarkupModel()
.addLineHighlighter(braceLine, HighlighterLayer.SELECTION, null);

GutterIconRenderer braceGutterIconRenderer = new GutterIconRenderer() {

@NotNull
@Override
public Icon getIcon() {
return new Icon() {
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
if (braceText.toString().toCharArray().length < 1) {
return;
}

g.setColor(textAttributes.getForegroundColor());
g.drawChars(braceText.toString().toCharArray(), 0, braceText.length(), 0, 0);
}

@Override
public int getIconWidth() {
return 1;
}

@Override
public int getIconHeight() {
return 1;
}
};
}

@Override
public boolean equals(Object obj) {
return false;
}

@Override
public int hashCode() {
return 1;
}
};

braceHighlighter.setGutterIconRenderer(braceGutterIconRenderer);
braceHighlighter.setGreedyToRight(true);

return braceHighlighter;
}

public Pair<RangeHighlighter, RangeHighlighter> highlightPair(BracePair bracePair) {
final Brace leftBrace = bracePair.getLeftBrace();
final Brace rightBrace = bracePair.getRightBrace();
Expand Down