From: robekras Date: Sun, 23 Dec 2012 18:01:51 +0000 (+0100) Subject: 1) Changed indentation of (single or double) quoted strings to align on the first... X-Git-Url: http://secure.phpeclipse.com 1) Changed indentation of (single or double) quoted strings to align on the first found single or double quote. Also uses spaces only or tabs (+ spaces) according the preference settings. Signed-off-by: robekras --- diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java index 14dff9c..d7642bf 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java @@ -26,7 +26,7 @@ import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.texteditor.ITextEditorExtension3; /** - * Auto indent strategy for java strings + * Auto indent strategy for double quoted PHP strings */ public class JavaStringAutoIndentStrategyDQ extends DefaultIndentLineAutoEditStrategy { @@ -40,20 +40,20 @@ public class JavaStringAutoIndentStrategyDQ extends * the given input string * @return the displayable string. */ - private String displayString(String inputString, String indentation, - String delimiter) { - + private String displayString (String inputString, String indentation, String delimiter) { int length = inputString.length(); StringBuffer buffer = new StringBuffer(length); - java.util.StringTokenizer tokenizer = new java.util.StringTokenizer( - inputString, "\n\r", true); //$NON-NLS-1$ - while (tokenizer.hasMoreTokens()) { - - String token = tokenizer.nextToken(); - if (token.equals("\r")) { //$NON-NLS-1$ - buffer.append("\\r"); //$NON-NLS-1$ - if (tokenizer.hasMoreTokens()) { + java.util.StringTokenizer tokenizer = + new java.util.StringTokenizer (inputString, "\n\r", true); //$NON-NLS-1$ + + while (tokenizer.hasMoreTokens ()) { + String token = tokenizer.nextToken (); + + if (token.equals ("\r")) { //$NON-NLS-1$ + buffer.append ("\\r"); //$NON-NLS-1$ + if (tokenizer.hasMoreTokens ()) { token = tokenizer.nextToken(); + if (token.equals("\n")) { //$NON-NLS-1$ buffer.append("\\n"); //$NON-NLS-1$ buffer.append("\" . " + delimiter); //$NON-NLS-1$ @@ -77,6 +77,7 @@ public class JavaStringAutoIndentStrategyDQ extends } StringBuffer tokenBuffer = new StringBuffer(); + for (int i = 0; i < token.length(); i++) { char c = token.charAt(i); switch (c) { @@ -111,6 +112,7 @@ public class JavaStringAutoIndentStrategyDQ extends } buffer.append(tokenBuffer); } + return buffer.toString(); } @@ -121,43 +123,122 @@ public class JavaStringAutoIndentStrategyDQ extends * @param partitioning * the document partitioning */ - public JavaStringAutoIndentStrategyDQ(String partitioning) { - super(); + public JavaStringAutoIndentStrategyDQ (String partitioning) { + super (); fPartitioning = partitioning; } - private boolean isLineDelimiter(IDocument document, String text) { + /** + * Check whether the 'text' is one of the allowed line delimiters (e.g. \n \r) + * + * @param document The document object for which we do the check + * @param text The text with the possible delimiter + * @return + * - true if 'text' is a valid delimiter + * - false if 'text' is not a valid delimiter + */ + private boolean isLineDelimiter (IDocument document, String text) { String[] delimiters = document.getLegalLineDelimiters(); - if (delimiters != null) - return TextUtilities.equals(delimiters, text) > -1; + + if (delimiters != null) { + return TextUtilities.equals (delimiters, text) > -1; + } + return false; } - - private String getLineIndentation(IDocument document, int offset) + + /** + * Get back the indentation of the line to which the offset belongs. + * + * It searches the first occurrence of a white space (non space/non tab) + * + * @param document + * @param offset + * @return + * @throws BadLocationException + */ + private String getLineIndentation (IDocument document, int offset) throws BadLocationException { - + String indentation = ""; + int start; + int end; + int length; + // find start of line - int adjustedOffset = (offset == document.getLength() ? offset - 1 - : offset); - IRegion line = document.getLineInformationOfOffset(adjustedOffset); - int start = line.getOffset(); - - // find white spaces - int end = findEndOfWhiteSpace(document, start, offset); + int adjustedOffset = (offset == document.getLength() ? offset - 1 : offset);// Check whether the offset is not at the end of file + IRegion line = document.getLineInformationOfOffset (adjustedOffset); // Get the start and the length of the line + + start = line.getOffset (); // Get the start of the line + end = findStringStart (document, start, offset); + + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore(); + length = end - start; + + if (preferenceStore.getBoolean (PreferenceConstants.EDITOR_SPACES_FOR_TABS)) { // Indentation with spaces only + if (length > 0) { + indentation = String.format ("%" + length + "s", ""); + } + } + else { // Indentation with tabs + if (length > 0) { + int spaces; + int tabs; + int tabWidth = preferenceStore.getInt (PreferenceConstants.EDITOR_TAB_WIDTH); - return document.get(start, end - start); + tabs = length / tabWidth; + spaces = length % tabWidth; + + indentation = new String (new char[tabs]).replace('\0', '\t'); + indentation += String.format ("%" + spaces + "s", ""); + } + } + + return indentation; } - - private String getModifiedText(String string, String indentation, - String delimiter) throws BadLocationException { - return displayString(string, indentation, delimiter); + + /** + * Return the position of the string start (first occurrence of a quote or double quote) + * + * @param offset + * @param start + * @param document + * @return + */ + private int findStringStart(IDocument document, int offset, int end) throws BadLocationException { + while (offset < end) { + char c = document.getChar (offset); + + if ((c == '\'') || (c == '\"')) { + return offset; + } + + offset++; + } + + return end; } - private void javaStringIndentAfterNewLine(IDocument document, - DocumentCommand command) throws BadLocationException { + /** + * + * @param string + * @param indentation + * @param delimiter + * @return + * @throws BadLocationException + */ + private String getModifiedText (String string, String indentation, String delimiter) + throws BadLocationException { + return displayString (string, indentation, delimiter); + } - ITypedRegion partition = TextUtilities.getPartition(document, - fPartitioning, command.offset, false); + /** + * + * @param document + * @param command + * @throws BadLocationException + */ + private void javaStringIndentAfterNewLine (IDocument document, DocumentCommand command) throws BadLocationException { + ITypedRegion partition = TextUtilities.getPartition (document, fPartitioning, command.offset, false); int offset = partition.getOffset(); int length = partition.getLength(); @@ -170,38 +251,49 @@ public class JavaStringAutoIndentStrategyDQ extends return; } - if (command.offset == offset + length - && document.getChar(offset + length - 1) == '\"') + if ((command.offset == offset + length) && + (document.getChar (offset + length - 1) == '\"')) { return; + } - String indentation = getLineIndentation(document, command.offset); - String delimiter = TextUtilities.getDefaultLineDelimiter(document); - - IRegion line = document.getLineInformationOfOffset(offset); - String string = document.get(line.getOffset(), offset - - line.getOffset()); - if (string.trim().length() != 0) - indentation += String.valueOf("\t\t"); //$NON-NLS-1$ + String indentation = getLineIndentation (document, command.offset); + String delimiter = TextUtilities.getDefaultLineDelimiter (document); - IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault() - .getPreferenceStore(); - if (isLineDelimiter(document, command.text)) + IRegion line = document.getLineInformationOfOffset (offset); + String string = document.get (line.getOffset(), + offset - line.getOffset()); // Get the current line as string +/* + if (string.trim().length() != 0) { + indentation += String.valueOf ("\t\t"); //$NON-NLS-1$ + } +*/ + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault ().getPreferenceStore(); + + if (isLineDelimiter (document, command.text)) { command.text = "\" ." + command.text + indentation + "\""; //$NON-NLS-1$//$NON-NLS-2$ - else if (command.text.length() > 1 - && preferenceStore - .getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ)) + } + else if ((command.text.length() > 1) && + preferenceStore.getBoolean (PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ)) { command.text = getModifiedText(command.text, indentation, delimiter); + } } - private boolean isSmartMode() { + /** + * + * @return + */ + private boolean isSmartMode () { IWorkbenchPage page = PHPeclipsePlugin.getActivePage(); + if (page != null) { - IEditorPart part = page.getActiveEditor(); + IEditorPart part = page.getActiveEditor (); + if (part instanceof ITextEditorExtension3) { ITextEditorExtension3 extension = (ITextEditorExtension3) part; return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT; } } + return false; } @@ -209,19 +301,18 @@ public class JavaStringAutoIndentStrategyDQ extends * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, * DocumentCommand) */ - public void customizeDocumentCommand(IDocument document, - DocumentCommand command) { + public void customizeDocumentCommand (IDocument document, DocumentCommand command) { try { - if (command.length != 0 || command.text == null) + if ((command.length != 0) || + (command.text == null)) { return; + } - IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault() - .getPreferenceStore(); + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore(); - if (preferenceStore - .getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_DQ) - && isSmartMode()) - javaStringIndentAfterNewLine(document, command); + if (preferenceStore.getBoolean (PreferenceConstants.EDITOR_WRAP_STRINGS_DQ) && isSmartMode ()) { + javaStringIndentAfterNewLine (document, command); + } } catch (BadLocationException e) { } diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java index d58f57b..0b29a0b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java @@ -26,7 +26,7 @@ import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.texteditor.ITextEditorExtension3; /** - * Auto indent strategy for java strings + * Auto indent strategy for single quoted PHP strings */ public class JavaStringAutoIndentStrategySQ extends DefaultIndentLineAutoEditStrategy { @@ -40,23 +40,22 @@ public class JavaStringAutoIndentStrategySQ extends * the given input string * @return the displayable string. */ - private String displayString(String inputString, String indentation, - String delimiter) { - + private String displayString (String inputString, String indentation, String delimiter) { int length = inputString.length(); StringBuffer buffer = new StringBuffer(length); - java.util.StringTokenizer tokenizer = new java.util.StringTokenizer( - inputString, "\n\r", true); //$NON-NLS-1$ - while (tokenizer.hasMoreTokens()) { + java.util.StringTokenizer tokenizer = + new java.util.StringTokenizer (inputString, "\n\r", true); //$NON-NLS-1$ + + while (tokenizer.hasMoreTokens ()) { + String token = tokenizer.nextToken (); - String token = tokenizer.nextToken(); if (token.equals("\r")) { //$NON-NLS-1$ buffer.append("\\r"); //$NON-NLS-1$ if (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); if (token.equals("\n")) { //$NON-NLS-1$ buffer.append("\\n"); //$NON-NLS-1$ - buffer.append("\' . " + delimiter); //$NON-NLS-1$ + buffer.append("\" . " + delimiter); //$NON-NLS-1$ buffer.append(indentation); buffer.append("\'"); //$NON-NLS-1$ continue; @@ -77,6 +76,7 @@ public class JavaStringAutoIndentStrategySQ extends } StringBuffer tokenBuffer = new StringBuffer(); + for (int i = 0; i < token.length(); i++) { char c = token.charAt(i); switch (c) { @@ -111,6 +111,7 @@ public class JavaStringAutoIndentStrategySQ extends } buffer.append(tokenBuffer); } + return buffer.toString(); } @@ -121,43 +122,122 @@ public class JavaStringAutoIndentStrategySQ extends * @param partitioning * the document partitioning */ - public JavaStringAutoIndentStrategySQ(String partitioning) { + public JavaStringAutoIndentStrategySQ (String partitioning) { super(); fPartitioning = partitioning; } + /** + * Check whether the 'text' is one of the allowed line delimiters (e.g. \n \r) + * + * @param document The document object for which we do the check + * @param text The text with the possible delimiter + * @return + * - true if 'text' is a valid delimiter + * - false if 'text' is not a valid delimiter + */ private boolean isLineDelimiter(IDocument document, String text) { String[] delimiters = document.getLegalLineDelimiters(); - if (delimiters != null) + + if (delimiters != null) { return TextUtilities.equals(delimiters, text) > -1; + } + return false; } + /** + * Get back the indentation of the line to which the offset belongs. + * + * It searches the first occurrence of a white space (non space/non tab) + * + * @param document + * @param offset + * @return + * @throws BadLocationException + */ private String getLineIndentation(IDocument document, int offset) throws BadLocationException { + String indentation = ""; + int start; + int end; + int length; // find start of line - int adjustedOffset = (offset == document.getLength() ? offset - 1 - : offset); - IRegion line = document.getLineInformationOfOffset(adjustedOffset); - int start = line.getOffset(); + int adjustedOffset = (offset == document.getLength() ? offset - 1 : offset);// Check whether the offset is not at the end of file + IRegion line = document.getLineInformationOfOffset (adjustedOffset); // Get the start and the length of the line + + start = line.getOffset (); // Get the start of the line + end = findStringStart (document, start, offset); + + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore(); + length = end - start; + + if (preferenceStore.getBoolean (PreferenceConstants.EDITOR_SPACES_FOR_TABS)) { // Indentation with spaces only + if (length > 0) { + indentation = String.format ("%" + length + "s", ""); + } + } + else { // Indentation with tabs + if (length > 0) { + int spaces; + int tabs; + int tabWidth = preferenceStore.getInt (PreferenceConstants.EDITOR_TAB_WIDTH); - // find white spaces - int end = findEndOfWhiteSpace(document, start, offset); + tabs = length / tabWidth; + spaces = length % tabWidth; + + indentation = new String (new char[tabs]).replace('\0', '\t'); + indentation += String.format ("%" + spaces + "s", ""); + } + } + + return indentation; + } + + /** + * Return the position of the string start (first occurrence of a quote or double quote) + * + * @param offset + * @param start + * @param document + * @return + */ + private int findStringStart(IDocument document, int offset, int end) throws BadLocationException { + while (offset < end) { + char c = document.getChar (offset); + + if ((c == '\'') || (c == '\"')) { + return offset; + } + + offset++; + } - return document.get(start, end - start); + return end; } - private String getModifiedText(String string, String indentation, - String delimiter) throws BadLocationException { + /** + * + * @param string + * @param indentation + * @param delimiter + * @return + * @throws BadLocationException + */ + private String getModifiedText (String string, String indentation, String delimiter) + throws BadLocationException { return displayString(string, indentation, delimiter); } - private void javaStringIndentAfterNewLine(IDocument document, - DocumentCommand command) throws BadLocationException { - - ITypedRegion partition = TextUtilities.getPartition(document, - fPartitioning, command.offset, false); + /** + * + * @param document + * @param command + * @throws BadLocationException + */ + private void javaStringIndentAfterNewLine (IDocument document, DocumentCommand command) throws BadLocationException { + ITypedRegion partition = TextUtilities.getPartition (document, fPartitioning, command.offset, false); int offset = partition.getOffset(); int length = partition.getLength(); @@ -170,38 +250,49 @@ public class JavaStringAutoIndentStrategySQ extends return; } - if (command.offset == offset + length - && document.getChar(offset + length - 1) == '\'') + if ((command.offset == offset + length) && + (document.getChar (offset + length - 1) == '\'')) { return; + } String indentation = getLineIndentation(document, command.offset); String delimiter = TextUtilities.getDefaultLineDelimiter(document); IRegion line = document.getLineInformationOfOffset(offset); - String string = document.get(line.getOffset(), offset - - line.getOffset()); - if (string.trim().length() != 0) + String string = document.get (line.getOffset(), + offset - line.getOffset()); // Get the current line as string +/* + if (string.trim().length() != 0) { indentation += String.valueOf("\t\t"); //$NON-NLS-1$ + } +*/ + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault ().getPreferenceStore(); - IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault() - .getPreferenceStore(); - if (isLineDelimiter(document, command.text)) + if (isLineDelimiter (document, command.text)) { command.text = "\' ." + command.text + indentation + "\'"; //$NON-NLS-1$//$NON-NLS-2$ - else if (command.text.length() > 1 - && preferenceStore - .getBoolean(PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ)) + } + else if ((command.text.length() > 1) && + preferenceStore.getBoolean (PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ)) { command.text = getModifiedText(command.text, indentation, delimiter); + } } + /** + * + * @return + */ private boolean isSmartMode() { IWorkbenchPage page = PHPeclipsePlugin.getActivePage(); + if (page != null) { IEditorPart part = page.getActiveEditor(); + if (part instanceof ITextEditorExtension3) { ITextEditorExtension3 extension = (ITextEditorExtension3) part; return extension.getInsertMode() == ITextEditorExtension3.SMART_INSERT; } } + return false; } @@ -209,19 +300,18 @@ public class JavaStringAutoIndentStrategySQ extends * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, * DocumentCommand) */ - public void customizeDocumentCommand(IDocument document, - DocumentCommand command) { + public void customizeDocumentCommand(IDocument document, DocumentCommand command) { try { - if (command.length != 0 || command.text == null) + if ((command.length != 0) || + (command.text == null)) { return; + } - IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault() - .getPreferenceStore(); + IPreferenceStore preferenceStore = PHPeclipsePlugin.getDefault().getPreferenceStore(); - if (preferenceStore - .getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ) - && isSmartMode()) + if (preferenceStore.getBoolean (PreferenceConstants.EDITOR_WRAP_STRINGS_SQ) && isSmartMode()) { javaStringIndentAfterNewLine(document, command); + } } catch (BadLocationException e) { }