mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
2004-12-03 Alain Magloire
Implement comment blocks(Code take from JDT Editor) * src/org/eclipse/cdt/internal/ui/action/AddBlockCommentAction.java * src/org/eclipse/cdt/internal/ui/action/BlockCommentAction.java * src/org/eclipse/cdt/internal/ui/action/RemoveBlockCommentAction.java * src/org/eclipse/cdt/internal/ui/editor/CEditor.java * src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties * src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java * src/org/eclipse/cdt/internal/ui/text/ CPartitionScanner.java * src/org/eclipse/cdt/internal/ui/text/CTextTools.java * src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java * src/org/eclipse/cdt/internal/ui/text/ICPartitions.java * plugin.xml
This commit is contained in:
parent
7426918742
commit
a71c47c4e8
12 changed files with 723 additions and 22 deletions
|
@ -1,3 +1,17 @@
|
|||
2004-12-03 Alain Magloire
|
||||
Implement comment blocks(Code take from JDT Editor)
|
||||
* src/org/eclipse/cdt/internal/ui/action/AddBlockCommentAction.java
|
||||
* src/org/eclipse/cdt/internal/ui/action/BlockCommentAction.java
|
||||
* src/org/eclipse/cdt/internal/ui/action/RemoveBlockCommentAction.java
|
||||
* src/org/eclipse/cdt/internal/ui/editor/CEditor.java
|
||||
* src/org/eclipse/cdt/internal/ui/editor/CEditorMessages.properties
|
||||
* src/org/eclipse/cdt/internal/ui/editor/ICEditorActionDefinitionIds.java
|
||||
* src/org/eclipse/cdt/internal/ui/text/ CPartitionScanner.java
|
||||
* src/org/eclipse/cdt/internal/ui/text/CTextTools.java
|
||||
* src/org/eclipse/cdt/internal/ui/text/FastCPartitionScanner.java
|
||||
* src/org/eclipse/cdt/internal/ui/text/ICPartitions.java
|
||||
* plugin.xml
|
||||
|
||||
2004-12-02 Alain Magloire
|
||||
Fix for 40081
|
||||
Textfield to choose NM.
|
||||
|
|
|
@ -828,6 +828,30 @@
|
|||
command="org.eclipse.cdt.ui.edit.text.c.uncomment"
|
||||
configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
|
||||
</keyBinding>
|
||||
<command
|
||||
name="%ActionDefinition.addBlockComment.name"
|
||||
description="%ActionDefinition.addBlockComment.description"
|
||||
category="org.eclipse.cdt.ui.category.source"
|
||||
id="org.eclipse.cdt.ui.edit.text.c.add.block.comment">
|
||||
</command>
|
||||
<keyBinding
|
||||
string="Ctrl+Shift+/"
|
||||
scope="org.eclipse.cdt.ui.cEditorScope"
|
||||
command="org.eclipse.cdt.ui.edit.text.c.add.block.comment"
|
||||
configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
|
||||
</keyBinding>
|
||||
<command
|
||||
name="%ActionDefinition.removeBlockComment.name"
|
||||
description="%ActionDefinition.removeBlockComment.description"
|
||||
category="org.eclipse.cdt.ui.category.source"
|
||||
id="org.eclipse.cdt.ui.edit.text.c.remove.block.comment">
|
||||
</command>
|
||||
<keyBinding
|
||||
string="Ctrl+Shift+\"
|
||||
scope="org.eclipse.cdt.ui.cEditorScope"
|
||||
command="org.eclipse.cdt.ui.edit.text.c.remove.block.comment"
|
||||
configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
|
||||
</keyBinding>
|
||||
<command
|
||||
name="%ActionDefinition.opendecl.name"
|
||||
category="org.eclipse.cdt.ui.category.source"
|
||||
|
|
|
@ -0,0 +1,196 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2004 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.actions;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.text.ICPartitions;
|
||||
import org.eclipse.jface.text.Assert;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.BadPartitioningException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IDocumentExtension3;
|
||||
import org.eclipse.jface.text.ITextSelection;
|
||||
import org.eclipse.jface.text.ITypedRegion;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
/**
|
||||
* Action that encloses the editor's current selection with Java block comment terminators
|
||||
* (<code>/*</code> and <code>*/</code>).
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public class AddBlockCommentAction extends BlockCommentAction {
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param bundle the resource bundle
|
||||
* @param prefix a prefix to be prepended to the various resource keys
|
||||
* (described in <code>ResourceAction</code> constructor), or
|
||||
* <code>null</code> if none
|
||||
* @param editor the text editor
|
||||
*/
|
||||
public AddBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
|
||||
super(bundle, prefix, editor);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.BlockCommentAction.Edit.EditFactory)
|
||||
*/
|
||||
protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
|
||||
int selectionOffset= selection.getOffset();
|
||||
int selectionEndOffset= selectionOffset + selection.getLength();
|
||||
List edits= new LinkedList();
|
||||
ITypedRegion partition= docExtension.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, selectionOffset, false);
|
||||
|
||||
handleFirstPartition(partition, edits, factory, selectionOffset);
|
||||
|
||||
while (partition.getOffset() + partition.getLength() < selectionEndOffset) {
|
||||
partition= handleInteriorPartition(partition, edits, factory, docExtension);
|
||||
}
|
||||
|
||||
handleLastPartition(partition, edits, factory, selectionEndOffset);
|
||||
|
||||
executeEdits(edits);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the first partition of the selected text.
|
||||
*
|
||||
* @param partition
|
||||
* @param edits
|
||||
* @param factory
|
||||
* @param offset
|
||||
*/
|
||||
private void handleFirstPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int offset) throws BadLocationException {
|
||||
|
||||
int partOffset= partition.getOffset();
|
||||
String partType= partition.getType();
|
||||
|
||||
Assert.isTrue(partOffset <= offset, "illegal partition"); //$NON-NLS-1$
|
||||
|
||||
// first partition: mark start of comment
|
||||
if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
|
||||
// Java code: right where selection starts
|
||||
edits.add(factory.createEdit(offset, 0, getCommentStart()));
|
||||
} else if (isSpecialPartition(partType)) {
|
||||
// special types: include the entire partition
|
||||
edits.add(factory.createEdit(partOffset, 0, getCommentStart()));
|
||||
} // javadoc: no mark, will only start after comment
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the end of the given partition and the start of the next partition, which is returned.
|
||||
*
|
||||
* @param partition
|
||||
* @param edits
|
||||
* @param factory
|
||||
* @param docExtension
|
||||
* @throws BadLocationException
|
||||
* @throws BadPartitioningException
|
||||
* @return the region
|
||||
*/
|
||||
private ITypedRegion handleInteriorPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, IDocumentExtension3 docExtension) throws BadPartitioningException, BadLocationException {
|
||||
|
||||
// end of previous partition
|
||||
String partType= partition.getType();
|
||||
int partEndOffset= partition.getOffset() + partition.getLength();
|
||||
int tokenLength= getCommentStart().length();
|
||||
|
||||
/*boolean wasJavadoc= false; // true if the previous partition is javadoc
|
||||
|
||||
if (partType == IJavaPartitions.JAVA_DOC) {
|
||||
|
||||
wasJavadoc= true;
|
||||
|
||||
} else*/
|
||||
if (partType == ICPartitions.C_MULTILINE_COMMENT) {
|
||||
|
||||
// already in a comment - remove ending mark
|
||||
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
|
||||
|
||||
}
|
||||
|
||||
// advance to next partition
|
||||
partition= docExtension.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, partEndOffset, false);
|
||||
partType= partition.getType();
|
||||
|
||||
// start of next partition
|
||||
// if (wasJavadoc) {
|
||||
//
|
||||
// // if previous was javadoc, and the current one is not, then add block comment start
|
||||
// if (partType == IDocument.DEFAULT_CONTENT_TYPE
|
||||
// || isSpecialPartition(partType)) {
|
||||
// edits.add(factory.createEdit(partition.getOffset(), 0, getCommentStart()));
|
||||
// }
|
||||
//
|
||||
// } else { // !wasJavadoc
|
||||
|
||||
/*if (partType == IJavaPartitions.JAVA_DOC) {
|
||||
// if next is javadoc, end block comment before
|
||||
edits.add(factory.createEdit(partition.getOffset(), 0, getCommentEnd()));
|
||||
} else*/ if (partType == ICPartitions.C_MULTILINE_COMMENT) {
|
||||
// already in a comment - remove startToken
|
||||
edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
|
||||
}
|
||||
// }
|
||||
|
||||
return partition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the end of the last partition.
|
||||
*
|
||||
* @param partition
|
||||
* @param edits
|
||||
* @param factory
|
||||
* @param endOffset
|
||||
*/
|
||||
private void handleLastPartition(ITypedRegion partition, List edits, Edit.EditFactory factory, int endOffset) throws BadLocationException {
|
||||
|
||||
String partType= partition.getType();
|
||||
|
||||
if (partType == IDocument.DEFAULT_CONTENT_TYPE) {
|
||||
// normal java: end comment where selection ends
|
||||
edits.add(factory.createEdit(endOffset, 0, getCommentEnd()));
|
||||
} else if (isSpecialPartition(partType)) {
|
||||
// special types: consume entire partition
|
||||
edits.add(factory.createEdit(partition.getOffset() + partition.getLength(), 0, getCommentEnd()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether <code>partType</code> is special, i.e. a Java <code>String</code>,
|
||||
* <code>Character</code>, or <code>Line End Comment</code> partition.
|
||||
*
|
||||
* @param partType the partition type to check
|
||||
* @return <code>true</code> if <code>partType</code> is special, <code>false</code> otherwise
|
||||
*/
|
||||
private boolean isSpecialPartition(String partType) {
|
||||
return /*partType == IJavaPartitions.JAVA_CHARACTER
|
||||
|| */partType == ICPartitions.C_STRING
|
||||
|| partType == ICPartitions.C_SINGLE_LINE_COMMENT;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jdt.internal.ui.actions.BlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
|
||||
*/
|
||||
protected boolean isValidSelection(ITextSelection selection) {
|
||||
return selection != null && !selection.isEmpty() && selection.getLength() > 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,327 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2004 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.actions;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import org.eclipse.jface.text.Assert;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.BadPartitioningException;
|
||||
import org.eclipse.jface.text.BadPositionCategoryException;
|
||||
import org.eclipse.jface.text.DefaultPositionUpdater;
|
||||
import org.eclipse.jface.text.DocumentEvent;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IDocumentExtension3;
|
||||
import org.eclipse.jface.text.IPositionUpdater;
|
||||
import org.eclipse.jface.text.IRewriteTarget;
|
||||
import org.eclipse.jface.text.ITextSelection;
|
||||
import org.eclipse.jface.text.Position;
|
||||
import org.eclipse.jface.viewers.ISelection;
|
||||
import org.eclipse.jface.viewers.ISelectionProvider;
|
||||
import org.eclipse.ui.IEditorInput;
|
||||
import org.eclipse.ui.texteditor.IDocumentProvider;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
import org.eclipse.ui.texteditor.ITextEditorExtension2;
|
||||
import org.eclipse.ui.texteditor.TextEditorAction;
|
||||
|
||||
/**
|
||||
* Common block comment code.
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public abstract class BlockCommentAction extends TextEditorAction {
|
||||
/**
|
||||
* Creates a new instance.
|
||||
* @param bundle
|
||||
* @param prefix
|
||||
* @param editor
|
||||
*/
|
||||
public BlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
|
||||
super(bundle, prefix, editor);
|
||||
}
|
||||
|
||||
/**
|
||||
* An edit is a kind of <code>DocumentEvent</code>, in this case an edit instruction, that is
|
||||
* affiliated with a <code>Position</code> on a document. The offset of the document event is
|
||||
* not stored statically, but taken from the affiliated <code>Position</code>, which gets
|
||||
* updated when other edits occur.
|
||||
*/
|
||||
static class Edit extends DocumentEvent {
|
||||
|
||||
/**
|
||||
* Factory for edits which manages the creation, installation and destruction of
|
||||
* position categories, position updaters etc. on a certain document. Once a factory has
|
||||
* been obtained, <code>Edit</code> objects can be obtained from it which will be linked to
|
||||
* the document by positions of one position category.
|
||||
* <p>Clients are required to call <code>release</code> once the <code>Edit</code>s are not
|
||||
* used any more, so the positions can be discarded.</p>
|
||||
*/
|
||||
public static class EditFactory {
|
||||
|
||||
/** The position category basename for this edits. */
|
||||
private static final String CATEGORY= "__positionalEditPositionCategory"; //$NON-NLS-1$
|
||||
|
||||
/** The count of factories. */
|
||||
private static int fgCount= 0;
|
||||
|
||||
/** This factory's category. */
|
||||
private final String fCategory;
|
||||
private IDocument fDocument;
|
||||
private IPositionUpdater fUpdater;
|
||||
|
||||
/**
|
||||
* Creates a new <code>EditFactory</code> with an unambiguous position category name.
|
||||
* @param document the document that is being edited.
|
||||
*/
|
||||
public EditFactory(IDocument document) {
|
||||
fCategory= CATEGORY + fgCount++;
|
||||
fDocument= document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new edition on the document of this factory.
|
||||
*
|
||||
* @param offset the offset of the edition at the point when is created.
|
||||
* @param length the length of the edition (not updated via the position update mechanism)
|
||||
* @param text the text to be replaced on the document
|
||||
* @return an <code>Edit</code> reflecting the edition on the document
|
||||
*/
|
||||
public Edit createEdit(int offset, int length, String text) throws BadLocationException {
|
||||
|
||||
if (!fDocument.containsPositionCategory(fCategory)) {
|
||||
fDocument.addPositionCategory(fCategory);
|
||||
fUpdater= new DefaultPositionUpdater(fCategory);
|
||||
fDocument.addPositionUpdater(fUpdater);
|
||||
}
|
||||
|
||||
Position position= new Position(offset);
|
||||
try {
|
||||
fDocument.addPosition(fCategory, position);
|
||||
} catch (BadPositionCategoryException e) {
|
||||
Assert.isTrue(false);
|
||||
}
|
||||
return new Edit(fDocument, length, text, position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases the position category on the document and uninstalls the position updater.
|
||||
* <code>Edit</code>s managed by this factory are not updated after this call.
|
||||
*/
|
||||
public void release() {
|
||||
if (fDocument != null && fDocument.containsPositionCategory(fCategory)) {
|
||||
fDocument.removePositionUpdater(fUpdater);
|
||||
try {
|
||||
fDocument.removePositionCategory(fCategory);
|
||||
} catch (BadPositionCategoryException e) {
|
||||
Assert.isTrue(false);
|
||||
}
|
||||
fDocument= null;
|
||||
fUpdater= null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The position in the document where this edit be executed. */
|
||||
private Position fPosition;
|
||||
|
||||
/**
|
||||
* Creates a new edition on <code>document</code>, taking its offset from <code>position</code>.
|
||||
*
|
||||
* @param document the document being edited
|
||||
* @param length the length of the edition
|
||||
* @param text the replacement text of the edition
|
||||
* @param position the position keeping the edition's offset
|
||||
*/
|
||||
protected Edit(IDocument document, int length, String text, Position position) {
|
||||
super(document, 0, length, text);
|
||||
fPosition= position;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jface.text.DocumentEvent#getOffset()
|
||||
*/
|
||||
public int getOffset() {
|
||||
return fPosition.getOffset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the edition on document. The offset is taken from the position.
|
||||
*
|
||||
* @throws BadLocationException if the execution of the document fails.
|
||||
*/
|
||||
public void perform() throws BadLocationException {
|
||||
getDocument().replace(getOffset(), getLength(), getText());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
if (!isEnabled())
|
||||
return;
|
||||
|
||||
ITextEditor editor= getTextEditor();
|
||||
if (editor == null || !ensureEditable(editor))
|
||||
return;
|
||||
|
||||
ITextSelection selection= getCurrentSelection();
|
||||
if (!isValidSelection(selection))
|
||||
return;
|
||||
|
||||
if (!validateEditorInputState())
|
||||
return;
|
||||
|
||||
IDocumentProvider docProvider= editor.getDocumentProvider();
|
||||
IEditorInput input= editor.getEditorInput();
|
||||
if (docProvider == null || input == null)
|
||||
return;
|
||||
|
||||
IDocument document= docProvider.getDocument(input);
|
||||
if (document == null)
|
||||
return;
|
||||
|
||||
IDocumentExtension3 docExtension;
|
||||
if (document instanceof IDocumentExtension3)
|
||||
docExtension= (IDocumentExtension3) document;
|
||||
else
|
||||
return;
|
||||
|
||||
IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class);
|
||||
if (target != null) {
|
||||
target.beginCompoundChange();
|
||||
}
|
||||
|
||||
Edit.EditFactory factory= new Edit.EditFactory(document);
|
||||
|
||||
try {
|
||||
runInternal(selection, docExtension, factory);
|
||||
|
||||
} catch (BadLocationException e) {
|
||||
// can happen on concurrent modification, deletion etc. of the document
|
||||
// -> don't complain, just bail out
|
||||
} catch (BadPartitioningException e) {
|
||||
// should not happen
|
||||
Assert.isTrue(false, "bad partitioning"); //$NON-NLS-1$
|
||||
} finally {
|
||||
factory.release();
|
||||
|
||||
if (target != null) {
|
||||
target.endCompoundChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls <code>perform</code> on all <code>Edit</code>s in <code>edits</code>.
|
||||
*
|
||||
* @param edits a list of <code>Edit</code>s
|
||||
* @throws BadLocationException if an <code>Edit</code> threw such an exception.
|
||||
*/
|
||||
protected void executeEdits(List edits) throws BadLocationException {
|
||||
for (Iterator it= edits.iterator(); it.hasNext();) {
|
||||
Edit edit= (Edit) it.next();
|
||||
edit.perform();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the editor is modifyable. If the editor is an instance of
|
||||
* <code>ITextEditorExtension2</code>, its <code>validateEditorInputState</code> method
|
||||
* is called, otherwise, the result of <code>isEditable</code> is returned.
|
||||
*
|
||||
* @param editor the editor to be checked
|
||||
* @return <code>true</code> if the editor is editable, <code>false</code> otherwise
|
||||
*/
|
||||
protected boolean ensureEditable(ITextEditor editor) {
|
||||
Assert.isNotNull(editor);
|
||||
|
||||
if (editor instanceof ITextEditorExtension2) {
|
||||
ITextEditorExtension2 ext= (ITextEditorExtension2) editor;
|
||||
return ext.validateEditorInputState();
|
||||
}
|
||||
|
||||
return editor.isEditable();
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.ui.texteditor.IUpdate#update()
|
||||
*/
|
||||
public void update() {
|
||||
super.update();
|
||||
|
||||
if (isEnabled()) {
|
||||
if (!canModifyEditor() || !isValidSelection(getCurrentSelection()))
|
||||
setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the editor's selection, or <code>null</code> if no selection can be obtained or the
|
||||
* editor is <code>null</code>.
|
||||
*
|
||||
* @return the selection of the action's editor, or <code>null</code>
|
||||
*/
|
||||
protected ITextSelection getCurrentSelection() {
|
||||
ITextEditor editor= getTextEditor();
|
||||
if (editor != null) {
|
||||
ISelectionProvider provider= editor.getSelectionProvider();
|
||||
if (provider != null) {
|
||||
ISelection selection= provider.getSelection();
|
||||
if (selection instanceof ITextSelection)
|
||||
return (ITextSelection) selection;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the real command once all the editor, document, and selection checks have succeeded.
|
||||
*
|
||||
* @param selection the current selection we are being called for
|
||||
* @param docExtension the document extension where we get the partitioning from
|
||||
* @param factory the edit factory we can use to create <code>Edit</code>s
|
||||
* @throws BadLocationException if an edition fails
|
||||
* @throws BadPartitioningException if a partitioning call fails
|
||||
*/
|
||||
protected abstract void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException;
|
||||
|
||||
/**
|
||||
* Checks whether <code>selection</code> is valid.
|
||||
*
|
||||
* @param selection the selection to check
|
||||
* @return <code>true</code> if the selection is valid, <code>false</code> otherwise
|
||||
*/
|
||||
protected abstract boolean isValidSelection(ITextSelection selection);
|
||||
|
||||
/**
|
||||
* Returns the text to be inserted at the selection start.
|
||||
*
|
||||
* @return the text to be inserted at the selection start
|
||||
*/
|
||||
protected String getCommentStart() {
|
||||
// for now: no space story
|
||||
return "/*"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the text to be inserted at the selection end.
|
||||
*
|
||||
* @return the text to be inserted at the selection end
|
||||
*/
|
||||
protected String getCommentEnd() {
|
||||
// for now: no space story
|
||||
return "*/"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2004 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.actions;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.text.ICPartitions;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.BadPartitioningException;
|
||||
import org.eclipse.jface.text.IDocumentExtension3;
|
||||
import org.eclipse.jface.text.ITextSelection;
|
||||
import org.eclipse.jface.text.ITypedRegion;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
|
||||
/**
|
||||
* Action that removes the enclosing comment marks from a Java block comment.
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public class RemoveBlockCommentAction extends BlockCommentAction {
|
||||
|
||||
/**
|
||||
* Creates a new instance.
|
||||
*
|
||||
* @param bundle the resource bundle
|
||||
* @param prefix a prefix to be prepended to the various resource keys
|
||||
* (described in <code>ResourceAction</code> constructor), or
|
||||
* <code>null</code> if none
|
||||
* @param editor the text editor
|
||||
*/
|
||||
public RemoveBlockCommentAction(ResourceBundle bundle, String prefix, ITextEditor editor) {
|
||||
super(bundle, prefix, editor);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#runInternal(org.eclipse.jface.text.ITextSelection, org.eclipse.jface.text.IDocumentExtension3, org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction.Edit.EditFactory)
|
||||
*/
|
||||
protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
|
||||
List edits= new LinkedList();
|
||||
int tokenLength= getCommentStart().length();
|
||||
|
||||
int offset= selection.getOffset();
|
||||
int endOffset= offset + selection.getLength();
|
||||
|
||||
ITypedRegion partition= docExtension.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, offset, false);
|
||||
int partOffset= partition.getOffset();
|
||||
int partEndOffset= partOffset + partition.getLength();
|
||||
|
||||
while (partEndOffset < endOffset) {
|
||||
|
||||
if (partition.getType() == ICPartitions.C_MULTILINE_COMMENT) {
|
||||
edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
|
||||
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
partition= docExtension.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, partEndOffset, false);
|
||||
partOffset= partition.getOffset();
|
||||
partEndOffset= partOffset + partition.getLength();
|
||||
}
|
||||
|
||||
if (partition.getType() == ICPartitions.C_MULTILINE_COMMENT) {
|
||||
edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
|
||||
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
executeEdits(edits);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.jdt.internal.ui.actions.AddBlockCommentAction#validSelection(org.eclipse.jface.text.ITextSelection)
|
||||
*/
|
||||
protected boolean isValidSelection(ITextSelection selection) {
|
||||
return selection != null && !selection.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -20,7 +20,9 @@ import org.eclipse.cdt.core.model.ISourceReference;
|
|||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
import org.eclipse.cdt.internal.ui.ICHelpContextIds;
|
||||
import org.eclipse.cdt.internal.ui.IContextMenuConstants;
|
||||
import org.eclipse.cdt.internal.ui.actions.AddBlockCommentAction;
|
||||
import org.eclipse.cdt.internal.ui.actions.FoldingActionGroup;
|
||||
import org.eclipse.cdt.internal.ui.actions.RemoveBlockCommentAction;
|
||||
import org.eclipse.cdt.internal.ui.browser.typehierarchy.OpenTypeHierarchyAction;
|
||||
import org.eclipse.cdt.internal.ui.editor.asm.AsmTextTools;
|
||||
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
|
||||
|
@ -607,6 +609,20 @@ public class CEditor extends TextEditor implements ISelectionChangedListener, IS
|
|||
setAction("Uncomment", action); //$NON-NLS-1$
|
||||
markAsStateDependentAction("Uncomment", true); //$NON-NLS-1$
|
||||
|
||||
action= new AddBlockCommentAction(CEditorMessages.getResourceBundle(), "AddBlockComment.", this); //$NON-NLS-1$
|
||||
action.setActionDefinitionId(ICEditorActionDefinitionIds.ADD_BLOCK_COMMENT);
|
||||
setAction("AddBlockComment", action); //$NON-NLS-1$
|
||||
markAsStateDependentAction("AddBlockComment", true); //$NON-NLS-1$
|
||||
markAsSelectionDependentAction("AddBlockComment", true); //$NON-NLS-1$
|
||||
//WorkbenchHelp.setHelp(action, ICHelpContextIds.ADD_BLOCK_COMMENT_ACTION);
|
||||
|
||||
action= new RemoveBlockCommentAction(CEditorMessages.getResourceBundle(), "RemoveBlockComment.", this); //$NON-NLS-1$
|
||||
action.setActionDefinitionId(ICEditorActionDefinitionIds.REMOVE_BLOCK_COMMENT);
|
||||
setAction("RemoveBlockComment", action); //$NON-NLS-1$
|
||||
markAsStateDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
|
||||
markAsSelectionDependentAction("RemoveBlockComment", true); //$NON-NLS-1$
|
||||
//WorkbenchHelp.setHelp(action, ICHelpContextIds.REMOVE_BLOCK_COMMENT_ACTION);
|
||||
|
||||
action = new TextOperationAction(CEditorMessages.getResourceBundle(), "Format.", this, ISourceViewer.FORMAT); //$NON-NLS-1$
|
||||
action.setActionDefinitionId(ICEditorActionDefinitionIds.FORMAT);
|
||||
setAction("Format", action); //$NON-NLS-1$
|
||||
|
|
|
@ -120,6 +120,14 @@ Uncomment.label=Uncommen&t
|
|||
Uncomment.tooltip=Uncomment the Selected C // comment Lines
|
||||
Uncomment.description=Uncomment the selected C // comment lines
|
||||
|
||||
AddBlockComment.label=Add &Block Comment
|
||||
AddBlockComment.tooltip=Enclose the Selection in a Block Comment
|
||||
AddBlockComment.description=Encloses the selection with block comment markers
|
||||
|
||||
RemoveBlockComment.label=Remove Bloc&k Comment
|
||||
RemoveBlockComment.tooltip=Remove Block Comment Markers Enclosing the Caret
|
||||
RemoveBlockComment.description=Removes any block comment markers enclosing the caret
|
||||
|
||||
Format.label=F&ormat
|
||||
Format.tooltip=Format the Selected Text
|
||||
Format.description=Format the selected text
|
||||
|
|
|
@ -35,7 +35,22 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition
|
|||
* (value <code>"org.eclipse.cdt.ui.edit.text.c.uncomment"</code>).
|
||||
*/
|
||||
public static final String UNCOMMENT = "org.eclipse.cdt.ui.edit.text.c.uncomment"; //$NON-NLS-1$
|
||||
|
||||
|
||||
/**
|
||||
* Action definition ID of the source -> add block comment action
|
||||
* (value <code>"org.eclipse.cdt.ui.edit.text.c.add.block.comment"</code>).
|
||||
* @since 3.0
|
||||
*/
|
||||
public static final String ADD_BLOCK_COMMENT= "org.eclipse.cdt.ui.edit.text.c.add.block.comment"; //$NON-NLS-1$
|
||||
|
||||
/**
|
||||
* Action definition ID of the source -> remove block comment action
|
||||
* (value <code>"org.eclipse.cdt.ui.edit.text.c.remove.block.comment"</code>).
|
||||
* @since 3.0
|
||||
*/
|
||||
public static final String REMOVE_BLOCK_COMMENT= "org.eclipse.cdt.ui.edit.text.c.remove.block.comment"; //$NON-NLS-1$
|
||||
|
||||
|
||||
/**
|
||||
* Action definition ID of the source -> format action
|
||||
* (value <code>"org.eclipse.cdt.ui.edit.text.c.format"</code>).
|
||||
|
@ -102,4 +117,5 @@ public interface ICEditorActionDefinitionIds extends ITextEditorActionDefinition
|
|||
* (value <code>"org.eclipse.cdt.ui.edit.text.c.open.editor"</code>).
|
||||
*/
|
||||
public static final String OPEN_EDITOR= "org.eclipse.cdt.ui.edit.text.c.open.editor"; //$NON-NLS-1$
|
||||
|
||||
}
|
||||
|
|
|
@ -23,16 +23,7 @@ import org.eclipse.jface.text.rules.WordRule;
|
|||
/**
|
||||
* This scanner recognizes comments
|
||||
*/
|
||||
public class CPartitionScanner extends RuleBasedPartitionScanner {
|
||||
|
||||
|
||||
private final static String SKIP= "__skip"; //$NON-NLS-1$
|
||||
|
||||
|
||||
public final static String C_MULTILINE_COMMENT= "c_multi_line_comment"; //$NON-NLS-1$
|
||||
public final static String C_SINGLE_LINE_COMMENT= "c_single_line_comment"; //$NON-NLS-1$
|
||||
public final static String C_STRING= "c_string"; //$NON-NLS-1$
|
||||
|
||||
public class CPartitionScanner extends RuleBasedPartitionScanner implements ICPartitions {
|
||||
|
||||
/**
|
||||
* Detector for empty comments.
|
||||
|
|
|
@ -170,9 +170,9 @@ public class CTextTools {
|
|||
public IDocumentPartitioner createDocumentPartitioner() {
|
||||
|
||||
String[] types= new String[] {
|
||||
CPartitionScanner.C_MULTILINE_COMMENT,
|
||||
CPartitionScanner.C_SINGLE_LINE_COMMENT,
|
||||
CPartitionScanner.C_STRING
|
||||
ICPartitions.C_MULTILINE_COMMENT,
|
||||
ICPartitions.C_SINGLE_LINE_COMMENT,
|
||||
ICPartitions.C_STRING
|
||||
};
|
||||
|
||||
return new DefaultPartitioner(getPartitionScanner(), types);
|
||||
|
|
|
@ -17,12 +17,7 @@ import org.eclipse.jface.text.rules.Token;
|
|||
* This scanner recognizes the C multi line comments, C single line comments,
|
||||
* C strings and C characters.
|
||||
*/
|
||||
public class FastCPartitionScanner implements IPartitionTokenScanner {
|
||||
|
||||
private final static String SKIP= "__skip"; //$NON-NLS-1$
|
||||
public final static String C_STRING= "c_string"; //$NON-NLS-1$
|
||||
public final static String C_SINGLE_LINE_COMMENT= "c_single_line_comment"; //$NON-NLS-1$
|
||||
public final static String C_MULTI_LINE_COMMENT= "c_multi_line_comment"; //$NON-NLS-1$
|
||||
public class FastCPartitionScanner implements IPartitionTokenScanner, ICPartitions {
|
||||
|
||||
// states
|
||||
private static final int CCODE= 0;
|
||||
|
@ -63,7 +58,7 @@ public class FastCPartitionScanner implements IPartitionTokenScanner {
|
|||
private final IToken[] fTokens= new IToken[] {
|
||||
new Token(null),
|
||||
new Token(C_SINGLE_LINE_COMMENT),
|
||||
new Token(C_MULTI_LINE_COMMENT),
|
||||
new Token(C_MULTILINE_COMMENT),
|
||||
new Token(SKIP),
|
||||
new Token(C_STRING)
|
||||
};
|
||||
|
@ -411,7 +406,7 @@ public class FastCPartitionScanner implements IPartitionTokenScanner {
|
|||
else if (contentType.equals(C_SINGLE_LINE_COMMENT))
|
||||
return SINGLE_LINE_COMMENT;
|
||||
|
||||
else if (contentType.equals(C_MULTI_LINE_COMMENT))
|
||||
else if (contentType.equals(C_MULTILINE_COMMENT))
|
||||
return MULTI_LINE_COMMENT;
|
||||
|
||||
else if (contentType.equals(C_STRING))
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2004 QNX Software Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.text;
|
||||
|
||||
/**
|
||||
*
|
||||
* The name of the C partitioning.
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface ICPartitions {
|
||||
|
||||
public final static String SKIP= "__skip"; //$NON-NLS-1$
|
||||
public final static String C_MULTILINE_COMMENT= "c_multi_line_comment"; //$NON-NLS-1$
|
||||
public final static String C_SINGLE_LINE_COMMENT= "c_single_line_comment"; //$NON-NLS-1$
|
||||
public final static String C_STRING= "c_string"; //$NON-NLS-1$
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue