Processes and adds the specified string. @param sb string to be added @param selected states if the text was selected @return returns the number spaces to move forward
(final StringBuilder sb, final boolean selected)
| 806 | * @return returns the number spaces to move forward |
| 807 | */ |
| 808 | int add(final StringBuilder sb, final boolean selected) { |
| 809 | if(sb.isEmpty()) return 0; |
| 810 | |
| 811 | int move = 0; |
| 812 | if(!selected && gui.gopts.get(GUIOptions.AUTO)) { |
| 813 | final char ch = sb.charAt(0); |
| 814 | final int next = pos + 1 < size() ? text[pos + 1] : 0; |
| 815 | final int curr = pos < size() ? text[pos] : 0; |
| 816 | final int prev = pos > 0 ? text[pos - 1] : 0; |
| 817 | final int pprv = pos > 1 ? text[pos - 2] : 0; |
| 818 | final int opening = OPENING.indexOf(ch); |
| 819 | if(opening != -1) { |
| 820 | // adds a closing to an opening bracket |
| 821 | if(CLOSING.indexOf(curr) != -1 || curr == 0 || ws(curr) || curr == '<') { |
| 822 | sb.append(CLOSING.charAt(opening)); |
| 823 | move = 1; |
| 824 | } |
| 825 | } else if(CLOSING.indexOf(ch) != -1) { |
| 826 | // closing bracket: ignore if it equals next character |
| 827 | if(ch == curr) { |
| 828 | sb.setLength(0); |
| 829 | move = 1; |
| 830 | } |
| 831 | close(); |
| 832 | } else if(ch == '"' || ch == '\'' || ch == '`') { |
| 833 | // quote: ignore if it equals next character |
| 834 | if(ch == curr) sb.setLength(0); |
| 835 | // add second quote |
| 836 | else if(!XMLToken.isNCChar(prev) && !XMLToken.isNCChar(curr)) sb.append(ch); |
| 837 | move = 1; |
| 838 | } else if(ch == '>') { |
| 839 | // closes an opening element |
| 840 | closeElem(sb); |
| 841 | move = 1; |
| 842 | } else if(ch == ':') { |
| 843 | // closes XQuery comments |
| 844 | if(prev == '(') { |
| 845 | sb.append(':'); |
| 846 | if(curr != ')') sb.append(')'); |
| 847 | move = 1; |
| 848 | } |
| 849 | } else if(ch == '~') { |
| 850 | // closes XQuery comments |
| 851 | if(prev == ':' && pprv == '(') { |
| 852 | sb.append(" "); |
| 853 | if(curr != ':') { |
| 854 | sb.append(':'); |
| 855 | if(curr != ')') sb.append(')'); |
| 856 | } else if(next != ')') { |
| 857 | sb.append(')'); |
| 858 | } |
| 859 | move = 2; |
| 860 | } |
| 861 | } else if(ch == '-') { |
| 862 | // closes XML comments |
| 863 | if(prev == '-' && pprv == '!' && pos > 2 && text[pos - 3] == '<') { |
| 864 | sb.append(" -->"); |
| 865 | move = 2; |