Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/formatter/DartFormatter.java

Issue 932423002: Issue 17356. Apply selection returned from formatter to the SourceViewer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, the Dart project authors. 2 * Copyright (c) 2013, the Dart project authors.
3 * 3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except
5 * in compliance with the License. You may obtain a copy of the License at 5 * in compliance with the License. You may obtain a copy of the License at
6 * 6 *
7 * http://www.eclipse.org/legal/epl-v10.html 7 * http://www.eclipse.org/legal/epl-v10.html
8 * 8 *
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 return result; 174 return result;
175 } catch (JSONException e) { 175 } catch (JSONException e) {
176 DartToolsPlugin.log(e); 176 DartToolsPlugin.log(e);
177 throw new IOException(e); 177 throw new IOException(e);
178 } 178 }
179 179
180 } 180 }
181 } 181 }
182 182
183 public static class DartStyleRunner { 183 public static class DartStyleRunner {
184 public static Point formatFile(IFile file, final Point selection, final IPro gressMonitor monitor)
185 throws IOException, CoreException {
186 final String fileName = file.getName();
187 final String sourcePath = file.getRawLocation().makeAbsolute().toOSString( );
184 188
185 public static void formatFile(final IFile file, final Point selection, 189 final CompilationUnitChange change = new CompilationUnitChange(fileName, f ile);
186 final IProgressMonitor monitor) throws IOException, CoreException { 190 change.setEdit(new MultiTextEdit());
191 change.initializeValidationData(monitor);
192 change.setSaveMode(TextFileChange.LEAVE_DIRTY);
187 193
188 final String sourcePath = file.getRawLocation().makeAbsolute().toOSString( ); 194 final Point newSelection = new Point(-1, 0);
189 final CompilationUnitChange[] change = new CompilationUnitChange[1];
190 195
191 ExecutionUtils.runLog(new RunnableEx() { 196 ExecutionUtils.runLog(new RunnableEx() {
192 @Override 197 @Override
193 public void run() throws Exception { 198 public void run() throws Exception {
194 199 int initialSelectionOffset = selection != null ? selection.x : -1;
195 int selectionStart = selection != null ? selection.x : -1; 200 int initialSelectionLength = selection != null ? selection.y : -1;
196 int selectionLength = selection != null ? selection.y : -1;
197 201
198 final CountDownLatch latch = new CountDownLatch(1); 202 final CountDownLatch latch = new CountDownLatch(1);
199 203
200 DartCore.getAnalysisServer().edit_format( 204 DartCore.getAnalysisServer().edit_format(
201 sourcePath, 205 sourcePath,
202 selectionStart, 206 initialSelectionOffset,
203 selectionLength, 207 initialSelectionLength,
204 new FormatConsumer() { 208 new FormatConsumer() {
205
206 @Override 209 @Override
207 public void computedFormat(List<SourceEdit> edits, int selection Offset, 210 public void computedFormat(List<SourceEdit> edits, int newSelect ionOffset,
208 int selectionLength) { 211 int newSelectionLength) {
212 newSelection.x = newSelectionOffset;
213 newSelection.y = newSelectionLength;
209 TextEdit[] textEdits = ServiceUtils_NEW.toLTK(edits); 214 TextEdit[] textEdits = ServiceUtils_NEW.toLTK(edits);
210 change[0] = new CompilationUnitChange(file.getName(), file);
211 change[0].setEdit(new MultiTextEdit());
212 change[0].initializeValidationData(monitor);
213 change[0].setSaveMode(TextFileChange.LEAVE_DIRTY);
214 try { 215 try {
215 for (TextEdit ltkEdit : textEdits) { 216 for (TextEdit ltkEdit : textEdits) {
216 change[0].addEdit(ltkEdit); 217 change.addEdit(ltkEdit);
217 } 218 }
218 } catch (MalformedTreeException e) { 219 } catch (MalformedTreeException e) {
219 throw new Error(file.getName() + " " + StringUtils.join(text Edits, " "), e); 220 throw new Error(fileName + " " + StringUtils.join(textEdits, " "), e);
220 } finally { 221 } finally {
221 latch.countDown(); 222 latch.countDown();
222 } 223 }
223 } 224 }
224 225
225 @Override 226 @Override
226 public void onError(RequestError requestError) { 227 public void onError(RequestError requestError) {
227 latch.countDown(); 228 latch.countDown();
228 } 229 }
229 }); 230 });
230 Uninterruptibles.awaitUninterruptibly(latch, 5000, TimeUnit.MILLISECON DS); 231 boolean success = Uninterruptibles.awaitUninterruptibly(
231 232 latch,
232 if (change[0] != null) { 233 5000,
233 new PerformChangeOperation(change[0]).run(monitor); 234 TimeUnit.MILLISECONDS);
235 if (success) {
236 new PerformChangeOperation(change).run(monitor);
234 } 237 }
235 } 238 }
236 }); 239 });
237 240 return newSelection;
238 } 241 }
239 } 242 }
240 243
241 public static class FormatFileAction extends WorkspaceAction { 244 public static class FormatFileAction extends WorkspaceAction {
242 245
243 private List<IFile> files = Arrays.asList(new IFile[0]); 246 private List<IFile> files = Arrays.asList(new IFile[0]);
244 247
245 public FormatFileAction(IShellProvider provider) { 248 public FormatFileAction(IShellProvider provider) {
246 super(provider, "Format"); 249 super(provider, "Format");
247 setId(DartEditorActionDefinitionIds.QUICK_FORMAT); 250 setId(DartEditorActionDefinitionIds.QUICK_FORMAT);
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 } 486 }
484 487
485 // private static List<String> buildArguments(IPath path) { 488 // private static List<String> buildArguments(IPath path) {
486 // ArrayList<String> args = new ArrayList<String>(); 489 // ArrayList<String> args = new ArrayList<String>();
487 // args.add("-w"); 490 // args.add("-w");
488 // args.add(path.toOSString()); 491 // args.add(path.toOSString());
489 // return args; 492 // return args;
490 // } 493 // }
491 494
492 } 495 }
OLDNEW
« no previous file with comments | « no previous file | editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/text/editor/DartEditor.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698