PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/textarea/StructureMatcher.java

#
Java | 239 lines | 152 code | 31 blank | 56 comment | 20 complexity | 8241aa05273f403d7a98caceb39d5eb1 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * StructureMatcher.java - Abstract interface for bracket matching, etc.
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.textarea;
  23. //{{{ Imports
  24. import java.awt.*;
  25. import org.gjt.sp.jedit.TextUtilities;
  26. //}}}
  27. /**
  28. * An interface for matching parts of a source file's stucture. The default
  29. * implementation matches brackets. The XML plugin provides an implementation
  30. * for matching XML tags.
  31. *
  32. * @author Slava Pestov
  33. * @version $Id: StructureMatcher.java 18808 2010-10-21 20:54:15Z daleanson $
  34. * @since jEdit 4.2pre3
  35. */
  36. public interface StructureMatcher
  37. {
  38. //{{{ getMatch() method
  39. /**
  40. * Returns the element matching the one at the given text area's
  41. * caret position, or null.
  42. * @since jEdit 4.2pre3
  43. */
  44. Match getMatch(TextArea textArea);
  45. //}}}
  46. //{{{ selectMatch() method
  47. /**
  48. * Selects from the caret to the matching structure element (if there is
  49. * one, otherwise the behavior of this method is undefined).
  50. * @since jEdit 4.2pre3
  51. */
  52. void selectMatch(TextArea textArea);
  53. //}}}
  54. //{{{ BracketMatcher class
  55. static class BracketMatcher implements StructureMatcher
  56. {
  57. public Match getMatch(TextArea textArea)
  58. {
  59. int offset = textArea.getCaretPosition()
  60. - textArea.getLineStartOffset(
  61. textArea.getCaretLine());
  62. if(offset != 0)
  63. {
  64. int bracketOffset = TextUtilities.findMatchingBracket(
  65. textArea.getBuffer(),
  66. textArea.getCaretLine(),
  67. offset - 1);
  68. if(bracketOffset != -1)
  69. {
  70. int bracketLine = textArea
  71. .getLineOfOffset(
  72. bracketOffset);
  73. return new Match(this,
  74. bracketLine,
  75. bracketOffset,
  76. bracketLine,
  77. bracketOffset + 1);
  78. }
  79. }
  80. return null;
  81. }
  82. public void selectMatch(TextArea textArea)
  83. {
  84. textArea.selectToMatchingBracket();
  85. }
  86. } //}}}
  87. //{{{ Match class
  88. /**
  89. * A structure match, denoted by a start and end position.
  90. * @since jEdit 4.2pre3
  91. */
  92. public static class Match
  93. {
  94. public StructureMatcher matcher;
  95. public int startLine;
  96. public int start;
  97. public int endLine;
  98. public int end;
  99. public Match() {}
  100. public Match(StructureMatcher matcher)
  101. {
  102. this.matcher = matcher;
  103. }
  104. public Match(StructureMatcher matcher, int startLine,
  105. int start, int endLine, int end)
  106. {
  107. this(matcher);
  108. this.startLine = startLine;
  109. this.start = start;
  110. this.endLine = endLine;
  111. this.end = end;
  112. }
  113. } //}}}
  114. //{{{ Highlight class
  115. /**
  116. * Paints the structure match highlight.
  117. */
  118. static class Highlight extends TextAreaExtension
  119. {
  120. Highlight(TextArea textArea)
  121. {
  122. this.textArea = textArea;
  123. }
  124. public void paintValidLine(Graphics2D gfx, int screenLine,
  125. int physicalLine, int start, int end, int y)
  126. {
  127. if(!textArea.getPainter().isStructureHighlightEnabled())
  128. return;
  129. Match match = textArea.getStructureMatch();
  130. if(match != null)
  131. {
  132. paintHighlight(gfx,screenLine,
  133. start,end,y,match);
  134. }
  135. }
  136. private int[] getOffsets(int screenLine, Match match)
  137. {
  138. int x1, x2;
  139. int matchStartLine = textArea.getScreenLineOfOffset(
  140. match.start);
  141. int matchEndLine = textArea.getScreenLineOfOffset(
  142. match.end);
  143. if(matchStartLine == screenLine)
  144. {
  145. x1 = match.start;
  146. }
  147. else
  148. {
  149. x1 = textArea.getScreenLineStartOffset(
  150. screenLine);
  151. }
  152. if(matchEndLine == screenLine)
  153. {
  154. x2 = match.end;
  155. }
  156. else
  157. {
  158. x2 = textArea.getScreenLineEndOffset(
  159. screenLine) - 1;
  160. }
  161. return new int[] {
  162. textArea.offsetToXY(x1).x,
  163. textArea.offsetToXY(x2).x
  164. };
  165. }
  166. private void paintHighlight(Graphics gfx, int screenLine,
  167. int start, int end, int y,
  168. Match match)
  169. {
  170. if(!textArea.isStructureHighlightVisible())
  171. return;
  172. if(match.start >= end || match.end < start)
  173. {
  174. return;
  175. }
  176. int matchStartLine = textArea.getScreenLineOfOffset(
  177. match.start);
  178. int matchEndLine = textArea.getScreenLineOfOffset(
  179. match.end);
  180. int fontHeight = textArea.getPainter().getFontHeight();
  181. y += textArea.getPainter().getLineExtraSpacing();
  182. int[] offsets = getOffsets(screenLine,match);
  183. int x1 = offsets[0];
  184. int x2 = offsets[1];
  185. gfx.setColor(textArea.getPainter().getStructureHighlightColor());
  186. gfx.drawLine(x1,y,x1,y + fontHeight - 1);
  187. gfx.drawLine(x2,y,x2,y + fontHeight - 1);
  188. if(matchStartLine == screenLine || screenLine == 0)
  189. gfx.drawLine(x1,y,x2,y);
  190. else
  191. {
  192. offsets = getOffsets(screenLine - 1,match);
  193. int prevX1 = offsets[0];
  194. int prevX2 = offsets[1];
  195. gfx.drawLine(Math.min(x1,prevX1),y,
  196. Math.max(x1,prevX1),y);
  197. gfx.drawLine(Math.min(x2,prevX2),y,
  198. Math.max(x2,prevX2),y);
  199. }
  200. if(matchEndLine == screenLine)
  201. {
  202. gfx.drawLine(x1,y + fontHeight - 1,
  203. x2,y + fontHeight - 1);
  204. }
  205. }
  206. private TextArea textArea;
  207. } //}}}
  208. }