Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2000, 2005 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
  
 package org.eclipse.ant.internal.ui.editor;
Auto edit strategy for Ant build files Current does special indenting.
 
 	
 	private AntModel fModel;
 	private int fAccumulatedChange= 0;
 	
 	public AntAutoEditStrategy(AntModel model) {
 		model;
 	}

Sets the indentation based on the Ant element node that contains the offset of the document command.

Parameters:
d the document to work on
c the command to deal with
 
 	private synchronized void autoIndentAfterNewLine(IDocument dDocumentCommand c) {
 		
 		if (c.offset == -1 || d.getLength() == 0 || .getProjectNode(false) == null) {
 			return;
 		}
 		
 		int position= (c.offset == d.getLength() ? c.offset  - 1 : c.offset);
 		if (node == null) {
 			return;
 		}
 		
 		try {
 			StringBuffer correct= XmlDocumentFormatter.getLeadingWhitespace(node.getOffset(), d);
 			if (!nextNodeIsEndTag(c.offsetd)) {
 				correct.append(XmlDocumentFormatter.createIndent());
 			}
 			StringBuffer bufnew StringBuffer(c.text);
 			buf.append(correct);
 			
 			int lined.getLineOfOffset(position);
 			IRegion regd.getLineInformation(line);
 			int lineEndreg.getOffset() + reg.getLength();
 			int contentStartfindEndOfWhiteSpace(dc.offsetlineEnd);
 			
 			c.length=  Math.max(contentStart - c.offset, 0);
 			c.caretOffsetc.offset + buf.length();
 			c.shiftsCaretfalse;
 			c.textbuf.toString();
 	
 		} catch (BadLocationException e) {
 			AntUIPlugin.log(e);
 		}
 	}
 	
 	private boolean nextNodeIsEndTag(int offsetIDocument document) {
 		if (offset + 1 > document.getLength()) {
 			return false;
 		}
 		try {
 			IRegion lineRegiondocument.getLineInformationOfOffset(offset);
 			offsetfindEndOfWhiteSpace(documentoffsetlineRegion.getOffset() + lineRegion.getLength());
 			String nextCharsdocument.get(offset, 2).trim();
 			if ("</".equals(nextChars) || "/>".equals(nextChars)) { //$NON-NLS-1$ //$NON-NLS-2$
 				return true;
 			}
 		} catch (BadLocationException e) {
 		}
 		return false;
 	}
 
 	/* (non-Javadoc)
 	 * @see org.eclipse.jface.text.IAutoEditStrategy#customizeDocumentCommand(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.DocumentCommand)
	 */
		if (c.length == 0 && c.text != null && isLineDelimiter(dc.text)) {
else if (c.text.length() > 1) {
			smartPaste(dc);
		}
	}
	 private boolean isLineDelimiter(IDocument documentString text) {
		String[] delimitersdocument.getLegalLineDelimiters();
		if (delimiters != null)
			return TextUtilities.equals(delimiterstext) > -1;
		return false;
	}
	public synchronized void reconciled() {
	}
	private void smartPaste(IDocument documentDocumentCommand command) {
		try {
			if (command.offset == -1 || document.getLength() == 0 || .getProjectNode(false) == null) {
				return;
			}
			String origChangecommand.text;
			int position= (command.offset == document.getLength() ? command.offset  - 1 : command.offset);
			if (node == null) {
				return;
			}
			// eat any WS before the insertion to the beginning of the line
			int firstLine= 1; // don't format the first line if it has other content before it
			IRegion linedocument.getLineInformationOfOffset(command.offset);
			String notSelecteddocument.get(line.getOffset(), command.offset - line.getOffset());
			if (notSelected.trim().length() == 0) {
				command.length += notSelected.length();
				command.offsetline.getOffset();
				firstLine= 0;
			}
			// handle the indentation computation inside a temporary document
			Document tempnew Document(command.text);
			// indent the first and second line
			// compute the relative indentation difference from the second line
			// (as the first might be partially selected) and use the value to
			// indent all other lines.
			boolean isIndentDetectedfalse;
			StringBuffer additionnew StringBuffer();
			int insertLength= 0;
			int linestemp.getNumberOfLines();
			for (int lfirstLinel < linesl++) { // we don't change the number of lines while adding indents
				int lineOffsetr.getOffset();
				int lineLengthr.getLength();
				if (lineLength == 0) { // don't modify empty lines
					continue;
				}
				if (!isIndentDetected){
					// indent the first pasted line
					StringBuffer current= XmlDocumentFormatter.getLeadingWhitespace(lineOffsettemp);
					StringBuffer correct= XmlDocumentFormatter.getLeadingWhitespace(node.getOffset(), document);
					correct.append(XmlDocumentFormatter.createIndent());
					insertLengthsubtractIndent(correctcurrentaddition);
					isIndentDetectedtrue;
				}
				// relatively indent all pasted lines 
				if (insertLength > 0) {
					addIndent(templaddition);
else if (insertLength < 0) {
					cutIndent(templ, -insertLength);
				}	
			}
			// modify the command
			if (!origChange.equals(temp.get())) {
				command.texttemp.get();
			}
catch (BadLocationException e) {
			AntUIPlugin.log(e);
		}
	}

Indents line line in document with indent. Leaves leading comment signs alone.

Parameters:
document the document
line the line
indent the indentation to insert
Throws:
org.eclipse.jface.text.BadLocationException on concurrent document modification
	private void addIndent(Document documentint lineCharSequence indentthrows BadLocationException {
		IRegion regiondocument.getLineInformation(line);
		int insertregion.getOffset();
		// insert indent
		document.replace(insert, 0, indent.toString());
	}

Cuts the visual equivalent of toDelete characters out of the indentation of line line in document.

Parameters:
document the document
line the line
toDelete the number of space equivalents to delete.
Throws:
org.eclipse.jface.text.BadLocationException on concurrent document modification
	private void cutIndent(Document documentint lineint toDeletethrows BadLocationException {
		IRegion regiondocument.getLineInformation(line);
		int fromregion.getOffset();
		int endOffsetregion.getOffset() + region.getLength();
		int tofrom;
		while (toDelete > 0 && to < endOffset) {
			char chdocument.getChar(to);
			if (!Character.isWhitespace(ch))
				break;
			toDelete -= computeVisualLength(ch);
			if (toDelete >= 0) {
				to++;
else {
				break;
			}
		}
		document.replace(fromto - fromnull);
	}

Returns the visual length of a given character taking into account the visual tabulator length.

Parameters:
ch the character to measure
Returns:
the visual length of ch
	private int computeVisualLength(char ch) {
		if (ch == '\t') {
		return 1;
	}

Returns the visual length of a given CharSequence taking into account the visual tabulator length.

Parameters:
seq the string to measure
Returns:
the visual length of seq
	private int computeVisualLength(CharSequence seq) {
		int size= 0;
		for (int i= 0; i < seq.length(); i++) {
			char chseq.charAt(i);
			if (ch == '\t') {
				size += tablen - size % tablen;
else {
				size++;
			}
		}
		return size;
	}

Computes the difference of two indentations and returns the difference in length of current and correct. If the return value is positive, addition is initialized with a substring of that length of correct.

Parameters:
correct the correct indentation
current the current indentation (might contain non-whitespace)
difference a string buffer - if the return value is positive, it will be cleared and set to the substring of current of that length
Returns:
the difference in length of correct and current
	private int subtractIndent(CharSequence correctCharSequence currentStringBuffer difference) {
		int c1computeVisualLength(correct);
		int c2computeVisualLength(current);
		int diffc1 - c2;
		if (diff <= 0) {
			return diff;
		}
		difference.setLength(0);
		int len= 0, i= 0;
		while (len < diff) {
			char ccorrect.charAt(i++);
			difference.append(c);
		}
		return diff;
	}

The preference setting for the visual tabulator display.

Returns:
the number of spaces displayed for a tabulator in the editor
	}
New to GrepCode? Check out our FAQ X