下面列出了怎么用org.eclipse.jface.text.BadPartitioningException的API类实例代码及写法,或者点击链接到github查看源代码。
public boolean isWordStart(char c) {
if ('=' != c && ':' != c || fDocument == null)
return false;
try {
// check whether it is the first '=' in the logical line
int i=fOffset-2;
while (Character.isWhitespace(fDocument.getChar(i))) {
i--;
}
ITypedRegion partition= null;
if (fDocument instanceof IDocumentExtension3)
partition= ((IDocumentExtension3)fDocument).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, i, false);
return partition != null && IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType());
} catch (BadLocationException ex) {
return false;
} catch (BadPartitioningException e) {
return false;
}
}
private static boolean getRenameKeysProposals(PropertiesAssistContext invocationContext, ArrayList<ICompletionProposal> resultingCollections)
throws BadLocationException, BadPartitioningException {
ISourceViewer sourceViewer= invocationContext.getSourceViewer();
IDocument document= invocationContext.getDocument();
int selectionOffset= invocationContext.getOffset();
int selectionLength= invocationContext.getLength();
IField field= null;
IType accessorClass= invocationContext.getAccessorType();
if (accessorClass == null || !isEclipseNLSUsed(accessorClass))
return false;
List<String> keys= getKeysFromSelection(document, selectionOffset, selectionLength);
if (keys == null || keys.size() != 1)
return false;
field= accessorClass.getField(keys.get(0));
if (!field.exists())
return false;
if (resultingCollections == null)
return true;
String name= PropertiesFileEditorMessages.PropertiesCorrectionProcessor_rename_in_workspace;
resultingCollections.add(new RenameKeyProposal(name, 5, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE), field, sourceViewer.getTextWidget().getShell()));
return true;
}
@Override
protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadLocationException, BadPartitioningException {
int selectionOffset= selection.getOffset();
int selectionEndOffset= selectionOffset + selection.getLength();
List<Edit> edits= new LinkedList<Edit>();
ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_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);
}
@Override
public boolean isWordStart(char c) {
if ('=' != c && ':' != c || fDocument == null)
return false;
try {
// check whether it is the first '=' in the logical line
int i = fOffset - 2;
while (Character.isWhitespace(fDocument.getChar(i))) {
i--;
}
ITypedRegion partition = null;
if (fDocument instanceof IDocumentExtension3)
partition = ((IDocumentExtension3) fDocument)
.getPartition(IEditorConfigPartitions.EDITOR_CONFIG_PARTITIONING, i, false);
return partition != null && IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType());
} catch (BadLocationException ex) {
return false;
} catch (BadPartitioningException e) {
return false;
}
}
@Override
public boolean isWordStart(char c) {
if ('=' != c && ':' != c || fDocument == null)
return false;
try {
// check whether it is the first '=' in the logical line
int i = fOffset - 2;
while (Character.isWhitespace(fDocument.getChar(i))) {
i--;
}
ITypedRegion partition = null;
if (fDocument instanceof IDocumentExtension3)
partition = ((IDocumentExtension3) fDocument)
.getPartition(IEditorConfigPartitions.EDITOR_CONFIG_PARTITIONING, i, false);
return partition != null && IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType());
} catch (BadLocationException ex) {
return false;
} catch (BadPartitioningException e) {
return false;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static String[] getAllDocumentContentTypes(IDocument document) throws BadPartitioningException {
if (document instanceof IDocumentExtension3) {
IDocumentExtension3 ext = (IDocumentExtension3) document;
String[] partitionings = ext.getPartitionings();
Set contentTypes = new HashSet();
contentTypes.add(IDocument.DEFAULT_CONTENT_TYPE);
int len = partitionings.length;
for (int i = 0; i < len; i++) {
String[] legalContentTypes = ext.getLegalContentTypes(partitionings[i]);
int len2 = legalContentTypes.length;
for (int j = 0; j < len2; j++) {
contentTypes.add(legalContentTypes[j]);
}
contentTypes.addAll(Arrays.asList(legalContentTypes));
}
return (String[]) contentTypes.toArray(new String[contentTypes.size()]);
}
return document.getLegalContentTypes();
}
private boolean isUnclosedPair(char c, IDocument document, int offset) throws BadLocationException
{
// TODO Refactor and combine this copy-pasted code from PeerCharacterCloser
int beginning = 0;
// Don't check from very beginning of the document! Be smarter/quicker and check from beginning of
// partition if we can
if (document instanceof IDocumentExtension3)
{
try
{
IDocumentExtension3 ext = (IDocumentExtension3) document;
ITypedRegion region = ext.getPartition(IDocumentExtension3.DEFAULT_PARTITIONING, offset, false);
beginning = region.getOffset();
}
catch (BadPartitioningException e)
{
// ignore
}
}
// Now check leading source and see if we're an unclosed pair.
String previous = document.get(beginning, offset - beginning);
boolean open = false;
int index = -1;
while ((index = previous.indexOf(c, index + 1)) != -1)
{
open = !open;
}
return open;
}
public static ICompletionProposal[] collectAssists(PropertiesAssistContext invocationContext) throws BadLocationException, BadPartitioningException {
ArrayList<ICompletionProposal> resultingCollections= new ArrayList<ICompletionProposal>();
getEscapeUnescapeBackslashProposals(invocationContext, resultingCollections);
getCreateFieldsInAccessorClassProposals(invocationContext, resultingCollections);
getRemovePropertiesProposals(invocationContext, resultingCollections);
getRenameKeysProposals(invocationContext, resultingCollections);
if (resultingCollections.size() == 0)
return null;
return resultingCollections.toArray(new ICompletionProposal[resultingCollections.size()]);
}
@Override
protected void runInternal(ITextSelection selection, IDocumentExtension3 docExtension, Edit.EditFactory factory) throws BadPartitioningException, BadLocationException {
List<Edit> edits= new LinkedList<Edit>();
int tokenLength= getCommentStart().length();
int offset= selection.getOffset();
int endOffset= offset + selection.getLength();
ITypedRegion partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, offset, false);
int partOffset= partition.getOffset();
int partEndOffset= partOffset + partition.getLength();
while (partEndOffset < endOffset) {
if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
}
partition= docExtension.getPartition(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
partOffset= partition.getOffset();
partEndOffset= partOffset + partition.getLength();
}
if (partition.getType() == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
edits.add(factory.createEdit(partOffset, tokenLength, "")); //$NON-NLS-1$
edits.add(factory.createEdit(partEndOffset - tokenLength, tokenLength, "")); //$NON-NLS-1$
}
executeEdits(edits);
}
public ITypedRegion getPartition(int position) throws BadLocationException {
if(!partitionContainsPosition(lastPartition, position)) {
if(documentExt3 != null) {
try {
lastPartition = documentExt3.getPartition(partitioning, position, false);
} catch (BadPartitioningException e) {
throw assertFail(); // Cannot happen, we have ensured that partitioning exists
}
} else {
lastPartition = document.getPartition(position);
}
}
return lastPartition;
}
@Override
public ITypedRegion[] computePartitioning(String partitioning, int offset, int length,
boolean includeZeroLengthPartitions) throws BadLocationException, BadPartitioningException {
fail("Unexpected call");
return null;
}
@Override
public String getContentType(String partitioning, int offset, boolean preferOpenPartitions)
throws BadLocationException, BadPartitioningException {
fail("Unexpected call");
return null;
}
@Override
public String[] getLegalContentTypes(String partitioning) throws BadPartitioningException {
fail("Unexpected call");
return null;
}
@Override
public ITypedRegion getPartition(String partitioning, int offset, boolean preferOpenPartitions)
throws BadLocationException, BadPartitioningException {
fail("Unexpected call");
return null;
}
private boolean isUnclosedPair(VerifyEvent event, IDocument document, int offset) throws BadLocationException
{
final char closingCharacter = getPeerCharacter(event.character);
// This doesn't matter if the user is not typing an "end" character.
if (closingCharacter != event.character)
return false;
char c = event.character;
int beginning = 0;
// Don't check from very beginning of the document! Be smarter/quicker and check from beginning of
// partition if we can.
// FIXME What type of partitions does this make sense for? We should check across "code" partitions. Limit to
// single string/comment partition?
if (document instanceof IDocumentExtension3)
{
try
{
IDocumentExtension3 ext = (IDocumentExtension3) document;
ITypedRegion region = getPartition(ext, IDocumentExtension3.DEFAULT_PARTITIONING, offset, false);
beginning = region.getOffset();
}
catch (BadPartitioningException e)
{
// ignore
}
}
// Now check leading source and see if we're an unclosed pair.
String previous = document.get(beginning, offset - beginning);
boolean open = false;
int index = -1;
while ((index = previous.indexOf(c, index + 1)) != -1)
{
// if (ignoreScope(document, beginning + index))
// continue;
open = !open;
if (open)
{
c = closingCharacter;
}
else
{
c = event.character;
}
}
return open;
}
protected ITypedRegion getPartition(IDocumentExtension3 ext, String defaultPartitioning, int offset, boolean b)
throws BadLocationException, BadPartitioningException
{
return ext.getPartition(defaultPartitioning, offset, b);
}
private static boolean getEscapeUnescapeBackslashProposals(IQuickAssistInvocationContext invocationContext, ArrayList<ICompletionProposal> resultingCollections) throws BadLocationException,
BadPartitioningException {
ISourceViewer sourceViewer= invocationContext.getSourceViewer();
IDocument document= sourceViewer.getDocument();
Point selectedRange= sourceViewer.getSelectedRange();
int selectionOffset= selectedRange.x;
int selectionLength= selectedRange.y;
int proposalOffset;
int proposalLength;
String text;
if (selectionLength == 0) {
if (selectionOffset != document.getLength()) {
char ch= document.getChar(selectionOffset);
if (ch == '=' || ch == ':') { //see PropertiesFilePartitionScanner()
return false;
}
}
ITypedRegion partition= null;
if (document instanceof IDocumentExtension3)
partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, invocationContext.getOffset(), false);
if (partition == null)
return false;
String type= partition.getType();
if (!(type.equals(IPropertiesFilePartitions.PROPERTY_VALUE) || type.equals(IDocument.DEFAULT_CONTENT_TYPE))) {
return false;
}
proposalOffset= partition.getOffset();
proposalLength= partition.getLength();
text= document.get(proposalOffset, proposalLength);
if (type.equals(IPropertiesFilePartitions.PROPERTY_VALUE)) {
text= text.substring(1); //see PropertiesFilePartitionScanner()
proposalOffset++;
proposalLength--;
}
} else {
proposalOffset= selectionOffset;
proposalLength= selectionLength;
text= document.get(proposalOffset, proposalLength);
}
if (PropertiesFileEscapes.containsUnescapedBackslash(text)) {
if (resultingCollections == null)
return true;
resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.escape(text, false, true, false), proposalOffset, proposalLength,
PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_escapeBackslashes));
return true;
}
if (PropertiesFileEscapes.containsEscapedBackslashes(text)) {
if (resultingCollections == null)
return true;
resultingCollections.add(new EscapeBackslashCompletionProposal(PropertiesFileEscapes.unescapeBackslashes(text), proposalOffset, proposalLength,
PropertiesFileEditorMessages.EscapeBackslashCompletionProposal_unescapeBackslashes));
return true;
}
return false;
}
private static boolean getCreateFieldsInAccessorClassProposals(PropertiesAssistContext invocationContext, ArrayList<ICompletionProposal> resultingCollections)
throws BadLocationException, BadPartitioningException {
IDocument document= invocationContext.getDocument();
int selectionOffset= invocationContext.getOffset();
int selectionLength= invocationContext.getLength();
List<String> fields= new ArrayList<String>();
IType accessorClass= invocationContext.getAccessorType();
if (accessorClass == null || !isEclipseNLSUsed(accessorClass))
return false;
List<String> keys= getKeysFromSelection(document, selectionOffset, selectionLength);
if (keys == null || keys.size() == 0)
return false;
for (Iterator<String> iterator= keys.iterator(); iterator.hasNext();) {
String key= iterator.next();
if (!isValidJavaIdentifier(key))
continue;
IField field= accessorClass.getField(key);
if (field.exists())
continue;
if (resultingCollections == null)
return true;
fields.add(key);
}
if (fields.size() == 0)
return false;
ICompilationUnit cu= accessorClass.getCompilationUnit();
try {
Change change= AccessorClassModifier.addFields(cu, fields);
String name= Messages.format(fields.size() == 1 ? PropertiesFileEditorMessages.PropertiesCorrectionProcessor_create_field_in_accessor_label : PropertiesFileEditorMessages.PropertiesCorrectionProcessor_create_fields_in_accessor_label, BasicElementLabels.getFileName(cu));
resultingCollections.add(new CUCorrectionProposal(name, cu, (TextChange) change, 5, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)));
} catch (CoreException e) {
JavaPlugin.log(e);
return false;
}
return true;
}
private static boolean getRemovePropertiesProposals(PropertiesAssistContext invocationContext, ArrayList<ICompletionProposal> resultingCollections)
throws BadLocationException, BadPartitioningException {
IDocument document= invocationContext.getDocument();
int selectionOffset= invocationContext.getOffset();
int selectionLength= invocationContext.getLength();
List<String> fields= new ArrayList<String>();
IFile file= invocationContext.getFile();
if (file == null)
return false;
IType accessorClass= invocationContext.getAccessorType();
if (accessorClass == null || !isEclipseNLSUsed(accessorClass))
return false;
List<String> keys= getKeysFromSelection(document, selectionOffset, selectionLength);
if (keys == null || keys.size() == 0)
return false;
if (resultingCollections == null)
return true;
for (Iterator<String> iterator= keys.iterator(); iterator.hasNext();) {
String key= iterator.next();
IField field= accessorClass.getField(key);
if (field.exists())
fields.add(key);
}
ICompilationUnit cu= accessorClass.getCompilationUnit();
try {
Change propertiesFileChange= NLSPropertyFileModifier.removeKeys(file.getFullPath(), keys);
Change[] changes;
if (fields.size() > 0) {
Change accessorChange= AccessorClassModifier.removeFields(cu, fields);
changes= new Change[] { propertiesFileChange, accessorChange };
} else {
changes= new Change[] { propertiesFileChange };
}
String name= (keys.size() == 1)
? PropertiesFileEditorMessages.PropertiesCorrectionProcessor_remove_property_label
: PropertiesFileEditorMessages.PropertiesCorrectionProcessor_remove_properties_label;
resultingCollections.add(new RemovePropertiesProposal(name, 4, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE), changes));
} catch (CoreException e) {
JavaPlugin.log(e);
return false;
}
return true;
}
/**
* Handles partition boundaries within the selection. The end of the current
* partition and the start of the next partition are examined for whether
* they contain comment tokens that interfere with the created comment.
* <p>
* Comment tokens are removed from interior multi-line comments. Javadoc
* comments are left as is; instead, multi-line comment tokens are inserted
* before and after Javadoc partitions to ensure that the entire selected
* area is commented.
* </p>
* <p>
* The next partition is returned.
* </p>
*
* @param partition the current partition
* @param edits the list of edits to add to
* @param factory the edit factory
* @param docExtension the document to get the partitions from
* @return the next partition after the current
* @throws BadLocationException if accessing the document fails - this can
* only happen if the document gets modified concurrently
* @throws BadPartitioningException if the document does not have a Java
* partitioning
*/
private ITypedRegion handleInteriorPartition(ITypedRegion partition, List<Edit> 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 == IJavaPartitions.JAVA_MULTI_LINE_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(IJavaPartitions.JAVA_PARTITIONING, partEndOffset, false);
partType= partition.getType();
// start of next partition
if (wasJavadoc) {
// if previous was javadoc, and the current one is not a comment,
// then add a 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 == IJavaPartitions.JAVA_MULTI_LINE_COMMENT) {
// already in a comment - remove startToken
edits.add(factory.createEdit(partition.getOffset(), getCommentStart().length(), "")); //$NON-NLS-1$
}
}
return partition;
}
/**
* 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;