如何在Xtext中编写语句后获得用于美化的制表空间 这是我的代码在Xtext语法中:
Block:
'_block'
name=ID
'_endblock'
;
和UI模板
override complete_BLOCK(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
super.complete_BLOCK(model, ruleCall, context, acceptor)
acceptor.accept(createCompletionProposal("_block \n
_endblock","_block",null,context));
}
如何在编写块语句后缩进制表符空间?
答案 0 :(得分:4)
实现格式化程序
formatter = {generateStub = true}
添加到工作流程的language = StandardLanguage {
部分MyDslFormatter
Xtend类并实现它调用格式化程序
这是一个非常天真的自动编辑策略的无故障保护
package org.xtext.example.mydsl1.ui;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentCommand;
import org.eclipse.jface.text.IAutoEditStrategy;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.xtext.ui.editor.autoedit.DefaultAutoEditStrategyProvider;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class YourAutoEditStrategyProvider extends DefaultAutoEditStrategyProvider {
public static class BlockStrategy implements IAutoEditStrategy {
private static final String BLOCK = "_block";
protected int findEndOfWhiteSpace(IDocument document, int offset, int end) throws BadLocationException {
while (offset < end) {
char c= document.getChar(offset);
if (c != ' ' && c != '\t') {
return offset;
}
offset++;
}
return end;
}
@Override
public void customizeDocumentCommand(IDocument d, DocumentCommand c) {
if ("\n".equals(c.text)) {
if (d.getLength()> BLOCK.length()) {
try {
if ((BLOCK+" ").equals(d.get(c.offset-BLOCK.length()-1, BLOCK.length()+1)) || (BLOCK).equals(d.get(c.offset-BLOCK.length(), BLOCK.length()))) {
int p= (c.offset == d.getLength() ? c.offset - 1 : c.offset);
IRegion info= d.getLineInformationOfOffset(p);
int start= info.getOffset();
// find white spaces
int end= findEndOfWhiteSpace(d, start, c.offset);
int l = 0;
StringBuilder buf= new StringBuilder(c.text);
if (end > start) {
// append to input
buf.append(d.get(start, end - start));
l += (end - start);
}
buf.append("\t");
buf.append("\n");
buf.append(d.get(start, end - start));
c.text= buf.toString();
c.caretOffset = c.offset+2+l;
c.shiftsCaret=false;
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
}
}
@Inject
private Provider<BlockStrategy> blockStrategy;
@Override
protected void configure(IEditStrategyAcceptor acceptor) {
super.configure(acceptor);
acceptor.accept(blockStrategy.get(), IDocument.DEFAULT_CONTENT_TYPE);
}
}
并且不要忘记绑定
class MyDslUiModule extends AbstractMyDslUiModule {
override bindAbstractEditStrategyProvider() {
YourAutoEditStrategyProvider
}
}