Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1021ceae7c669f5e35a25fa3fbe2a7f1a46c7d8 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*******************************************************************************
 * Copyright (c) 2013 Red Hat Inc.
 * 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:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.systemtap.ui.ide.actions;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextOperationTarget;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.Localization;
import org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPPartitionScanner;
import org.eclipse.linuxtools.systemtap.graphing.ui.widgets.ExceptionErrorDialog;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * Handler for command in charge of toggling comment prefixes. Based on
 * org.eclipse.cdt.internal.ui.actions.ToggleCommentAction.
 */
public class ToggleCommentHandler extends AbstractHandler {

    /** The text operation target */
    private ITextOperationTarget operationTarget;

    /**
     * Checks if the selected lines are all commented or not and
     * uncomments/comments them respectively.
     */
    @Override
    public Object execute(ExecutionEvent event) {
        ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
        if (editor == null || !editor.isEditable()) {
            return null;
        }

        updateOpTarget(editor);
        if (operationTarget == null) {
            return null;
        }

        ISelection selection = editor.getSelectionProvider().getSelection();
        IDocument document = editor.getDocumentProvider().getDocument(
                editor.getEditorInput());

        final int operationCode;
        if (isSelectionCommented(selection, document)) {
            operationCode = ITextOperationTarget.STRIP_PREFIX;
        } else {
            operationCode = ITextOperationTarget.PREFIX;
        }

        Shell shell = editor.getSite().getShell();
        if (!operationTarget.canDoOperation(operationCode)) {
            if (shell != null) {
                MessageDialog.openError(shell,
                        Localization.getString("ToggleComment_error_title"), //$NON-NLS-1$
                        Localization.getString("ToggleComment_error_message")); //$NON-NLS-1$
            }
            return null;
        }

        Display display = null;
        if (shell != null && !shell.isDisposed()) {
            display = shell.getDisplay();
        }

        BusyIndicator.showWhile(display, new Runnable() {
            @Override
            public void run() {
                operationTarget.doOperation(operationCode);
            }
        });

        return null;
    }

    /**
     * Creates a region describing the text block (something that starts at the
     * beginning of a line) completely containing the current selection.
     *
     * Note, the implementation has to match org.eclipse.jface.text.TextViewer;
     * .getTextBlockFromSelection().
     *
     * @param selection The selection to use
     * @param document The document
     * @return the region describing the text block comprising the given
     *         selection
     * @throws BadLocationException
     */
    public IRegion getTextBlockFromSelection(ITextSelection selection,
            IDocument document) throws BadLocationException {
        int start = document.getLineOffset(selection.getStartLine());
        int end;
        int endLine = selection.getEndLine();
        if (document.getNumberOfLines() > endLine + 1) {
            end = document.getLineOffset(endLine + 1);
        } else {
            end = document.getLength();
        }
        return new Region(start, end - start);
    }

    /**
     * Is the given selection on the specified document single-line commented?
     *
     * @param selection Selection to check
     * @param document The document
     * @return <code>true</code> iff all selected lines are commented
     */
    public boolean isSelectionCommented(ISelection selection,
            IDocument document) {

        if (!(selection instanceof ITextSelection)) {
            return false;
        }

        ITextSelection textSelection = (ITextSelection) selection;
        if (textSelection.getStartLine() < 0 || textSelection.getEndLine() < 0) {
            return false;
        }

        try {
            IRegion block = getTextBlockFromSelection(textSelection, document);
            ITypedRegion[] regions = TextUtilities.computePartitioning(
                    document, STPPartitionScanner.STP_PARTITIONING,
                    block.getOffset(), block.getLength(), false);

            int[] lines = new int[regions.length * 2]; // [startline, endline,
                                                        // startline, endline,
                                                        // ...]

            // For each partition in the text selection, figure out the
            // startline and endline.
            // Count the number of lines that are selected.
            for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
                // Start line of region
                lines[j] = getFirstCompleteLineOfRegion(regions[i], document);
                // End line of region
                int length = regions[i].getLength();
                int offset = regions[i].getOffset() + length;
                if (length > 0) {
                    offset--;
                }

                // If there is no startline for this region (startline = -1),
                // then there is no endline,
                // otherwise, get the line number of the endline and store it in
                // the array.
                lines[j + 1] = (lines[j] == -1 ? -1 : document
                        .getLineOfOffset(offset));

                assert i < regions.length;
                assert j < regions.length * 2;
            }

            // Perform the check
            boolean hasComment = false;
            for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
                String prefix = "//"; //$NON-NLS-1$
                if (lines[j] >= 0 && lines[j + 1] >= 0) {
                    if (isBlockCommented(lines[j], lines[j + 1], prefix,
                            document)) {
                        hasComment = true;
                    } else if (!isBlockEmpty(lines[j], lines[j + 1], document)) {
                        return false;
                    }
                }
            }
            return hasComment;
        } catch (BadLocationException e) {
            ExceptionErrorDialog.openError(e.getLocalizedMessage(), e);
        }

        return false;
    }

    /**
     * Returns the index of the first line whose start offset is in the given
     * text range.
     *
     * @param region the text range in characters where to find the line
     * @param document The document
     * @return the first line whose start index is in the given range, -1 if
     *         there is no such line
     */
    public int getFirstCompleteLineOfRegion(IRegion region, IDocument document) {
        try {
            int startLine = document.getLineOfOffset(region.getOffset());

            int offset = document.getLineOffset(startLine);
            if (offset >= region.getOffset()) {
                return startLine;
            }

            offset = document.getLineOffset(startLine + 1);
            return (offset > region.getOffset() + region.getLength() ? -1
                : startLine + 1);
        } catch (BadLocationException e) {
            ExceptionErrorDialog.openError(e.getLocalizedMessage(), e);
        }

        return -1;
    }

    /**
     * Determines whether each line is empty
     *
     * @param startLine Start line in document
     * @param endLine End line in document
     * @param document The document
     * @return <code>true</code> if each line from <code>startLine</code> to and
     *         including <code>endLine</code> is empty
     */
    public boolean isBlockEmpty(int startLine, int endLine, IDocument document) {
        try {
            for (int i = startLine; i <= endLine; i++) {
                IRegion line = document.getLineInformation(i);
                String text = document.get(line.getOffset(), line.getLength());

                boolean isEmptyLine = text.trim().length() == 0;
                if (!isEmptyLine) {
                    return false;
                }
            }
            return true;
        } catch (BadLocationException e) {
            ExceptionErrorDialog.openError(e.getLocalizedMessage(), e);
        }

        return false;
    }

    /**
     * Determines whether each line is prefixed by one of the prefixes.
     *
     * @param startLine Start line in document
     * @param endLine End line in document
     * @param prefix Comment prefix
     * @param document The document
     * @return <code>true</code> iff each line from <code>startLine</code> to
     *         and including <code>endLine</code> is prepended by the
     *         <code>prefix</code>, ignoring whitespace at the begin of line
     */
    public boolean isBlockCommented(int startLine, int endLine, String prefix,
            IDocument document) {
        try {
            // Check for occurrences of prefixes in the given lines
            boolean hasComment = false;
            for (int i = startLine; i <= endLine; i++) {
                IRegion line = document.getLineInformation(i);
                String text = document.get(line.getOffset(), line.getLength());

                boolean isEmptyLine = text.trim().length() == 0;
                if (isEmptyLine) {
                    continue;
                }

                int prefixIndex = text.indexOf(prefix, 0);

                if (prefixIndex == -1) {
                    // Found a line which is not commented
                    return false;
                }
                String s = document.get(line.getOffset(), prefixIndex);
                s = s.trim();
                if (s.length() != 0) {
                    // Found a line which is not commented
                    return false;
                }
                hasComment = true;
            }
            return hasComment;
        } catch (BadLocationException e) {
            ExceptionErrorDialog.openError(e.getLocalizedMessage(), e);
        }

        return false;
    }

    /**
     * Update text operation target based on the specified text editor.
     *
     * @param editor ITextEditor editor to associate operation target to.
     */
    private void updateOpTarget(ITextEditor editor) {
        if (editor != null) {
            operationTarget = (ITextOperationTarget) editor
                    .getAdapter(ITextOperationTarget.class);
        }
    }
}

Back to the top