package com.redhat.ceylon.eclipse.code.refactor;

import static java.lang.Character.isUpperCase;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Text;

final class SubwordIterator implements KeyListener {
    private final Text text;
    private static final int MOD; 
    static {
        String platform = SWT.getPlatform();
        if ("carbon".equals (platform) || 
            "cocoa".equals (platform)) {
            MOD = SWT.MOD3;
        }
        else {
            MOD = SWT.MOD1;
        }
    }

    SubwordIterator(Text text) {
        this.text = text;
    }

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.keyCode==SWT.ARROW_RIGHT && 
                e.stateMask==MOD) {
            int len = text.getText().length();
            for (int i = text.getCaretPosition()+1; i<len; i++) {
                if (isUpperCase(text.getText().charAt(i))) {
                    text.setSelection(i);
                    e.doit = false;
                    break;
                }
            }
        }
        if (e.keyCode==SWT.ARROW_LEFT && 
                e.stateMask==MOD) {
            for (int i = text.getCaretPosition()-1; i>=0; i--) {
                if (isUpperCase(text.getText().charAt(i))) {
                    text.setSelection(i);
                    e.doit = false;
                    break;
                }
            }
        }
    }
}
@@ -935,31 +935,29 @@
 text.get
-CaretPosi
+Selec
 tion()
+.y
 +1; i%3Cle
@@ -1289,23 +1289,815 @@
 .get
-CaretPosition()
+Selection().x-1; i%3E=0; i--) %7B%0A                if (isUpperCase(text.getText().charAt(i))) %7B%0A                    text.setSelection(i);%0A                    e.doit = false;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (e.keyCode==SWT.ARROW_RIGHT && %0A                e.stateMask==(MOD%7CSWT.SHIFT)) %7B%0A            int len = text.getText().length();%0A            for (int i = text.getSelection().y+1; i%3Clen; i++) %7B%0A                if (isUpperCase(text.getText().charAt(i))) %7B%0A                    text.setSelection(text.getSelection().x, i);%0A                    e.doit = false;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        if (e.keyCode==SWT.ARROW_LEFT && %0A                e.stateMask==(MOD%7CSWT.SHIFT)) %7B%0A            for (int i = text.getSelection().x
 -1; 
@@ -2201,32 +2201,55 @@
 t.setSelection(i
+, text.getSelection().y
 );%0A             

improve SubwordIterator