auto-indent with bugs
Sylvestre Koumar [Mon, 21 Sep 2009 13:24:03 +0000 (15:24 +0200)]
scilab/modules/xpad/src/java/org/scilab/modules/xpad/actions/PrintPreviewAction.java
scilab/modules/xpad/src/java/org/scilab/modules/xpad/style/ScilabStyleDocument.java

index ab0c3ec..d5165da 100644 (file)
@@ -14,9 +14,8 @@ package org.scilab.modules.xpad.actions;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
-import java.awt.print.PrinterJob;
+import java.awt.print.Printable;
 
-import javax.print.attribute.HashPrintRequestAttributeSet;
 import javax.swing.KeyStroke;
 
 import org.scilab.modules.gui.menuitem.MenuItem;
@@ -29,9 +28,10 @@ public class PrintPreviewAction extends DefaultAction {
        super("Print Preview...", editor);
     }
     
-//    public void doAction() {
-//
-//    }
+    public void doAction() {
+       //Printable p = getEditor().getTextPane().getPrintable();
+
+    }
 
     public static MenuItem createMenu(Xpad editor) {
        return createMenu("Print Preview...", null, new PrintPreviewAction(editor), KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK+ActionEvent.SHIFT_MASK));
index 3846e2a..7bc098d 100644 (file)
@@ -61,7 +61,7 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                }
        };
 
-       private boolean autoIndent = false;
+       private boolean autoIndent = true;
        private boolean autoColorize = true;
        private boolean colorizeInprogress = false;
        private boolean indentInprogress = false;
@@ -324,7 +324,16 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                }
        }
 
-       public void applyIndent(int start, int end, String previous_tab, int level_one) throws BadLocationException {
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       public void applyAutoIndent(int start, int end, String previous_tab) throws BadLocationException {
 
                String indentedText = "";
                boolean got_case = false;
@@ -335,6 +344,203 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                try {
                        // Get the document text to indent
                        text_to_indent = this.getText(start, end-start);
+                       System.out.println("{"+text_to_indent+"}");
+               } catch (BadLocationException e) {
+                       e.printStackTrace();
+               }
+
+               // Get all line break positions &
+               // each line of the document
+               Vector<Integer> line_break = new Vector<Integer>(); // positions of line break
+               Vector<String> all_lines = new Vector<String>(); // the document line by line
+               String line = "";
+               for (int i = 0; i < text_to_indent.length(); i++) {
+                       line = line.concat(text_to_indent.charAt(i)+"");
+
+                       if (text_to_indent.charAt(i)=='\n') {
+                               line_break.add(i);
+                               all_lines.add(line);
+                               line = "";
+                       }
+                       if (i==text_to_indent.length()-1) {
+                               all_lines.add(line);
+                               line = "";
+                       }
+               }
+
+               Vector<String> all_lines_without_spaces = new Vector<String>(); // the document line by line
+               // without spaces
+               for (int i = 0; i < all_lines.size(); i++) {
+                       String no_space_line = removeFirstSpaces(all_lines.elementAt(i));
+                       all_lines_without_spaces.add(no_space_line);
+               }
+
+               boolean got_select = false;
+               int indent_level = 0;
+
+               for (int i = 0; i < all_lines_without_spaces.size(); i++) {
+                       
+                       // Get commands for each lines
+                       command_list = getOperatorList(all_lines_without_spaces.elementAt(i));
+
+                       // Check if in one line all operators are matching,
+                       // so we can know if the next line needs indentation or not
+                       // ex: if %T then function foo(1) endfunction end => doesn't need indentation
+                       // Warning: vector_match looks like [IN, if, IN function, OUT, endfunction, OUT, end]
+                       Vector<String> vector_match = matchingOperators(command_list);
+
+
+                       indentedText += previous_tab;
+
+                       // Here start the indentation process
+                       if (vector_match.size() > 0) {
+
+                               // If we have 'IN' command
+                               if (vector_match.elementAt(0).equals(IN)) {
+                                       
+                                       if ((vector_match.elementAt(1).toLowerCase().equals("else")) || 
+                                                       (vector_match.elementAt(1).toLowerCase().equals("elseif"))) {
+                                               
+                                               if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                                       indentedText = previous_tab + all_lines_without_spaces.elementAt(i);
+                                                       if (indentedText.length() >= 2) {
+                                                               String text_tab = indentedText.substring(0, 2);
+                                                               if (text_tab.equals("  ")) {
+                                                                       indentedText = indentedText.substring(2, indentedText.length());
+                                                               }
+                                                       }
+                                                       indentedText += previous_tab;
+                                                       break;
+                                               }
+                                               
+                                       } else if (vector_match.elementAt(1).toLowerCase().equals("select")) {
+                                               
+                                               got_select = true;
+                                               
+                                               if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                                       indentedText = previous_tab + all_lines_without_spaces.elementAt(i);
+                                                       indentedText += previous_tab + TABULATION;
+                                                       break;
+                                               }
+
+                                               // If we have 'case' command, case needs 'select' to be correct
+                                       } else if ((vector_match.elementAt(1).toLowerCase().equals("case"))) {
+                                               
+                                               if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                                       
+                                                       String tmp_cas_tab = "";
+                                                       tmp_cas_tab = getLineTabulations(indentedText);
+                                                       
+                                                       if (tmp_cas_tab.length() == 2) {
+                                                               System.out.println("222222222222222");
+                                                       } else {
+                                                               System.out.println(tmp_cas_tab.length());
+                                                       }
+                                                       
+                                                       
+                                                       
+                                                       if (previous_tab.length() == 2) {
+                                                               indentedText += all_lines_without_spaces.elementAt(i);
+                                                               indentedText += previous_tab + TABULATION;
+                                                       } else if (previous_tab.length() >= 2) {
+                                                               String tmp_tab = previous_tab.substring(0, 2);
+                                                               if (tmp_tab.equals("  ")) {
+                                                                       indentedText = previous_tab.substring(2, previous_tab.length());
+                                                                       indentedText += all_lines_without_spaces.elementAt(i);
+                                                                       indentedText += previous_tab.substring(2, previous_tab.length()) + TABULATION;
+                                                               }
+                                                       }
+                                                       
+//                                                     String case_tab = "";
+//                                                     if (previous_tab.length() > 2) {
+//                                                             case_tab = TABULATION;
+//                                                     } else {
+//                                                             case_tab = "";
+//                                                     }
+//                                                     
+//                                                     indentedText += case_tab + all_lines_without_spaces.elementAt(i);
+//                                                     indentedText += previous_tab + TABULATION;
+                                                       break;
+                                               }
+                                               
+                                       } else {
+                                               
+                                               if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                                       indentedText = previous_tab + all_lines_without_spaces.elementAt(i);
+                                                       indentedText += previous_tab + TABULATION;
+                                                       break;
+                                               }
+                                       }
+
+                                       // If we have "OUT' operator
+                               } else if (vector_match.elementAt(0).equals(OUT)) {
+                                       
+                                       if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                               
+                                               if (previous_tab.length() >= 2 && got_select == false) {
+                                                       String tmp_tab = previous_tab.substring(0, 2);
+                                                       if (tmp_tab.equals("  ")) {
+                                                               indentedText = previous_tab.substring(2, previous_tab.length());
+                                                               indentedText += all_lines_without_spaces.elementAt(i);
+                                                               indentedText += previous_tab.substring(2, previous_tab.length());
+                                                       }
+                                               }
+                                               break;
+                                       }
+                               } 
+                       } else {
+                               
+                               if (autoIndent == true && all_lines_without_spaces.size() == 2) {
+                                       indentedText = previous_tab + all_lines_without_spaces.elementAt(i);
+                                       indentedText += previous_tab;
+                                       break;
+                               }
+                       }
+                       vector_match.clear();
+
+               } // end for
+
+               // Display the indentation
+               this.replace(start, end-start, indentedText, null);
+
+       }
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       
+       public void applyIndent(int start, int end, String previous_tab, int level_one) throws BadLocationException {
+               
+               String indentedText = "";
+               boolean got_case = false;
+
+               String text_to_indent = ""; // the entire document
+               Vector<String> command_list = new Vector<String>(); // list of commands in the document
+
+               try {
+                       // Get the document text to indent
+                       text_to_indent = this.getText(start, end-start);
+                       System.out.println("{"+text_to_indent+"}");
                } catch (BadLocationException e) {
                        e.printStackTrace();
                }
@@ -390,14 +596,14 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
 
                                // If we have 'IN' command
                                if (vector_match.elementAt(0).equals(IN)) {
-
+                                       
                                        for (int j = 0; j < indent_level; j++) {
                                                indentedText += TABULATION;
                                        }
 
                                        if ((vector_match.elementAt(1).toLowerCase().equals("else")) || 
                                                        (vector_match.elementAt(1).toLowerCase().equals("elseif"))) {
-
+                                               
                                                if (indentedText.length() >= 2) {
                                                        String text_tab = indentedText.substring(indentedText.length()-2, indentedText.length());
                                                        if (text_tab.equals("  ") && indent_level > 0) {
@@ -410,12 +616,13 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                                        } else if (vector_match.elementAt(1).toLowerCase().equals("select")) {
                                                got_select = true;
                                                indent_level++;
-
+                                               
                                                // If we have 'case' command, case needs 'select' to be correct
                                        } else if ((vector_match.elementAt(1).toLowerCase().equals("case")) && 
                                                        (got_case == false) && (got_select == true) ) {
                                                got_case = true;
                                                indent_level++;
+                                               
                                        } else {
                                                indent_level++;
                                        }
@@ -440,23 +647,10 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                                                got_case = false;
                                        }
                                        
-                                       // If we have only an OUT command to indent
-                                       // If AUTO INDENT
-                                       if (autoIndent == true && all_lines_without_spaces.size() == 1 && level_one == 0) {
-                                               indentedText = previous_tab + all_lines_without_spaces.elementAt(i);
-                                               if (indentedText.length() >= 2) {
-                                                       String text_tab = indentedText.substring(0, 2);
-                                                       if (text_tab.equals("  ")) {
-                                                               indentedText = indentedText.substring(2, indentedText.length());
-                                                       }
-                                               }
-                                               break;
-                                       }
-                                       
                                        indentedText += all_lines_without_spaces.elementAt(i);
                                } 
                        } else {
-
+                               
                                for (int j = 0; j < indent_level; j++) {
                                        indentedText += TABULATION;
                                }
@@ -481,11 +675,12 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                //            - gestion des commentaire /* */
                //                        - analyser la ligne du document qui est modifie et non plus le document en entier pour la colorisation
                //                              dans le cas d'une quotation ou commentaire(/* */) analyser le document en entier
+               
        }
 
        
        public void applySelectionIndent() {
-
+               
                editor = getEditor();
                String tab = "";
                int selection_start = 0;
@@ -501,25 +696,35 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                lineStartPosition =  this.getParagraphElement(selection_start).getStartOffset();
                lineEndPosition = this.getParagraphElement(selection_end).getEndOffset()-1;
                
+//             System.out.println(lineStartPosition);
+//             System.out.println(lineEndPosition);
+               
                
-               System.out.println(lineStartPosition);
-               System.out.println(lineEndPosition);
+               
+               int previous_line_start = 0;
+               int previous_line_end = 0;
+               String previous_line;
+               
+               int current_line_start = 0;
+               int current_line_end = 0;
                
                if (lineStartPosition == 0) {
                        tab = "";
                } else {
                        // Get previous line
-                       int previous_line_start =  this.getParagraphElement(lineStartPosition-1).getStartOffset();
-                       int previous_line_end = this.getParagraphElement(lineStartPosition-1).getEndOffset()-1;
-                       String previous_line = "";
+                       previous_line_start =  this.getParagraphElement(lineStartPosition-1).getStartOffset();
+                       previous_line_end = this.getParagraphElement(lineStartPosition-1).getEndOffset()-1;
+                       previous_line = "";
                        
-                       int current_line_start = this.getParagraphElement(selection_start).getStartOffset();
-                       int current_line_end = this.getParagraphElement(selection_end).getEndOffset()-1;
+                       current_line_start = this.getParagraphElement(selection_start).getStartOffset();
+                       current_line_end = this.getParagraphElement(selection_end).getEndOffset()-1;
                        String current_line = "";
                        
                        try {
                                previous_line = this.getText(previous_line_start, previous_line_end-previous_line_start);
                                current_line = this.getText(current_line_start, current_line_end-current_line_start);
+                               System.out.println("previous_line = "+previous_line);
+                               
                                // Get previous line's tabulation
                                tab = getLineTabulations(previous_line);
                                // Check if we have commands and if they match in the previous line
@@ -530,12 +735,15 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                                Vector<String> current_command_list = getOperatorList(current_line);
                                Vector<String> current_vector_match = matchingOperators(current_command_list);
                                
-                               if (previous_vector_match.size() > 0) {
-                                       if (previous_vector_match.elementAt(0).equals(IN)) {
-                                               level_one = 1;
-                                       } 
-                               } else {
-                                       level_one = 0;
+                               if (!autoIndent) {
+
+                                       if (previous_vector_match.size() > 0) {
+                                               if (previous_vector_match.elementAt(0).equals(IN)) {
+                                                       level_one = 1;
+                                               } 
+                                       } else {
+                                               level_one = 0;
+                                       }
                                }
                                
                        } catch (BadLocationException e) {
@@ -544,8 +752,21 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                }
                                
                try {
-                       applyIndent(lineStartPosition, lineEndPosition, tab, level_one);
-                       tab = "";
+                       if (autoIndent) {
+                               lineStartPosition = previous_line_start;
+                               lineEndPosition = current_line_start;
+                               
+                               System.out.println("-------> "+lineStartPosition);
+                               System.out.println("-------> "+lineEndPosition);
+                               System.out.println("TAB = {"+tab+"}");
+                               
+                               applyAutoIndent(lineStartPosition, lineEndPosition, tab);
+                               tab = "";
+                               
+                       } else {
+                               applyIndent(lineStartPosition, lineEndPosition, tab, level_one);
+                               tab = "";
+                       }
                } catch (BadLocationException e) {
                        e.printStackTrace();
                }
@@ -1451,31 +1672,19 @@ public class ScilabStyleDocument extends DefaultStyledDocument implements Docume
                                public void run() {
                                        //resetStyle();
                                        if (autoIndent) {
-                                               //indent();
-//                                             IndentAction.getXpadEditor();
-//                                             editor = getEditor();
-//                                             int  caretPosition = editor.getTextPane().getCaretPosition();
-//                                             
-//                                             try {
-//                                                     if (!(caretPosition == 0)) {
-//                                                             caretPosition = caretPosition -1;
-//                                                     }
-//                                                     if (editor.getTextPane().getText(caretPosition, 1).equals("\n")) {
-//                                                             System.out.println("JE SAUTE UNE LIGNE");
-//
-//                                                             // Get start & end offsets of the selected text
-//                                                             int selection_start = editor.getTextPane().getSelectionStart();
-//                                                             int selection_end = editor.getTextPane().getSelectionEnd();
-//
-//                                                             // Get start offsets of the first selected line & end offsets of the last selected line
-//                                                             lineStartPosition =  editor.getTextPane().getStyledDocument().getParagraphElement(selection_start).getStartOffset();
-//                                                             lineEndPosition = editor.getTextPane().getStyledDocument().getParagraphElement(selection_end).getEndOffset()-1;
-//                                                             
-//                                                             indent();
-//                                                     }
-//                                             } catch (BadLocationException e) {
-//                                                     e.printStackTrace();
-//                                             }
+
+                                               IndentAction.getXpadEditor();
+                                               editor = getEditor();
+                                               int  caretPosition = editor.getTextPane().getCaretPosition();
+                                               
+                                               try {
+                                                       if (editor.getTextPane().getText(caretPosition-1, 1).equals("\n")) {
+                                                               System.out.println("------------ SAUTE de LIGNE ------------");
+                                                               indent();
+                                                       }
+                                               } catch (BadLocationException e) {
+                                                       e.printStackTrace();
+                                               }
                                        }
                                        if (autoColorize) {
                                                //colorize();