| Index: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java
|
| diff --git a/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java
|
| index e9d2c6d6bd9b3bed2c6d91aa8dba0a30bb653066..59199f0b28240d93a61869e4dab553e51c9b6c99 100644
|
| --- a/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java
|
| +++ b/editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java
|
| @@ -181,42 +181,43 @@ public class DartFormatter {
|
| }
|
|
|
| public static class DartStyleRunner {
|
| + public static Point formatFile(IFile file, final Point selection, final IProgressMonitor monitor)
|
| + throws IOException, CoreException {
|
| + final String fileName = file.getName();
|
| + final String sourcePath = file.getRawLocation().makeAbsolute().toOSString();
|
|
|
| - public static void formatFile(final IFile file, final Point selection,
|
| - final IProgressMonitor monitor) throws IOException, CoreException {
|
| + final CompilationUnitChange change = new CompilationUnitChange(fileName, file);
|
| + change.setEdit(new MultiTextEdit());
|
| + change.initializeValidationData(monitor);
|
| + change.setSaveMode(TextFileChange.LEAVE_DIRTY);
|
|
|
| - final String sourcePath = file.getRawLocation().makeAbsolute().toOSString();
|
| - final CompilationUnitChange[] change = new CompilationUnitChange[1];
|
| + final Point newSelection = new Point(-1, 0);
|
|
|
| ExecutionUtils.runLog(new RunnableEx() {
|
| @Override
|
| public void run() throws Exception {
|
| -
|
| - int selectionStart = selection != null ? selection.x : -1;
|
| - int selectionLength = selection != null ? selection.y : -1;
|
| + int initialSelectionOffset = selection != null ? selection.x : -1;
|
| + int initialSelectionLength = selection != null ? selection.y : -1;
|
|
|
| final CountDownLatch latch = new CountDownLatch(1);
|
|
|
| DartCore.getAnalysisServer().edit_format(
|
| sourcePath,
|
| - selectionStart,
|
| - selectionLength,
|
| + initialSelectionOffset,
|
| + initialSelectionLength,
|
| new FormatConsumer() {
|
| -
|
| @Override
|
| - public void computedFormat(List<SourceEdit> edits, int selectionOffset,
|
| - int selectionLength) {
|
| + public void computedFormat(List<SourceEdit> edits, int newSelectionOffset,
|
| + int newSelectionLength) {
|
| + newSelection.x = newSelectionOffset;
|
| + newSelection.y = newSelectionLength;
|
| TextEdit[] textEdits = ServiceUtils_NEW.toLTK(edits);
|
| - change[0] = new CompilationUnitChange(file.getName(), file);
|
| - change[0].setEdit(new MultiTextEdit());
|
| - change[0].initializeValidationData(monitor);
|
| - change[0].setSaveMode(TextFileChange.LEAVE_DIRTY);
|
| try {
|
| for (TextEdit ltkEdit : textEdits) {
|
| - change[0].addEdit(ltkEdit);
|
| + change.addEdit(ltkEdit);
|
| }
|
| } catch (MalformedTreeException e) {
|
| - throw new Error(file.getName() + " " + StringUtils.join(textEdits, " "), e);
|
| + throw new Error(fileName + " " + StringUtils.join(textEdits, " "), e);
|
| } finally {
|
| latch.countDown();
|
| }
|
| @@ -227,14 +228,16 @@ public class DartFormatter {
|
| latch.countDown();
|
| }
|
| });
|
| - Uninterruptibles.awaitUninterruptibly(latch, 5000, TimeUnit.MILLISECONDS);
|
| -
|
| - if (change[0] != null) {
|
| - new PerformChangeOperation(change[0]).run(monitor);
|
| + boolean success = Uninterruptibles.awaitUninterruptibly(
|
| + latch,
|
| + 5000,
|
| + TimeUnit.MILLISECONDS);
|
| + if (success) {
|
| + new PerformChangeOperation(change).run(monitor);
|
| }
|
| }
|
| });
|
| -
|
| + return newSelection;
|
| }
|
| }
|
|
|
|
|