From bd19266880a2927d7bc84326cac2b72b77bedfcb Mon Sep 17 00:00:00 2001 From: khartlage Date: Sat, 26 Jun 2004 17:05:11 +0000 Subject: [PATCH 1/1] Fixed bug in CodeFormatter; for partial formatting select complete range --- .../phpdt/internal/formatter/CodeFormatter.java | 10 +++ .../ui/text/java/JavaFormattingStrategy.java | 79 ++++++++++---------- .../phpeclipse/phpeditor/JavaSourceViewer.java | 8 ++- 3 files changed, 58 insertions(+), 39 deletions(-) diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java index 7efbade..e4ec3cf 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/formatter/CodeFormatter.java @@ -15,14 +15,22 @@ import java.io.StringReader; import java.util.Hashtable; import java.util.Locale; import java.util.Map; + import net.sourceforge.phpdt.core.ICodeFormatter; import net.sourceforge.phpdt.core.compiler.CharOperation; import net.sourceforge.phpdt.core.compiler.ITerminalSymbols; import net.sourceforge.phpdt.core.compiler.InvalidInputException; import net.sourceforge.phpdt.internal.compiler.ConfigurableOption; import net.sourceforge.phpdt.internal.compiler.parser.Scanner; +import net.sourceforge.phpdt.internal.corext.codemanipulation.StubUtility; +import net.sourceforge.phpdt.internal.corext.util.Strings; import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions; import net.sourceforge.phpdt.internal.formatter.impl.SplitLine; +import net.sourceforge.phpdt.internal.ui.preferences.CodeFormatterPreferencePage; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.formatter.IContentFormatterExtension; +import org.eclipse.jface.text.formatter.IFormattingContext; /** *

How to format a piece of code ?

* */ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { + // IContentFormatterExtension { public FormatterOptions options; /** * Represents a block in the constructions stack. @@ -2536,4 +2545,5 @@ public class CodeFormatter implements ITerminalSymbols, ICodeFormatter { public void setInitialIndentationLevel(int newIndentationLevel) { this.initialIndentationLevel = currentLineIndentationLevel = indentationLevel = newIndentationLevel; } + } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaFormattingStrategy.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaFormattingStrategy.java index 264938e..56ba202 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaFormattingStrategy.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/java/JavaFormattingStrategy.java @@ -1,6 +1,5 @@ /* - * (c) Copyright IBM Corp. 2000, 2001. - * All Rights Reserved. + * (c) Copyright IBM Corp. 2000, 2001. All Rights Reserved. */ package net.sourceforge.phpdt.internal.ui.text.java; @@ -15,40 +14,44 @@ import org.eclipse.jface.text.formatter.IFormattingStrategy; import org.eclipse.jface.text.source.ISourceViewer; public class JavaFormattingStrategy implements IFormattingStrategy { - - private String fInitialIndentation; - private ISourceViewer fViewer; - - public JavaFormattingStrategy(ISourceViewer viewer) { - fViewer = viewer; - } - - /** - * @see IFormattingStrategy#formatterStarts(String) - */ - public void formatterStarts(String initialIndentation) { - fInitialIndentation= initialIndentation; - } - - /** - * @see IFormattingStrategy#formatterStops() - */ - public void formatterStops() { - } - - /** - * @see IFormattingStrategy#format(String, boolean, String, int[]) - */ - public String format(String content, boolean isLineStart, String indentation, int[] positions) { - ICodeFormatter formatter= ToolFactory.createCodeFormatter(); - - IDocument doc= fViewer.getDocument(); - String lineDelimiter= StubUtility.getLineDelimiterFor(doc); - - int indent= 0; - if (fInitialIndentation != null) { - indent= Strings.computeIndent(fInitialIndentation, CodeFormatterPreferencePage.getTabSize()); - } - return formatter.format(content, indent, positions, lineDelimiter); - } + + private String fInitialIndentation; + + private ISourceViewer fViewer; + + public JavaFormattingStrategy(ISourceViewer viewer) { + fViewer = viewer; + } + + /** + * @see IFormattingStrategy#formatterStarts(String) + */ + public void formatterStarts(String initialIndentation) { + fInitialIndentation = initialIndentation; + } + + /** + * @see IFormattingStrategy#formatterStops() + */ + public void formatterStops() { + } + + /** + * @see IFormattingStrategy#format(String, boolean, String, int[]) + */ + public String format(String content, boolean isLineStart, String indentation, + int[] positions) { + ICodeFormatter formatter = ToolFactory.createCodeFormatter(); + + IDocument doc = fViewer.getDocument(); + String lineDelimiter = StubUtility.getLineDelimiterFor(doc); + + int indent = 0; + if (fInitialIndentation != null) { + indent = Strings.computeIndent(fInitialIndentation, + CodeFormatterPreferencePage.getTabSize()); + } + + return formatter.format(content, indent, positions, lineDelimiter); + } } \ No newline at end of file diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java index 2c2f4f4..e3e75ac 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/phpeditor/JavaSourceViewer.java @@ -38,7 +38,7 @@ import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants; public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener { - + /** * Text operation code for requesting the outline for the current input. */ @@ -134,6 +134,12 @@ public class JavaSourceViewer extends ProjectionViewer implements IPropertyChang case SHOW_HIERARCHY: // fHierarchyPresenter.showInformation(); return; + case FORMAT: + Point point = getSelectedRange(); + if (point.y==0) { + setSelectedRange(0, getDocument().getLength()); + } + break; } super.doOperation(operation); -- 1.7.1