Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 140629 Details for
Bug 86520
[editor] toggle comment always uses <!-- -->
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
The Long Awaited Commenting Fix
86520_commenting.txt (text/plain), 118.20 KB, created by
Ian Tewksbury
on 2009-07-01 17:02:48 EDT
(
hide
)
Description:
The Long Awaited Commenting Fix
Filename:
MIME Type:
Creator:
Ian Tewksbury
Created:
2009-07-01 17:02:48 EDT
Size:
118.20 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.wst.xml.ui >Index: plugin.xml >=================================================================== >RCS file: /cvsroot/webtools/sourceediting/plugins/org.eclipse.wst.xml.ui/plugin.xml,v >retrieving revision 1.106 >diff -u -r1.106 plugin.xml >--- plugin.xml 29 Jun 2009 17:17:08 -0000 1.106 >+++ plugin.xml 1 Jul 2009 20:39:22 -0000 >@@ -1171,7 +1171,7 @@ > </enabledWhen> > </handler> > <handler >- class="org.eclipse.wst.xml.ui.internal.handlers.ToggleCommentHandler" >+ class="org.eclipse.wst.xml.ui.internal.handlers.ToggleXMLCommentHandler" > commandId="org.eclipse.wst.sse.ui.toggle.comment"> > <activeWhen> > <reference definitionId="org.eclipse.wst.xml.ui.comments"/> >@@ -1181,7 +1181,7 @@ > </enabledWhen> > </handler> > <handler >- class="org.eclipse.wst.xml.ui.internal.handlers.AddBlockCommentHandler" >+ class="org.eclipse.wst.xml.ui.internal.handlers.AddXMLBlockCommentHandler" > commandId="org.eclipse.wst.sse.ui.add.block.comment"> > <activeWhen> > <reference definitionId="org.eclipse.wst.xml.ui.comments"/> >@@ -1191,7 +1191,7 @@ > </enabledWhen> > </handler> > <handler >- class="org.eclipse.wst.xml.ui.internal.handlers.RemoveBlockCommentHandler" >+ class="org.eclipse.wst.xml.ui.internal.handlers.RemoveXMLBlockCommentHandler" > commandId="org.eclipse.wst.sse.ui.remove.block.comment"> > <activeWhen> > <reference definitionId="org.eclipse.wst.xml.ui.comments"/> >Index: src/org/eclipse/wst/xml/ui/internal/handlers/CommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/CommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/CommentHandler.java >--- src/org/eclipse/wst/xml/ui/internal/handlers/CommentHandler.java 27 Mar 2008 02:58:48 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,103 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2008 Standards for Technology in Automotive Retail 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: >- * David Carver - initial API and implementation, bug 212330 >- * >- *******************************************************************************/ >-package org.eclipse.wst.xml.ui.internal.handlers; >- >-import org.eclipse.core.commands.AbstractHandler; >-import org.eclipse.core.commands.ExecutionEvent; >-import org.eclipse.core.commands.ExecutionException; >-import org.eclipse.core.commands.IHandler; >-import org.eclipse.jface.text.BadLocationException; >-import org.eclipse.jface.text.IDocument; >-import org.eclipse.jface.text.ITextSelection; >-import org.eclipse.jface.text.TextSelection; >-import org.eclipse.jface.viewers.ISelection; >-import org.eclipse.jface.viewers.ISelectionProvider; >-import org.eclipse.ui.IEditorPart; >-import org.eclipse.ui.handlers.HandlerUtil; >-import org.eclipse.ui.texteditor.ITextEditor; >-import org.eclipse.wst.xml.ui.internal.Logger; >- >-public class CommentHandler extends AbstractHandler implements IHandler { >- static final String CLOSE_COMMENT = "-->"; //$NON-NLS-1$ >- static final String OPEN_COMMENT = "<!--"; //$NON-NLS-1$ >- >- public CommentHandler() { >- super(); >- } >- >- public Object execute(ExecutionEvent event) throws ExecutionException { >- IEditorPart editor = HandlerUtil.getActiveEditor(event); >- ITextEditor textEditor = null; >- if (editor instanceof ITextEditor) >- textEditor = (ITextEditor) editor; >- else { >- Object o = editor.getAdapter(ITextEditor.class); >- if (o != null) >- textEditor = (ITextEditor) o; >- } >- if (textEditor != null) { >- IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); >- if (document != null) { >- // get current text selection >- ITextSelection textSelection = getCurrentSelection(textEditor); >- if (textSelection.isEmpty()) { >- return null; >- } >- >- processAction(textEditor, document, textSelection); >- } >- } >- >- return null; >- } >- >- protected ITextSelection getCurrentSelection(ITextEditor textEditor) { >- ISelectionProvider provider = textEditor.getSelectionProvider(); >- if (provider != null) { >- ISelection selection = provider.getSelection(); >- if (selection instanceof ITextSelection) { >- return (ITextSelection) selection; >- } >- } >- return TextSelection.emptySelection(); >- } >- >- void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >- // Implementations to over ride. >- } >- >- protected void removeOpenCloseComments(IDocument document, int offset, int length) { >- try { >- int adjusted_length = length; >- >- // remove open comments >- String string = document.get(offset, length); >- int index = string.lastIndexOf(OPEN_COMMENT); >- while (index != -1) { >- document.replace(offset + index, OPEN_COMMENT.length(), ""); //$NON-NLS-1$ >- index = string.lastIndexOf(OPEN_COMMENT, index - 1); >- adjusted_length -= OPEN_COMMENT.length(); >- } >- >- // remove close comments >- string = document.get(offset, adjusted_length); >- index = string.lastIndexOf(CLOSE_COMMENT); >- while (index != -1) { >- document.replace(offset + index, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$ >- index = string.lastIndexOf(CLOSE_COMMENT, index - 1); >- } >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- } >-} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/AddBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/AddBlockCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/AddBlockCommentHandler.java >--- src/org/eclipse/wst/xml/ui/internal/handlers/AddBlockCommentHandler.java 27 Mar 2008 02:58:48 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,77 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2008 Standards for Technology in Automotive Retail 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: >- * David Carver - initial API and implementation, bug 212330 >- * >- *******************************************************************************/ >-package org.eclipse.wst.xml.ui.internal.handlers; >- >-import org.eclipse.core.commands.IHandler; >-import org.eclipse.jface.text.BadLocationException; >-import org.eclipse.jface.text.IDocument; >-import org.eclipse.jface.text.ITextSelection; >-import org.eclipse.ui.texteditor.ITextEditor; >-import org.eclipse.wst.sse.core.StructuredModelManager; >-import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >-import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; >-import org.eclipse.wst.xml.core.internal.document.CommentImpl; >-import org.eclipse.wst.xml.ui.internal.Logger; >-import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >- >-public class AddBlockCommentHandler extends CommentHandler implements IHandler { >- >- public AddBlockCommentHandler() { >- super(); >- } >- >- void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >- IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >- if (model != null) { >- try { >- IndexedRegion selectionStartIndexedRegion = model.getIndexedRegion(textSelection.getOffset()); >- IndexedRegion selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength()); >- >- if (selectionStartIndexedRegion == null) { >- return; >- } >- if ((selectionEndIndexedRegion == null) && (textSelection.getLength() > 0)) { >- selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength() - 1); >- } >- if (selectionEndIndexedRegion == null) { >- return; >- } >- >- int openCommentOffset = selectionStartIndexedRegion.getStartOffset(); >- int closeCommentOffset = selectionEndIndexedRegion.getEndOffset() + OPEN_COMMENT.length(); >- >- if ((textSelection.getLength() == 0) && (selectionStartIndexedRegion instanceof CommentImpl)) { >- return; >- } >- >- model.beginRecording(this, XMLUIMessages.AddBlockComment_tooltip); >- model.aboutToChangeModel(); >- >- try { >- document.replace(openCommentOffset, 0, OPEN_COMMENT); >- document.replace(closeCommentOffset, 0, CLOSE_COMMENT); >- super.removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length()); >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- finally { >- model.changedModel(); >- model.endRecording(this); >- } >- } >- finally { >- model.releaseFromEdit(); >- } >- } >- } >-} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/RemoveBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/RemoveBlockCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/RemoveBlockCommentHandler.java >--- src/org/eclipse/wst/xml/ui/internal/handlers/RemoveBlockCommentHandler.java 27 Mar 2008 02:58:48 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,84 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2008 Standards for Technology in Automotive Retail 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: >- * David Carver - initial API and implementation, bug 212330 >- * >- *******************************************************************************/ >-package org.eclipse.wst.xml.ui.internal.handlers; >- >-import org.eclipse.core.commands.IHandler; >-import org.eclipse.jface.text.BadLocationException; >-import org.eclipse.jface.text.IDocument; >-import org.eclipse.jface.text.ITextSelection; >-import org.eclipse.ui.texteditor.ITextEditor; >-import org.eclipse.wst.sse.core.StructuredModelManager; >-import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >-import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; >-import org.eclipse.wst.xml.core.internal.document.CommentImpl; >-import org.eclipse.wst.xml.ui.internal.Logger; >-import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >- >-/** >- * @author dcarver >- * >- */ >-public class RemoveBlockCommentHandler extends CommentHandler implements IHandler { >- >- public RemoveBlockCommentHandler() { >- super(); >- } >- >- void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >- IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >- if (model != null) { >- try { >- IndexedRegion selectionStartIndexedRegion = model.getIndexedRegion(textSelection.getOffset()); >- IndexedRegion selectionEndIndexedRegion = model.getIndexedRegion(textSelection.getOffset() + textSelection.getLength()); >- >- if ((selectionStartIndexedRegion == null) || (selectionEndIndexedRegion == null)) { >- return; >- } >- >- int openCommentOffset = selectionStartIndexedRegion.getStartOffset(); >- int closeCommentOffset = selectionEndIndexedRegion.getEndOffset() - OPEN_COMMENT.length() - CLOSE_COMMENT.length(); >- >- model.beginRecording(this, XMLUIMessages.RemoveBlockComment_tooltip); >- model.aboutToChangeModel(); >- >- try { >- if (textSelection.getLength() == 0) { >- if (selectionStartIndexedRegion instanceof CommentImpl) { >- document.replace(openCommentOffset, OPEN_COMMENT.length(), ""); //$NON-NLS-1$ >- document.replace(closeCommentOffset, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$ >- } >- } >- else { >- if (selectionStartIndexedRegion instanceof CommentImpl) { >- document.replace(openCommentOffset, OPEN_COMMENT.length(), ""); //$NON-NLS-1$ >- } >- >- if (selectionEndIndexedRegion instanceof CommentImpl) { >- document.replace(closeCommentOffset, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$ >- } >- } >- removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length()); >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- finally { >- model.changedModel(); >- model.endRecording(this); >- } >- } >- finally { >- model.releaseFromEdit(); >- } >- } >- } >-} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/ToggleCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/ToggleCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/ToggleCommentHandler.java >--- src/org/eclipse/wst/xml/ui/internal/handlers/ToggleCommentHandler.java 27 Mar 2008 02:58:48 -0000 1.2 >+++ /dev/null 1 Jan 1970 00:00:00 -0000 >@@ -1,193 +0,0 @@ >-/******************************************************************************* >- * Copyright (c) 2008 Standards for Technology in Automotive Retail 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: >- * David Carver - initial API and implementation, bug 212330 >- * >- *******************************************************************************/ >-package org.eclipse.wst.xml.ui.internal.handlers; >- >-import org.eclipse.core.commands.ExecutionEvent; >-import org.eclipse.core.commands.ExecutionException; >-import org.eclipse.core.commands.IHandler; >-import org.eclipse.jface.text.BadLocationException; >-import org.eclipse.jface.text.IDocument; >-import org.eclipse.jface.text.IRegion; >-import org.eclipse.jface.text.ITextSelection; >-import org.eclipse.jface.text.Position; >-import org.eclipse.jface.text.TextSelection; >-import org.eclipse.jface.viewers.ISelectionProvider; >-import org.eclipse.ui.IEditorPart; >-import org.eclipse.ui.handlers.HandlerUtil; >-import org.eclipse.ui.texteditor.ITextEditor; >-import org.eclipse.wst.sse.core.StructuredModelManager; >-import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >-import org.eclipse.wst.xml.ui.internal.Logger; >-import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >- >-public class ToggleCommentHandler extends CommentHandler implements IHandler { >- public ToggleCommentHandler() { >- super(); >- } >- >- public Object execute(ExecutionEvent event) throws ExecutionException { >- IEditorPart editor = HandlerUtil.getActiveEditor(event); >- ITextEditor textEditor = null; >- if (editor instanceof ITextEditor) >- textEditor = (ITextEditor) editor; >- else { >- Object o = editor.getAdapter(ITextEditor.class); >- if (o != null) >- textEditor = (ITextEditor) o; >- } >- if (textEditor != null) { >- IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); >- if (document != null) { >- // get current text selection >- ITextSelection textSelection = getCurrentSelection(textEditor); >- if (textSelection.isEmpty()) { >- return null; >- } >- >- processAction(textEditor, document, textSelection); >- } >- } >- return null; >- } >- >- void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >- // get text selection lines info >- int selectionStartLine = textSelection.getStartLine(); >- int selectionEndLine = textSelection.getEndLine(); >- try { >- int selectionEndLineOffset = document.getLineOffset(selectionEndLine); >- int selectionEndOffset = textSelection.getOffset() + textSelection.getLength(); >- >- // adjust selection end line >- if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) { >- selectionEndLine--; >- } >- >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- >- // save the selection position since it will be changing >- Position selectionPosition = null; >- boolean updateStartOffset = false; >- try { >- selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength()); >- document.addPosition(selectionPosition); >- >- // extra check if commenting from beginning of line >- int selectionStartLineOffset = document.getLineOffset(selectionStartLine); >- if ((textSelection.getLength() > 0) && (selectionStartLineOffset == textSelection.getOffset()) && !isCommentLine(document, selectionStartLine)) { >- updateStartOffset = true; >- } >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- >- processAction(document, selectionStartLine, selectionEndLine); >- >- updateCurrentSelection(textEditor, selectionPosition, document, updateStartOffset); >- } >- >- private void processAction(IDocument document, int selectionStartLine, int selectionEndLine) { >- IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >- if (model != null) { >- try { >- model.beginRecording(this, XMLUIMessages.ToggleComment_tooltip); >- model.aboutToChangeModel(); >- >- for (int i = selectionStartLine; i <= selectionEndLine; i++) { >- try { >- if (document.getLineLength(i) > 0) { >- if (isCommentLine(document, i)) { >- int lineOffset = document.getLineOffset(i); >- IRegion region = document.getLineInformation(i); >- String string = document.get(region.getOffset(), region.getLength()); >- int openCommentOffset = lineOffset + string.indexOf(OPEN_COMMENT); >- int closeCommentOffset = lineOffset + string.indexOf(CLOSE_COMMENT) - OPEN_COMMENT.length(); >- uncomment(document, openCommentOffset, closeCommentOffset); >- } >- else { >- int openCommentOffset = document.getLineOffset(i); >- int lineDelimiterLength = document.getLineDelimiter(i) == null ? 0 : document.getLineDelimiter(i).length(); >- int closeCommentOffset = openCommentOffset + document.getLineLength(i) - lineDelimiterLength + OPEN_COMMENT.length(); >- comment(document, openCommentOffset, closeCommentOffset); >- } >- } >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- } >- } >- finally { >- model.changedModel(); >- model.endRecording(this); >- model.releaseFromEdit(); >- } >- } >- } >- >- private boolean isCommentLine(IDocument document, int line) { >- boolean isComment = false; >- >- try { >- IRegion region = document.getLineInformation(line); >- String string = document.get(region.getOffset(), region.getLength()).trim(); >- isComment = (string.length() >= OPEN_COMMENT.length() + CLOSE_COMMENT.length()) && string.startsWith(OPEN_COMMENT) && string.endsWith(CLOSE_COMMENT); >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- return isComment; >- } >- >- private void comment(IDocument document, int openCommentOffset, int closeCommentOffset) { >- try { >- document.replace(openCommentOffset, 0, OPEN_COMMENT); >- document.replace(closeCommentOffset, 0, CLOSE_COMMENT); >- removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length()); >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- } >- >- private void uncomment(IDocument document, int openCommentOffset, int closeCommentOffset) { >- try { >- document.replace(openCommentOffset, OPEN_COMMENT.length(), ""); //$NON-NLS-1$ >- document.replace(closeCommentOffset, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$ >- } >- catch (BadLocationException e) { >- Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >- } >- } >- >- private void updateCurrentSelection(ITextEditor textEditor, Position selectionPosition, IDocument document, boolean updateStartOffset) { >- // update the selection if text selection changed >- if (selectionPosition != null) { >- ITextSelection selection = null; >- if (updateStartOffset) { >- selection = new TextSelection(document, selectionPosition.getOffset() - OPEN_COMMENT.length(), selectionPosition.getLength() + OPEN_COMMENT.length()); >- } >- else { >- selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength()); >- } >- ISelectionProvider provider = textEditor.getSelectionProvider(); >- if (provider != null) { >- provider.setSelection(selection); >- } >- document.removePosition(selectionPosition); >- } >- } >-} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/RemoveXMLBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/RemoveXMLBlockCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/RemoveXMLBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/xml/ui/internal/handlers/RemoveXMLBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,105 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.xml.ui.internal.handlers; >+ >+import java.util.List; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; >+import org.eclipse.wst.xml.ui.internal.Logger; >+ >+/** >+ * The handler used to remove a block comment to a XML file >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.RemoveXMLBlockCommentHandler COPY: RemoveXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.RemoveHTMLBlockCommentHandler COPY: RemoveHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.RemoveJSPBlockCommentHandler COPY: RemoveJSPBlockCommentHandler >+ */ >+public class RemoveXMLBlockCommentHandler extends AbstractXMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public RemoveXMLBlockCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Removes all block comments from the given text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ List commentRegions = getCommentedRegions(typedRegions); >+ >+ //remove in reverse order as to not effect offset of other regions >+ model.aboutToChangeModel(); >+ for(int i = commentRegions.size()-1; i >= 0; --i) { >+ ITypedRegion typedRegion = (ITypedRegion)commentRegions.get(i); >+ IRegion commentRegion = new Region(typedRegion.getOffset(), typedRegion.getLength()); >+ >+ String regionContent = getRegionContent(document, commentRegion); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ String commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ >+ /*if the user did not highlight the entire region with the comment then we >+ *need to ask the model for the structured region that the user selected >+ *in to find the opening and closing comment blocks to remove >+ */ >+ if(commentPrefix == null || commentSuffix == null) { >+ IStructuredDocumentRegion structuredRegion = >+ model.getStructuredDocument().getRegionAtCharacterOffset( >+ typedRegion.getOffset()); >+ commentRegion = new Region(structuredRegion.getStartOffset(), structuredRegion.getLength()); >+ >+ regionContent = getRegionContent(document, commentRegion); >+ commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ } >+ >+ int commentPrefixOffset = commentRegion.getOffset() + regionContent.indexOf(commentPrefix); >+ int commentSuffixOffset = commentRegion.getOffset() - commentPrefix.length(); >+ commentSuffixOffset += regionContent.lastIndexOf(commentSuffix); >+ >+ //remove comment block >+ uncomment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/ToggleXMLCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/ToggleXMLCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/ToggleXMLCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/xml/ui/internal/handlers/ToggleXMLCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,124 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.xml.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.Position; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * The handler used to toggle per line comments >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.ToggleXMLCommentHandler COPY: ToggleXMLCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.ToggleHTMLCommentHandler COPY: ToggleHTMLCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.ToggleJSPCommentHandler COPY: ToggleJSPCommentHandler >+ */ >+public class ToggleXMLCommentHandler extends AbstractXMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public ToggleXMLCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Break the selection down into lines and toggle the commenting for each line >+ * >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AbstractJSPCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ // get text selection lines info >+ int selectionStartLine = textSelection.getStartLine(); >+ int selectionEndLine = textSelection.getEndLine(); >+ try { >+ int selectionEndLineOffset = document.getLineOffset(selectionEndLine); >+ int selectionEndOffset = textSelection.getOffset() + textSelection.getLength(); >+ >+ // adjust selection end line >+ if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) { >+ selectionEndLine--; >+ } >+ >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ // save the selection position since it will be changing >+ Position selectionPosition = null; >+ try { >+ selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength()); >+ document.addPosition(selectionPosition); >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ //toggle each line >+ processAction(document, selectionStartLine, selectionEndLine); >+ >+ updateCurrentSelection(textEditor, selectionPosition, document); >+ } >+ >+ /** >+ * For all of the lines between <code>selectionStartLine</code> and <code>selectionEndLine</code> >+ * inclusive, toggle the commenting on that line. >+ * >+ * @param document the document to toggle the commenting in >+ * @param selectionStartLine the first line to start comment toggling >+ * @param selectionEndLine the last line to comment toggle >+ */ >+ private void processAction(IDocument document, int selectionStartLine, int selectionEndLine) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ model.beginRecording(this, XMLUIMessages.ToggleComment_tooltip); >+ model.aboutToChangeModel(); >+ >+ for (int line = selectionStartLine; line <= selectionEndLine; ++line) { >+ try { >+ IRegion lineRegion = document.getLineInformation(line); >+ >+ //don't toggle empty lines >+ String content = getRegionContent(document, lineRegion); >+ if (content.trim().length() > 0) { >+ //toggle the commenting on the line >+ model.aboutToChangeModel(); >+ toggleLineComment(document, lineRegion); >+ } >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/AbstractXMLCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/AbstractXMLCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/AbstractXMLCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/xml/ui/internal/handlers/AbstractXMLCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,97 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.xml.ui.internal.handlers; >+ >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler; >+import org.eclipse.wst.xml.core.text.IXMLPartitions; >+ >+/** >+ * The abstract comment handler for XML files. >+ * >+ * @see AbstractStructuredCommentHandler >+ */ >+public abstract class AbstractXMLCommentHandler extends AbstractStructuredCommentHandler { >+ protected static final String XML_BLOCK_COMMENT_PREFIX = "<!--"; //$NON-NLS-1$ >+ protected static final String XML_BLOCK_COMMENT_SUFFIX = "-->"; //$NON-NLS-1$ >+ >+ private static final String[] POSSIBLE_COMMENT_PREFIXES = { >+ XML_BLOCK_COMMENT_PREFIX >+ }; >+ >+ private static final String[] POSSIBLE_COMMENT_SUFFIXES = { >+ XML_BLOCK_COMMENT_SUFFIX >+ }; >+ >+ private static final String[] XML_COMMENT_PARTITION_TYPES = { >+ IXMLPartitions.XML_COMMENT >+ }; >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AbstractXMLCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * There are no line comments in XML >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getLineCommentPrefix(java.util.List) >+ */ >+ protected String getLineCommentPrefix(ITypedRegion[] regions) { >+ return null; >+ } >+ >+ /** >+ * There is only one type of start comment in XML >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentPrefix(java.util.List) >+ */ >+ protected String getBlockCommentPrefix(ITypedRegion[] regions) { >+ return XML_BLOCK_COMMENT_PREFIX; >+ } >+ >+ /** >+ * There is only one type of comment suffix in XML >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentSuffix(java.util.List) >+ */ >+ protected String getBlockCommentSuffix(ITypedRegion[] regions) { >+ return XML_BLOCK_COMMENT_SUFFIX; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentOpenings() >+ */ >+ protected String[] getPossibleCommentPrefixes() { >+ return POSSIBLE_COMMENT_PREFIXES; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentClosings() >+ */ >+ protected String[] getPossibleCommentSuffixes() { >+ return POSSIBLE_COMMENT_SUFFIXES; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getCommentTypes() >+ */ >+ protected String[] getCommentPartitionTypes() { >+ return XML_COMMENT_PARTITION_TYPES; >+ } >+} >Index: src/org/eclipse/wst/xml/ui/internal/handlers/AddXMLBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/xml/ui/internal/handlers/AddXMLBlockCommentHandler.java >diff -N src/org/eclipse/wst/xml/ui/internal/handlers/AddXMLBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/xml/ui/internal/handlers/AddXMLBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,85 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.xml.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * The handler used to add a block comment to an XML file >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.AddXMLBlockCommentHandler COPY: AddXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.AddHTMLBlockCommentHandler COPY: AddHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AddJSPBlockCommentHandler COPY: AddJSPBlockCommentHandler >+ */ >+public class AddXMLBlockCommentHandler extends AbstractXMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AddXMLBlockCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Adds a block comment to the given text selection based on the types in the text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ String regionContent = getRegionContent(document, region); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ >+ boolean alreadyCommented = isEntireRegionCommented(typedRegions); >+ >+ //if selection not already commented >+ if(commentPrefix == null && !alreadyCommented) { >+ commentPrefix = getBlockCommentPrefix(typedRegions); >+ String commentSuffix = getBlockCommentSuffix(typedRegions); >+ >+ int commentPrefixOffset = region.getOffset(); >+ int commentSuffixOffset = commentPrefixOffset + region.getLength() + commentPrefix.length(); >+ >+ model.beginRecording(this, XMLUIMessages.AddBlockComment_tooltip); >+ model.aboutToChangeModel(); >+ comment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >#P org.eclipse.jst.jsp.ui >Index: plugin.xml >=================================================================== >RCS file: /cvsroot/webtools/sourceediting/plugins/org.eclipse.jst.jsp.ui/plugin.xml,v >retrieving revision 1.88 >diff -u -r1.88 plugin.xml >--- plugin.xml 29 Jun 2009 17:17:00 -0000 1.88 >+++ plugin.xml 1 Jul 2009 20:39:24 -0000 >@@ -72,7 +72,7 @@ > target="org.eclipse.jst.jsp.core.jspsource" /> > <provisionalDefinition > type="activecontexts" >- value="org.eclipse.jst.jsp.core.jspsource, org.eclipse.jst.jsp.ui.structured.text.editor.jsp.scope, org.eclipse.wst.html.core.htmlsource, org.eclipse.core.runtime.xml, org.eclipse.wst.xml.navigation, org.eclipse.wst.xml.comments, org.eclipse.wst.xml.selection" >+ value="org.eclipse.jst.jsp.core.jspsource, org.eclipse.jst.jsp.ui.structured.text.editor.jsp.scope, org.eclipse.wst.html.core.htmlsource, org.eclipse.core.runtime.xml, org.eclipse.wst.xml.navigation, org.eclipse.jst.jsp.comments, org.eclipse.wst.xml.selection" > target="org.eclipse.jst.jsp.core.jspsource" /> > > <contentOutlineConfiguration >@@ -482,6 +482,12 @@ > description="%scope.jsp.core.jspsource.description" > id="org.eclipse.jst.jsp.core.jspsource"> > </context> >+ <context >+ name="%scope.structured.text.editor.jsp.comments.name" >+ parentId="org.eclipse.wst.sse.ui.structuredTextEditorScope" >+ description="%scope.structured.text.editor.jsp.comments.description" >+ id="org.eclipse.jst.jsp.comments"> >+ </context> > </extension> > > <!-- Keybindings for jsp source editor --> >@@ -1011,10 +1017,58 @@ > </command> > <separator name="jspRefactorBegin" visible="false"></separator> > </menuContribution> >+ >+ <menuContribution locationURI="menu:sourceMenuId?after=sourceBegin"> >+ <command commandId="org.eclipse.wst.sse.ui.toggle.comment" >+ id="ToggleComment" >+ mnemonic="%command.toggle.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.add.block.comment" >+ id="AddBlockComment" >+ mnemonic="%command.add.block.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.remove.block.comment" >+ id="RemoveBlockComment" >+ mnemonic="%command.remove.block.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </visibleWhen> >+ </command> >+ </menuContribution> >+ >+ <menuContribution >+ locationURI="popup:sourcePopupMenuId?after=sourceBegin"> >+ <command commandId="org.eclipse.wst.sse.ui.toggle.comment" id="ToggleComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.add.block.comment" id="AddBlockComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.remove.block.comment" id="RemoveBlockComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <separator name="format" visible="true"></separator> >+ </menuContribution> > </extension> > >- <!-- Enable the FormatHandler for JSP Content Type --> >+ > <extension point="org.eclipse.ui.handlers"> >+ <!-- Enable the FormatHandler for JSP Content Type --> > <handler > class="org.eclipse.wst.sse.ui.internal.handlers.FormatHandler" > commandId="org.eclipse.wst.sse.ui.format"> >@@ -1025,6 +1079,37 @@ > <reference definitionId="org.eclipse.wst.jsp.ui.jspContentType.definition"></reference> > </enabledWhen> > </handler> >+ >+ <handler >+ class="org.eclipse.jst.jsp.ui.internal.handlers.ToggleJSPCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.toggle.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </enabledWhen> >+ </handler> >+ <handler >+ class="org.eclipse.jst.jsp.ui.internal.handlers.AddJSPBlockCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.add.block.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </enabledWhen> >+ </handler> >+ <handler >+ class="org.eclipse.jst.jsp.ui.internal.handlers.RemoveJSPBlockCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.remove.block.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.jst.jsp.ui.comments"/> >+ </enabledWhen> >+ </handler> > </extension> > > <!-- Set up a definition for JSP Content Types --> >@@ -1040,4 +1125,15 @@ > </definition> > </extension> > >+ <!-- Set up a definition for JSP Comments handler --> >+ <extension point="org.eclipse.core.expressions.definitions"> >+ <definition id="org.eclipse.jst.jsp.ui.comments"> >+ <with variable="activeContexts"> >+ <iterate operator="or"> >+ <equals value="org.eclipse.jst.jsp.comments"/> >+ </iterate> >+ </with> >+ </definition> >+ </extension> >+ > </plugin> >Index: plugin.properties >=================================================================== >RCS file: /cvsroot/webtools/sourceediting/plugins/org.eclipse.jst.jsp.ui/plugin.properties,v >retrieving revision 1.25 >diff -u -r1.25 plugin.properties >--- plugin.properties 21 May 2009 22:02:24 -0000 1.25 >+++ plugin.properties 1 Jul 2009 20:39:24 -0000 >@@ -50,6 +50,8 @@ > scope.structured.text.editor.jsp.description=Editing JSP Source > scope.jsp.core.jspsource.name=JSP Source > scope.jsp.core.jspsource.description=JSP Source >+scope.structured.text.editor.jsp.comments.name=JSP Source Comments >+scope.structured.text.editor.jsp.comments.description=JSP Source Comments > JSP_Type_Rename_Participant_Extension_Element.name=JSP Type Rename Participant > JSP_Method_Rename_Participant_Extension_Element.name=JSP Method Rename Participant > JSP_Package_Rename_Participant_Extension_Element.name=JSP Package Rename Participant >@@ -62,6 +64,7 @@ > JSP_Attribute_value_context_type_Extension_Element.name=JSP Attribute value > JSP_Query_Participant_Extension_Element.name=JSP Query Participant > JSP_Extension_Element.label=JSP >+ > #org.eclipse.ui.newWizards extension point > _UI_WIZARD_NAME = JSP > _UI_WIZARD_CREATE_NEW_FILE = Create a new JavaServer Page >@@ -104,3 +107,8 @@ > ## > Java_Element_hyperlink=Java Element > Taglib_hyperlink=Tag Library Descriptor >+ >+# Menu contributions >+command.toggle.comment.mnemonic=T >+command.add.block.comment.mnemonic=A >+command.remove.block.comment.mnemonic=R >Index: src/org/eclipse/jst/jsp/ui/internal/handlers/AddJSPBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/jst/jsp/ui/internal/handlers/AddJSPBlockCommentHandler.java >diff -N src/org/eclipse/jst/jsp/ui/internal/handlers/AddJSPBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jst/jsp/ui/internal/handlers/AddJSPBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,85 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 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.jst.jsp.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * <p>The handler used to add a block comment to a JSP file</p> >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.AddXMLBlockCommentHandler COPY: AddXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.AddHTMLBlockCommentHandler COPY: AddHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AddJSPBlockCommentHandler COPY: AddJSPBlockCommentHandler >+ */ >+public class AddJSPBlockCommentHandler extends AbstractJSPCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AddJSPBlockCommentHandler() { >+ super(); >+ } >+ >+ >+ /** >+ * Adds a block comment to the given text selection based on the types in the text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ String regionContent = getRegionContent(document, region); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ >+ boolean alreadyCommented = isEntireRegionCommented(typedRegions); >+ >+ //if selection not already commented >+ if(commentPrefix == null && !alreadyCommented) { >+ commentPrefix = getBlockCommentPrefix(typedRegions); >+ String commentSuffix = getBlockCommentSuffix(typedRegions); >+ >+ int commentPrefixOffset = region.getOffset(); >+ int commentSuffixOffset = commentPrefixOffset + region.getLength() + commentPrefix.length(); >+ >+ model.beginRecording(this, XMLUIMessages.AddBlockComment_tooltip); >+ model.aboutToChangeModel(); >+ comment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/jst/jsp/ui/internal/handlers/ToggleJSPCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/jst/jsp/ui/internal/handlers/ToggleJSPCommentHandler.java >diff -N src/org/eclipse/jst/jsp/ui/internal/handlers/ToggleJSPCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jst/jsp/ui/internal/handlers/ToggleJSPCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,123 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 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.jst.jsp.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.Position; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * The handler used to toggle per line comments >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.ToggleXMLCommentHandler COPY: ToggleXMLCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.ToggleHTMLCommentHandler COPY: ToggleHTMLCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.ToggleJSPCommentHandler COPY: ToggleJSPCommentHandler >+ */ >+public class ToggleJSPCommentHandler extends AbstractJSPCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public ToggleJSPCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Break the selection down into lines and toggle the commenting for each line >+ * >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AbstractJSPCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ // get text selection lines info >+ int selectionStartLine = textSelection.getStartLine(); >+ int selectionEndLine = textSelection.getEndLine(); >+ try { >+ int selectionEndLineOffset = document.getLineOffset(selectionEndLine); >+ int selectionEndOffset = textSelection.getOffset() + textSelection.getLength(); >+ >+ // adjust selection end line >+ if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) { >+ selectionEndLine--; >+ } >+ >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ // save the selection position since it will be changing >+ Position selectionPosition = null; >+ try { >+ selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength()); >+ document.addPosition(selectionPosition); >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ //toggle each line >+ processAction(document, selectionStartLine, selectionEndLine); >+ >+ updateCurrentSelection(textEditor, selectionPosition, document); >+ } >+ >+ /** >+ * For all of the lines between <code>selectionStartLine</code> and <code>selectionEndLine</code> >+ * inclusive, toggle the commenting on that line. >+ * >+ * @param document the document to toggle the commenting in >+ * @param selectionStartLine the first line to start comment toggling >+ * @param selectionEndLine the last line to comment toggle >+ */ >+ private void processAction(IDocument document, int selectionStartLine, int selectionEndLine) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ model.beginRecording(this, XMLUIMessages.ToggleComment_tooltip); >+ model.aboutToChangeModel(); >+ >+ for (int line = selectionStartLine; line <= selectionEndLine; ++line) { >+ try { >+ IRegion lineRegion = document.getLineInformation(line); >+ >+ //don't toggle empty lines >+ String content = getRegionContent(document, lineRegion); >+ if (content.trim().length() > 0) { >+ //toggle the commenting on the line >+ model.aboutToChangeModel(); >+ toggleLineComment(document, lineRegion); >+ } >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/jst/jsp/ui/internal/handlers/RemoveJSPBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/jst/jsp/ui/internal/handlers/RemoveJSPBlockCommentHandler.java >diff -N src/org/eclipse/jst/jsp/ui/internal/handlers/RemoveJSPBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jst/jsp/ui/internal/handlers/RemoveJSPBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,104 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 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.jst.jsp.ui.internal.handlers; >+ >+import java.util.List; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; >+import org.eclipse.wst.xml.ui.internal.Logger; >+ >+/** >+ * The handler used to remove a block comment to a JSP file >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.RemoveXMLBlockCommentHandler COPY: RemoveXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.RemoveHTMLBlockCommentHandler COPY: RemoveHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.RemoveJSPBlockCommentHandler COPY: RemoveJSPBlockCommentHandler >+ */ >+public class RemoveJSPBlockCommentHandler extends AbstractJSPCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public RemoveJSPBlockCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Removes all block comments from the given text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ List commentRegions = getCommentedRegions(typedRegions); >+ >+ //remove in reverse order as to not effect offset of other regions >+ model.aboutToChangeModel(); >+ for(int i = commentRegions.size()-1; i >= 0; --i) { >+ ITypedRegion typedRegion = (ITypedRegion)commentRegions.get(i); >+ IRegion commentRegion = new Region(typedRegion.getOffset(), typedRegion.getLength()); >+ >+ String regionContent = getRegionContent(document, commentRegion); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ String commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ >+ /*if the user did not highlight the entire region with the comment then we >+ *need to ask the model for the structured region that the user selected >+ *in to find the opening and closing comment blocks to remove >+ */ >+ if(commentPrefix == null || commentSuffix == null) { >+ IStructuredDocumentRegion structuredRegion = >+ model.getStructuredDocument().getRegionAtCharacterOffset( >+ typedRegion.getOffset()); >+ commentRegion = new Region(structuredRegion.getStartOffset(), structuredRegion.getLength()); >+ >+ regionContent = getRegionContent(document, commentRegion); >+ commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ } >+ >+ int commentPrefixOffset = commentRegion.getOffset() + regionContent.indexOf(commentPrefix); >+ int commentSuffixOffset = commentRegion.getOffset() - commentPrefix.length(); >+ commentSuffixOffset += regionContent.lastIndexOf(commentSuffix); >+ >+ //remove comment block >+ uncomment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/jst/jsp/ui/internal/handlers/AbstractJSPCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/jst/jsp/ui/internal/handlers/AbstractJSPCommentHandler.java >diff -N src/org/eclipse/jst/jsp/ui/internal/handlers/AbstractJSPCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/jst/jsp/ui/internal/handlers/AbstractJSPCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,168 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 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.jst.jsp.ui.internal.handlers; >+ >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jst.jsp.core.text.IJSPPartitions; >+import org.eclipse.wst.html.core.text.IHTMLPartitions; >+import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler; >+ >+/** >+ * The abstract comment handler for JSP files. >+ * >+ * @see AbstractStructuredCommentHandler >+ */ >+public abstract class AbstractJSPCommentHandler extends AbstractStructuredCommentHandler { >+ protected static final String HTML_BLOCK_COMMENT_PREFIX = "<!--"; //$NON-NLS-1$ >+ protected static final String HTML_BLOCK_COMMENT_SUFFIX = "-->"; //$NON-NLS-1$ >+ >+ protected static final String JSP_BLOCK_COMMENT_PREFIX = "<%--"; //$NON-NLS-1$ >+ protected static final String JSP_BLOCK_COMMENT_SUFFIX = "--%>"; //$NON-NLS-1$ >+ >+ protected static final String JAVA_LINE_COMMENT_PREFIX = "//"; //$NON-NLS-1$ >+ protected static final String JAVA_BLOCK_COMMENT_PREFIX = "/*"; //$NON-NLS-1$ >+ protected static final String JAVA_BLOCK_COMMENT_SUFFIX = "*/"; //$NON-NLS-1$ >+ >+ private static final String[] POSSIBLE_COMMENT_PREFIXES = { >+ HTML_BLOCK_COMMENT_PREFIX, JSP_BLOCK_COMMENT_PREFIX, >+ JAVA_BLOCK_COMMENT_PREFIX, JAVA_LINE_COMMENT_PREFIX >+ }; >+ >+ private static final String[] POSSIBLE_COMMENT_SUFFIXES = { >+ HTML_BLOCK_COMMENT_SUFFIX, JSP_BLOCK_COMMENT_SUFFIX, JAVA_BLOCK_COMMENT_SUFFIX >+ }; >+ >+ private static final List JAVA_PARTITION_TYPES = new ArrayList(); >+ private static final List JSP_PARTITION_TYPES = new ArrayList(); >+ static { >+ JAVA_PARTITION_TYPES.add(IJSPPartitions.JSP_CONTENT_JAVA); >+ JAVA_PARTITION_TYPES.add(IHTMLPartitions.SCRIPT); >+ JSP_PARTITION_TYPES.add(IJSPPartitions.JSP_COMMENT); >+ JSP_PARTITION_TYPES.add(IJSPPartitions.JSP_DIRECTIVE); >+ JSP_PARTITION_TYPES.add(IJSPPartitions.JSP_CONTENT_DELIMITER); >+ JSP_PARTITION_TYPES.add(IJSPPartitions.JSP_DEFAULT); >+ } >+ >+ private static final String[] JSP_COMMENT_PARTITION_TYPES = { >+ IJSPPartitions.JSP_COMMENT, IHTMLPartitions.HTML_COMMENT >+ }; >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AbstractJSPCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getLineCommentPrefix(org.eclipse.jface.text.ITypedRegion[]) >+ */ >+ protected String getLineCommentPrefix(ITypedRegion[] regions) { >+ String openComment = null; >+ >+ if(regions.length == 1) { >+ String type = regions[0].getType(); >+ if(type == IJSPPartitions.JSP_CONTENT_JAVA || type == IHTMLPartitions.SCRIPT) { >+ openComment = JAVA_LINE_COMMENT_PREFIX; >+ } >+ } >+ >+ return openComment; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentPrefix(org.eclipse.jface.text.ITypedRegion[]) >+ */ >+ protected String getBlockCommentPrefix(ITypedRegion[] regions) { >+ String commentPrefix = null; >+ >+ List copy = new ArrayList(regions.length); >+ for(int i = 0; i < regions.length; ++i) { >+ copy.add(regions[i].getType()); >+ } >+ >+ boolean hadJavaTypes = copy.removeAll(JAVA_PARTITION_TYPES); >+ boolean hadJSPTypes = copy.removeAll(JSP_PARTITION_TYPES); >+ >+ /* if only java regions then java comment prefix >+ * else if had java regions or jsp regions then jsp comment prefix >+ * else html regions only so html comment prefix >+ */ >+ if(hadJavaTypes && copy.size() == 0) { >+ commentPrefix = JAVA_BLOCK_COMMENT_PREFIX; >+ } else if(hadJavaTypes || hadJSPTypes) { >+ commentPrefix = JSP_BLOCK_COMMENT_PREFIX; >+ } else { >+ commentPrefix = HTML_BLOCK_COMMENT_PREFIX; >+ } >+ >+ return commentPrefix; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentSuffix(org.eclipse.jface.text.ITypedRegion[]) >+ */ >+ protected String getBlockCommentSuffix(ITypedRegion[] regions) { >+ String commentSuffix = null; >+ >+ List copy = new ArrayList(regions.length); >+ for(int i = 0; i < regions.length; ++i) { >+ copy.add(regions[i].getType()); >+ } >+ >+ boolean hadJavaTypes = copy.removeAll(JAVA_PARTITION_TYPES); >+ boolean hadJSPTypes = copy.removeAll(JSP_PARTITION_TYPES); >+ >+ /* if only java regions then java comment suffix >+ * else if had java regions or jsp regions then jsp comment suffix >+ * else html regions only so html comment suffix >+ */ >+ if(hadJavaTypes && copy.size() == 0) { >+ commentSuffix = JAVA_BLOCK_COMMENT_SUFFIX; >+ } else if(hadJavaTypes || hadJSPTypes) { >+ commentSuffix = JSP_BLOCK_COMMENT_SUFFIX; >+ } else { >+ commentSuffix = HTML_BLOCK_COMMENT_SUFFIX; >+ } >+ >+ return commentSuffix; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentPrefixes() >+ */ >+ protected String[] getPossibleCommentPrefixes() { >+ return POSSIBLE_COMMENT_PREFIXES; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentSuffixes() >+ */ >+ protected String[] getPossibleCommentSuffixes() { >+ return POSSIBLE_COMMENT_SUFFIXES; >+ } >+ >+ /** >+ * <p><b>LIMITATION:</b> there needs to be a partition type for commented JAVA and SCRIPT regions (BUG ######) >+ * this effects being able to remove comment blocks in JAVA and SCRIPT regions as well as undesired behavior >+ * when adding a comment block to already commented JAVA and SCRIPT regions. Once new partition types >+ * are created for commented JAVA and SCRIPT regions they should be returned here as well.</p> >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getCommentPartitionTypes() >+ */ >+ protected String[] getCommentPartitionTypes() { >+ return JSP_COMMENT_PARTITION_TYPES; >+ } >+} >#P org.eclipse.wst.html.ui >Index: plugin.xml >=================================================================== >RCS file: /cvsroot/webtools/sourceediting/plugins/org.eclipse.wst.html.ui/plugin.xml,v >retrieving revision 1.86 >diff -u -r1.86 plugin.xml >--- plugin.xml 29 Jun 2009 17:17:17 -0000 1.86 >+++ plugin.xml 1 Jul 2009 20:39:24 -0000 >@@ -60,7 +60,7 @@ > target="org.eclipse.wst.html.core.htmlsource" /> > <provisionalDefinition > type="activecontexts" >- value="org.eclipse.wst.html.core.htmlsource, org.eclipse.wst.html.occurrences, org.eclipse.core.runtime.xml, org.eclipse.wst.xml.navigation, org.eclipse.wst.xml.comments, org.eclipse.wst.xml.selection" >+ value="org.eclipse.wst.html.core.htmlsource, org.eclipse.wst.html.occurrences, org.eclipse.core.runtime.xml, org.eclipse.wst.xml.navigation, org.eclipse.wst.html.comments, org.eclipse.wst.xml.selection" > target="org.eclipse.wst.html.core.htmlsource" /> > </extension> > <extension >@@ -275,6 +275,12 @@ > description="%scope.structured.text.editor.html.occurrences.description" > id="org.eclipse.wst.html.occurrences"> > </context> >+ <context >+ name="%scope.structured.text.editor.html.comments.name" >+ parentId="org.eclipse.wst.sse.ui.structuredTextEditorScope" >+ description="%scope.structured.text.editor.html.comments.description" >+ id="org.eclipse.wst.html.comments"> >+ </context> > </extension> > > <!-- initialize html ui preferences --> >@@ -535,6 +541,37 @@ > </enabledWhen> > </handler> > >+ <handler >+ class="org.eclipse.wst.html.ui.internal.handlers.ToggleHTMLCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.toggle.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </enabledWhen> >+ </handler> >+ <handler >+ class="org.eclipse.wst.html.ui.internal.handlers.AddHTMLBlockCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.add.block.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </enabledWhen> >+ </handler> >+ <handler >+ class="org.eclipse.wst.html.ui.internal.handlers.RemoveHTMLBlockCommentHandler" >+ commandId="org.eclipse.wst.sse.ui.remove.block.comment"> >+ <activeWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </activeWhen> >+ <enabledWhen> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </enabledWhen> >+ </handler> >+ > </extension> > > <extension >@@ -581,6 +618,17 @@ > </definition> > </extension> > >+ <!-- Set up a definition for HTML Comments handler --> >+ <extension point="org.eclipse.core.expressions.definitions"> >+ <definition id="org.eclipse.wst.html.ui.comments"> >+ <with variable="activeContexts"> >+ <iterate operator="or"> >+ <equals value="org.eclipse.wst.html.comments"/> >+ </iterate> >+ </with> >+ </definition> >+ </extension> >+ > <!-- Define theme - Many of the color overrides and definitions come from > the XML plugin --> > <extension >@@ -627,4 +675,52 @@ > </definition> > </extension> > >+ <extension point="org.eclipse.ui.menus"> >+ <menuContribution locationURI="menu:sourceMenuId?after=sourceBegin"> >+ <command commandId="org.eclipse.wst.sse.ui.toggle.comment" >+ id="ToggleComment" >+ mnemonic="%command.toggle.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.add.block.comment" >+ id="AddBlockComment" >+ mnemonic="%command.add.block.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.remove.block.comment" >+ id="RemoveBlockComment" >+ mnemonic="%command.remove.block.comment.mnemonic" >+ style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"/> >+ </visibleWhen> >+ </command> >+ </menuContribution> >+ >+ <menuContribution >+ locationURI="popup:sourcePopupMenuId?after=sourceBegin"> >+ <command commandId="org.eclipse.wst.sse.ui.toggle.comment" id="ToggleComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.add.block.comment" id="AddBlockComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <command commandId="org.eclipse.wst.sse.ui.remove.block.comment" id="RemoveBlockComment" style="push"> >+ <visibleWhen checkEnabled="false"> >+ <reference definitionId="org.eclipse.wst.html.ui.comments"></reference> >+ </visibleWhen> >+ </command> >+ <separator name="format" visible="true"></separator> >+ </menuContribution> >+ </extension> > </plugin> >Index: plugin.properties >=================================================================== >RCS file: /cvsroot/webtools/sourceediting/plugins/org.eclipse.wst.html.ui/plugin.properties,v >retrieving revision 1.29 >diff -u -r1.29 plugin.properties >--- plugin.properties 12 Mar 2009 00:43:43 -0000 1.29 >+++ plugin.properties 1 Jul 2009 20:39:24 -0000 >@@ -70,9 +70,15 @@ > scope.structured.text.editor.html.description=Editing HTML Source > scope.structured.text.editor.html.occurrences.name=HTML Source Occurrences > scope.structured.text.editor.html.occurrences.description=HTML Source Occurrences >+scope.structured.text.editor.html.comments.name=HTML Source Comments >+scope.structured.text.editor.html.comments.description=HTML Source Comments > > Colors.scriptAreaBorder=Script Area Border > hyperlinkDetector.anchor.name=Anchors > hyperlink.target.script.name=HTML Client Script > hyperlink.target.eventhandler.name=HTML Event Handlers > >+# Menu contributions >+command.toggle.comment.mnemonic=T >+command.add.block.comment.mnemonic=A >+command.remove.block.comment.mnemonic=R >Index: src/org/eclipse/wst/html/ui/internal/handlers/AddHTMLBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/html/ui/internal/handlers/AddHTMLBlockCommentHandler.java >diff -N src/org/eclipse/wst/html/ui/internal/handlers/AddHTMLBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/html/ui/internal/handlers/AddHTMLBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,85 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.html.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * The handler used to add a block comment to an XML file >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.AddXMLBlockCommentHandler COPY: AddXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.AddHTMLBlockCommentHandler COPY: AddHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AddJSPBlockCommentHandler COPY: AddJSPBlockCommentHandler >+ */ >+public class AddHTMLBlockCommentHandler extends AbstractHTMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AddHTMLBlockCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Adds a block comment to the given text selection based on the types in the text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ String regionContent = getRegionContent(document, region); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ >+ boolean alreadyCommented = isEntireRegionCommented(typedRegions); >+ >+ //if selection not already commented >+ if(commentPrefix == null && !alreadyCommented) { >+ commentPrefix = getBlockCommentPrefix(typedRegions); >+ String commentSuffix = getBlockCommentSuffix(typedRegions); >+ >+ int commentPrefixOffset = region.getOffset(); >+ int commentSuffixOffset = commentPrefixOffset + region.getLength() + commentPrefix.length(); >+ >+ model.beginRecording(this, XMLUIMessages.AddBlockComment_tooltip); >+ model.aboutToChangeModel(); >+ comment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/wst/html/ui/internal/handlers/AbstractHTMLCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/html/ui/internal/handlers/AbstractHTMLCommentHandler.java >diff -N src/org/eclipse/wst/html/ui/internal/handlers/AbstractHTMLCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/html/ui/internal/handlers/AbstractHTMLCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,156 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.html.ui.internal.handlers; >+ >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.wst.html.core.text.IHTMLPartitions; >+import org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler; >+import org.eclipse.wst.xml.core.text.IXMLPartitions; >+import org.eclipse.wst.xml.ui.internal.handlers.AbstractXMLCommentHandler; >+ >+/** >+ * The abstract comment handler for HTML files. >+ * >+ * @see AbstractStructuredCommentHandler >+ */ >+public abstract class AbstractHTMLCommentHandler extends AbstractXMLCommentHandler { >+ protected static final String SCRIPT_LINE_COMMENT_PREFIX = "//"; //$NON-NLS-1$ >+ protected static final String SCRIPT_BLOCK_COMMENT_PREFIX = "/*"; //$NON-NLS-1$ >+ protected static final String SCRIPT_BLOCK_COMMENT_SUFFIX = "*/"; //$NON-NLS-1$ >+ >+ private static final String[] POSSIBLE_COMMENT_PREFIXES = { >+ XML_BLOCK_COMMENT_PREFIX, SCRIPT_LINE_COMMENT_PREFIX, SCRIPT_BLOCK_COMMENT_PREFIX >+ }; >+ >+ private static final String[] POSSIBLE_COMMENT_SUFFIXES = { >+ XML_BLOCK_COMMENT_SUFFIX, SCRIPT_BLOCK_COMMENT_SUFFIX >+ }; >+ >+ private static final String[] HTML_COMMENT_PARTITION_TYPES = { >+ IXMLPartitions.XML_COMMENT, IHTMLPartitions.HTML_COMMENT >+ }; >+ >+ private static final List SCRIPT_PARTITION_TYPES = new ArrayList(); >+ static { >+ SCRIPT_PARTITION_TYPES.add(IHTMLPartitions.SCRIPT); >+ } >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AbstractHTMLCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * script regions have line comments >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getLineCommentPrefix(java.util.List) >+ */ >+ protected String getLineCommentPrefix(ITypedRegion[] regions) { >+ String openComment = null; >+ >+ if(regions.length == 1) { >+ String type = regions[0].getType(); >+ if(type == IHTMLPartitions.SCRIPT) { >+ openComment = SCRIPT_LINE_COMMENT_PREFIX; >+ } >+ } >+ >+ return openComment; >+ } >+ >+ /** >+ * There is only one type of start comment in XML >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentPrefix(java.util.List) >+ */ >+ protected String getBlockCommentPrefix(ITypedRegion[] regions) { >+ String commentPrefix = null; >+ >+ List copy = new ArrayList(regions.length); >+ for(int i = 0; i < regions.length; ++i) { >+ copy.add(regions[i].getType()); >+ } >+ >+ boolean hadScriptTypes = copy.removeAll(SCRIPT_PARTITION_TYPES); >+ >+ //if only script regions then script comment prefix >+ //else ask parent comment handler >+ if(hadScriptTypes && copy.size() == 0) { >+ commentPrefix = SCRIPT_BLOCK_COMMENT_PREFIX; >+ } else { >+ commentPrefix = super.getBlockCommentPrefix(regions); >+ } >+ >+ return commentPrefix; >+ } >+ >+ /** >+ * There is only one type of comment suffix in XML >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getBlockCommentSuffix(java.util.List) >+ */ >+ protected String getBlockCommentSuffix(ITypedRegion[] regions) { >+ String commentSuffix = null; >+ >+ List copy = new ArrayList(regions.length); >+ for(int i = 0; i < regions.length; ++i) { >+ copy.add(regions[i].getType()); >+ } >+ >+ boolean hadScriptTypes = copy.removeAll(SCRIPT_PARTITION_TYPES); >+ >+ //if only script regions then script comment prefix >+ //else ask parent comment handler >+ if(hadScriptTypes && copy.size() == 0) { >+ commentSuffix = SCRIPT_BLOCK_COMMENT_SUFFIX; >+ } else { >+ commentSuffix = super.getBlockCommentSuffix(regions); >+ } >+ >+ return commentSuffix; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentOpenings() >+ */ >+ protected String[] getPossibleCommentPrefixes() { >+ return POSSIBLE_COMMENT_PREFIXES; >+ } >+ >+ /** >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getPossibleCommentClosings() >+ */ >+ protected String[] getPossibleCommentSuffixes() { >+ return POSSIBLE_COMMENT_SUFFIXES; >+ } >+ >+ /** >+ * <p><b>LIMITATION:</b> there needs to be a partition type for commented JAVA and SCRIPT regions (BUG ######) >+ * this effects being able to remove comment blocks in JAVA and SCRIPT regions as well as undesired behavior >+ * when adding a comment block to already commented JAVA and SCRIPT regions. Once new partition types >+ * are created for commented JAVA and SCRIPT regions they should be returned here as well.</p> >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#getCommentTypes() >+ */ >+ protected String[] getCommentPartitionTypes() { >+ return HTML_COMMENT_PARTITION_TYPES; >+ } >+} >Index: src/org/eclipse/wst/html/ui/internal/handlers/RemoveHTMLBlockCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/html/ui/internal/handlers/RemoveHTMLBlockCommentHandler.java >diff -N src/org/eclipse/wst/html/ui/internal/handlers/RemoveHTMLBlockCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/html/ui/internal/handlers/RemoveHTMLBlockCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,105 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.html.ui.internal.handlers; >+ >+import java.util.List; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Region; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; >+import org.eclipse.wst.xml.ui.internal.Logger; >+ >+/** >+ * The handler used to remove a block comment to a XML file >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.RemoveXMLBlockCommentHandler COPY: RemoveXMLBlockCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.RemoveHTMLBlockCommentHandler COPY: RemoveHTMLBlockCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.RemoveJSPBlockCommentHandler COPY: RemoveJSPBlockCommentHandler >+ */ >+public class RemoveHTMLBlockCommentHandler extends AbstractHTMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public RemoveHTMLBlockCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Removes all block comments from the given text selection >+ * >+ * @see org.eclipse.wst.sse.ui.internal.handlers.AbstractStructuredCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ IRegion region = new Region(textSelection.getOffset(), textSelection.getLength()); >+ >+ ITypedRegion[] typedRegions = document.computePartitioning(region.getOffset(), region.getLength()); >+ List commentRegions = getCommentedRegions(typedRegions); >+ >+ //remove in reverse order as to not effect offset of other regions >+ model.aboutToChangeModel(); >+ for(int i = commentRegions.size()-1; i >= 0; --i) { >+ ITypedRegion typedRegion = (ITypedRegion)commentRegions.get(i); >+ IRegion commentRegion = new Region(typedRegion.getOffset(), typedRegion.getLength()); >+ >+ String regionContent = getRegionContent(document, commentRegion); >+ String commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ String commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ >+ /*if the user did not highlight the entire region with the comment then we >+ *need to ask the model for the structured region that the user selected >+ *in to find the opening and closing comment blocks to remove >+ */ >+ if(commentPrefix == null || commentSuffix == null) { >+ IStructuredDocumentRegion structuredRegion = >+ model.getStructuredDocument().getRegionAtCharacterOffset( >+ typedRegion.getOffset()); >+ commentRegion = new Region(structuredRegion.getStartOffset(), structuredRegion.getLength()); >+ >+ regionContent = getRegionContent(document, commentRegion); >+ commentPrefix = startsWith(regionContent, getPossibleCommentPrefixes()); >+ commentSuffix = endsWith(regionContent, getPossibleCommentSuffixes()); >+ } >+ >+ int commentPrefixOffset = commentRegion.getOffset() + regionContent.indexOf(commentPrefix); >+ int commentSuffixOffset = commentRegion.getOffset() - commentPrefix.length(); >+ commentSuffixOffset += regionContent.lastIndexOf(commentSuffix); >+ >+ //remove comment block >+ uncomment(document, commentPrefixOffset, commentSuffixOffset, commentPrefix, commentSuffix); >+ } >+ } catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >Index: src/org/eclipse/wst/html/ui/internal/handlers/ToggleHTMLCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/html/ui/internal/handlers/ToggleHTMLCommentHandler.java >diff -N src/org/eclipse/wst/html/ui/internal/handlers/ToggleHTMLCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/html/ui/internal/handlers/ToggleHTMLCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,124 @@ >+/******************************************************************************* >+ * Copyright (c) 2008, 2009 Standards for Technology in Automotive Retail 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: >+ * David Carver - initial API and implementation, bug 212330 >+ * IBM Corporation - additional contributions >+ * >+ *******************************************************************************/ >+package org.eclipse.wst.html.ui.internal.handlers; >+ >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.Position; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.core.StructuredModelManager; >+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; >+import org.eclipse.wst.xml.ui.internal.Logger; >+import org.eclipse.wst.xml.ui.internal.XMLUIMessages; >+ >+/** >+ * The handler used to toggle per line comments >+ * >+ * <p><b>NOTE:</b> there are currently many copies of this class except for >+ * who they are inheriting from, this is because there is no multiple >+ * inheritance in Java.</p> >+ * >+ * @see org.eclipse.wst.xml.ui.internal.handlers.ToggleXMLCommentHandler COPY: ToggleXMLCommentHandler >+ * @see org.eclipse.wst.html.ui.internal.handlers.ToggleHTMLCommentHandler COPY: ToggleHTMLCommentHandler >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.ToggleJSPCommentHandler COPY: ToggleJSPCommentHandler >+ */ >+public class ToggleHTMLCommentHandler extends AbstractHTMLCommentHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public ToggleHTMLCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * Break the selection down into lines and toggle the commenting for each line >+ * >+ * @see org.eclipse.jst.jsp.ui.internal.handlers.AbstractJSPCommentHandler#processAction(org.eclipse.ui.texteditor.ITextEditor, org.eclipse.jface.text.IDocument, org.eclipse.jface.text.ITextSelection) >+ */ >+ protected void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection) { >+ // get text selection lines info >+ int selectionStartLine = textSelection.getStartLine(); >+ int selectionEndLine = textSelection.getEndLine(); >+ try { >+ int selectionEndLineOffset = document.getLineOffset(selectionEndLine); >+ int selectionEndOffset = textSelection.getOffset() + textSelection.getLength(); >+ >+ // adjust selection end line >+ if ((selectionEndLine > selectionStartLine) && (selectionEndLineOffset == selectionEndOffset)) { >+ selectionEndLine--; >+ } >+ >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ // save the selection position since it will be changing >+ Position selectionPosition = null; >+ try { >+ selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength()); >+ document.addPosition(selectionPosition); >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ >+ //toggle each line >+ processAction(document, selectionStartLine, selectionEndLine); >+ >+ updateCurrentSelection(textEditor, selectionPosition, document); >+ } >+ >+ /** >+ * For all of the lines between <code>selectionStartLine</code> and <code>selectionEndLine</code> >+ * inclusive, toggle the commenting on that line. >+ * >+ * @param document the document to toggle the commenting in >+ * @param selectionStartLine the first line to start comment toggling >+ * @param selectionEndLine the last line to comment toggle >+ */ >+ private void processAction(IDocument document, int selectionStartLine, int selectionEndLine) { >+ IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); >+ if (model != null) { >+ try { >+ model.beginRecording(this, XMLUIMessages.ToggleComment_tooltip); >+ model.aboutToChangeModel(); >+ >+ for (int line = selectionStartLine; line <= selectionEndLine; ++line) { >+ try { >+ IRegion lineRegion = document.getLineInformation(line); >+ >+ //don't toggle empty lines >+ String content = getRegionContent(document, lineRegion); >+ if (content.trim().length() > 0) { >+ //toggle the commenting on the line >+ model.aboutToChangeModel(); >+ toggleLineComment(document, lineRegion); >+ } >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ } >+ finally { >+ model.changedModel(); >+ model.endRecording(this); >+ model.releaseFromEdit(); >+ } >+ } >+ } >+} >#P org.eclipse.wst.sse.ui >Index: src/org/eclipse/wst/sse/ui/internal/handlers/AbstractStructuredCommentHandler.java >=================================================================== >RCS file: src/org/eclipse/wst/sse/ui/internal/handlers/AbstractStructuredCommentHandler.java >diff -N src/org/eclipse/wst/sse/ui/internal/handlers/AbstractStructuredCommentHandler.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ src/org/eclipse/wst/sse/ui/internal/handlers/AbstractStructuredCommentHandler.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,487 @@ >+/******************************************************************************* >+ * Copyright (c) 2009 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.wst.sse.ui.internal.handlers; >+ >+import java.util.ArrayList; >+import java.util.List; >+ >+import org.eclipse.core.commands.AbstractHandler; >+import org.eclipse.core.commands.ExecutionEvent; >+import org.eclipse.core.commands.ExecutionException; >+import org.eclipse.jface.text.BadLocationException; >+import org.eclipse.jface.text.IDocument; >+import org.eclipse.jface.text.IRegion; >+import org.eclipse.jface.text.ITextSelection; >+import org.eclipse.jface.text.ITypedRegion; >+import org.eclipse.jface.text.Position; >+import org.eclipse.jface.text.TextSelection; >+import org.eclipse.jface.viewers.ISelection; >+import org.eclipse.jface.viewers.ISelectionProvider; >+import org.eclipse.ui.IEditorPart; >+import org.eclipse.ui.handlers.HandlerUtil; >+import org.eclipse.ui.texteditor.ITextEditor; >+import org.eclipse.wst.sse.ui.internal.Logger; >+ >+/** >+ * This class contains all of the shared functionality for structured comment handlers >+ */ >+public abstract class AbstractStructuredCommentHandler extends AbstractHandler { >+ >+ /** >+ * Default constructor must exist because sub classes are created by java reflection >+ */ >+ public AbstractStructuredCommentHandler() { >+ super(); >+ } >+ >+ /** >+ * <p>Gets the important information out of the event and passes it onto >+ * the internal method processAction.</p> >+ * >+ * <p>It is suggested that this function not be overridden and instead >+ * implementers should put all functionality in their implementation >+ * of <code>processAction</code></p> >+ * >+ * (non-Javadoc) >+ * @see org.eclipse.wst.xml.ui.internal.handlers.CommentHandler#execute(org.eclipse.core.commands.ExecutionEvent) >+ */ >+ public Object execute(ExecutionEvent event) throws ExecutionException { >+ IEditorPart editor = HandlerUtil.getActiveEditor(event); >+ ITextEditor textEditor = null; >+ if (editor instanceof ITextEditor) >+ textEditor = (ITextEditor) editor; >+ else { >+ Object o = editor.getAdapter(ITextEditor.class); >+ if (o != null) >+ textEditor = (ITextEditor) o; >+ } >+ if (textEditor != null) { >+ IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); >+ if (document != null) { >+ // get current text selection >+ ITextSelection textSelection = getCurrentSelection(textEditor); >+ if (textSelection.isEmpty()) { >+ return null; >+ } >+ >+ //call the implimenters code to deal with the event >+ processAction(textEditor, document, textSelection); >+ } >+ } >+ return null; >+ } >+ >+ /** >+ * This method is called by the public execute method whenever the comment handler >+ * is invoked. This method should be used for the logic of handling the >+ * structured comment event >+ * >+ * @param textEditor the text editor the initiating event was caused in >+ * @param document the document the text editor is editing >+ * @param textSelection the user selection when the event was caused >+ */ >+ protected abstract void processAction(ITextEditor textEditor, IDocument document, ITextSelection textSelection); >+ >+ /** >+ * Given a list of partition types in a user selection should return >+ * the appropriate line comment prefix, or null if there is not one >+ * >+ * @param types the partition types in the user selection >+ * @return the appropriate line comment prefix, or <code>null</code> if there is not one >+ */ >+ protected abstract String getLineCommentPrefix(ITypedRegion[] regions); >+ >+ /** >+ * <p>Given a list of typed regions in a user selection should return >+ * the appropriate block comment prefix, or null if there is not one.</p> >+ * >+ * <p><b>IMPORTANT:</b> If there is a block comment prefix for a list of types there >+ * should also be a block comment suffix for that same list of types</p> >+ * >+ * @param regions the typed regions in the user selection >+ * @return the appropriate block comment prefix, or <code>null</code> if there is not one >+ */ >+ protected abstract String getBlockCommentPrefix(ITypedRegion[] regions); >+ >+ /** >+ * <p>Given a list of typed regions in a user selection should return >+ * the appropriate block comment suffix, or null if there is not one.</p> >+ * >+ * <p><b>IMPORTANT:</b> If there is a block comment prefix returned by >+ * <code>getBlockCommentPrefix</code> then this method needs to return >+ * the appropriate block comment suffix for the prefix that >+ * <code>getBlockCommentPrefix</code> would return if invoked.</p> >+ * >+ * @param types the typed regions in the user selection >+ * @return the appropriate block comment suffix, or <code>null</code> if there is not one >+ */ >+ protected abstract String getBlockCommentSuffix(ITypedRegion[] regions); >+ >+ /** >+ * Should return all possible comment suffixes that could >+ * possible appear in the document type that this handler >+ * is being implemented for. This includes both >+ * line and block comment prefixes. >+ * >+ * @return all possible comment prefixes >+ */ >+ protected abstract String[] getPossibleCommentPrefixes(); >+ >+ /** >+ * Should return all possible comment suffixes that could >+ * possible appear in the document type that this handler >+ * is being implemented for. >+ * >+ * @return all possible comment suffixes >+ */ >+ protected abstract String[] getPossibleCommentSuffixes(); >+ >+ /** >+ * Should return all partition types that represent commented >+ * partitions for the document type that this handler is being >+ * implemented for >+ * >+ * @return all partition types that represent commented partitions >+ */ >+ protected abstract String[] getCommentPartitionTypes(); >+ >+ /** >+ * <p>Given a document and a region representing one line in that document >+ * comments that line if it is not commented or un-comments it if it is >+ * already commented. When commenting an uncommented line it will first >+ * try to use a line only comment, if there is not a line only comment >+ * prefix appropriate for the given region then it will surround the line >+ * with a block comment.</p> >+ * >+ * <p><b>NOTE:</b> it is a good idea if a model is at hand when calling this to >+ * warn the model of an impending update</p> >+ * >+ * @param document the document with the line to be commented or uncommented >+ * @param lineRegion the region representing the line to be commented or uncommented >+ * @throws BadLocationException thrown if the given region is a bad location in the given document >+ */ >+ protected void toggleLineComment(IDocument document, IRegion lineRegion) throws BadLocationException { >+ >+ String regionContent = getRegionContent(document, lineRegion); >+ String openComment = startsWith(regionContent, getPossibleCommentPrefixes()); >+ String closeComment = endsWith(regionContent, getPossibleCommentSuffixes()); >+ >+ //if there is an opening comment then remove comment >+ //else add comment >+ if (openComment != null) { >+ String lineContent = document.get(lineRegion.getOffset(), lineRegion.getLength()); >+ int openCommentOffset = lineRegion.getOffset() + lineContent.indexOf(openComment); >+ int closeCommentOffset = lineRegion.getOffset() - openComment.length(); >+ if(closeComment != null) { >+ closeCommentOffset += lineContent.lastIndexOf(closeComment); >+ } >+ uncomment(document, openCommentOffset, closeCommentOffset, openComment, closeComment); >+ } >+ else { >+ ITypedRegion[] typedRegions = document.computePartitioning(lineRegion.getOffset(), lineRegion.getLength()); >+ >+ openComment = getLineCommentPrefix(typedRegions); >+ closeComment = null; >+ >+ //if there is no line comment prefix then use a block comment >+ if(openComment == null) { >+ openComment = getBlockCommentPrefix(typedRegions); >+ closeComment = getBlockCommentSuffix(typedRegions); >+ } >+ >+ //add the comment prefix & suffix >+ int commentPrefixOffset = lineRegion.getOffset(); >+ int commentSuffixOffset = commentPrefixOffset + lineRegion.getLength() + openComment.length(); >+ comment(document, commentPrefixOffset, commentSuffixOffset, openComment, closeComment); >+ } >+ } >+ >+ /** >+ * <p>This method modifies the given document to place the given comment >+ * prefix at the given comment prefix offset and the given comment >+ * suffix at the given comment suffix offset. In the case of adding >+ * a line comment that does not have a suffix, pass <code>null</code> >+ * for the comment suffix and it and its associated offset will >+ * be ignored.</p> >+ * >+ * <p><b>NOTE:</b> it is a good idea if a model is at hand when calling this to >+ * warn the model of an impending update</p> >+ * >+ * @param document the document to add the comment to >+ * @param commentPrefixOffset the offset of the comment prefix >+ * @param commentSuffixOffset the offset of the comment suffix >+ * (ignored if <code>commentSuffix</code> is <code>null</code>) >+ * @param commentPrefix the prefix of the comment to add at its associated given offset >+ * @param commentSuffix the suffix of the comment to add at its associated given offset, >+ * or null if there is not suffix to add for this comment >+ */ >+ protected void comment(IDocument document, int commentPrefixOffset, int commentSuffixOffset, String commentPrefix, String commentSuffix) { >+ try { >+ document.replace(commentPrefixOffset, 0, commentPrefix); >+ if(commentSuffix != null) { >+ document.replace(commentSuffixOffset, 0, commentSuffix); >+ } >+ removeEnclosedCommentPrefixesAndSuffixes(document, commentPrefixOffset, commentSuffixOffset - commentPrefixOffset, commentPrefix, commentSuffix); >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ >+ /** >+ * <p>This method modifies the given document to remove the given comment >+ * prefix at the given comment prefix offset and the given comment >+ * suffix at the given comment suffix offset. In the case of removing >+ * a line comment that does not have a suffix, pass <code>null</code> >+ * for the comment suffix and it and its associated offset will >+ * be ignored.</p> >+ * >+ * <p><b>NOTE:</b> it is a good idea if a model is at hand when calling this to >+ * warn the model of an impending update</p> >+ * >+ * @param document the document to remove the comment from >+ * @param commentPrefixOffset the offset of the comment prefix >+ * @param commentSuffixOffset the offset of the comment suffix >+ * (ignored if <code>commentSuffix</code> is <code>null</code>) >+ * @param commentPrefix the prefix of the comment to remove from its associated given offset >+ * @param commentSuffix the suffix of the comment to remove from its associated given offset, >+ * or null if there is not suffix to remove for this comment >+ */ >+ protected void uncomment(IDocument document, int commentPrefixOffset, int commentSuffixOffset, String commentPrefix, String commentSuffix) { >+ try { >+ >+ document.replace(commentPrefixOffset, commentPrefix.length(), ""); //$NON-NLS-1$ >+ >+ if(commentSuffix != null) { >+ document.replace(commentSuffixOffset, commentSuffix.length(), ""); //$NON-NLS-1$ >+ } >+ >+ removeEnclosedCommentPrefixesAndSuffixes(document, commentPrefixOffset, commentSuffixOffset - commentPrefixOffset, commentPrefix, commentSuffix); >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ >+ /** >+ * Determines if all of the given typed regions are comment regions. >+ * This is useful for deciding if an entire user highlighted region >+ * is already commented or not. >+ * >+ * @param regions the typed regions to check to see if they are all comment regions >+ * @return <code>true</code> if all of the given <code>regions</code> are comment regions >+ * <code>false</code> otherwise. >+ */ >+ protected boolean isEntireRegionCommented(ITypedRegion[] regions) { >+ String[] commentTypes = getCommentPartitionTypes(); >+ boolean alreadyCommented = regions.length > 0 && commentTypes.length > 0; >+ >+ for(int type = 0; type < regions.length && alreadyCommented; ++type) { >+ alreadyCommented &= isTypeCommentType(regions[type].getType()); >+ } >+ >+ return alreadyCommented; >+ } >+ >+ /** >+ * Given typed regions returns a list of only those who's partition type >+ * is a comment partition type. This is useful for finding already commented >+ * regions in a user selection. >+ * >+ * @param typedRegions the regions to find those regions that who's >+ * partition type is a comment partition type >+ * @return a <code>List</code> of the <code>ITypedRegion</code>s who's type >+ * is a comment partition type >+ */ >+ protected List getCommentedRegions(ITypedRegion[] typedRegions) { >+ List commentedRegions = new ArrayList(); >+ >+ for(int i = 0; i < typedRegions.length; ++i) { >+ if(isTypeCommentType(typedRegions[i].getType())) { >+ commentedRegions.add(typedRegions[i]); >+ } >+ } >+ >+ return commentedRegions; >+ } >+ >+ /** >+ * Convince method to determine if the given <code>line</code> >+ * starts with any of the given <code>options</code>. >+ * >+ * @param line the determine if this starts with any of the given <code>options</code> >+ * @param options check to see if the given <code>line</code> starts with any of these >+ * @return <code>true</code> if the given <code>line</code> >+ * starts with any of the given <code>options</code>, <code>false</code> otherwise. >+ */ >+ protected static String startsWith(String line, String[] options) { >+ String startsWith = null; >+ >+ for(int i = 0; i < options.length && startsWith == null; ++i) { >+ if( line.startsWith(options[i])) { >+ startsWith = options[i]; >+ } >+ } >+ >+ return startsWith; >+ } >+ >+ /** >+ * Convince method to determine if the given <code>line</code> >+ * ends with any of the given <code>options</code>. >+ * >+ * @param line the determine if this ends with any of the given <code>options</code> >+ * @param options check to see if the given <code>line</code> ends with any of these >+ * @return <code>true</code> if the given <code>line</code> >+ * ends with any of the given <code>options</code>, <code>false</code> otherwise. >+ */ >+ protected static String endsWith(String line, String[] options) { >+ String endsWith = null; >+ >+ for(int i = 0; i < options.length && endsWith == null; ++i) { >+ if(line.endsWith(options[i])) { >+ endsWith = options[i]; >+ } >+ } >+ >+ return endsWith; >+ } >+ >+ /** >+ * Get the content of the given <code>region</code> in the given <code>document</code> >+ * >+ * @param document get the content of the given <code>region</code> from this >+ * @param region get the content of this from the given <code>document</code> >+ * @return the content of the given <code>region</code> from the given <code>document</code> >+ * @throws BadLocationException if the given <code>region</code> is not in the given <code>document</code> >+ */ >+ static protected String getRegionContent(IDocument document, IRegion region) throws BadLocationException { >+ return document.get(region.getOffset(), region.getLength()).trim(); >+ } >+ >+ >+ /** >+ * Updates the current user selection in the given <code>textEditor</code> >+ * >+ * @param textEditor the text editor to set the user selection in >+ * @param selectionPosition the new user selection >+ * @param document the document the new user selection is in >+ */ >+ protected static void updateCurrentSelection(ITextEditor textEditor, Position selectionPosition, IDocument document) { >+ // update the selection if text selection changed >+ if (selectionPosition != null) { >+ ITextSelection selection = null; >+ >+ selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength()); >+ >+ ISelectionProvider provider = textEditor.getSelectionProvider(); >+ if (provider != null) { >+ provider.setSelection(selection); >+ } >+ document.removePosition(selectionPosition); >+ } >+ } >+ >+ /** >+ * When commenting a region it is best to remove any already existing comments >+ * of the same comment format that will be inclosed by the region to be commented >+ * to avoid any overlap that may cause a comment to start or more likely end early. >+ * Thus the purpose of this method given an offset and a length will remove all >+ * occurrences of the given comment prefix and suffix from the range. The >+ * given offset should be the offset of where the new comment starts, the method >+ * will automatically leave the surrounding comment prefix and suffix. >+ * >+ * @param document the document to remove any extra comment prefixes or suffixes from >+ * @param commentPrefixOffset the offset of the newly added comment prefix >+ * @param commentLength the length of the newly commented region(including the newly added comment suffix) >+ * @param commentPrefix the comment prefix that should be removed if found in the newly commented range >+ * @param commentSuffix the comment suffix that should be removed if found in the newly commented range. >+ * This can be null if there is no comment suffix for this type of comment. >+ */ >+ private void removeEnclosedCommentPrefixesAndSuffixes(IDocument document, int commentPrefixOffset, int commentLength, String commentPrefix, String commentSuffix) { >+ try { >+ >+ int innerOffset = commentPrefixOffset; >+ int innerLength = commentLength; >+ >+ //adjust the offset for the surrounding comment prefix >+ innerOffset += commentPrefix.length(); >+ >+ >+ //adjust the length for the surrounding comment suffix if there is one >+ if(commentSuffix != null) { >+ innerLength -= commentSuffix.length(); >+ } >+ >+ int adjustedLength = innerLength; >+ String string; >+ int index; >+ >+ // remove enclosed comment prefixes >+ string = document.get(innerOffset, innerLength); >+ index = string.lastIndexOf(commentPrefix); >+ while (index != -1) { >+ document.replace(innerOffset + index, commentPrefix.length(), ""); //$NON-NLS-1$ >+ index = string.lastIndexOf(commentPrefix, index - 1); >+ adjustedLength -= commentPrefix.length(); >+ } >+ >+ // remove enclosed comment suffixes >+ if(commentSuffix != null) { >+ string = document.get(innerOffset, adjustedLength); >+ index = string.lastIndexOf(commentSuffix); >+ while (index != -1) { >+ document.replace(innerOffset + index, commentSuffix.length(), ""); //$NON-NLS-1$ >+ index = string.lastIndexOf(commentSuffix, index - 1); >+ } >+ } >+ } >+ catch (BadLocationException e) { >+ Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); >+ } >+ } >+ >+ /** >+ * Gets the current user selection in the given <code>textEditor</code> >+ * >+ * @param textEditor get the user selection from here >+ * @return the current user selection in <code>textEdtior</code> >+ */ >+ private ITextSelection getCurrentSelection(ITextEditor textEditor) { >+ ISelectionProvider provider = textEditor.getSelectionProvider(); >+ if (provider != null) { >+ ISelection selection = provider.getSelection(); >+ if (selection instanceof ITextSelection) { >+ return (ITextSelection) selection; >+ } >+ } >+ return TextSelection.emptySelection(); >+ } >+ >+ /** >+ * Determine if the given partition type is a comment partition type >+ * >+ * @param type check to see if this partition type is a comment partition type >+ * @return <code>true</code> if the given <code>type</code> is a comment partition type, >+ * <code>false</code> otherwise. >+ */ >+ private boolean isTypeCommentType(String type) { >+ String[] commentTypes = getCommentPartitionTypes(); >+ boolean isCommentType = false; >+ >+ for(int commentType = 0; commentType < commentTypes.length && !isCommentType; ++commentType) { >+ isCommentType |= (type == commentTypes[commentType]); >+ } >+ >+ return isCommentType; >+ } >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 86520
:
140629
|
140906
|
146755
|
147138
|
159026
|
159376
|
160677
|
160843
|
160936
|
160938
|
166769
|
178931