Search in sources :

Example 1 with SelectionRange

use of org.whole.lang.ui.tools.SelectionRange in project whole by wholeplatform.

the class TextualKeyHandler method calculateDoubleClickSelection.

public IWholeSelection calculateDoubleClickSelection(EditPoint editPoint) {
    ITextualEntityPart targetPart = (ITextualEntityPart) editPoint.focus;
    IEntity targetEntity = targetPart.getModelEntity();
    int endPosition = -1, startPosition = -1;
    if (enableSmartSelection(targetEntity)) {
        String text = targetEntity.wStringValue();
        int position = targetPart.getCaretPosition();
        int positions = targetPart.getCaretPositions();
        Matcher matcher;
        if (position == 0) {
            matcher = RIGHT_PATTERN.matcher(text);
            if (matcher.find()) {
                startPosition = 0;
                endPosition = matcher.end();
            }
        } else if (position == positions) {
            matcher = LEFT_PATTERN.matcher(text);
            if (matcher.find()) {
                startPosition = matcher.start();
                endPosition = positions;
            }
        } else {
            if (Character.isLetterOrDigit(text.charAt(position))) {
                if (Character.isLetterOrDigit(text.charAt(position - 1))) {
                    // XX
                    String substring = text.substring(0, position);
                    matcher = LEFT_PATTERN.matcher(substring);
                    if (matcher.find())
                        startPosition = matcher.start();
                    matcher = RIGHT_PATTERN.matcher(text.substring(position));
                    if (matcher.find())
                        endPosition = position + matcher.end();
                } else {
                    // .X
                    matcher = RIGHT_PATTERN.matcher(text.substring(position));
                    if (matcher.find()) {
                        startPosition = position + matcher.start();
                        endPosition = position + matcher.end();
                    }
                }
            } else {
                if (Character.isLetterOrDigit(text.charAt(position - 1))) {
                    // X.
                    matcher = LEFT_PATTERN.matcher(text.substring(0, position));
                    if (matcher.find()) {
                        startPosition = matcher.start();
                        endPosition = position;
                    }
                } else {
                    // ..
                    startPosition = position - 1;
                    endPosition = position;
                }
            }
        }
    }
    if (endPosition >= 0 && startPosition >= 0)
        return new SelectionRange(editPoint.focus, startPosition, endPosition);
    else
        return super.calculateDoubleClickSelection(editPoint);
}
Also used : ITextualEntityPart(org.whole.lang.ui.editparts.ITextualEntityPart) IEntity(org.whole.lang.model.IEntity) Matcher(java.util.regex.Matcher) SelectionRange(org.whole.lang.ui.tools.SelectionRange) EditPoint(org.whole.lang.ui.tools.EditPoint)

Aggregations

Matcher (java.util.regex.Matcher)1 IEntity (org.whole.lang.model.IEntity)1 ITextualEntityPart (org.whole.lang.ui.editparts.ITextualEntityPart)1 EditPoint (org.whole.lang.ui.tools.EditPoint)1 SelectionRange (org.whole.lang.ui.tools.SelectionRange)1