/*
 * @author: fabioz
 * Created: January 2004
 * License: Common Public License v1.0
 */

package org.python.pydev.editor.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Creates a comment block
 * 
 * @author Fabio Zadrozny
 */
public class PyAddBlockComment extends PyAction {

	/**
	 * Hi.
	 * Insert a comment block.
	 *                #===...
	 * 				  #
	 * 				  #===...
	 * (TODO:This could be customized somewhere...)
	 */
	public void run(IAction action) {
		try {
			ITextEditor textEditor = getTextEditor();

			IDocument doc =
				textEditor.getDocumentProvider().getDocument(
					textEditor.getEditorInput());
			ITextSelection selection =
				(ITextSelection) textEditor
					.getSelectionProvider()
					.getSelection();

			IRegion startLine = null;

			int startLineIndex = selection.getStartLine();

			String endLineDelim = getDelimiter(doc, startLineIndex);
			startLine = doc.getLineInformation(startLineIndex);

			IRegion line = doc.getLineInformation(startLineIndex);
			int pos = line.getOffset();
			StringBuffer strBuf = new StringBuffer();
			strBuf.append(endLineDelim);
			strBuf.append(
				"#===============================================================================");
			strBuf.append(endLineDelim);
			strBuf.append("# ");
			strBuf.append(endLineDelim);
			strBuf.append(
				"#===============================================================================");
			strBuf.append(endLineDelim);
			strBuf.append(endLineDelim);

			doc.replace(pos, 0, strBuf.toString());

		} catch (Exception e) {
			beep(e);
		}
	}
}

@@ -465,16 +465,8 @@
 /**%0A
-%09 * Hi.%0A
 %09 * 
@@ -903,24 +903,322 @@
 lection();%0A%0A
+%09%09%09//strange things happen when we try to get the line information on the last line of the%0A%09%09%09//document (it doesn't return the end line delimiter correctly), so we always get on position%0A%09%09%09//0 and check to see if we are not in the last line. %0A%09%09%09if (doc.getNumberOfLines() %3C= 1)%0A%09%09%09%09return;%0A%09%09%09%09%0A
 %09%09%09IRegion s
@@ -1329,30 +1329,17 @@
 er(doc, 
-startLineIndex
+0
 );%0A%09%09%09st
@@ -1458,31 +1458,165 @@
 int 
-pos = line.getOffset();
+lineLenght = line.getLength();%0A%09%09%09int pos = line.getOffset();%0A%09%09%09%0A%09%09%09String content = doc.get(pos, lineLenght);%0A%09%09%09String comment = getFullCommentLine();%0A%09%09%09
 %0A%09%09%09
@@ -1657,16 +1657,20 @@
 ffer();%0A
+%09%09%09%0A
 %09%09%09strBu
@@ -1714,97 +1714,33 @@
 end(
-%0A%09%09%09%09%22#===============================================================================%22);
+comment); //#=========...
 %0A%09%09%09
@@ -1760,32 +1760,36 @@
 (endLineDelim);%0A
+%09%09%09%0A
 %09%09%09strBuf.append
@@ -1792,24 +1792,55 @@
 pend(%22# %22);%0A
+%09%09%09strBuf.append(content);%0A%09%09%09%0A
 %09%09%09strBuf.ap
@@ -1880,248 +1880,373 @@
 end(
-%0A%09%09%09%09%22#===============================================================================%22);%0A%09%09%09strBuf.append(endLineDelim);%0A%09%09%09strBuf.append(endLineDelim
+comment); //#=========...%0A%09%09%09strBuf.append(endLineDelim);%0A%09%09%09%0A%09%09%09doc.replace(pos, lineLenght, strBuf.toString()
 );%0A
-%0A
 %09%09%09
-doc.replace(pos, 0, strBuf.toString());%0A%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09beep(e);%0A%09%09%7D
+textEditor.selectAndReveal(pos+strBuf.length()-comment.length()-4,0);%0A%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09beep(e);%0A%09%09%7D%0A%09%7D%0A%09%0A%09private String getFullCommentLine()%7B%0A%09%09return %22#===============================================================================%22;
 %0A%09%7D%0A

Comment block now goes to right position.