|
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* |
| 11 |
*******************************************************************************/ |
| 12 |
|
| 13 |
package org.eclipse.wst.xml.ui.internal.actions; |
| 14 |
|
| 15 |
import org.eclipse.jface.action.IAction; |
| 16 |
import org.eclipse.jface.text.BadLocationException; |
| 17 |
import org.eclipse.jface.text.IDocument; |
| 18 |
import org.eclipse.jface.text.IRegion; |
| 19 |
import org.eclipse.jface.text.ITextSelection; |
| 20 |
import org.eclipse.jface.text.Position; |
| 21 |
import org.eclipse.jface.text.TextSelection; |
| 22 |
import org.eclipse.jface.viewers.ISelectionProvider; |
| 23 |
import org.eclipse.ui.texteditor.ITextEditor; |
| 24 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 25 |
import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager; |
| 26 |
import org.eclipse.wst.xml.ui.internal.Logger; |
| 27 |
import org.eclipse.wst.xml.ui.internal.XMLUIMessages; |
| 28 |
|
| 29 |
/** |
| 30 |
* Toggle comment action delegate for XML editor |
| 31 |
*/ |
| 32 |
public class ToggleCommentActionXMLDelegate extends AbstractCommentActionXMLDelegate { |
| 33 |
public void init(IAction action) { |
| 34 |
if (action != null) { |
| 35 |
action.setText(XMLUIMessages.ToggleComment_label); |
| 36 |
action.setToolTipText(XMLUIMessages.ToggleComment_tooltip); |
| 37 |
action.setDescription(XMLUIMessages.ToggleComment_description); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void processAction(IDocument document, ITextSelection textSelection) { |
| 42 |
// get text selection lines info |
| 43 |
int selectionStartLine = textSelection.getStartLine(); |
| 44 |
int selectionEndLine = textSelection.getEndLine(); |
| 45 |
try { |
| 46 |
int selectionEndLineOffset = document.getLineOffset(selectionEndLine); |
| 47 |
int selectionEndOffset = textSelection.getOffset() + textSelection.getLength(); |
| 48 |
|
| 49 |
// adjust selection end line |
| 50 |
if (selectionEndLine > selectionStartLine && selectionEndLineOffset == selectionEndOffset) |
| 51 |
selectionEndLine--; |
| 52 |
|
| 53 |
} |
| 54 |
catch (BadLocationException e) { |
| 55 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 56 |
} |
| 57 |
|
| 58 |
// save the selection position since it will be changing |
| 59 |
Position selectionPosition = null; |
| 60 |
boolean updateStartOffset = false; |
| 61 |
try { |
| 62 |
selectionPosition = new Position(textSelection.getOffset(), textSelection.getLength()); |
| 63 |
document.addPosition(selectionPosition); |
| 64 |
|
| 65 |
// extra check if commenting from beginning of line |
| 66 |
int selectionStartLineOffset = document.getLineOffset(selectionStartLine); |
| 67 |
if (textSelection.getLength() > 0 && selectionStartLineOffset == textSelection.getOffset() && !isCommentLine(document, selectionStartLine)) |
| 68 |
updateStartOffset = true; |
| 69 |
} |
| 70 |
catch (BadLocationException e) { |
| 71 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 72 |
} |
| 73 |
|
| 74 |
processAction(document, selectionStartLine, selectionEndLine); |
| 75 |
|
| 76 |
updateCurrentSelection(selectionPosition, document, updateStartOffset); |
| 77 |
} |
| 78 |
|
| 79 |
private void processAction(IDocument document, int selectionStartLine, int selectionEndLine) { |
| 80 |
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForEdit(document); |
| 81 |
if (model != null) { |
| 82 |
try { |
| 83 |
model.beginRecording(this, XMLUIMessages.ToggleComment_tooltip); |
| 84 |
model.aboutToChangeModel(); |
| 85 |
|
| 86 |
for (int i = selectionStartLine; i <= selectionEndLine; i++) { |
| 87 |
try { |
| 88 |
if (document.getLineLength(i) > 0) { |
| 89 |
if (isCommentLine(document, i)) { |
| 90 |
int lineOffset = document.getLineOffset(i); |
| 91 |
IRegion region = document.getLineInformation(i); |
| 92 |
String string = document.get(region.getOffset(), region.getLength()); |
| 93 |
int openCommentOffset = lineOffset + string.indexOf(OPEN_COMMENT); |
| 94 |
int closeCommentOffset = lineOffset + string.indexOf(CLOSE_COMMENT) - OPEN_COMMENT.length(); |
| 95 |
uncomment(document, openCommentOffset, closeCommentOffset); |
| 96 |
} |
| 97 |
else { |
| 98 |
int openCommentOffset = document.getLineOffset(i); |
| 99 |
int lineDelimiterLength = document.getLineDelimiter(i) == null ? 0 : document.getLineDelimiter(i).length(); |
| 100 |
int closeCommentOffset = openCommentOffset + document.getLineLength(i) - lineDelimiterLength + OPEN_COMMENT.length(); |
| 101 |
comment(document, openCommentOffset, closeCommentOffset); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
catch (BadLocationException e) { |
| 106 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
finally { |
| 111 |
model.changedModel(); |
| 112 |
model.endRecording(this); |
| 113 |
model.releaseFromEdit(); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private boolean isCommentLine(IDocument document, int line) { |
| 119 |
boolean isComment = false; |
| 120 |
|
| 121 |
try { |
| 122 |
IRegion region = document.getLineInformation(line); |
| 123 |
String string = document.get(region.getOffset(), region.getLength()).trim(); |
| 124 |
isComment = string.length() >= OPEN_COMMENT.length() + CLOSE_COMMENT.length() && string.startsWith(OPEN_COMMENT) && string.endsWith(CLOSE_COMMENT); |
| 125 |
} |
| 126 |
catch (BadLocationException e) { |
| 127 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 128 |
} |
| 129 |
return isComment; |
| 130 |
} |
| 131 |
|
| 132 |
private void comment(IDocument document, int openCommentOffset, int closeCommentOffset) { |
| 133 |
try { |
| 134 |
document.replace(openCommentOffset, 0, OPEN_COMMENT); |
| 135 |
document.replace(closeCommentOffset, 0, CLOSE_COMMENT); |
| 136 |
removeOpenCloseComments(document, openCommentOffset + OPEN_COMMENT.length(), closeCommentOffset - openCommentOffset - CLOSE_COMMENT.length()); |
| 137 |
} |
| 138 |
catch (BadLocationException e) { |
| 139 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private void uncomment(IDocument document, int openCommentOffset, int closeCommentOffset) { |
| 144 |
try { |
| 145 |
document.replace(openCommentOffset, OPEN_COMMENT.length(), ""); //$NON-NLS-1$ |
| 146 |
document.replace(closeCommentOffset, CLOSE_COMMENT.length(), ""); //$NON-NLS-1$ |
| 147 |
} |
| 148 |
catch (BadLocationException e) { |
| 149 |
Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
private void updateCurrentSelection(Position selectionPosition, IDocument document, boolean updateStartOffset) { |
| 154 |
if (fEditor instanceof ITextEditor) { |
| 155 |
// update the selection if text selection changed |
| 156 |
if (selectionPosition != null) { |
| 157 |
ITextSelection selection = null; |
| 158 |
if (updateStartOffset) |
| 159 |
selection = new TextSelection(document, selectionPosition.getOffset() - OPEN_COMMENT.length(), selectionPosition.getLength() + OPEN_COMMENT.length()); |
| 160 |
else |
| 161 |
selection = new TextSelection(document, selectionPosition.getOffset(), selectionPosition.getLength()); |
| 162 |
ISelectionProvider provider = ((ITextEditor) fEditor).getSelectionProvider(); |
| 163 |
if (provider != null) { |
| 164 |
provider.setSelection(selection); |
| 165 |
} |
| 166 |
document.removePosition(selectionPosition); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |