PageRenderTime 31ms CodeModel.GetById 20ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/textarea/StructureMatcher.java

#
Java | 236 lines | 148 code | 32 blank | 56 comment | 22 complexity | 8839bf940833b15c9bb6133a71b98b33 MD5 | raw file
  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
 23package org.gjt.sp.jedit.textarea;
 24
 25//{{{ Imports
 26import java.awt.*;
 27import org.gjt.sp.jedit.Buffer;
 28import org.gjt.sp.jedit.TextUtilities;
 29//}}}
 30
 31/**
 32 * An interface for matching parts of a source file's stucture. The default
 33 * implementation matches brackets. The XML plugin provides an implementation
 34 * for matching XML tags.
 35 *
 36 * @author Slava Pestov
 37 * @version $Id: StructureMatcher.java 4815 2003-06-27 20:02:06Z spestov $
 38 * @since jEdit 4.2pre3
 39 */
 40public interface StructureMatcher
 41{
 42	//{{{ getMatch() method
 43	/**
 44	 * Returns the element matching the one at the given text area's
 45	 * caret position, or null.
 46	 * @since jEdit 4.2pre3
 47	 */
 48	Match getMatch(JEditTextArea textArea);
 49	//}}}
 50
 51	//{{{ selectMatch() method
 52	/**
 53	 * Selects from the caret to the matching structure element (if there is
 54	 * one, otherwise the behavior of this method is undefined).
 55	 * @since jEdit 4.2pre3
 56	 */
 57	void selectMatch(JEditTextArea textArea);
 58	//}}}
 59
 60	//{{{ BracketMatcher class
 61	static class BracketMatcher implements StructureMatcher
 62	{
 63		public Match getMatch(JEditTextArea textArea)
 64		{
 65			int offset = textArea.getCaretPosition()
 66				- textArea.getLineStartOffset(
 67				textArea.getCaretLine());
 68
 69			if(offset != 0)
 70			{
 71				int bracketOffset = TextUtilities.findMatchingBracket(
 72					textArea.getBuffer(),
 73					textArea.getCaretLine(),
 74					offset - 1);
 75				if(bracketOffset != -1)
 76				{
 77					int bracketLine = textArea
 78						.getLineOfOffset(
 79						bracketOffset);
 80					return new Match(this,
 81						bracketLine,
 82						bracketOffset,
 83						bracketLine,
 84						bracketOffset + 1);
 85				}
 86			}
 87
 88			return null;
 89		}
 90
 91		public void selectMatch(JEditTextArea textArea)
 92		{
 93			textArea.selectToMatchingBracket();
 94		}
 95	} //}}}
 96
 97	//{{{ Match class
 98	/**
 99	 * A structure match, denoted by a start and end position.
100	 * @since jEdit 4.2pre3
101	 */
102	public static class Match
103	{
104		public StructureMatcher matcher;
105		public int startLine;
106		public int start;
107		public int endLine;
108		public int end;
109
110		public Match() {}
111
112		public Match(StructureMatcher matcher)
113		{
114			this.matcher = matcher;
115		}
116
117		public Match(StructureMatcher matcher, int startLine,
118			int start, int endLine, int end)
119		{
120			this(matcher);
121			this.startLine = startLine;
122			this.start = start;
123			this.endLine = endLine;
124			this.end = end;
125		}
126	} //}}}
127
128	//{{{ Highlight class
129	/**
130	 * Paints the structure match highlight.
131	 */
132	static class Highlight extends TextAreaExtension
133	{
134		Highlight(JEditTextArea textArea)
135		{
136			this.textArea = textArea;
137			returnValue = new Point();
138		}
139
140		public void paintValidLine(Graphics2D gfx, int screenLine,
141			int physicalLine, int start, int end, int y)
142		{
143			if(!textArea.getPainter().isStructureHighlightEnabled())
144				return;
145
146			Match match = textArea.getStructureMatch();
147			if(match != null)
148			{
149				paintHighlight(gfx,screenLine,physicalLine,
150					start,end,y,match);
151			}
152		}
153
154		private void paintHighlight(Graphics gfx, int screenLine,
155			int physicalLine, int start, int end, int y,
156			Match match)
157		{
158			if(!textArea.isStructureHighlightVisible())
159				return;
160
161			if(match.start >= end || match.end < start)
162			{
163				return;
164			}
165
166			int matchStartLine = textArea.getScreenLineOfOffset(
167				match.start);
168			int matchEndLine = textArea.getScreenLineOfOffset(
169				match.end);
170
171			FontMetrics fm = textArea.getPainter().getFontMetrics();
172			int height = fm.getHeight();
173
174			int x1, x2;
175
176			if(matchStartLine == screenLine)
177			{
178				x1 = match.start - textArea.getLineStartOffset(
179					match.startLine);
180			}
181			else
182				x1 = 0;
183
184			if(matchEndLine == screenLine)
185			{
186				x2 = match.end - textArea.getLineStartOffset(
187					match.endLine);
188			}
189			else
190			{
191				x2 = textArea.getScreenLineEndOffset(screenLine)
192					- textArea.getScreenLineStartOffset(screenLine);
193			}
194
195			x1 = textArea.offsetToXY(physicalLine,x1,returnValue).x;
196			x2 = textArea.offsetToXY(physicalLine,x2,returnValue).x;
197
198			gfx.setColor(textArea.getPainter().getStructureHighlightColor());
199
200			gfx.drawLine(x1,y,x1,y + height - 1);
201			gfx.drawLine(x2,y,x2,y + height - 1);
202
203			if(matchStartLine == screenLine || screenLine == 0)
204				gfx.drawLine(x1,y,x2,y);
205			else
206			{
207				int prevX1, prevX2;
208
209				if(matchStartLine == screenLine - 1)
210				{
211					prevX1 = match.start - textArea.getLineStartOffset(
212						match.startLine);
213				}
214				else
215					prevX1 = 0;
216
217				prevX2 = textArea.getScreenLineEndOffset(screenLine - 1)
218					- textArea.getScreenLineStartOffset(screenLine - 1);
219
220				prevX1 = textArea.offsetToXY(physicalLine - 1,prevX1,returnValue).x;
221				prevX2 = textArea.offsetToXY(physicalLine - 1,prevX2,returnValue).x;
222
223				gfx.drawLine(Math.min(x1,prevX1),y,
224					Math.max(x1,prevX1),y);
225				gfx.drawLine(Math.min(x2,prevX2),y,
226					Math.max(x2,prevX2),y);
227			}
228
229			if(matchEndLine == screenLine)
230				gfx.drawLine(x1,y + height - 1,x2,y + height - 1);
231		}
232
233		private JEditTextArea textArea;
234		private Point returnValue;
235	} //}}}
236}