package org.antlr.works.syntax;

import org.antlr.works.ate.syntax.misc.ATEToken;
import org.antlr.works.components.grammar.CEditorGrammar;
import org.antlr.works.prefs.AWPrefs;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Segment;
import java.text.CharacterIterator;
/*

[The "BSD licence"]
Copyright (c) 2005-2006 Jean Bovet
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

public class GrammarAutoIndent {

    public static void autoIndentOnSpecificKeys(CEditorGrammar editor, Document doc, int offset, int length) throws BadLocationException {
        String s = doc.getText(offset-1, length+1);
        if(s.length() < 2)
            return;
        
        char c1 = s.charAt(0);
        char c2 = s.charAt(1);
        if(c1 == '\n' || c1 == '\r') {
            if(c2 == '|') {
                doc.remove(offset, 1);
                doc.insertString(offset, "\t"+c2+"\t", null);
            } else if(c2 == ';') {
                doc.remove(offset, 1);
                doc.insertString(offset, "\t"+c2, null);
            }
        } else if(c2 == ':') {
            // If we know we are in a block token ignore the auto-indent on ':'
            ATEToken token = editor.getCurrentToken();
            if(token != null && token.type == GrammarSyntaxLexer.TOKEN_BLOCK)
                    return;

            // Try to reach the beginning of the line by parsing only an ID
            // (which is the rule name)
            boolean beginningOfRule = true;
            int originalOffset = offset;
            while(--offset >= 0) {
                String t = doc.getText(offset, 1);
                char c = t.charAt(0);
                if(c == '\n' || c == '\r') {
                    // beginning of line reached
                    // Little hack: go back to see if
                    // we see a '{' indicating that we are in an action and should not
                    // perform auto-indenting - this is not a perfect solution,
                    // only a hack until I find a better solution later ;-)
                    Segment seg = new Segment();
                    doc.getText(0, offset, seg);
                    c = seg.last();
                    while(c != CharacterIterator.DONE) {
                        // If we see a '}', pretend that we are after
                        // the end of an action - so break here
                        if(c == '}')
                            break;

                        // If we see a '{', pretend that we are in an action
                        // and so don't auto-indent
                        if(c == '{') {
                            beginningOfRule = false;
                            break;
                        }

                        c = seg.previous();
                    }
                    break;
                }
                if(c != ' ' && c != '_' && !Character.isLetterOrDigit(c)) {
                    beginningOfRule = false;
                    break;
                }
            }
            if(beginningOfRule && AWPrefs.autoIndentColonInRule()) {
                int lengthOfRule = originalOffset-offset;
                int tabSize = AWPrefs.getEditorTabSize();

                if(lengthOfRule > tabSize+1) {
                    doc.remove(originalOffset, 1);
                    doc.insertString(originalOffset, "\n\t:\t", null);
                } else if(lengthOfRule < tabSize+1) {
                    doc.remove(originalOffset, 1);
                    doc.insertString(originalOffset, "\t:\t", null);
                } else {
                    doc.insertString(originalOffset+1, "\t", null);
                }
            }
        }
    }

}
