📄 gutter.java
字号:
} //}}} //{{{ setStructureHighlightEnabled() method /** * Enables or disables structure highlighting. * @param structureHighlight True if structure highlighting should be * enabled, false otherwise * @since jEdit 4.2pre3 */ public final void setStructureHighlightEnabled(boolean structureHighlight) { this.structureHighlight = structureHighlight; repaint(); } //}}} //{{{ getMarkerHighlightColor() method public Color getMarkerHighlightColor() { return markerHighlightColor; } //}}} //{{{ setMarkerHighlightColor() method public void setMarkerHighlightColor(Color markerHighlightColor) { this.markerHighlightColor = markerHighlightColor; } //}}} //{{{ isMarkerHighlightEnabled() method public boolean isMarkerHighlightEnabled() { return markerHighlight; } //}}} //{{{ isMarkerHighlightEnabled() public void setMarkerHighlightEnabled(boolean markerHighlight) { this.markerHighlight = markerHighlight; } //}}} //}}} //{{{ Private members //{{{ Instance variables private static final int FOLD_MARKER_SIZE = 12; private View view; private JEditTextArea textArea; private ExtensionManager extensionMgr; private int baseline; private Dimension gutterSize = new Dimension(0,0); private Dimension collapsedSize = new Dimension(0,0); private Color intervalHighlight; private Color currentLineHighlight; private Color foldColor; private FontMetrics fm; private int alignment; private int interval; private boolean currentLineHighlightEnabled; private boolean expanded; private boolean structureHighlight; private Color structureHighlightColor; private boolean markerHighlight; private Color markerHighlightColor; private int borderWidth; private Border focusBorder, noFocusBorder; //}}} //{{{ paintLine() method private void paintLine(Graphics2D gfx, int line, int y) { Buffer buffer = textArea.getBuffer(); if(!buffer.isLoaded()) return; int lineHeight = textArea.getPainter().getFontMetrics() .getHeight(); ChunkCache.LineInfo info = textArea.chunkCache.getLineInfo(line); int physicalLine = info.physicalLine; // Skip lines beyond EOF if(physicalLine == -1) return; //{{{ Paint fold triangles if(info.firstSubregion && buffer.isFoldStart(physicalLine)) { int _y = y + lineHeight / 2; gfx.setColor(foldColor); if(textArea.displayManager .isLineVisible(physicalLine + 1)) { gfx.drawLine(1,_y - 3,10,_y - 3); gfx.drawLine(2,_y - 2,9,_y - 2); gfx.drawLine(3,_y - 1,8,_y - 1); gfx.drawLine(4,_y,7,_y); gfx.drawLine(5,_y + 1,6,_y + 1); } else { gfx.drawLine(4,_y - 5,4,_y + 4); gfx.drawLine(5,_y - 4,5,_y + 3); gfx.drawLine(6,_y - 3,6,_y + 2); gfx.drawLine(7,_y - 2,7,_y + 1); gfx.drawLine(8,_y - 1,8,_y); } } else if(info.lastSubregion && buffer.isFoldEnd(physicalLine)) { gfx.setColor(foldColor); int _y = y + lineHeight / 2; gfx.drawLine(4,_y,4,_y + 3); gfx.drawLine(4,_y + 3,7,_y + 3); } //}}} //{{{ Paint bracket scope else if(structureHighlight) { StructureMatcher.Match match = textArea.getStructureMatch(); int caretLine = textArea.getCaretLine(); if(textArea.isStructureHighlightVisible() && physicalLine >= Math.min(caretLine,match.startLine) && physicalLine <= Math.max(caretLine,match.startLine)) { int caretScreenLine; if(caretLine > textArea.getLastPhysicalLine()) caretScreenLine = Integer.MAX_VALUE; else if(textArea.displayManager.isLineVisible( textArea.getCaretLine())) { caretScreenLine = textArea .getScreenLineOfOffset( textArea.getCaretPosition()); } else { caretScreenLine = -1; } int structScreenLine; if(match.startLine > textArea.getLastPhysicalLine()) structScreenLine = Integer.MAX_VALUE; else if(textArea.displayManager.isLineVisible( match.startLine)) { structScreenLine = textArea .getScreenLineOfOffset( match.start); } else { structScreenLine = -1; } if(caretScreenLine > structScreenLine) { int tmp = caretScreenLine; caretScreenLine = structScreenLine; structScreenLine = tmp; } gfx.setColor(structureHighlightColor); if(structScreenLine == caretScreenLine) { // do nothing } // draw |^ else if(line == caretScreenLine) { gfx.fillRect(5, y + lineHeight / 2, 5, 2); gfx.fillRect(5, y + lineHeight / 2, 2, lineHeight - lineHeight / 2); } // draw |_ else if(line == structScreenLine) { gfx.fillRect(5, y, 2, lineHeight / 2); gfx.fillRect(5, y + lineHeight / 2, 5, 2); } // draw | else if(line > caretScreenLine && line < structScreenLine) { gfx.fillRect(5, y, 2, lineHeight); } } } //}}} //{{{ Paint line numbers if(info.firstSubregion && expanded) { String number = Integer.toString(physicalLine + 1); int offset; switch (alignment) { case RIGHT: offset = gutterSize.width - collapsedSize.width - (fm.stringWidth(number) + 1); break; case CENTER: offset = ((gutterSize.width - collapsedSize.width) - fm.stringWidth(number)) / 2; break; case LEFT: default: offset = 0; break; } boolean highlightCurrentLine = currentLineHighlightEnabled && textArea.selection.size() == 0; if (physicalLine == textArea.getCaretLine() && highlightCurrentLine) { gfx.setColor(currentLineHighlight); } else if (interval > 1 && (line + textArea.getFirstLine() + 1) % interval == 0) gfx.setColor(intervalHighlight); else gfx.setColor(getForeground()); gfx.drawString(number, FOLD_MARKER_SIZE + offset, baseline + y); } //}}} } //}}} //}}} //{{{ MouseHandler class class MouseHandler extends MouseInputAdapter { MouseActions mouseActions = new MouseActions("gutter"); boolean drag; int toolTipInitialDelay, toolTipReshowDelay; //{{{ mouseEntered() method public void mouseEntered(MouseEvent e) { ToolTipManager ttm = ToolTipManager.sharedInstance(); toolTipInitialDelay = ttm.getInitialDelay(); toolTipReshowDelay = ttm.getReshowDelay(); ttm.setInitialDelay(0); ttm.setReshowDelay(0); } //}}} //{{{ mouseExited() method public void mouseExited(MouseEvent evt) { ToolTipManager ttm = ToolTipManager.sharedInstance(); ttm.setInitialDelay(toolTipInitialDelay); ttm.setReshowDelay(toolTipReshowDelay); } //}}} //{{{ mousePressed() method public void mousePressed(MouseEvent e) { textArea.requestFocus(); if(GUIUtilities.isPopupTrigger(e) || e.getX() >= getWidth() - borderWidth * 2) { e.translatePoint(-getWidth(),0); textArea.mouseHandler.mousePressed(e); drag = true; } else { Buffer buffer = textArea.getBuffer(); int screenLine = e.getY() / textArea.getPainter() .getFontMetrics().getHeight(); int line = textArea.chunkCache.getLineInfo(screenLine) .physicalLine; if(line == -1) return; //{{{ Determine action String defaultAction; String variant; if(buffer.isFoldStart(line)) { defaultAction = "toggle-fold"; variant = "fold"; } else if(structureHighlight && textArea.isStructureHighlightVisible() && textArea.lineInStructureScope(line)) { defaultAction = "match-struct"; variant = "struct"; } else return; String action = mouseActions.getActionForEvent( e,variant); if(action == null) action = defaultAction; //}}} //{{{ Handle actions StructureMatcher.Match match = textArea .getStructureMatch(); if(action.equals("select-fold")) { textArea.displayManager.expandFold(line,true); textArea.selectFold(line); } else if(action.equals("narrow-fold")) { int[] lines = buffer.getFoldAtLine(line); textArea.displayManager.narrow(lines[0],lines[1]); } else if(action.startsWith("toggle-fold")) { if(textArea.displayManager .isLineVisible(line + 1)) { textArea.displayManager.collapseFold(line); } else { if(action.endsWith("-fully")) { textArea.displayManager .expandFold(line, true); } else { textArea.displayManager .expandFold(line, false); } } } else if(action.equals("match-struct")) { if(match != null) textArea.setCaretPosition(match.end); } else if(action.equals("select-struct")) { if(match != null) { match.matcher.selectMatch( textArea); } } else if(action.equals("narrow-struct")) { if(match != null) { int start = Math.min( match.startLine, textArea.getCaretLine()); int end = Math.max( match.endLine, textArea.getCaretLine()); textArea.displayManager.narrow(start,end); } } //}}} } } //}}} //{{{ mouseDragged() method public void mouseDragged(MouseEvent e) { if(drag /* && e.getX() >= getWidth() - borderWidth * 2 */) { e.translatePoint(-getWidth(),0); textArea.mouseHandler.mouseDragged(e); } } //}}} //{{{ mouseReleased() method public void mouseReleased(MouseEvent e) { if(drag && e.getX() >= getWidth() - borderWidth * 2) { e.translatePoint(-getWidth(),0); textArea.mouseHandler.mouseReleased(e); } drag = false; } //}}} } //}}} //{{{ MarkerHighlight class class MarkerHighlight extends TextAreaExtension { //{{{ paintValidLine() method public void paintValidLine(Graphics2D gfx, int screenLine, int physicalLine, int start, int end, int y) { if(isMarkerHighlightEnabled()) { Buffer buffer = textArea.getBuffer(); if(buffer.getMarkerInRange(start,end) != null) { gfx.setColor(getMarkerHighlightColor()); FontMetrics fm = textArea.getPainter().getFontMetrics(); gfx.fillRect(0,y,textArea.getGutter() .getWidth(),fm.getHeight()); } } } //}}} //{{{ getToolTipText() method public String getToolTipText(int x, int y) { if(isMarkerHighlightEnabled()) { int line = y / textArea.getPainter().getFontMetrics().getHeight(); int start = textArea.getScreenLineStartOffset(line); int end = textArea.getScreenLineEndOffset(line); if(start == -1 || end == -1) return null; Marker marker = textArea.getBuffer().getMarkerInRange(start,end); if(marker != null) { char shortcut = marker.getShortcut(); if(shortcut == '\0') return jEdit.getProperty("view.gutter.marker.no-name"); else { String[] args = { String.valueOf(shortcut) }; return jEdit.getProperty("view.gutter.marker",args); } } } return null; } //}}} } //}}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -