1 11 package org.eclipse.jdt.internal.ui.text.java; 12 13 import org.eclipse.jface.preference.IPreferenceStore; 14 15 import org.eclipse.jface.text.BadLocationException; 16 import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy; 17 import org.eclipse.jface.text.DocumentCommand; 18 import org.eclipse.jface.text.IDocument; 19 import org.eclipse.jface.text.IRegion; 20 import org.eclipse.jface.text.ITypedRegion; 21 import org.eclipse.jface.text.TextUtilities; 22 23 import org.eclipse.ui.IEditorPart; 24 import org.eclipse.ui.IWorkbenchPage; 25 import org.eclipse.ui.texteditor.ITextEditorExtension3; 26 27 import org.eclipse.jdt.ui.PreferenceConstants; 28 29 import org.eclipse.jdt.internal.ui.JavaPlugin; 30 31 34 public class JavaStringAutoIndentStrategy extends DefaultIndentLineAutoEditStrategy { 35 36 private String fPartitioning; 37 38 44 private String displayString(String inputString, String indentation, String delimiter) { 45 46 int length = inputString.length(); 47 StringBuffer buffer = new StringBuffer (length); 48 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer (inputString, "\n\r", true); while (tokenizer.hasMoreTokens()){ 50 51 String token = tokenizer.nextToken(); 52 if (token.equals("\r")) { buffer.append("\\r"); if (tokenizer.hasMoreTokens()) { 55 token = tokenizer.nextToken(); 56 if (token.equals("\n")) { buffer.append("\\n"); buffer.append("\" + " + delimiter); buffer.append(indentation); 60 buffer.append("\""); continue; 62 } else { 63 buffer.append("\" + " + delimiter); buffer.append(indentation); 65 buffer.append("\""); } 67 } else { 68 continue; 69 } 70 } else if (token.equals("\n")) { buffer.append("\\n"); buffer.append("\" + " + delimiter); buffer.append(indentation); 74 buffer.append("\""); continue; 76 } 77 78 StringBuffer tokenBuffer = new StringBuffer (); 79 for (int i = 0; i < token.length(); i++){ 80 char c = token.charAt(i); 81 switch (c) { 82 case '\r' : 83 tokenBuffer.append("\\r"); break; 85 case '\n' : 86 tokenBuffer.append("\\n"); break; 88 case '\b' : 89 tokenBuffer.append("\\b"); break; 91 case '\t' : 92 tokenBuffer.append("\t"); break; 95 case '\f' : 96 tokenBuffer.append("\\f"); break; 98 case '\"' : 99 tokenBuffer.append("\\\""); break; 101 case '\'' : 102 tokenBuffer.append("\\'"); break; 104 case '\\' : 105 tokenBuffer.append("\\\\"); break; 107 default : 108 tokenBuffer.append(c); 109 } 110 } 111 buffer.append(tokenBuffer); 112 } 113 return buffer.toString(); 114 } 115 116 121 public JavaStringAutoIndentStrategy(String partitioning) { 122 super(); 123 fPartitioning= partitioning; 124 } 125 126 private boolean isLineDelimiter(IDocument document, String text) { 127 String [] delimiters= document.getLegalLineDelimiters(); 128 if (delimiters != null) 129 return TextUtilities.equals(delimiters, text) > -1; 130 return false; 131 } 132 133 private String getLineIndentation(IDocument document, int offset) throws BadLocationException { 134 135 int adjustedOffset= (offset == document.getLength() ? offset - 1 : offset); 137 IRegion line= document.getLineInformationOfOffset(adjustedOffset); 138 int start= line.getOffset(); 139 140 int end= findEndOfWhiteSpace(document, start, offset); 142 143 return document.get(start, end - start); 144 } 145 146 private String getModifiedText(String string, String indentation, String delimiter) { 147 return displayString(string, indentation, delimiter); 148 } 149 150 private void javaStringIndentAfterNewLine(IDocument document, DocumentCommand command) throws BadLocationException { 151 152 ITypedRegion partition= TextUtilities.getPartition(document, fPartitioning, command.offset, true); 153 int offset= partition.getOffset(); 154 int length= partition.getLength(); 155 156 if (command.offset == offset + length && document.getChar(offset + length - 1) == '\"') 157 return; 158 159 String indentation= getLineIndentation(document, command.offset); 160 String delimiter= TextUtilities.getDefaultLineDelimiter(document); 161 162 IRegion line= document.getLineInformationOfOffset(offset); 163 String string= document.get(line.getOffset(), offset - line.getOffset()); 164 if (string.trim().length() != 0) 165 indentation += String.valueOf("\t\t"); 167 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 168 if (isLineDelimiter(document, command.text)) 169 command.text= "\" +" + command.text + indentation + "\""; else if (command.text.length() > 1 && preferenceStore.getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS)) 171 command.text= getModifiedText(command.text, indentation, delimiter); 172 } 173 174 private boolean isSmartMode() { 175 IWorkbenchPage page= JavaPlugin.getActivePage(); 176 if (page != null) { 177 IEditorPart part= page.getActiveEditor(); 178 if (part instanceof ITextEditorExtension3) { 179 ITextEditorExtension3 extension= (ITextEditorExtension3) part; 180 return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT; 181 } 182 } 183 return false; 184 } 185 186 189 public void customizeDocumentCommand(IDocument document, DocumentCommand command) { 190 try { 191 if (command.text == null) 192 return; 193 194 IPreferenceStore preferenceStore= JavaPlugin.getDefault().getPreferenceStore(); 195 196 if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS) && isSmartMode()) { 197 javaStringIndentAfterNewLine(document, command); 198 } 199 200 } catch (BadLocationException e) { 201 } 202 } 203 } 204 | Popular Tags |