| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library trydart.interaction_manager; | 5 library trydart.interaction_manager; |
| 6 | 6 |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 | 8 |
| 9 import 'dart:convert' show | 9 import 'dart:convert' show |
| 10 JSON; | 10 JSON; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 import 'package:compiler/implementation/source_file.dart' show | 22 import 'package:compiler/implementation/source_file.dart' show |
| 23 StringSourceFile; | 23 StringSourceFile; |
| 24 | 24 |
| 25 import 'compilation.dart' show | 25 import 'compilation.dart' show |
| 26 scheduleCompilation; | 26 scheduleCompilation; |
| 27 | 27 |
| 28 import 'ui.dart' show | 28 import 'ui.dart' show |
| 29 currentTheme, | 29 currentTheme, |
| 30 hackDiv, | 30 hackDiv, |
| 31 inputPre, | 31 mainEditorPane, |
| 32 observer, | 32 observer, |
| 33 outputDiv; | 33 outputDiv; |
| 34 | 34 |
| 35 import 'decoration.dart' show | 35 import 'decoration.dart' show |
| 36 CodeCompletionDecoration, | 36 CodeCompletionDecoration, |
| 37 Decoration, | 37 Decoration, |
| 38 DiagnosticDecoration, | 38 DiagnosticDecoration, |
| 39 error, | 39 error, |
| 40 info, | 40 info, |
| 41 warning; | 41 warning; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 hackDiv = newDiv; | 163 hackDiv = newDiv; |
| 164 } | 164 } |
| 165 | 165 |
| 166 // TODO(ahe): This method should be cleaned up. It is too large. | 166 // TODO(ahe): This method should be cleaned up. It is too large. |
| 167 void onMutation(List<MutationRecord> mutations, MutationObserver observer) { | 167 void onMutation(List<MutationRecord> mutations, MutationObserver observer) { |
| 168 print('onMutation'); | 168 print('onMutation'); |
| 169 | 169 |
| 170 for (String query in const ['a.diagnostic>span', | 170 for (String query in const ['a.diagnostic>span', |
| 171 '.dart-code-completion', | 171 '.dart-code-completion', |
| 172 '.hazed-suggestion']) { | 172 '.hazed-suggestion']) { |
| 173 for (Element element in inputPre.querySelectorAll(query)) { | 173 for (Element element in mainEditorPane.querySelectorAll(query)) { |
| 174 element.remove(); | 174 element.remove(); |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 Selection selection = window.getSelection(); | 178 Selection selection = window.getSelection(); |
| 179 | 179 |
| 180 for (MutationRecord record in mutations) { | 180 for (MutationRecord record in mutations) { |
| 181 if (record.addedNodes.isEmpty) continue; | 181 if (record.addedNodes.isEmpty) continue; |
| 182 for (Node node in record.addedNodes) { | 182 for (Node node in record.addedNodes) { |
| 183 if (node.parent == null) continue; | 183 if (node.parent == null) continue; |
| 184 StringBuffer buffer = new StringBuffer(); | 184 StringBuffer buffer = new StringBuffer(); |
| 185 int selectionOffset = htmlToText(node, buffer, selection); | 185 int selectionOffset = htmlToText(node, buffer, selection); |
| 186 Text newNode = new Text('$buffer'); | 186 Text newNode = new Text('$buffer'); |
| 187 node.replaceWith(newNode); | 187 node.replaceWith(newNode); |
| 188 if (selectionOffset != -1) { | 188 if (selectionOffset != -1) { |
| 189 selection.collapse(newNode, selectionOffset); | 189 selection.collapse(newNode, selectionOffset); |
| 190 } | 190 } |
| 191 } | 191 } |
| 192 } | 192 } |
| 193 | 193 |
| 194 if (!inputPre.nodes.isEmpty && inputPre.nodes.last is Text) { | 194 if (!mainEditorPane.nodes.isEmpty && mainEditorPane.nodes.last is Text) { |
| 195 Text text = inputPre.nodes.last; | 195 Text text = mainEditorPane.nodes.last; |
| 196 if (!text.text.endsWith('\n')) { | 196 if (!text.text.endsWith('\n')) { |
| 197 text.appendData('\n'); | 197 text.appendData('\n'); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 int offset = 0; | 201 int offset = 0; |
| 202 int anchorOffset = 0; | 202 int anchorOffset = 0; |
| 203 bool hasSelection = false; | 203 bool hasSelection = false; |
| 204 Node anchorNode = selection.anchorNode; | 204 Node anchorNode = selection.anchorNode; |
| 205 // TODO(ahe): Try to share walk4 methods. | 205 // TODO(ahe): Try to share walk4 methods. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 223 } | 223 } |
| 224 | 224 |
| 225 var child = node.firstChild; | 225 var child = node.firstChild; |
| 226 while (child != null) { | 226 while (child != null) { |
| 227 walk4(child); | 227 walk4(child); |
| 228 if (hasSelection) return; | 228 if (hasSelection) return; |
| 229 child = child.nextNode; | 229 child = child.nextNode; |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 if (selection.isCollapsed) { | 232 if (selection.isCollapsed) { |
| 233 walk4(inputPre); | 233 walk4(mainEditorPane); |
| 234 } | 234 } |
| 235 | 235 |
| 236 editor.currentSource = inputPre.text; | 236 editor.currentSource = mainEditorPane.text; |
| 237 inputPre.nodes.clear(); | 237 mainEditorPane.nodes.clear(); |
| 238 inputPre.appendText(editor.currentSource); | 238 mainEditorPane.appendText(editor.currentSource); |
| 239 if (hasSelection) { | 239 if (hasSelection) { |
| 240 selection.collapse(inputPre.firstChild, anchorOffset); | 240 selection.collapse(mainEditorPane.firstChild, anchorOffset); |
| 241 } | 241 } |
| 242 | 242 |
| 243 editor.isMalformedInput = false; | 243 editor.isMalformedInput = false; |
| 244 for (var n in new List.from(inputPre.nodes)) { | 244 for (var n in new List.from(mainEditorPane.nodes)) { |
| 245 if (n is! Text) continue; | 245 if (n is! Text) continue; |
| 246 Text node = n; | 246 Text node = n; |
| 247 String text = node.text; | 247 String text = node.text; |
| 248 | 248 |
| 249 Token token = tokenize(text); | 249 Token token = tokenize(text); |
| 250 int offset = 0; | 250 int offset = 0; |
| 251 editor.seenIdentifiers = new Set<String>.from(mock.identifiers); | 251 editor.seenIdentifiers = new Set<String>.from(mock.identifiers); |
| 252 for (; token.kind != EOF_TOKEN; token = token.next) { | 252 for (; token.kind != EOF_TOKEN; token = token.next) { |
| 253 Decoration decoration = editor.getDecoration(token); | 253 Decoration decoration = editor.getDecoration(token); |
| 254 if (decoration == null) continue; | 254 if (decoration == null) continue; |
| 255 bool hasSelection = false; | 255 bool hasSelection = false; |
| 256 int selectionOffset = selection.anchorOffset; | 256 int selectionOffset = selection.anchorOffset; |
| 257 | 257 |
| 258 if (selection.isCollapsed && selection.anchorNode == node) { | 258 if (selection.isCollapsed && selection.anchorNode == node) { |
| 259 hasSelection = true; | 259 hasSelection = true; |
| 260 selectionOffset = selection.anchorOffset; | 260 selectionOffset = selection.anchorOffset; |
| 261 } | 261 } |
| 262 int splitPoint = token.charOffset - offset; | 262 int splitPoint = token.charOffset - offset; |
| 263 Text str = node.splitText(splitPoint); | 263 Text str = node.splitText(splitPoint); |
| 264 Text after = str.splitText(token.charCount); | 264 Text after = str.splitText(token.charCount); |
| 265 offset += splitPoint + token.charCount; | 265 offset += splitPoint + token.charCount; |
| 266 inputPre.insertBefore(after, node.nextNode); | 266 mainEditorPane.insertBefore(after, node.nextNode); |
| 267 inputPre.insertBefore(decoration.applyTo(str), after); | 267 mainEditorPane.insertBefore(decoration.applyTo(str), after); |
| 268 | 268 |
| 269 if (hasSelection && selectionOffset > node.length) { | 269 if (hasSelection && selectionOffset > node.length) { |
| 270 selectionOffset -= node.length; | 270 selectionOffset -= node.length; |
| 271 if (selectionOffset > str.length) { | 271 if (selectionOffset > str.length) { |
| 272 selectionOffset -= str.length; | 272 selectionOffset -= str.length; |
| 273 selection.collapse(after, selectionOffset); | 273 selection.collapse(after, selectionOffset); |
| 274 } else { | 274 } else { |
| 275 selection.collapse(str, selectionOffset); | 275 selection.collapse(str, selectionOffset); |
| 276 } | 276 } |
| 277 } | 277 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 String prefix = ""; | 380 String prefix = ""; |
| 381 if (text is Text) prefix = text.data.trim(); | 381 if (text is Text) prefix = text.data.trim(); |
| 382 updateInlineSuggestion(prefix, element.text); | 382 updateInlineSuggestion(prefix, element.text); |
| 383 } | 383 } |
| 384 | 384 |
| 385 void endCompletion({bool acceptSuggestion: false}) { | 385 void endCompletion({bool acceptSuggestion: false}) { |
| 386 if (acceptSuggestion) { | 386 if (acceptSuggestion) { |
| 387 suggestionAccepted(); | 387 suggestionAccepted(); |
| 388 } | 388 } |
| 389 activeCompletion.classes.remove('active'); | 389 activeCompletion.classes.remove('active'); |
| 390 inputPre.querySelectorAll('.hazed-suggestion').forEach((e) => e.remove()); | 390 mainEditorPane.querySelectorAll('.hazed-suggestion') |
| 391 .forEach((e) => e.remove()); |
| 391 // The above changes create mutation records. This implicitly fire mutation | 392 // The above changes create mutation records. This implicitly fire mutation |
| 392 // events that result in saving the source code in local storage. | 393 // events that result in saving the source code in local storage. |
| 393 // TODO(ahe): Consider making this more explicit. | 394 // TODO(ahe): Consider making this more explicit. |
| 394 state = new InitialState(context); | 395 state = new InitialState(context); |
| 395 } | 396 } |
| 396 | 397 |
| 397 void suggestionAccepted() { | 398 void suggestionAccepted() { |
| 398 if (inlineSuggestion != null) { | 399 if (inlineSuggestion != null) { |
| 399 Text text = new Text(inlineSuggestion); | 400 Text text = new Text(inlineSuggestion); |
| 400 activeCompletion.replaceWith(text); | 401 activeCompletion.replaceWith(text); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 event.getModifierState("Fn") || | 592 event.getModifierState("Fn") || |
| 592 event.getModifierState("Meta") || | 593 event.getModifierState("Meta") || |
| 593 event.getModifierState("NumLock") || | 594 event.getModifierState("NumLock") || |
| 594 event.getModifierState("ScrollLock") || | 595 event.getModifierState("ScrollLock") || |
| 595 event.getModifierState("Scroll") || | 596 event.getModifierState("Scroll") || |
| 596 event.getModifierState("Win") || | 597 event.getModifierState("Win") || |
| 597 event.getModifierState("Shift") || | 598 event.getModifierState("Shift") || |
| 598 event.getModifierState("SymbolLock") || | 599 event.getModifierState("SymbolLock") || |
| 599 event.getModifierState("OS"); | 600 event.getModifierState("OS"); |
| 600 } | 601 } |
| OLD | NEW |