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 analysis_server.src.protocol.protocol_internal; | 5 library analysis_server.src.protocol.protocol_internal; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:convert' hide JsonDecoder; | 8 import 'dart:convert' hide JsonDecoder; |
9 | 9 |
10 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 10 import 'package:analysis_server/protocol/protocol.dart'; |
| 11 import 'package:analysis_server/protocol/protocol_generated.dart'; |
11 | 12 |
12 final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS = | 13 final Map<String, RefactoringKind> REQUEST_ID_REFACTORING_KINDS = |
13 new HashMap<String, RefactoringKind>(); | 14 new HashMap<String, RefactoringKind>(); |
14 | 15 |
15 /** | 16 /** |
16 * Adds the given [sourceEdits] to the list in [sourceFileEdit]. | 17 * Adds the given [sourceEdits] to the list in [sourceFileEdit]. |
17 */ | 18 */ |
18 void addAllEditsForSource( | 19 void addAllEditsForSource( |
19 SourceFileEdit sourceFileEdit, Iterable<SourceEdit> edits) { | 20 SourceFileEdit sourceFileEdit, Iterable<SourceEdit> edits) { |
20 edits.forEach(sourceFileEdit.add); | 21 edits.forEach(sourceFileEdit.add); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 * a JSON presentation. | 238 * a JSON presentation. |
238 */ | 239 */ |
239 abstract class HasToJson { | 240 abstract class HasToJson { |
240 /** | 241 /** |
241 * Returns a JSON presentation of the object. | 242 * Returns a JSON presentation of the object. |
242 */ | 243 */ |
243 Map<String, Object> toJson(); | 244 Map<String, Object> toJson(); |
244 } | 245 } |
245 | 246 |
246 /** | 247 /** |
247 * Jenkins hash function, optimized for small integers. Borrowed from | |
248 * sdk/lib/math/jenkins_smi_hash.dart. | |
249 * | |
250 * TODO(paulberry): Move to somewhere that can be shared with other code. | |
251 */ | |
252 class JenkinsSmiHash { | |
253 static int combine(int hash, int value) { | |
254 hash = 0x1fffffff & (hash + value); | |
255 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | |
256 return hash ^ (hash >> 6); | |
257 } | |
258 | |
259 static int finish(int hash) { | |
260 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | |
261 hash = hash ^ (hash >> 11); | |
262 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | |
263 } | |
264 | |
265 static int hash2(a, b) => finish(combine(combine(0, a), b)); | |
266 | |
267 static int hash4(a, b, c, d) => | |
268 finish(combine(combine(combine(combine(0, a), b), c), d)); | |
269 } | |
270 | |
271 /** | |
272 * Base class for decoding JSON objects. The derived class must implement | 248 * Base class for decoding JSON objects. The derived class must implement |
273 * error reporting logic. | 249 * error reporting logic. |
274 */ | 250 */ |
275 abstract class JsonDecoder { | 251 abstract class JsonDecoder { |
276 /** | 252 /** |
277 * Retrieve the RefactoringKind that should be assumed when decoding | 253 * Retrieve the RefactoringKind that should be assumed when decoding |
278 * refactoring feedback objects, or null if no refactoring feedback object is | 254 * refactoring feedback objects, or null if no refactoring feedback object is |
279 * expected to be encountered. | 255 * expected to be encountered. |
280 */ | 256 */ |
281 RefactoringKind get refactoringKind; | 257 RefactoringKind get refactoringKind; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 new Response.invalidParameter(_request, jsonPath, buffer.toString())); | 415 new Response.invalidParameter(_request, jsonPath, buffer.toString())); |
440 } | 416 } |
441 | 417 |
442 @override | 418 @override |
443 dynamic missingKey(String jsonPath, String key) { | 419 dynamic missingKey(String jsonPath, String key) { |
444 return new RequestFailure(new Response.invalidParameter( | 420 return new RequestFailure(new Response.invalidParameter( |
445 _request, jsonPath, 'Expected to contain key ${JSON.encode(key)}')); | 421 _request, jsonPath, 'Expected to contain key ${JSON.encode(key)}')); |
446 } | 422 } |
447 } | 423 } |
448 | 424 |
| 425 abstract class RequestParams implements HasToJson { |
| 426 /** |
| 427 * Return a request whose parameters are taken from this object and that has |
| 428 * the given [id]. |
| 429 */ |
| 430 Request toRequest(String id); |
| 431 } |
| 432 |
449 /** | 433 /** |
450 * JsonDecoder for decoding responses from the server. This is intended to be | 434 * JsonDecoder for decoding responses from the server. This is intended to be |
451 * used only for testing. Errors are reported using bare [Exception] objects. | 435 * used only for testing. Errors are reported using bare [Exception] objects. |
452 */ | 436 */ |
453 class ResponseDecoder extends JsonDecoder { | 437 class ResponseDecoder extends JsonDecoder { |
454 final RefactoringKind refactoringKind; | 438 final RefactoringKind refactoringKind; |
455 | 439 |
456 ResponseDecoder(this.refactoringKind); | 440 ResponseDecoder(this.refactoringKind); |
457 | 441 |
458 @override | 442 @override |
459 dynamic mismatch(String jsonPath, String expected, [Object actual]) { | 443 dynamic mismatch(String jsonPath, String expected, [Object actual]) { |
460 StringBuffer buffer = new StringBuffer(); | 444 StringBuffer buffer = new StringBuffer(); |
461 buffer.write('Expected '); | 445 buffer.write('Expected '); |
462 buffer.write(expected); | 446 buffer.write(expected); |
463 if (actual != null) { | 447 if (actual != null) { |
464 buffer.write(' found "'); | 448 buffer.write(' found "'); |
465 buffer.write(JSON.encode(actual)); | 449 buffer.write(JSON.encode(actual)); |
466 buffer.write('"'); | 450 buffer.write('"'); |
467 } | 451 } |
468 buffer.write(' at '); | 452 buffer.write(' at '); |
469 buffer.write(jsonPath); | 453 buffer.write(jsonPath); |
470 return new Exception(buffer.toString()); | 454 return new Exception(buffer.toString()); |
471 } | 455 } |
472 | 456 |
473 @override | 457 @override |
474 dynamic missingKey(String jsonPath, String key) { | 458 dynamic missingKey(String jsonPath, String key) { |
475 return new Exception('Missing key $key at $jsonPath'); | 459 return new Exception('Missing key $key at $jsonPath'); |
476 } | 460 } |
477 } | 461 } |
| 462 |
| 463 /** |
| 464 * The result data associated with a response. |
| 465 */ |
| 466 abstract class ResponseResult implements HasToJson { |
| 467 /** |
| 468 * Return a response whose result data is this object for the request with the |
| 469 * given [id]. |
| 470 */ |
| 471 Response toResponse(String id); |
| 472 } |
OLD | NEW |