| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012, the Dart project authors. | 2 * Copyright (c) 2012, 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 * not be {@code null}. | 75 * not be {@code null}. |
| 76 */ | 76 */ |
| 77 private IDocument document; | 77 private IDocument document; |
| 78 | 78 |
| 79 /** | 79 /** |
| 80 * Synchronize against this field before accessing {@link #dirtyRegion} | 80 * Synchronize against this field before accessing {@link #dirtyRegion} |
| 81 */ | 81 */ |
| 82 private final Object lock = new Object(); | 82 private final Object lock = new Object(); |
| 83 | 83 |
| 84 /** | 84 /** |
| 85 * The region of source that has changed, which may be empty. Synchronize agai
nst {@link #lock} | 85 * The region of source that has changed and needs to be reconciled, or empty
if analysis of this |
| 86 * before accessing this field. | 86 * file is up to date, or {@code null} if the entire file needs to be reconcil
ed. Synchronize |
| 87 * against {@link #lock} before accessing this field. |
| 87 */ | 88 */ |
| 88 private DartReconcilingRegion dirtyRegion = DartReconcilingRegion.EMPTY; | 89 private DartReconcilingRegion dirtyRegion = DartReconcilingRegion.EMPTY; |
| 89 | 90 |
| 90 /** | 91 /** |
| 91 * Listen for analysis results for the source being edited and update the edit
or. | 92 * Listen for analysis results for the source being edited and update the edit
or. |
| 92 */ | 93 */ |
| 93 private final AnalysisListener analysisListener = new AnalysisListener() { | 94 private final AnalysisListener analysisListener = new AnalysisListener() { |
| 94 | 95 |
| 95 @Override | 96 @Override |
| 96 public void complete(AnalysisEvent event) { | 97 public void complete(AnalysisEvent event) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 118 public void documentAboutToBeChanged(DocumentEvent event) { | 119 public void documentAboutToBeChanged(DocumentEvent event) { |
| 119 } | 120 } |
| 120 | 121 |
| 121 @Override | 122 @Override |
| 122 public void documentChanged(DocumentEvent event) { | 123 public void documentChanged(DocumentEvent event) { |
| 123 | 124 |
| 124 // Record the source region that has changed | 125 // Record the source region that has changed |
| 125 String newText = event.getText(); | 126 String newText = event.getText(); |
| 126 int newLength = newText != null ? newText.length() : 0; | 127 int newLength = newText != null ? newText.length() : 0; |
| 127 synchronized (lock) { | 128 synchronized (lock) { |
| 128 dirtyRegion = dirtyRegion.add(event.getOffset(), event.getLength(), newL
ength); | 129 if (dirtyRegion != null) { |
| 130 dirtyRegion = dirtyRegion.add(event.getOffset(), event.getLength(), ne
wLength); |
| 131 } |
| 129 } | 132 } |
| 130 editor.applyResolvedUnit(null); | 133 editor.applyResolvedUnit(null); |
| 131 | 134 |
| 132 // Start analysis immediately if "." pressed to improve code completion re
sponse | 135 // Start analysis immediately if "." pressed to improve code completion re
sponse |
| 133 if (".".equals(newText)) { | 136 if (".".endsWith(newText)) { |
| 134 reconcile(); | 137 reconcile(); |
| 135 } | 138 } |
| 136 } | 139 } |
| 137 }; | 140 }; |
| 138 | 141 |
| 139 /** | 142 /** |
| 140 * Construct a new instance for the specified editor. | 143 * Construct a new instance for the specified editor. |
| 141 * | 144 * |
| 142 * @param editor the editor (not {@code null}) | 145 * @param editor the editor (not {@code null}) |
| 143 */ | 146 */ |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 | 218 |
| 216 /** | 219 /** |
| 217 * Activates reconciling of the current dirty region. | 220 * Activates reconciling of the current dirty region. |
| 218 */ | 221 */ |
| 219 public void reconcile() { | 222 public void reconcile() { |
| 220 InstrumentationBuilder instrumentation = Instrumentation.builder("DartReconc
ilingStrategy-reconcile"); | 223 InstrumentationBuilder instrumentation = Instrumentation.builder("DartReconc
ilingStrategy-reconcile"); |
| 221 try { | 224 try { |
| 222 instrumentation.data("Name", editor.getTitle()); | 225 instrumentation.data("Name", editor.getTitle()); |
| 223 DartReconcilingRegion r; | 226 DartReconcilingRegion region; |
| 224 synchronized (lock) { | 227 synchronized (lock) { |
| 225 if (dirtyRegion.isEmpty()) { | 228 region = dirtyRegion; |
| 226 return; | |
| 227 } | |
| 228 r = dirtyRegion; | |
| 229 dirtyRegion = DartReconcilingRegion.EMPTY; | 229 dirtyRegion = DartReconcilingRegion.EMPTY; |
| 230 } | 230 } |
| 231 instrumentation.data("Offset", r.getOffset()); | 231 if (region == null) { |
| 232 instrumentation.data("OldLength", r.getOldLength()); | 232 String code = document.get(); |
| 233 instrumentation.data("NewLength", r.getNewLength()); | 233 instrumentation.data("Length", code.length()); |
| 234 sourceChanged(document.get(), r.getOffset(), r.getOldLength(), r.getNewLen
gth()); | 234 sourceChanged(code); |
| 235 performAnalysisInBackground(); | 235 performAnalysisInBackground(); |
| 236 } else if (!region.isEmpty()) { |
| 237 instrumentation.data("Offset", region.getOffset()); |
| 238 instrumentation.data("OldLength", region.getOldLength()); |
| 239 instrumentation.data("NewLength", region.getNewLength()); |
| 240 sourceChanged( |
| 241 document.get(), |
| 242 region.getOffset(), |
| 243 region.getOldLength(), |
| 244 region.getNewLength()); |
| 245 performAnalysisInBackground(); |
| 246 } |
| 236 } finally { | 247 } finally { |
| 237 instrumentation.log(); | 248 instrumentation.log(); |
| 238 } | 249 } |
| 239 } | 250 } |
| 240 | 251 |
| 241 @Override | 252 @Override |
| 242 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { | 253 public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { |
| 243 reconcile(); | 254 reconcile(); |
| 244 } | 255 } |
| 245 | 256 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 } | 349 } |
| 339 | 350 |
| 340 /** | 351 /** |
| 341 * Apply analysis results only if there are no pending source changes. | 352 * Apply analysis results only if there are no pending source changes. |
| 342 */ | 353 */ |
| 343 private void applyAnalysisResult(CompilationUnit unit) { | 354 private void applyAnalysisResult(CompilationUnit unit) { |
| 344 if (unit == null) { | 355 if (unit == null) { |
| 345 return; | 356 return; |
| 346 } | 357 } |
| 347 synchronized (lock) { | 358 synchronized (lock) { |
| 348 if (!dirtyRegion.isEmpty()) { | 359 if (dirtyRegion == null || !dirtyRegion.isEmpty()) { |
| 349 return; | 360 return; |
| 350 } | 361 } |
| 351 } | 362 } |
| 352 editor.applyResolvedUnit(unit); | 363 editor.applyResolvedUnit(unit); |
| 353 } | 364 } |
| 354 | 365 |
| 355 /** | 366 /** |
| 356 * Get the resolved compilation unit from the editor's analysis context and ap
ply that unit. | 367 * Get the resolved compilation unit from the editor's analysis context and ap
ply that unit. |
| 357 * | 368 * |
| 358 * @return {@code true} if a resolved unit was obtained and applied | 369 * @return {@code true} if a resolved unit was obtained and applied |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 * @param newLength the number of characters in the replacement text | 421 * @param newLength the number of characters in the replacement text |
| 411 */ | 422 */ |
| 412 private void sourceChanged(String code, int offset, int oldLength, int newLeng
th) { | 423 private void sourceChanged(String code, int offset, int oldLength, int newLeng
th) { |
| 413 AnalysisContext context = editor.getInputAnalysisContext(); | 424 AnalysisContext context = editor.getInputAnalysisContext(); |
| 414 Source source = editor.getInputSource(); | 425 Source source = editor.getInputSource(); |
| 415 if (context != null && source != null) { | 426 if (context != null && source != null) { |
| 416 context.setChangedContents(source, code, offset, oldLength, newLength); | 427 context.setChangedContents(source, code, offset, oldLength, newLength); |
| 417 } | 428 } |
| 418 } | 429 } |
| 419 } | 430 } |
| OLD | NEW |