Ignore:
Timestamp:
04/04/06 09:20:16 (18 years ago)
Author:
boris
Message:
  • merging adrians work in auto indention in modelica editor
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/editor/ModelicaAutoIndentStrategy.java

    r440 r441  
    22 * This file is part of Modelica Development Tooling.
    33 *
    4  * Copyright (c) 2005, Linkpings universitet, Department of
     4 * Copyright (c) 2005, Linköpings universitet, Department of
    55 * Computer and Information Science, PELAB
    66 *
     
    2323 *   distribution.
    2424 *
    25  * * Neither the name of Linkpings universitet nor the names of its
     25 * * Neither the name of Link�pings universitet nor the names of its
    2626 *   contributors may be used to endorse or promote products derived from
    2727 *   this software without specific prior written permission.
     
    4040 */
    4141
    42 
    4342package org.modelica.mdt.ui.editor;
    4443
     
    5049import org.eclipse.jface.text.TextUtilities;
    5150
    52 
     51/**
     52 * This class implements the auto indent strategy for modelica source
     53 * code editor.
     54 *
     55 * @author: Adrian Pop
     56 */
    5357
    5458class ModelicaAutoIdentStrategy implements IAutoEditStrategy
    55 
    5659{
    5760
     61    static public class key
     62    {
     63        protected String name;
     64        protected boolean indentPlus;
     65        protected boolean indentMinus;     
     66        public key(String name, boolean indentPlus, boolean indentMinus)
     67        {
     68            this.name        = name;
     69            this.indentPlus  = indentPlus;  /* should we indent++ */
     70            this.indentMinus = indentMinus; /* should we indent-- */
     71        }
     72       
     73        String getName() { return name; }
     74        boolean getIndentPlus() { return indentPlus; }
     75        boolean getIndentMinus() { return indentPlus; }     
     76    }
     77   
    5878    /* keywords that influence indenting */
    59 
    60     static public String[] keywords =
    61         {
    62 
    63             "algorithm", "and", "annotation", "assert", "block", "break",
    64             "class", "connect", "connector", "constant", "der","discrete",
    65             "each", "else", "elseif", "elsewhen", "encapsulated", "end",
    66             "enumeration", "equation", "extends", "external", "false", "final",
    67             "flow", "for", "function", "if", "import", "in", "initial", "inner",
    68             "input", "loop", "model", "not", "or", "outer", "output",
    69             "overload", "package", "parameter", "partial", "protected",
    70             "public", "record", "redeclare", "replacable", "return",
    71             "terminate", "then", "time", "true", "type", "when", "while",
    72             "within",
     79    static public key[] keywords =
     80        {
     81            new key("algorithm", true, false),
     82            new key("and", false, false),
     83            new key("annotation", false, false),
     84            new key("assert", false, false),
     85            new key("block", true, false),
     86            new key("break", false, false),
     87            new key("class", true, false),
     88            new key("connect", false, false),
     89            new key("connector", true, false),
     90            new key("constant", false, false),
     91            new key("der", false, false),
     92            new key("discrete", false, false),
     93            new key("each", false, false),
     94            new key("else", false, false),
     95            new key("elseif", false, false),
     96            new key("elsewhen", false, false),
     97            new key("encapsulated", false, false),
     98            new key("end", false, true),
     99            new key("enumeration", false, false),
     100            new key("equation", true, false),
     101            new key("extends", false, false),
     102            new key("external", false, false),
     103            new key("false", false, false),
     104            new key("final", false, false),
     105            new key("flow", false, false),
     106            new key("for", false, false),
     107            new key("function", true, false),
     108            new key("if", false, false),
     109            new key("import", false, false),
     110            new key("in", false, false),
     111            new key("initial", false, false),
     112            new key("inner", false, false),
     113            new key("input", false, false),
     114            new key("loop", false, false),
     115            new key("model", true, false),
     116            new key("not", false, false),
     117            new key("or", false, false),
     118            new key("outer", false, false),
     119            new key("output", false, false),
     120            new key("overload", false, false),
     121            new key("package", true, false),
     122            new key("parameter", false, false),
     123            new key("partial", false, false),
     124            new key("protected", false, false),
     125            new key("public", false, false),
     126            new key("record", true, false),
     127            new key("redeclare", false, false),
     128            new key("replacable", false, false),
     129            new key("return", false, false),
     130            new key("terminate", false, false),
     131            new key("then", true, false),
     132            new key("time", false, false),
     133            new key("true", false, false),
     134            new key("type", true, false),
     135            new key("when", false, false),
     136            new key("while", true, false),
     137            new key("within", true, false),
    73138           
    74139            /* The following are Meta-Modelica Keywords */
    75             "as", "case", "equality", "failure", "list", "local", "match",
    76             "matchcontinue" , "tuple", "uniontype"
     140            new key("as", false, false),
     141            new key("case", true, false),
     142            new key("equality", false, false),
     143            new key("failure", false, false),
     144            new key("list", false, false),
     145            new key("local", true, false),
     146            new key("match", true, false),
     147            new key("matchcontinue", true, false),
     148            new key("tuple", false, false),
     149            new key("uniontype", true, false)
    77150        };
     151
    78152
    79153    /**
     
    95169     */
    96170
     171
    97172    protected int findEndOfWhiteSpace(IDocument document, int offset, int end)
    98173        throws BadLocationException
     
    102177        {
    103178            char c= document.getChar(offset);
     179
    104180
    105181            if (c != ' ' && c != '\t')
     
    108184            }
    109185
     186
    110187            offset++;
    111188        }
     
    114191    }
    115192
    116    
    117193
    118194    /**
     
    130206     *            the command to deal with
    131207     */
    132 
    133208    private void autoIndentAfterNewLine(IDocument d, DocumentCommand c)
    134209    {
     
    139214        }
    140215
     216
    141217        try
    142218        {
    143219            /* find start of line */
     220
    144221
    145222            int p = (c.offset == d.getLength() ? c.offset - 1 : c.offset);
     
    147224            int start = info.getOffset();
    148225
     226
    149227            /* find white spaces */
    150228
    151229            int end = findEndOfWhiteSpace(d, start, c.offset);
    152230
     231
    153232            StringBuffer buf = new StringBuffer(c.text);
     233
    154234
    155235            if (end > start)
    156236            {
     237
    157238
    158239                /* append to input */
    159240                buf.append(d.get(start, end - start));
    160241            }
    161 
     242           
     243            /* adrpo --
     244             *  we copy the indentation from line above.
     245             *  and now we need to decide if we increase/decrease
     246             *  the indent
     247             */
     248            /* take the text appearing in this line and do some
     249             * checking on it.
     250             */
     251            if ( c.offset > end )
     252            {
     253                String lineStr = d.get(end, c.offset - end);
     254               
     255                for (int i=0; i < keywords.length; i++ )
     256                {
     257                    if (keywords[i].indentPlus && lineStr.startsWith(keywords[i].name))
     258                    {
     259                        buf.append("  ");
     260                    }
     261                    else if (keywords[i].indentMinus && lineStr.startsWith(keywords[i].name))
     262                    {
     263                        int l = buf.length();
     264                        /* decrease indent */
     265                        if (l >= 2 && buf.charAt(l-1) == ' ' && buf.charAt(l-2) == ' ')
     266                        buf.delete(l-2, l);
     267                    }
     268                }
     269            }
    162270            c.text = buf.toString();
    163271        }
     
    166274
    167275            /* stop work */
    168         }
    169 
    170     }
     276            excp.printStackTrace();
     277        }
     278    }
     279
    171280
    172281    private boolean isLineDelimiter(IDocument document, String text)
    173282    {
    174         String[] delimiters= document.getLegalLineDelimiters();
     283        String[] delimiters = document.getLegalLineDelimiters();
     284
    175285
    176286        if (delimiters != null)
     
    179289        }
    180290
     291
    181292        return false;
    182293    }
     294
    183295
    184296    private void smartIndentOnTab(IDocument document, DocumentCommand command)
     
    193305    }
    194306
    195     private void smartIndentOnKeypress(IDocument document, DocumentCommand command)
     307
     308    private void smartIndentOnKeypress(IDocument document,
     309            DocumentCommand command)
    196310    {
    197311        switch (command.text.charAt(0))
     
    201315            break;
    202316
     317
    203318            /* add more here if needed */
    204319            /* ok */
    205320        }
    206321    }
    207        
    208322
    209323    private void smartPaste(IDocument document, DocumentCommand command)
    210324    {
     325
    211326
    212327        /*
     
    216331         */
    217332    }
     333
    218334
    219335    /**
     
    227343             return;
    228344         }
     345
    229346       
    230347        if (c.length == 0 && c.text != null && isLineDelimiter(d, c.text))
     
    232349            autoIndentAfterNewLine(d, c);
    233350        }
     351
    234352        else if (c.text.length() == 1)
    235353        {
     354
    236355            smartIndentOnKeypress(d, c);
    237356        }
     357
    238358        else if (c.text.length() > 1)
    239359        {
     360
    240361            smartPaste(d, c);
    241362        }
     
    243364}
    244365
     366
     367
     368
Note: See TracChangeset for help on using the changeset viewer.