PageRenderTime 122ms CodeModel.GetById 70ms app.highlight 11ms RepoModel.GetById 20ms app.codeStats 0ms

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

#
Java | 167 lines | 107 code | 18 blank | 42 comment | 16 complexity | 4879e85c032b7df502a4e36696c0c1cc MD5 | raw file
  1/*
  2 * CloseBracketIndentRule.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 java.util.List;
 26import org.gjt.sp.jedit.buffer.JEditBuffer;
 27import org.gjt.sp.jedit.TextUtilities;
 28import org.gjt.sp.util.StandardUtilities;
 29
 30/**
 31 * @author Slava Pestov
 32 * @version $Id: CloseBracketIndentRule.java 18919 2010-11-04 10:52:55Z kpouer $
 33 */
 34public class CloseBracketIndentRule extends BracketIndentRule
 35{
 36	//{{{ CloseBracketIndentRule constructor
 37	public CloseBracketIndentRule(char closeBracket, boolean aligned)
 38	{
 39		super(TextUtilities.getComplementaryBracket(closeBracket,null),
 40			closeBracket);
 41		this.aligned = aligned;
 42	} //}}}
 43
 44	//{{{ apply() method
 45	public void apply(JEditBuffer buffer, int thisLineIndex,
 46		int prevLineIndex, int prevPrevLineIndex,
 47		List<IndentAction> indentActions)
 48	{
 49		int index;
 50		if(aligned)
 51			index = thisLineIndex;
 52		else
 53			index = prevLineIndex;
 54
 55		if(index == -1)
 56			return;
 57
 58		CharSequence lineText = buffer.getLineSegment(index);
 59		int offset;
 60		for (offset = lineText.length() - 1; offset >= 0; offset--)
 61		{
 62			if (lineText.charAt(offset) == closeBracket)
 63				break;
 64		}
 65		if(offset == -1)
 66			return;
 67
 68		int closeCount = getBrackets(buffer, index).closeCount;
 69		if(closeCount != 0)
 70		{
 71			AlignBracket alignBracket
 72				= new AlignBracket(buffer,index,offset);
 73			/*
 74			Consider the following Common Lisp code (with one more opening
 75			bracket than closing):
 76
 77			(defun emit-push-long (arg)
 78			  (cond ((eql arg 0)
 79			      (emit 'lconst_0))
 80			    ((eql arg 1)
 81			      (emit 'lconst_1)))
 82
 83			even though we have a closing bracket match on line 3,
 84			the next line must be indented relative to the
 85			corresponding opening bracket from line 1.
 86			*/
 87			int openLine = alignBracket.getOpenBracketLine();
 88			if(openLine != -1)
 89			{
 90				int column = alignBracket.getOpenBracketColumn();
 91				alignBracket.setExtraIndent(
 92					getBrackets(buffer, openLine,
 93						0, column).openCount);
 94			}
 95
 96			indentActions.add(alignBracket);
 97		}
 98	} //}}}
 99
100	private boolean aligned;
101
102	//{{{ AlignBracket class
103	private static class AlignBracket implements IndentAction
104	{
105		private int line, offset;
106		private int openBracketLine;
107		private int openBracketColumn;
108		private CharSequence openBracketLineText;
109		private int extraIndent;
110
111		public AlignBracket(JEditBuffer buffer, int line, int offset)
112		{
113			this.line = line;
114			this.offset = offset;
115
116			int openBracketIndex = TextUtilities.findMatchingBracket(
117				buffer,this.line,this.offset);
118			if(openBracketIndex == -1)
119				openBracketLine = -1;
120			else
121			{
122				openBracketLine = buffer.getLineOfOffset(openBracketIndex);
123				openBracketColumn = openBracketIndex -
124					buffer.getLineStartOffset(openBracketLine);
125				openBracketLineText = buffer.getLineSegment(openBracketLine);
126			}
127		}
128
129		public int getExtraIndent()
130		{
131			return extraIndent;
132		}
133
134		public void setExtraIndent(int extraIndent)
135		{
136			this.extraIndent = extraIndent;
137		}
138
139		public int getOpenBracketColumn()
140		{
141			return openBracketColumn;
142		}
143
144		public int getOpenBracketLine()
145		{
146			return openBracketLine;
147		}
148
149		public int calculateIndent(JEditBuffer buffer, int line, int oldIndent,
150			int newIndent)
151		{
152			if(openBracketLineText == null)
153				return newIndent;
154			else
155			{
156				return StandardUtilities.getLeadingWhiteSpaceWidth(
157					openBracketLineText,buffer.getTabSize())
158					+ (extraIndent * buffer.getIndentSize());
159			}
160		}
161
162		public boolean keepChecking()
163		{
164			return false;
165		}
166	} //}}}
167}