Chaitanya
Chaitanya

Reputation: 11

how to indent for tab space after writing a statement

How to get a tab space for beautification after writing a statement in Xtext.
Here is my code is in Xtext grammar :

 Block:
      '_block' 
         name=ID
     '_endblock' 
    ;

and UI template is

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));

}

How do I indent for a tab space after writing a block statement?

Upvotes: 1

Views: 835

Answers (1)

Christian Dietrich
Christian Dietrich

Reputation: 11868

to implement a formatter

  1. open the mwe2 file
  2. add formatter = {generateStub = true} to the language = StandardLanguage { section of the workflow
  3. regenerate the language
  4. open the MyDslFormatter Xtend class and implement it

to call the formatter

  1. mark the section to format or dont mark to format everything
  2. call rightclick -> Source -> Format or the Shortcut Cmd/Crtl + Shift + F

here is a very naive no failsafe impl of an auto edit strategy

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);
    }

}

and dont forget to bind

class MyDslUiModule extends AbstractMyDslUiModule {

    override bindAbstractEditStrategyProvider() {
        YourAutoEditStrategyProvider
    }

}

Upvotes: 4

Related Questions