Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2014-10-22 17:32:43 -0400
committerSergey Prigogin2014-10-27 13:16:34 -0400
commit1be15476ed99918ea8a9674d8ceb7884a430fc69 (patch)
treec858cd57486a1930e44b7bc7be41b0aa4f9cc93c /org.eclipse.jface.text
parent19a57c077903e4a962f812a2208fce2597e8ec7c (diff)
downloadeclipse.platform.text-1be15476ed99918ea8a9674d8ceb7884a430fc69.tar.gz
eclipse.platform.text-1be15476ed99918ea8a9674d8ceb7884a430fc69.tar.xz
eclipse.platform.text-1be15476ed99918ea8a9674d8ceb7884a430fc69.zip
Bug 441448 - Give plugins implementing code formatters more freedom whenI20141029-2000I20141029-0800I20141028-2000I20141028-0800
dealing with empty selection The change introduces a new method SourceViewer.createFormattingContext(int, int) that gives overriders more freedom compared to the createFormattingContext method without parameters. The default implementation of the new method calls the old one and interprets an empty selection as formatting of the entire document. Change-Id: I210688466ff07a3a90eab8e1ecc16b4c6e01d088 Signed-off-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Diffstat (limited to 'org.eclipse.jface.text')
-rw-r--r--org.eclipse.jface.text/META-INF/MANIFEST.MF2
-rw-r--r--org.eclipse.jface.text/pom.xml2
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/MultiPassContentFormatter.java14
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java49
4 files changed, 54 insertions, 13 deletions
diff --git a/org.eclipse.jface.text/META-INF/MANIFEST.MF b/org.eclipse.jface.text/META-INF/MANIFEST.MF
index 2afb2a767..b863df0bb 100644
--- a/org.eclipse.jface.text/META-INF/MANIFEST.MF
+++ b/org.eclipse.jface.text/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.jface.text
-Bundle-Version: 3.9.100.qualifier
+Bundle-Version: 3.10.0.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package:
diff --git a/org.eclipse.jface.text/pom.xml b/org.eclipse.jface.text/pom.xml
index ff340e563..eef8fc2e9 100644
--- a/org.eclipse.jface.text/pom.xml
+++ b/org.eclipse.jface.text/pom.xml
@@ -18,6 +18,6 @@
</parent>
<groupId>org.eclipse.jface</groupId>
<artifactId>org.eclipse.jface.text</artifactId>
- <version>3.9.100-SNAPSHOT</version>
+ <version>3.10.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/MultiPassContentFormatter.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/MultiPassContentFormatter.java
index 4ba610192..6965a18cc 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/MultiPassContentFormatter.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/formatter/MultiPassContentFormatter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Sergey Prigogin (Google) - bug 441448
*******************************************************************************/
package org.eclipse.jface.text.formatter;
@@ -177,9 +178,14 @@ public class MultiPassContentFormatter implements IContentFormatter, IContentFor
try {
- final int delta= offset - document.getLineInformationOfOffset(offset).getOffset();
- offset -= delta;
- length += delta;
+ if (length != 0) {
+ // Extend the selection to the beginning of line if it is not empty.
+ // An empty selection must remain empty since it may be treated in
+ // a special way by the formatter.
+ final int delta= offset - document.getLineInformationOfOffset(offset).getOffset();
+ offset -= delta;
+ length += delta;
+ }
} catch (BadLocationException exception) {
// Do nothing
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java
index fc2d11fdb..43b2a92d4 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/source/SourceViewer.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* Tom Eicher (Avaloq Evolution AG) - block selection mode
* Tom Hofmann (Perspectix AG) - bug 297572
+ * Sergey Prigogin (Google) - bug 441448
*******************************************************************************/
package org.eclipse.jface.text.source;
@@ -816,6 +817,40 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi
}
/**
+ * Creates a new formatting context for a format operation. If the returned context has
+ * the {@link FormattingContextProperties#CONTEXT_REGION} property set to an {@link IRegion},
+ * the section of the document defined by that region is formatted, otherwise the whole
+ * document is formatted.
+ * <p>
+ * The default implementation calls {@link #createFormattingContext()} and sets
+ * the {@link FormattingContextProperties#CONTEXT_REGION} property if the selection is
+ * not empty. Overriding methods may implement a different logic, or return <code>null</code>
+ * to indicate that the formatting operation should not proceed.
+ * <p>
+ * Returning <code>null</code> may be used, for example, when the user clicks on
+ * the <b>Cancel</b> button in a dialog that, in case of an empty selection, asks the user
+ * whether formatting should be applied to the whole document or to the current statement.
+ * Please notice that returning <code>null</code> from this method cancels the already initiated
+ * formatting operation unlike {@link #canDoOperation(int)}, which is used for enabling and
+ * disabling formatting actions.
+ * <p>
+ * After the use of the context, clients are required to call its <code>dispose</code> method.
+ *
+ * @param selectionOffset the character offset of the selection in the document
+ * @param selectionLength the length of the selection
+ * @return The new formatting context, or <code>null</code> to cancel the formatting
+ * @since 3.10
+ */
+ protected IFormattingContext createFormattingContext(int selectionOffset, int selectionLength) {
+ IFormattingContext context= createFormattingContext();
+ if (selectionLength != 0) {
+ context.setProperty(FormattingContextProperties.CONTEXT_REGION,
+ new Region(selectionOffset, selectionLength));
+ }
+ return context;
+ }
+
+ /**
* Position storing block selection information in order to maintain a column selection.
*
* @since 3.5
@@ -969,13 +1004,13 @@ public class SourceViewer extends TextViewer implements ISourceViewer, ISourceVi
if (fContentFormatter instanceof IContentFormatterExtension) {
final IContentFormatterExtension extension= (IContentFormatterExtension) fContentFormatter;
- context= createFormattingContext();
- if (selection.y == 0) {
- context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
- } else {
- context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
- context.setProperty(FormattingContextProperties.CONTEXT_REGION, new Region(selection.x, selection.y));
+ context= createFormattingContext(selection.x, selection.y);
+ if (context == null) {
+ return;
}
+ Object region = context.getProperty(FormattingContextProperties.CONTEXT_REGION);
+ context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT,
+ Boolean.valueOf(region == null));
extension.format(document, context);
} else {
IRegion r;

Back to the top