All pastes #2096239 Raw Edit

328510

public text v1 · immutable
#2096239 ·published 2011-12-25 07:40 UTC
rendered paste body
package com.horlivost.codepad.logic.indent;import android.widget.EditText;public class IndentManager {		private EditText codeArea;	int tabPlace = 0;		/**	* Constructor: IndentManager.IndentManager (	* 	* @param text	*            ) Creates a new Instance of the class IndentManager	*/	public IndentManager(EditText text) {		this.codeArea = text;	}		public IndentManager() {		// Dummy!		codeArea = null;	}		public void keyTyped(int keycode) {				int caretPos = codeArea.getSelectionStart();		String text = codeArea.getText().toString();				// if (keycode == '(') {			// printClosingSymbol(")");			// } else if (keycode == '[') {			// printClosingSymbol("]");			// } else if (keycode == '\"') {			// try {				// if (text.substring(caretPos, caretPos + 1).equals("\"")) {					// codeArea.setSelection(caretPos, caretPos + 1);					// } else {					// printClosingSymbol("\"");					// }				// } catch (IndexOutOfBoundsException e) {				// e.printStackTrace();				// }			// } else if (keycode == ']') {			// try {				// if (text.substring(caretPos, caretPos + 1).equals("]")) {					// codeArea.setSelection(caretPos, caretPos + 1);					// }				// } catch (IndexOutOfBoundsException e) {				// e.printStackTrace();				// }			// } else if (keycode == ')') {			// try {				// if (text.substring(caretPos, caretPos + 1).equals(")")) {					// codeArea.setSelection(caretPos, caretPos + 1);					// }				// } catch (IndexOutOfBoundsException e) {				// e.printStackTrace();				// }			// } else if (keycode == '}') {		// int n = codeArea.getSelectionStart();		// try {			// if (calculatePreviousTabPlace() == -1) {				// return;				// }			// if (text.substring(n - 1, n).equals("\t")) {				// codeArea.setSelection(n - 1, n);				// // the tab space is replaced by closing brace automatically				// // when the key is released				// }			// } catch (IndexOutOfBoundsException e) {			// e.printStackTrace();			// }		// } else if (keycode == '\n') {		String ch = peviousLineLastChar();		System.out.println("ch>>>>> " + ch);		if (ch == null) {			return;		}		tabPlace = calculatePreviousTabPlace();		if (ch.equals("{") ) {				tabPlace++;			}			for (int i = 0; i < tabPlace; i++) {				codeArea.append("\t", 0,				1);			}			// }	}		/**	* Method: IndentManager.printClosingSymbol - (void)		* 		* @param c		*            if char such as ",(,[ are pressed the corresponding ",),] are		*            printed		*/		private void printClosingSymbol(String c) {		int temp = codeArea.getSelectionStart();		codeArea.append(c, temp, c.length());		codeArea.setSelection(temp);			}		/**	* Method: IndentManager.calculatePreviousTabPlace - (int)		* 		* @return nuber of tabspaces that the previous line left		*/		private int calculatePreviousTabPlace() {		String ch;		int i = 0;		try {						int lastLine = codeArea.getLayout().getLineForOffset(			codeArea.getSelectionStart() - 1);			while (true) {				char c = codeArea.getText().toString()					.charAt(codeArea.getLayout().getLineEnd(lastLine) - 1);				if (c != '\n')					lastLine--;				else				break;			}			// if it was the first line then there will be no previous line's			// tabs			if (lastLine <= 0) {				return 0;			}						int n = codeArea.getLayout().getLineStart(lastLine);			String text = codeArea.getText().toString();			ch = text.substring(n, n + 1);			while (ch.equals("\t")) {				i++;				n++;				ch = text.substring(n, n+1);			}		} catch (Exception e) {			e.printStackTrace();			return -1;		}				return i;	}		/**	* Method: IndentManager.peviousLineLastChar - (String)		* 		* @return		*/		private String peviousLineLastChar() {		String ch;		try {			int n = codeArea.getLayout().getLineEnd(			codeArea.getLayout().getLineForOffset(			codeArea.getSelectionStart()) - 1);			if (n == 1) {				return null;			}			ch = codeArea.getText().toString().substring(n - 2, n - 1);		} catch (Exception e) {			e.printStackTrace();			return null;		}		return ch;	}		public String format(String str) {		str = str.replaceAll("\t", "");		int i;		int level = 0;		int single_line_indent_level = 0;		String newString = "";		int j;		i = 0;		while (i < str.length() && str.charAt(i) == ' ')			i++;		for (; i < str.length(); i++) {			if (str.charAt(i) == '{' && single_line_indent_level == 0)					level++;				else if (str.charAt(i) == '{' && single_line_indent_level != 0) {						if (newString.charAt(newString.length() - 1) == '\t')							newString = newString.substring(0, newString.length() - 1);						single_line_indent_level--;						level++;					} else if (str.charAt(i) == ')' && str.charAt(i + 1) == '\n') {						single_line_indent_level++;					} else if (str.charAt(i) == ';' && single_line_indent_level != 0) {						single_line_indent_level--;					} else if (str.charAt(i) == '}') {					level--;					if (newString.charAt(newString.length() - 1) == '\t')						newString = newString.substring(0, newString.length() - 1);				} else if (str.charAt(i) == '\n') {					newString += str.charAt(i);					i++;					while (i < str.length() && str.charAt(i) == ' ')						i++;					i--;					for (j = 0; j < level + single_line_indent_level; j++)						newString += "\t";					continue;				}				if (i >= str.length())					break;				newString += str.charAt(i);			}			return newString;		}				public void format() {			String str = codeArea.getText().toString();			str = format(str);			codeArea.append(str);		}	}