[SCM] Lisaac eclipse plugin branch, master, updated. 35c1aa11404b2bac880aa3434ddb3b194cd5adbc

Damien Bouvarel dams.bouvarel at wanadoo.fr
Tue Apr 14 16:26:55 UTC 2009


The following commit has been merged in the master branch:
commit ea4882aef30844c8f8b1368bbbc050c98b1466e7
Author: Damien Bouvarel <dams.bouvarel at wanadoo.fr>
Date:   Wed Apr 8 00:58:53 2009 +0200

    Comment Toggle

diff --git a/plugin.xml b/plugin.xml
index 6818baa..9d1dba6 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -221,6 +221,13 @@
                toolbarPath="org.eclipse.lisaac.toolbar"
                tooltip="Create new prototype">
          </action>
+         <action
+               class="org.eclipse.lisaac.actions.ToggleComment"
+               definitionId="org.eclipse.lisaac.cmd4"
+               id="org.eclipse.lisaac.comment1"
+               label="Toggle Comment"
+               menubarPath="org.eclipse.lisaac.sourcemenu/indent">
+         </action>
       </actionSet>
    </extension>
    <extension
@@ -244,6 +251,11 @@
             id="org.eclipse.lisaac.cmd3"
             name="New Prototype">
       </command>
+      <command
+            categoryId="org.eclipse.lisaac.commands"
+            id="org.eclipse.lisaac.cmd4"
+            name="Toggle comment">
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.bindings">
diff --git a/src/org/eclipse/lisaac/actions/ToggleComment.java b/src/org/eclipse/lisaac/actions/ToggleComment.java
new file mode 100644
index 0000000..999d8ac
--- /dev/null
+++ b/src/org/eclipse/lisaac/actions/ToggleComment.java
@@ -0,0 +1,128 @@
+package org.eclipse.lisaac.actions;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.rules.ICharacterScanner;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.lisaac.LisaacPlugin;
+import org.eclipse.lisaac.editors.LisaacEditor;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.IWorkbenchWindowActionDelegate;
+
+public class ToggleComment implements IWorkbenchWindowActionDelegate {
+
+	/**
+	 * The action has been activated. The argument of the
+	 * method represents the 'real' action sitting
+	 * in the workbench UI.
+	 * @see IWorkbenchWindowActionDelegate#run
+	 */
+	public void run(IAction action) {
+		IWorkbenchWindow w = LisaacPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow();
+
+		IWorkbenchPart part = w.getPartService().getActivePart();
+		if (part instanceof LisaacEditor) {
+			IDocument document = ((LisaacEditor)part).getDocument();
+			//
+			ITextSelection selection = (ITextSelection) ((LisaacEditor)part).getSelectionProvider().getSelection();
+
+			try {
+				if (selection.getStartLine() == selection.getEndLine()) {
+					// single line comment
+					
+					int startPos = document.getLineOffset(selection.getStartLine());
+					int pos = startPos;
+					char c;
+					do {
+						c = document.getChar(pos);
+						if (c != ICharacterScanner.EOF) {
+							if (c == '/') {
+								pos++;
+								c = document.getChar(pos);
+								if (c != ICharacterScanner.EOF && c == '/') {
+									document.replace(pos-1, 2, "");// delete comment
+									return;
+								}
+							}
+						}
+						pos++;
+					} while (c != ICharacterScanner.EOF && pos <= startPos+selection.getLength());
+					
+					// add comment
+					document.replace(startPos, 0, "//");
+					
+				} else {
+					// multiline comment
+					int startPos = document.getLineOffset(selection.getStartLine());
+					int pos = startPos;
+					boolean deleteComment=false;
+					char c;
+					do {
+						c = document.getChar(pos);
+						if (c != ICharacterScanner.EOF) {
+							if (c == '/') {
+								pos++;
+								c = document.getChar(pos);
+								if (c != ICharacterScanner.EOF && c == '*') {
+									document.replace(pos-1, 2, "");// delete comment
+									deleteComment = true;
+									pos++;
+								}
+							}
+							if (c == '*') {
+								pos++;
+								c = document.getChar(pos);
+								if (c != ICharacterScanner.EOF && c == '/') {
+									if (deleteComment) {
+										document.replace(pos-1, 2, "");// delete comment
+									}
+								}
+							}
+						}
+						pos++;
+					} while (c != ICharacterScanner.EOF && pos <= startPos+selection.getLength());
+					
+					// add comment
+					if (! deleteComment) {
+						document.replace(startPos, 0, "/*");
+						int ofs = document.getLineOffset(selection.getEndLine());
+						ofs += document.getLineLength(selection.getEndLine());
+						document.replace(ofs, 0, "*/");
+					}
+				}
+				
+			} catch(BadLocationException e) {
+			}
+			//
+		}
+	}
+
+	/**
+	 * Selection in the workbench has been changed. We 
+	 * can change the state of the 'real' action here
+	 * if we want, but this can only happen after 
+	 * the delegate has been created.
+	 * @see IWorkbenchWindowActionDelegate#selectionChanged
+	 */
+	public void selectionChanged(IAction action, ISelection selection) {
+	}
+
+	/**
+	 * We will cache window object in order to
+	 * be able to provide parent shell for the message dialog.
+	 * @see IWorkbenchWindowActionDelegate#init
+	 */
+	public void init(IWorkbenchWindow window) {
+	}
+
+	/**
+	 * We can use this method to dispose of any system
+	 * resources we previously allocated.
+	 * @see IWorkbenchWindowActionDelegate#dispose
+	 */
+	public void dispose() {
+	}
+}

-- 
Lisaac eclipse plugin



More information about the Lisaac-commits mailing list