Eclipse SWT StyledText ウィジェットを使用して、選択したテキスト ブロックをタブまたはシフト + タブ キーでインデント/インデント解除するにはどうすればよいですか?
質問する
1484 次
1 に答える
1
これでうまくいくようです...
protected Control createContents(Composite parent){
......
//add the listeners...
text_code_impl.addVerifyKeyListener(new VerifyKeyListener() {
public void verifyKey(VerifyEvent e) {
if (e.keyCode == SWT.TAB) {
if ((e.stateMask & SWT.SHIFT) != 0){
e.doit = text_code_impl_shift_tab_pressed();
} else {
e.doit = text_code_impl_tab_pressed();
}
}
}
});
text_code_impl.addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
if (e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
e.doit = false; //allows verifyKey listener to fire
}
}
});
}
boolean text_code_impl_tab_pressed()
{
if (text_code_impl.getSelectionText().equals("")){
return true;
}
Point range = text_code_impl.getSelectionRange();
int start = range.x;
int length = text_code_impl.getSelectionCount();
String txt = text_code_impl.getText();
while (start > 0 && txt.charAt(start-1) != '\n') {
start--;
length++;
}
int replace_length = length;
text_code_impl.setSelectionRange(start, length);
text_code_impl.showSelection();
String sel_text = text_code_impl.getSelectionText();
String[] lines = sel_text.split("\n");
String new_text = "";
for (int x=0; x < lines.length; x++){
if (x > 0){
new_text += '\n';
}
new_text += '\t';
length++;
new_text += lines[x];
}
text_code_impl.replaceTextRange(start, replace_length, new_text);
text_code_impl.setSelectionRange(start, length);
text_code_impl.showSelection();
return false;
}
boolean text_code_impl_shift_tab_pressed()
{
if (text_code_impl.getSelectionText().equals("")){
return true;
}
Point range = text_code_impl.getSelectionRange();
int start = range.x;
int length = text_code_impl.getSelectionCount();
String txt = text_code_impl.getText();
while (start > 0 && txt.charAt(start-1) != '\n') {
--start;
++length;
}
int replace_length = length;
text_code_impl.setSelectionRange(start, length);
text_code_impl.showSelection();
String sel_text = text_code_impl.getSelectionText();
String[] lines = sel_text.split("\n");
String new_text = "";
for (int x=0; x < lines.length; x++){
if (x > 0){
new_text += '\n';
}
if (lines[x].charAt(0) == '\t'){
new_text += lines[x].substring(1);
length--;
} else if (lines[x].startsWith(" ")){
new_text += lines[x].substring(1);
length--;
} else {
new_text += lines[x];
}
}
text_code_impl.replaceTextRange(start, replace_length, new_text);
text_code_impl.setSelectionRange(start, length);
text_code_impl.showSelection();
return false;
}
于 2010-10-01T19:56:54.800 に答える