PageRenderTime 84ms CodeModel.GetById 70ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/indent/BracketIndentRule.java

#
Java | 168 lines | 119 code | 16 blank | 33 comment | 14 complexity | 8273c1dfed1b0b7e9771a9c073be3cd4 MD5 | raw file
  1/*
  2 * BracketIndentRule.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2005 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.indent;
 24
 25import javax.swing.text.Segment;
 26import org.gjt.sp.jedit.buffer.JEditBuffer;
 27import org.gjt.sp.jedit.syntax.Token;
 28import org.gjt.sp.jedit.syntax.TokenHandler;
 29import org.gjt.sp.jedit.syntax.TokenMarker;
 30
 31/**
 32 * @author Slava Pestov
 33 * @version $Id: BracketIndentRule.java 18919 2010-11-04 10:52:55Z kpouer $
 34 */
 35public abstract class BracketIndentRule implements IndentRule
 36{
 37	//{{{ BracketIndentRule constructor
 38	public BracketIndentRule(char openBracket, char closeBracket)
 39	{
 40		this.openBracket = openBracket;
 41		this.closeBracket = closeBracket;
 42	} //}}}
 43
 44	//{{{ Brackets class
 45	public static class Brackets
 46	{
 47		int openCount;
 48		int closeCount;
 49	} //}}}
 50
 51	//{{{ getBrackets() method
 52	public Brackets getBrackets(JEditBuffer buffer, int lineIndex)
 53	{
 54		return getBrackets(buffer, lineIndex,
 55			0, buffer.getLineLength(lineIndex));
 56	} //}}}
 57
 58	//{{{ getBrackets() method
 59	public Brackets getBrackets(JEditBuffer buffer, int lineIndex,
 60		int begin, int end)
 61	{
 62		LineScanner scanner = new LineScanner(begin, end);
 63		buffer.markTokens(lineIndex, scanner);
 64		return scanner.result;
 65	} //}}}
 66
 67	//{{{ toString() method
 68	public String toString()
 69	{
 70		return getClass().getName() + "[" + openBracket + ","
 71			+ closeBracket + "]";
 72	} //}}}
 73
 74	protected char openBracket, closeBracket;
 75
 76	//{{{ class LineScanner
 77	private class LineScanner implements TokenHandler
 78	{
 79		public final Brackets result;
 80
 81		private int scannedIndex;
 82		private final int beginIndex;
 83		private final int endIndex;
 84
 85		public LineScanner(int begin, int end)
 86		{
 87			this.result = new Brackets();
 88			this.scannedIndex = 0;
 89			this.beginIndex = begin;
 90			this.endIndex = end;
 91		}
 92
 93		boolean rejectsToken(byte id)
 94		{
 95			// Rejects comments and literals.
 96			// Accepts all others.
 97			switch (id)
 98			{
 99			case Token.COMMENT1:
100			case Token.COMMENT2:
101			case Token.COMMENT3:
102			case Token.COMMENT4:
103			case Token.LITERAL1:
104			case Token.LITERAL2:
105			case Token.LITERAL3:
106			case Token.LITERAL4:
107				return true;
108			default:
109				return false;
110			}
111		}
112
113		private void scan(Segment seg, int offset, int length)
114		{
115			int index = scannedIndex;
116			if (index >= endIndex)
117			{
118				return;
119			}
120			if (index < beginIndex)
121			{
122				int numToSkip = beginIndex - index;
123				if (numToSkip >= length)
124				{
125					return;
126				}
127				offset += numToSkip;
128				length -= numToSkip;
129				index = beginIndex;
130			}
131			if (index + length > endIndex)
132			{
133				length = endIndex - index;
134			}
135
136			for (int i = 0; i < length; ++i)
137			{
138				char c = seg.array[seg.offset + offset + i];
139				if(c == openBracket)
140				{
141					result.openCount++;
142				}
143				else if(c == closeBracket)
144				{
145					if(result.openCount != 0)
146						result.openCount--;
147					else
148						result.closeCount++;
149				}
150			}
151		}
152
153		public void handleToken(Segment seg
154			, byte id, int offset, int length
155			, TokenMarker.LineContext context)
156		{
157			if (!rejectsToken(id))
158			{
159				scan(seg, offset, length);
160			}
161			scannedIndex += length;
162		}
163
164		public void setLineContext(TokenMarker.LineContext lineContext)
165		{
166		}
167	} //}}}
168}