| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library domain.context; | |
| 6 | |
| 7 import 'package:analysis_server/src/analysis_server.dart'; | |
| 8 import 'package:analysis_server/src/constants.dart'; | |
| 9 import 'package:analysis_server/src/protocol.dart'; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 | |
| 13 /** | |
| 14 * Instances of the class [ContextDomainHandler] implement a [RequestHandler] | |
| 15 * that handles requests in the context domain. | |
| 16 * | |
| 17 * TODO(scheglov) this class is replaces with [AnalysisDomainHandler]. | |
| 18 */ | |
| 19 class ContextDomainHandler implements RequestHandler { | |
| 20 /** | |
| 21 * The analysis server that is using this handler to process requests. | |
| 22 */ | |
| 23 final AnalysisServer server; | |
| 24 | |
| 25 /** | |
| 26 * Initialize a newly created handler to handle requests for the given [server
]. | |
| 27 */ | |
| 28 ContextDomainHandler(this.server); | |
| 29 | |
| 30 @override | |
| 31 Response handleRequest(Request request) { | |
| 32 try { | |
| 33 String requestName = request.method; | |
| 34 if (requestName == APPLY_CHANGES_NAME) { | |
| 35 return applyChanges(request); | |
| 36 } else if (requestName == GET_FIXES_NAME) { | |
| 37 return getFixes(request); | |
| 38 } else if (requestName == SET_OPTIONS_NAME) { | |
| 39 return setOptions(request); | |
| 40 } else if (requestName == SET_PRIORITY_SOURCES_NAME) { | |
| 41 return setPrioritySources(request); | |
| 42 } | |
| 43 } on RequestFailure catch (exception) { | |
| 44 return exception.response; | |
| 45 } | |
| 46 return null; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Inform the specified context that the changes encoded in the change set | |
| 51 * have been made. Any invalidated analysis results will be flushed from the | |
| 52 * context. | |
| 53 */ | |
| 54 Response applyChanges(Request request) { | |
| 55 AnalysisContext context = getAnalysisContext(request); | |
| 56 RequestDatum changesData = request.getRequiredParameter(CHANGES_PARAM); | |
| 57 ChangeSet changeSet = createChangeSet( | |
| 58 request, | |
| 59 context.sourceFactory, | |
| 60 changesData); | |
| 61 | |
| 62 context.applyChanges(changeSet); | |
| 63 server.addContextToWorkQueue(context); | |
| 64 Response response = new Response(request.id); | |
| 65 return response; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Convert the given JSON object into a [ChangeSet], using the given | |
| 70 * [sourceFactory] to convert the embedded strings into sources. | |
| 71 */ | |
| 72 ChangeSet createChangeSet(Request request, SourceFactory sourceFactory, | |
| 73 RequestDatum jsonData) { | |
| 74 ChangeSet changeSet = new ChangeSet(); | |
| 75 if (jsonData.hasKey(ADDED)) { | |
| 76 convertSources(request, sourceFactory, jsonData[ADDED], (Source source) { | |
| 77 changeSet.addedSource(source); | |
| 78 }); | |
| 79 } | |
| 80 if (jsonData.hasKey(MODIFIED_PARAM)) { | |
| 81 convertSources(request, sourceFactory, jsonData[MODIFIED_PARAM], (Source s
ource) { | |
| 82 changeSet.changedSource(source); | |
| 83 }); | |
| 84 } | |
| 85 if (jsonData.hasKey(REMOVED)) { | |
| 86 convertSources(request, sourceFactory, jsonData[REMOVED], (Source source)
{ | |
| 87 changeSet.removedSource(source); | |
| 88 }); | |
| 89 } | |
| 90 return changeSet; | |
| 91 } | |
| 92 | |
| 93 /** | |
| 94 * If the given [sources] is a list of strings, use the given [sourceFactory] | |
| 95 * to convert each string into a source and pass the source to the given | |
| 96 * [handler]. Otherwise, throw an exception indicating that the data in the | |
| 97 * request was not valid. | |
| 98 */ | |
| 99 void convertSources(Request request, SourceFactory sourceFactory, RequestDatum
sources, void handler(Source source)) { | |
| 100 convertToSources(sourceFactory, sources.asStringList()).forEach(handler); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Return the list of fixes that are available for problems related to the | |
| 105 * given error in the specified context. | |
| 106 */ | |
| 107 Response getFixes(Request request) { | |
| 108 // TODO(brianwilkerson) Implement this. | |
| 109 Response response = new Response(request.id); | |
| 110 return response; | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Set the options controlling analysis within a context to the given set of | |
| 115 * options. | |
| 116 */ | |
| 117 Response setOptions(Request request) { | |
| 118 AnalysisContext context = getAnalysisContext(request); | |
| 119 | |
| 120 context.analysisOptions = createAnalysisOptions(request); | |
| 121 Response response = new Response(request.id); | |
| 122 return response; | |
| 123 } | |
| 124 | |
| 125 /** | |
| 126 * Return the set of analysis options associated with the given [request], or | |
| 127 * throw a [RequestFailure] exception if the analysis options are not valid. | |
| 128 */ | |
| 129 AnalysisOptions createAnalysisOptions(Request request) { | |
| 130 RequestDatum optionsData = request.getRequiredParameter(OPTIONS); | |
| 131 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); | |
| 132 optionsData.forEachMap((String key, RequestDatum value) { | |
| 133 if (key == CACHE_SIZE_OPTION) { | |
| 134 options.cacheSize = value.asInt(); | |
| 135 } else if (key == GENERATE_HINTS_OPTION) { | |
| 136 options.hint = value.asBool(); | |
| 137 } else if (key == GENERATE_DART2JS_OPTION) { | |
| 138 options.dart2jsHint = value.asBool(); | |
| 139 } else if (key == PROVIDE_ERRORS_OPTION) { | |
| 140 // options.provideErrors = value.asBool(); | |
| 141 } else if (key == PROVIDE_NAVIGATION_OPTION) { | |
| 142 // options.provideNavigation = value.asBool(); | |
| 143 } else if (key == PROVIDE_OUTLINE_OPTION) { | |
| 144 // options.provideOutline = value.asBool(); | |
| 145 } else { | |
| 146 throw new RequestFailure(new Response.unknownAnalysisOption(request, key
)); | |
| 147 } | |
| 148 }); | |
| 149 return options; | |
| 150 } | |
| 151 | |
| 152 /** | |
| 153 * Set the priority sources in the specified context to the sources in the | |
| 154 * given array. | |
| 155 */ | |
| 156 Response setPrioritySources(Request request) { | |
| 157 AnalysisContext context = getAnalysisContext(request); | |
| 158 List<String> sourcesData = request.getRequiredParameter(SOURCES_PARAM).asStr
ingList(); | |
| 159 List<Source> sources = convertToSources(context.sourceFactory, sourcesData); | |
| 160 | |
| 161 context.analysisPriorityOrder = sources; | |
| 162 Response response = new Response(request.id); | |
| 163 return response; | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Convert the given list of strings into a list of sources owned by the given | |
| 168 * [sourceFactory]. | |
| 169 */ | |
| 170 List<Source> convertToSources(SourceFactory sourceFactory, List<String> source
sData) { | |
| 171 List<Source> sources = new List<Source>(); | |
| 172 sourcesData.forEach((String string) { | |
| 173 sources.add(sourceFactory.fromEncoding(string)); | |
| 174 }); | |
| 175 return sources; | |
| 176 } | |
| 177 | |
| 178 /** | |
| 179 * Return the analysis context specified by the given request, or throw a | |
| 180 * [RequestFailure] exception if either there is no specified context or if | |
| 181 * the specified context does not exist. | |
| 182 */ | |
| 183 AnalysisContext getAnalysisContext(Request request) { | |
| 184 // TODO(scheglov) remove it after migrating to the new API | |
| 185 return null; | |
| 186 // String contextId = request.getRequiredParameter(CONTEXT_ID_PARAM).asString
(); | |
| 187 // AnalysisContext context = server.contextMap[contextId]; | |
| 188 // if (context == null) { | |
| 189 // throw new RequestFailure(new Response.contextDoesNotExist(request)); | |
| 190 // } | |
| 191 // return context; | |
| 192 } | |
| 193 } | |
| OLD | NEW |