Java Xtext-带退格的自动编辑
假设以下情况: 我们有一个文档,其中包含空格Java Xtext-带退格的自动编辑,java,eclipse,editor,xtext,Java,Eclipse,Editor,Xtext,假设以下情况: 我们有一个文档,其中包含空格 (例如,表示为u) 在这(4)个空格后面有插入符号 我希望编辑器在用户按backspace时删除所有4个空格,而不是一个空格 我正在扩展DefaultIndentLineAutoEditStrategy,在这里我重写了以下方法 public void customizeDocumentCommand(IDocument d,DocumentCommand c) 我面临两个问题: 如何从DocumentCommand检测已使用的退格?如果使用换行符c.
(例如,表示为u) 在这(4)个空格后面有插入符号 我希望编辑器在用户按backspace时删除所有4个空格,而不是一个空格 我正在扩展DefaultIndentLineAutoEditStrategy,在这里我重写了以下方法
public void customizeDocumentCommand(IDocument d,DocumentCommand c)
我面临两个问题:
c.text
包含“\n”
或“\r\n”
,但如果使用退格符,则等于”
“\b”
追加到c.text
不起作用好的,我设法实现了它
如果(c.text.equals(“”)和&c.length==1)
条件检测到backspace/delete的使用c.offset-=3;
c、 长度=4代码>
private void handleBackspace(IDocument d, DocumentCommand c) {
if (c.offset == -1 || d.getLength() == 0)
return;
int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
IRegion info;
try {
info = d.getLineInformationOfOffset(p);
String line = d.get(info.getOffset(), info.getLength());
int lineoffset = info.getOffset();
/*Make sure unindent is made only if user is indented and has caret in correct position */
if ((p-lineoffset+1)%4==0&&((line.startsWith(" ") && !line.startsWith(" ")) || (line.startsWith(" ") && !line.startsWith(" ")))){ //1 or 2 level fixed indent
c.offset-=3;
c.length=4;
}
}catch (org.eclipse.jface.text.BadLocationException e) {
e.printStackTrace();
}
}
好的,我设法实现了它
如果(c.text.equals(“”)和&c.length==1)
条件检测到backspace/delete的使用c.offset-=3;
c、 长度=4代码>
private void handleBackspace(IDocument d, DocumentCommand c) {
if (c.offset == -1 || d.getLength() == 0)
return;
int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
IRegion info;
try {
info = d.getLineInformationOfOffset(p);
String line = d.get(info.getOffset(), info.getLength());
int lineoffset = info.getOffset();
/*Make sure unindent is made only if user is indented and has caret in correct position */
if ((p-lineoffset+1)%4==0&&((line.startsWith(" ") && !line.startsWith(" ")) || (line.startsWith(" ") && !line.startsWith(" ")))){ //1 or 2 level fixed indent
c.offset-=3;
c.length=4;
}
}catch (org.eclipse.jface.text.BadLocationException e) {
e.printStackTrace();
}
}
设置command.offset和command.length没有帮助吗?如果(c.text.equals(“”&&c.length==1)和c.offset-=3;c、 长度=4;就这么做了。谢谢。设置command.offset和command.length没有帮助吗?好的,如果(c.text.equals(“”&&c.length==1)和c.offset-=3;c、 长度=4;就这么做了。谢谢