Skip to main content
summaryrefslogtreecommitdiffstats
blob: 09d81d18403c3dcf07457ea17d63d94e1e457616 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2005, 2006 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.xml.ui.internal.actions;

import org.eclipse.jface.action.IAction;
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.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;

/**
 * Toggle comment action delegate for XML editor
 */
public class ToggleCommentActionXMLDelegate extends AbstractCommentActionXMLDelegate {
	public void init(IAction action) {
		if (action != null) {
			action.setText(XMLUIMessages.ToggleComment_label);
			action.setToolTipText(XMLUIMessages.ToggleComment_tooltip);
			action.setDescription(XMLUIMessages.ToggleComment_description);
		}
	}

	void processAction(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(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(Position selectionPosition, IDocument document, boolean updateStartOffset) {
		if (fEditor instanceof ITextEditor) {
			// 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 = ((ITextEditor) fEditor).getSelectionProvider();
				if (provider != null) {
					provider.setSelection(selection);
				}
				document.removePosition(selectionPosition);
			}
		}
	}
}

Back to the top