blob: 30105c0fb9374e7c9b49dd8e204926a2c02a4dc2 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.dltk.ui.text;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DefaultTextDoubleClickStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextUtilities;
/**
* Double click strategy that selects the partition if double click occurs at
* the partition boundaries.
*
* @since 3.0
*/
public class PartitionDoubleClickSelector extends
DefaultTextDoubleClickStrategy {
private String fPartitioning;
int fLeftBorder;
int fRightBorder;
int fHitDelta;
/**
* Creates a new partition double click selector for the given document
* partitioning.
*
* @param partitioning
* the document partitioning
* @param leftBorder
* number of characters to ignore from the left border of the
* partition
* @param rightBorder
* number of characters to ignore from the right border of the
* partition
*/
public PartitionDoubleClickSelector(String partitioning, int leftBorder,
int rightBorder) {
this(partitioning, leftBorder, rightBorder, 1);
}
/**
* Creates a new partition double click selector for the given document
* partitioning.
*
* @param partitioning
* the document partitioning
* @param leftBorder
* number of characters to ignore from the left border of the
* partition
* @param rightBorder
* number of characters to ignore from the right border of the
* partition
* @param hitDeltaOffset
* character offset delta relative to partition borders used to
* define the trigger character
* @since 3.0
*/
public PartitionDoubleClickSelector(String partitioning, int leftBorder,
int rightBorder, int hitDeltaOffset) {
fPartitioning = partitioning;
fLeftBorder = leftBorder;
fRightBorder = rightBorder;
fHitDelta = hitDeltaOffset;
}
/*
* @see DefaultTextDoubleClickStrategy#findExtendedDoubleClickSelection()
*
* @since Eclipse 3.5
*/
@Override
protected IRegion findExtendedDoubleClickSelection(IDocument document,
int offset) {
IRegion match = super
.findExtendedDoubleClickSelection(document, offset);
if (match != null)
return match;
try {
ITypedRegion region = TextUtilities.getPartition(document,
fPartitioning, offset, true);
if (offset == region.getOffset() + fHitDelta
|| offset == region.getOffset() + region.getLength()
- fHitDelta) {
if (fLeftBorder == 0 && fRightBorder == 0)
return region;
if (fRightBorder == -1) {
String delimiter = document.getLineDelimiter(document
.getLineOfOffset(region.getOffset()
+ region.getLength() - 1));
if (delimiter == null)
fRightBorder = 0;
else
fRightBorder = delimiter.length();
}
return new Region(region.getOffset() + fLeftBorder,
region.getLength() - fLeftBorder - fRightBorder);
}
} catch (BadLocationException e) {
return null;
}
return null;
}
}