/*******************************************************************************
* Copyright (c) 2007, 2009 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 - initial API and implementation
*******************************************************************************/
package org.eclipse.linuxtools.internal.rpm.ui.editor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.ITextViewer;
public class SpecfileDoubleClickStrategy implements ITextDoubleClickStrategy {
protected ITextViewer fText;
//lTerminator should be either '\r' or '\n' depending on the OS
final char lTerminator = System.getProperty("line.separator").charAt(0); //$NON-NLS-1$
@Override
public void doubleClicked(ITextViewer part) {
int pos = part.getSelectedRange().x;
if (pos < 0) {
return;
}
fText = part;
if (!selectComment(pos)) {
selectWord(pos);
}
}
protected boolean selectComment(int caretPos) {
IDocument doc = fText.getDocument();
int startPos, endPos;
try {
int pos = caretPos;
char c = ' ';
while (pos >= 0) {
c = doc.getChar(pos);
if (c == '\\') {
pos -= 2;
continue;
}
if ( c == lTerminator || c == '\"') {
break;
}
--pos;
}
if (c != '\"') {
return false;
}
startPos = pos;
pos = caretPos;
int length = doc.getLength();
c = ' ';
while (pos < length) {
c = doc.getChar(pos);
if ( c == lTerminator || c == '\"' ) {
break;
}
++pos;
}
if (c != '\"')
return false;
endPos = pos;
int offset = startPos + 1;
int len = endPos - offset;
fText.setSelectedRange(offset, len);
return true;
} catch (BadLocationException x) {
}
return false;
}
protected boolean selectWord(int caretPos) {
IDocument doc = fText.getDocument();
int startPos, endPos;
try {
int pos = caretPos;
char c;
while (pos >= 0) {
c = doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c)) {
break;
}
--pos;
}
startPos = pos;
pos = caretPos;
int length = doc.getLength();
while (pos < length) {
c = doc.getChar(pos);
if (!Character.isJavaIdentifierPart(c)) {
break;
}
++pos;
}
endPos = pos;
selectRange(startPos, endPos);
return true;
} catch (BadLocationException x) {
}
return false;
}
private void selectRange(int startPos, int stopPos) {
int offset = startPos + 1;
int length = stopPos - offset;
fText.setSelectedRange(offset, length);
}
}