PageRenderTime 42ms CodeModel.GetById 30ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 222 lines | 126 code | 29 blank | 67 comment | 2 complexity | 294c32ed2eb380fbf751e61546f430ff 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 * IndentAction.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 org.gjt.sp.jedit.buffer.JEditBuffer;
 26import org.gjt.sp.util.StandardUtilities;
 27
 28/**
 29 * @author Slava Pestov
 30 * @version $Id: IndentAction.java 15614 2009-06-30 07:21:56Z voituk $
 31 */
 32public interface IndentAction
 33{
 34	/**
 35	 * @param buffer The buffer
 36	 * @param line The line number that matched the rule; not necessarily
 37	 * the line being indented.
 38	 * @param oldIndent Original indent.
 39	 * @param newIndent The new indent -- ie, indent returned by previous
 40	 * indent action.
 41	 */
 42	int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
 43		int newIndent);
 44
 45	/**
 46	 * @return true if the indent engine should keep processing after
 47	 * this rule.
 48	 */
 49	boolean keepChecking();
 50
 51	/** See comments for each instance of this class below. */
 52	class Collapse implements IndentAction
 53	{
 54		/**
 55		 * This does nothing; it is merely a sentinel for the
 56		 * <code>OpenBracketIndentRule</code>.
 57		 */
 58		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
 59			int newIndent)
 60		{
 61			return newIndent;
 62		}
 63
 64		public boolean keepChecking()
 65		{
 66			return true;
 67		}
 68
 69		private Collapse()
 70		{
 71		}
 72	}
 73
 74	class Reset implements IndentAction
 75	{
 76		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
 77			int newIndent)
 78		{
 79			return oldIndent;
 80		}
 81
 82		public boolean keepChecking()
 83		{
 84			return true;
 85		}
 86	}
 87
 88	class Increase implements IndentAction
 89	{
 90		private int amount;
 91
 92		public Increase()
 93		{
 94			amount = 1;
 95		}
 96
 97		public Increase(int amount)
 98		{
 99			this.amount = amount;
100		}
101
102		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
103			int newIndent)
104		{
105			return newIndent + buffer.getIndentSize() * amount;
106		}
107
108		public boolean keepChecking()
109		{
110			return true;
111		}
112
113		public boolean equals(Object o)
114		{
115			if(o instanceof Increase)
116				return ((Increase)o).amount == amount;
117			else
118				return false;
119		}
120	}
121
122	class Decrease implements IndentAction
123	{
124		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
125			int newIndent)
126		{
127			return newIndent - buffer.getIndentSize();
128		}
129
130		public boolean keepChecking()
131		{
132			return true;
133		}
134	}
135
136	/**
137	* @author Matthieu Casanova
138	*/
139	class AlignOffset implements IndentAction
140	{
141		private int offset;
142
143		public AlignOffset(int offset)
144		{
145			this.offset = offset;
146		}
147
148		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
149			int newIndent)
150		{
151			return offset;
152		}
153
154		public boolean keepChecking()
155		{
156			return false;
157		}
158	}
159
160	/**
161	* Indent action used for deep indent.
162	* @author Matthieu Casanova
163	*/
164	class AlignParameter implements IndentAction
165	{
166		private int openParensColumn;
167
168		public AlignParameter(int openParensColumn)
169		{
170			this.openParensColumn = openParensColumn;
171		}
172
173		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
174				     int newIndent)
175		{
176			return openParensColumn + 1;
177		}
178
179		public boolean keepChecking()
180		{
181			return false;
182		}
183	}
184
185	/**
186	 * Used to cancel increases in indentation.
187	 *
188	 * @author Marcelo Vanzin
189	 */
190	class NoIncrease implements IndentAction
191	{
192		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
193				           int newIndent)
194		{
195			int current = StandardUtilities.getLeadingWhiteSpaceWidth(
196					buffer.getLineSegment(line),buffer.getTabSize());
197			return (current < newIndent) ? current : newIndent;
198		}
199
200		public boolean keepChecking()
201		{
202			return true;
203		}
204	}
205
206	/**
207	 * This handles the following Java code:
208	 * if(something)
209	 * { // no indentation on this line, even though previous matches a rule
210	 */
211	Collapse PrevCollapse		= new Collapse();
212	/**
213	 * This handles cases like:
214	 * if (foo)
215	 *     bar;
216	 * for (something; condition; action) {
217	 * }
218	 * Without this the "for" line would be incorrectly indented.
219	 */
220	Collapse PrevPrevCollapse	= new Collapse();
221}
222