From d186c6f7f8de97000d275016b6192fe9bd5d819f Mon Sep 17 00:00:00 2001 From: jsurfer Date: Sun, 12 Sep 2004 06:52:56 +0000 Subject: [PATCH] added StringAutoIndentStrategy for single quoted strings --- .../ui/preferences/ColorSettingPreviewCode.txt | 14 +- .../ui/preferences/JavaEditorPreferencePage.java | 35 +++- .../ui/preferences/PreferencesMessages.properties | 6 +- .../ui/text/java/JavaStringAutoIndentStrategy.java | 206 -------------------- .../text/java/JavaStringAutoIndentStrategyDQ.java | 206 ++++++++++++++++++++ .../text/java/JavaStringAutoIndentStrategySQ.java | 206 ++++++++++++++++++++ .../sourceforge/phpdt/ui/PreferenceConstants.java | 30 +++- .../ui/text/PHPSourceViewerConfiguration.java | 14 +- .../phpeclipse/phpeditor/PHPUnitEditor.java | 2 +- 9 files changed, 483 insertions(+), 236 deletions(-) delete mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategy.java create mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java create mode 100644 net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/ColorSettingPreviewCode.txt b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/ColorSettingPreviewCode.txt index d7615dd..c89390a 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/ColorSettingPreviewCode.txt +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/ColorSettingPreviewCode.txt @@ -1,13 +1,19 @@ -ClassName. - * {@link com.yourCompany.aPackage.SuperClass} + * * @author author */ class ClassName extends SuperClass { /* This comment may span multiple lines. */ - $integer= 0; + private $integer = 0; // This comment may span only this line - $string= "zero"; + private $string = "zero"; + + public function info() { + # call a predefined php function + phpinfo(); + return "test"; + } } ?> \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/JavaEditorPreferencePage.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/JavaEditorPreferencePage.java index 89a22b3..69d820c 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/JavaEditorPreferencePage.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/JavaEditorPreferencePage.java @@ -238,7 +238,9 @@ public class JavaEditorPreferencePage extends PreferencePage implements IWorkben // private Button fAddJavaDocTagsButton; - private Button fEscapeStringsButton; + private Button fEscapeStringsButtonDQ; + + private Button fEscapeStringsButtonSQ; // private Button fGuessMethodArgumentsButton; private SourceViewer fPreviewViewer; @@ -455,9 +457,13 @@ public class JavaEditorPreferencePage extends PreferencePage implements IWorkben // overlayKeys.add(new OverlayPreferenceStore.OverlayKey( // OverlayPreferenceStore.BOOLEAN, // PreferenceConstants.EDITOR_CLOSE_JAVADOCS)); - overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_WRAP_STRINGS)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_WRAP_STRINGS_DQ)); overlayKeys - .add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ESCAPE_STRINGS)); + .add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_WRAP_STRINGS_SQ)); + overlayKeys + .add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ)); + overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS)); overlayKeys @@ -1053,15 +1059,21 @@ public class JavaEditorPreferencePage extends PreferencePage implements IWorkben group.setLayout(layout); group.setText(PreferencesMessages.getString("JavaEditorPreferencePage.typing.description")); //$NON-NLS-1$ - label = PreferencesMessages.getString("JavaEditorPreferencePage.wrapStrings"); - //$NON-NLS-1$ - Button button = addCheckBox(group, label, PreferenceConstants.EDITOR_WRAP_STRINGS, 1); + label = PreferencesMessages.getString("JavaEditorPreferencePage.wrapStringsDQ");//$NON-NLS-1$ + Button button = addCheckBox(group, label, PreferenceConstants.EDITOR_WRAP_STRINGS_DQ, 1); - label = PreferencesMessages.getString("JavaEditorPreferencePage.escapeStrings"); - //$NON-NLS-1$ - fEscapeStringsButton = addCheckBox(group, label, PreferenceConstants.EDITOR_ESCAPE_STRINGS, 1); - createDependency(button, fEscapeStringsButton); + label = PreferencesMessages.getString("JavaEditorPreferencePage.escapeStringsDQ");//$NON-NLS-1$ + fEscapeStringsButtonDQ = addCheckBox(group, label, PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ, 1); + createDependency(button, fEscapeStringsButtonDQ); + label = PreferencesMessages.getString("JavaEditorPreferencePage.wrapStringsSQ");//$NON-NLS-1$ + addCheckBox(group, label, PreferenceConstants.EDITOR_WRAP_STRINGS_SQ, 1); + + label = PreferencesMessages.getString("JavaEditorPreferencePage.escapeStringsSQ"); + //$NON-NLS-1$ + fEscapeStringsButtonSQ = addCheckBox(group, label, PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ, 1); + createDependency(button, fEscapeStringsButtonSQ); + label = PreferencesMessages.getString("JavaEditorPreferencePage.smartPaste"); //$NON-NLS-1$ addCheckBox(group, label, PreferenceConstants.EDITOR_SMART_PASTE, 1); @@ -1426,7 +1438,8 @@ public class JavaEditorPreferencePage extends PreferencePage implements IWorkben // boolean closeJavaDocs = fOverlayStore // .getBoolean(PreferenceConstants.EDITOR_CLOSE_JAVADOCS); // fAddJavaDocTagsButton.setEnabled(closeJavaDocs); - fEscapeStringsButton.setEnabled(fOverlayStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS)); + fEscapeStringsButtonDQ.setEnabled(fOverlayStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_DQ)); + fEscapeStringsButtonSQ.setEnabled(fOverlayStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ)); // boolean fillMethodArguments= // fOverlayStore.getBoolean(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES); // fGuessMethodArgumentsButton.setEnabled(fillMethodArguments); diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/PreferencesMessages.properties b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/PreferencesMessages.properties index 4997e09..ccb6825 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/PreferencesMessages.properties +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/preferences/PreferencesMessages.properties @@ -178,8 +178,10 @@ JavaEditorPreferencePage.closeStringsSQ= Close &single quoted strings JavaEditorPreferencePage.closeBrackets= Close &brackets and parenthesis JavaEditorPreferencePage.closeBraces= Cl&ose braces JavaEditorPreferencePage.closeJavaDocs= Close PHP&docs and comments -JavaEditorPreferencePage.wrapStrings= Wra&p PHP strings -JavaEditorPreferencePage.escapeStrings= &Escape text when pasting into a string literal +JavaEditorPreferencePage.wrapStringsDQ= Wra&p double quoted PHP strings +JavaEditorPreferencePage.escapeStringsDQ= &Escape text when pasting into a double quoted PHP string +JavaEditorPreferencePage.wrapStringsSQ= Wra&p single quoted PHP strings +JavaEditorPreferencePage.escapeStringsSQ= &Escape text when pasting into a single quoted PHP string JavaEditorPreferencePage.addJavaDocTags= Add PHPdoc &tags JavaEditorPreferencePage.smartPaste= Pasting fo&r correct indentation diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategy.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategy.java deleted file mode 100644 index ea00344..0000000 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategy.java +++ /dev/null @@ -1,206 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2003 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package net.sourceforge.phpdt.internal.ui.text.java; - -import net.sourceforge.phpdt.ui.PreferenceConstants; -import net.sourceforge.phpeclipse.PHPeclipsePlugin; - -import org.eclipse.jface.preference.IPreferenceStore; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.jface.text.DefaultAutoIndentStrategy; -import org.eclipse.jface.text.DocumentCommand; -import org.eclipse.jface.text.IDocument; -import org.eclipse.jface.text.IRegion; -import org.eclipse.jface.text.ITypedRegion; -import org.eclipse.jface.text.TextUtilities; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.IWorkbenchPage; -import org.eclipse.ui.texteditor.ITextEditorExtension3; - - -/** - * Auto indent strategy for java strings - */ -public class JavaStringAutoIndentStrategy extends DefaultAutoIndentStrategy { - - private String fPartitioning; - - /** - * The input string doesn't contain any line delimiter. - * - * @param inputString the given input string - * @return the displayable string. - */ - 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()) { - token = tokenizer.nextToken(); - if (token.equals("\n")) { //$NON-NLS-1$ - buffer.append("\\n"); //$NON-NLS-1$ - buffer.append("\" . " + delimiter); //$NON-NLS-1$ - buffer.append(indentation); - buffer.append("\""); //$NON-NLS-1$ - continue; - } else { - buffer.append("\" . " + delimiter); //$NON-NLS-1$ - buffer.append(indentation); - buffer.append("\""); //$NON-NLS-1$ - } - } else { - continue; - } - } else if (token.equals("\n")) { //$NON-NLS-1$ - buffer.append("\\n"); //$NON-NLS-1$ - buffer.append("\" . " + delimiter); //$NON-NLS-1$ - buffer.append(indentation); - buffer.append("\""); //$NON-NLS-1$ - continue; - } - - StringBuffer tokenBuffer = new StringBuffer(); - for (int i = 0; i < token.length(); i++){ - char c = token.charAt(i); - switch (c) { - case '\r' : - tokenBuffer.append("\\r"); //$NON-NLS-1$ - break; - case '\n' : - tokenBuffer.append("\\n"); //$NON-NLS-1$ - break; - case '\b' : - tokenBuffer.append("\\b"); //$NON-NLS-1$ - break; - case '\t' : - // keep tabs verbatim - tokenBuffer.append("\t"); //$NON-NLS-1$ - break; - case '\f' : - tokenBuffer.append("\\f"); //$NON-NLS-1$ - break; - case '\"' : - tokenBuffer.append("\\\""); //$NON-NLS-1$ - break; - case '\'' : - tokenBuffer.append("\\'"); //$NON-NLS-1$ - break; - case '\\' : - tokenBuffer.append("\\\\"); //$NON-NLS-1$ - break; - default : - tokenBuffer.append(c); - } - } - buffer.append(tokenBuffer); - } - return buffer.toString(); - } - - /** - * Creates a new Java string auto indent strategy for the given document partitioning. - * - * @param partitioning the document partitioning - */ - public JavaStringAutoIndentStrategy(String partitioning) { - super(); - fPartitioning= partitioning; - } - - private boolean isLineDelimiter(IDocument document, String text) { - String[] delimiters= document.getLegalLineDelimiters(); - if (delimiters != null) - return TextUtilities.equals(delimiters, text) > -1; - return false; - } - - private String getLineIndentation(IDocument document, int offset) throws BadLocationException { - - // 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); - - return document.get(start, end - start); - } - - 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); - int offset= partition.getOffset(); - int length= partition.getLength(); - - if (command.offset == offset) { - // we are really just before the string partition -> feet the event through the java indenter - // new JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document, command); - return; - } - - 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$ - - 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)) - command.text= getModifiedText(command.text, indentation, delimiter); - } - - 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; - } - - /* - * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand) - */ - public void customizeDocumentCommand(IDocument document, DocumentCommand command) { - try { - if (command.length != 0 || command.text == null) - return; - - IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore(); - - if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS) && isSmartMode()) - javaStringIndentAfterNewLine(document, command); - - } catch (BadLocationException e) { - } - } -} 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 new file mode 100644 index 0000000..746691c --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategyDQ.java @@ -0,0 +1,206 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package net.sourceforge.phpdt.internal.ui.text.java; + +import net.sourceforge.phpdt.ui.PreferenceConstants; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DefaultAutoIndentStrategy; +import org.eclipse.jface.text.DocumentCommand; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.TextUtilities; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.texteditor.ITextEditorExtension3; + + +/** + * Auto indent strategy for java strings + */ +public class JavaStringAutoIndentStrategyDQ extends DefaultAutoIndentStrategy { + + private String fPartitioning; + + /** + * The input string doesn't contain any line delimiter. + * + * @param inputString the given input string + * @return the displayable string. + */ + 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()) { + token = tokenizer.nextToken(); + if (token.equals("\n")) { //$NON-NLS-1$ + buffer.append("\\n"); //$NON-NLS-1$ + buffer.append("\" . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\""); //$NON-NLS-1$ + continue; + } else { + buffer.append("\" . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\""); //$NON-NLS-1$ + } + } else { + continue; + } + } else if (token.equals("\n")) { //$NON-NLS-1$ + buffer.append("\\n"); //$NON-NLS-1$ + buffer.append("\" . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\""); //$NON-NLS-1$ + continue; + } + + StringBuffer tokenBuffer = new StringBuffer(); + for (int i = 0; i < token.length(); i++){ + char c = token.charAt(i); + switch (c) { + case '\r' : + tokenBuffer.append("\\r"); //$NON-NLS-1$ + break; + case '\n' : + tokenBuffer.append("\\n"); //$NON-NLS-1$ + break; + case '\b' : + tokenBuffer.append("\\b"); //$NON-NLS-1$ + break; + case '\t' : + // keep tabs verbatim + tokenBuffer.append("\t"); //$NON-NLS-1$ + break; + case '\f' : + tokenBuffer.append("\\f"); //$NON-NLS-1$ + break; + case '\"' : + tokenBuffer.append("\\\""); //$NON-NLS-1$ + break; + case '\'' : + tokenBuffer.append("\\'"); //$NON-NLS-1$ + break; + case '\\' : + tokenBuffer.append("\\\\"); //$NON-NLS-1$ + break; + default : + tokenBuffer.append(c); + } + } + buffer.append(tokenBuffer); + } + return buffer.toString(); + } + + /** + * Creates a new Java string auto indent strategy for the given document partitioning. + * + * @param partitioning the document partitioning + */ + public JavaStringAutoIndentStrategyDQ(String partitioning) { + super(); + fPartitioning= partitioning; + } + + private boolean isLineDelimiter(IDocument document, String text) { + String[] delimiters= document.getLegalLineDelimiters(); + if (delimiters != null) + return TextUtilities.equals(delimiters, text) > -1; + return false; + } + + private String getLineIndentation(IDocument document, int offset) throws BadLocationException { + + // 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); + + return document.get(start, end - start); + } + + 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); + int offset= partition.getOffset(); + int length= partition.getLength(); + + if (command.offset == offset) { + // we are really just before the string partition -> feet the event through the java indenter + // new JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document, command); + return; + } + + 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$ + + 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)) + command.text= getModifiedText(command.text, indentation, delimiter); + } + + 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; + } + + /* + * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand) + */ + public void customizeDocumentCommand(IDocument document, DocumentCommand command) { + try { + if (command.length != 0 || command.text == null) + return; + + IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore(); + + 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 new file mode 100644 index 0000000..52e4c96 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaStringAutoIndentStrategySQ.java @@ -0,0 +1,206 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package net.sourceforge.phpdt.internal.ui.text.java; + +import net.sourceforge.phpdt.ui.PreferenceConstants; +import net.sourceforge.phpeclipse.PHPeclipsePlugin; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DefaultAutoIndentStrategy; +import org.eclipse.jface.text.DocumentCommand; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.ITypedRegion; +import org.eclipse.jface.text.TextUtilities; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.texteditor.ITextEditorExtension3; + + +/** + * Auto indent strategy for java strings + */ +public class JavaStringAutoIndentStrategySQ extends DefaultAutoIndentStrategy { + + private String fPartitioning; + + /** + * The input string doesn't contain any line delimiter. + * + * @param inputString the given input string + * @return the displayable string. + */ + 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()) { + token = tokenizer.nextToken(); + if (token.equals("\n")) { //$NON-NLS-1$ + buffer.append("\\n"); //$NON-NLS-1$ + buffer.append("\' . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\'"); //$NON-NLS-1$ + continue; + } else { + buffer.append("\' . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\'"); //$NON-NLS-1$ + } + } else { + continue; + } + } else if (token.equals("\n")) { //$NON-NLS-1$ + buffer.append("\\n"); //$NON-NLS-1$ + buffer.append("\' . " + delimiter); //$NON-NLS-1$ + buffer.append(indentation); + buffer.append("\'"); //$NON-NLS-1$ + continue; + } + + StringBuffer tokenBuffer = new StringBuffer(); + for (int i = 0; i < token.length(); i++){ + char c = token.charAt(i); + switch (c) { + case '\r' : + tokenBuffer.append("\\r"); //$NON-NLS-1$ + break; + case '\n' : + tokenBuffer.append("\\n"); //$NON-NLS-1$ + break; + case '\b' : + tokenBuffer.append("\\b"); //$NON-NLS-1$ + break; + case '\t' : + // keep tabs verbatim + tokenBuffer.append("\t"); //$NON-NLS-1$ + break; + case '\f' : + tokenBuffer.append("\\f"); //$NON-NLS-1$ + break; + case '\"' : + tokenBuffer.append("\\\""); //$NON-NLS-1$ + break; + case '\'' : + tokenBuffer.append("\\'"); //$NON-NLS-1$ + break; + case '\\' : + tokenBuffer.append("\\\\"); //$NON-NLS-1$ + break; + default : + tokenBuffer.append(c); + } + } + buffer.append(tokenBuffer); + } + return buffer.toString(); + } + + /** + * Creates a new Java string auto indent strategy for the given document partitioning. + * + * @param partitioning the document partitioning + */ + public JavaStringAutoIndentStrategySQ(String partitioning) { + super(); + fPartitioning= partitioning; + } + + private boolean isLineDelimiter(IDocument document, String text) { + String[] delimiters= document.getLegalLineDelimiters(); + if (delimiters != null) + return TextUtilities.equals(delimiters, text) > -1; + return false; + } + + private String getLineIndentation(IDocument document, int offset) throws BadLocationException { + + // 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); + + return document.get(start, end - start); + } + + 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); + int offset= partition.getOffset(); + int length= partition.getLength(); + + if (command.offset == offset) { + // we are really just before the string partition -> feet the event through the java indenter + // new JavaAutoIndentStrategy(fPartitioning).customizeDocumentCommand(document, command); + return; + } + + 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$ + + 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_SQ)) + command.text= getModifiedText(command.text, indentation, delimiter); + } + + 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; + } + + /* + * @see org.eclipse.jface.text.IAutoIndentStrategy#customizeDocumentCommand(IDocument, DocumentCommand) + */ + public void customizeDocumentCommand(IDocument document, DocumentCommand command) { + try { + if (command.length != 0 || command.text == null) + return; + + IPreferenceStore preferenceStore= PHPeclipsePlugin.getDefault().getPreferenceStore(); + + if (preferenceStore.getBoolean(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ) && isSmartMode()) + javaStringIndentAfterNewLine(document, command); + + } catch (BadLocationException e) { + } + } +} diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/PreferenceConstants.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/PreferenceConstants.java index a4d6700..ec2fb75 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/PreferenceConstants.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/PreferenceConstants.java @@ -769,7 +769,7 @@ public class PreferenceConstants { *

* @since 2.1 */ - public final static String EDITOR_WRAP_STRINGS= "wrapStrings"; //$NON-NLS-1$ + public final static String EDITOR_WRAP_STRINGS_DQ= "wrapStringsDQ"; //$NON-NLS-1$ /** * A named preference that controls whether the 'escape strings' feature is @@ -779,7 +779,27 @@ public class PreferenceConstants { *

* @since 3.0 */ - public final static String EDITOR_ESCAPE_STRINGS= "escapeStrings"; //$NON-NLS-1$ + public final static String EDITOR_ESCAPE_STRINGS_DQ= "escapeStringsDQ"; //$NON-NLS-1$ + /** + * A named preference that controls whether the 'wrap strings' feature is + * enabled. + *

+ * Value is of type Boolean. + *

+ * @since 2.1 + */ + public final static String EDITOR_WRAP_STRINGS_SQ= "wrapStringsDQ"; //$NON-NLS-1$ + + /** + * A named preference that controls whether the 'escape strings' feature is + * enabled. + *

+ * Value is of type Boolean. + *

+ * @since 3.0 + */ + public final static String EDITOR_ESCAPE_STRINGS_SQ= "escapeStringsSQ"; //$NON-NLS-1$ + /** * A named preference that controls if content assist inserts the common * prefix of all proposals before presenting choices. @@ -2395,8 +2415,10 @@ public final static String EDITOR_TEXT_FONT= "net.sourceforge.phpdt.ui.editors.t store.setDefault(PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP, true); store.setDefault(PreferenceConstants.EDITOR_CLOSE_BRACES, true); store.setDefault(PreferenceConstants.EDITOR_CLOSE_JAVADOCS, true); - store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS, true); - store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS, false); + store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS_DQ, true); + store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS_DQ, false); + store.setDefault(PreferenceConstants.EDITOR_WRAP_STRINGS_SQ, true); + store.setDefault(PreferenceConstants.EDITOR_ESCAPE_STRINGS_SQ, false); store.setDefault(PreferenceConstants.EDITOR_ADD_JAVADOC_TAGS, true); store.setDefault(PreferenceConstants.EDITOR_FORMAT_JAVADOCS, true); store.setDefault(PreferenceConstants.EDITOR_DISABLE_OVERWRITE_MODE, false); diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/text/PHPSourceViewerConfiguration.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/text/PHPSourceViewerConfiguration.java index 735dcec..d1e9583 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/text/PHPSourceViewerConfiguration.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/ui/text/PHPSourceViewerConfiguration.java @@ -19,7 +19,6 @@ import net.sourceforge.phpdt.internal.ui.text.ContentAssistPreference; import net.sourceforge.phpdt.internal.ui.text.HTMLTextPresenter; import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions; import net.sourceforge.phpdt.internal.ui.text.JavaAnnotationHover; -import net.sourceforge.phpdt.internal.ui.text.JavaColorManager; import net.sourceforge.phpdt.internal.ui.text.JavaCompositeReconcilingStrategy; import net.sourceforge.phpdt.internal.ui.text.JavaElementProvider; import net.sourceforge.phpdt.internal.ui.text.JavaOutlineInformationControl; @@ -27,7 +26,8 @@ import net.sourceforge.phpdt.internal.ui.text.JavaPresentationReconciler; import net.sourceforge.phpdt.internal.ui.text.JavaReconciler; import net.sourceforge.phpdt.internal.ui.text.PreferencesAdapter; import net.sourceforge.phpdt.internal.ui.text.java.JavaFormattingStrategy; -import net.sourceforge.phpdt.internal.ui.text.java.JavaStringAutoIndentStrategy; +import net.sourceforge.phpdt.internal.ui.text.java.JavaStringAutoIndentStrategyDQ; +import net.sourceforge.phpdt.internal.ui.text.java.JavaStringAutoIndentStrategySQ; import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverDescriptor; import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy; import net.sourceforge.phpdt.internal.ui.text.java.hover.JavaInformationProvider; @@ -36,7 +36,6 @@ import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor; import net.sourceforge.phpdt.ui.PreferenceConstants; import net.sourceforge.phpeclipse.IPreferenceConstants; import net.sourceforge.phpeclipse.PHPeclipsePlugin; -import net.sourceforge.phpeclipse.phpeditor.PHPEditor; import net.sourceforge.phpeclipse.phpeditor.html.HTMLFormattingStrategy; import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor; import net.sourceforge.phpeclipse.phpeditor.php.PHPAutoIndentStrategy; @@ -45,9 +44,6 @@ import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor; import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner; import net.sourceforge.phpeclipse.phpeditor.php.PHPDoubleClickSelector; import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner; -import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner; -import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner; -import net.sourceforge.phpeclipse.phpeditor.util.PHPColorProvider; import net.sourceforge.phpeclipse.xml.ui.XMLPlugin; import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLConfiguration; import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLPartitionScanner; @@ -413,8 +409,10 @@ public class PHPSourceViewerConfiguration extends SourceViewerConfiguration { * (non-Javadoc) Method declared on SourceViewerConfiguration */ public IAutoIndentStrategy getAutoIndentStrategy(ISourceViewer sourceViewer, String contentType) { - if (IPHPPartitions.PHP_STRING_DQ.equals(contentType) || IPHPPartitions.PHP_STRING_SQ.equals(contentType)) - return new JavaStringAutoIndentStrategy(getConfiguredDocumentPartitioning(sourceViewer)); + if (IPHPPartitions.PHP_STRING_DQ.equals(contentType)) + return new JavaStringAutoIndentStrategyDQ(getConfiguredDocumentPartitioning(sourceViewer)); + if (IPHPPartitions.PHP_STRING_SQ.equals(contentType)) + return new JavaStringAutoIndentStrategySQ(getConfiguredDocumentPartitioning(sourceViewer)); return (PHPDocumentPartitioner.PHP_TEMPLATE_DATA.equals(contentType) || PHPDocumentPartitioner.PHP_SCRIPT_CODE.equals(contentType) || IDocument.DEFAULT_CONTENT_TYPE.equals(contentType) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java index 5aa3cca..ae9f4ca 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/PHPUnitEditor.java @@ -1305,7 +1305,7 @@ public class PHPUnitEditor extends PHPEditor { //implements private final static String CLOSE_STRINGS_SQ_PHP = PreferenceConstants.EDITOR_CLOSE_STRINGS_SQ_PHP; /** Preference key for automatically wrapping Java strings */ - private final static String WRAP_STRINGS = PreferenceConstants.EDITOR_WRAP_STRINGS; +// private final static String WRAP_STRINGS = PreferenceConstants.EDITOR_WRAP_STRINGS_DQ; /** Preference key for automatically closing brackets and parenthesis */ private final static String CLOSE_BRACKETS_PHP = PreferenceConstants.EDITOR_CLOSE_BRACKETS_PHP; -- 1.7.1