OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014, the Dart project authors. | 2 * Copyright (c) 2014, 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 |
11 * or implied. See the License for the specific language governing permissions a
nd limitations under | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 | 14 |
15 package com.google.dart.tools.debug.core.source; | 15 package com.google.dart.tools.debug.core.source; |
16 | 16 |
17 import com.google.common.collect.Maps; | 17 import com.google.common.collect.Maps; |
18 import com.google.common.util.concurrent.Uninterruptibles; | 18 import com.google.common.util.concurrent.Uninterruptibles; |
19 import com.google.dart.server.CreateContextConsumer; | 19 import com.google.dart.server.CreateContextConsumer; |
20 import com.google.dart.server.MapUriConsumer; | 20 import com.google.dart.server.MapUriConsumer; |
21 import com.google.dart.tools.core.DartCore; | 21 import com.google.dart.tools.core.DartCore; |
22 import com.google.dart.tools.core.DartCoreDebug; | 22 import com.google.dart.tools.core.DartCoreDebug; |
23 import com.google.dart.tools.core.analysis.model.IFileInfo; | 23 import com.google.dart.tools.core.analysis.model.IFileInfo; |
24 import com.google.dart.tools.core.analysis.model.ProjectManager; | 24 import com.google.dart.tools.core.analysis.model.ProjectManager; |
25 import com.google.dart.tools.core.internal.util.ResourceUtil; | 25 import com.google.dart.tools.core.internal.util.ResourceUtil; |
26 import com.google.dart.tools.debug.core.DartLaunchConfigWrapper; | 26 import com.google.dart.tools.debug.core.DartLaunchConfigWrapper; |
27 | 27 |
| 28 import org.eclipse.core.resources.IContainer; |
28 import org.eclipse.core.resources.IFile; | 29 import org.eclipse.core.resources.IFile; |
29 import org.eclipse.core.resources.IResource; | 30 import org.eclipse.core.resources.IResource; |
30 import org.eclipse.debug.core.ILaunch; | 31 import org.eclipse.debug.core.ILaunch; |
31 import org.eclipse.debug.core.ILaunchConfiguration; | 32 import org.eclipse.debug.core.ILaunchConfiguration; |
32 | 33 |
33 import java.io.File; | 34 import java.io.File; |
34 import java.net.URI; | 35 import java.net.URI; |
35 import java.net.URISyntaxException; | 36 import java.net.URISyntaxException; |
36 import java.util.Map; | 37 import java.util.Map; |
37 import java.util.concurrent.CountDownLatch; | 38 import java.util.concurrent.CountDownLatch; |
38 import java.util.concurrent.TimeUnit; | 39 import java.util.concurrent.TimeUnit; |
39 | 40 |
40 /** | 41 /** |
41 * A helper for converting URIs to files paths and vice versa. | 42 * A helper for converting URIs to files paths and vice versa. |
42 */ | 43 */ |
43 public class UriToFileResolver { | 44 public class UriToFileResolver { |
44 private final IResource resource; | 45 private IContainer container; |
45 private final String resourcePath; | |
46 private final Map<String, String> urlToFileCache = Maps.newHashMap(); | 46 private final Map<String, String> urlToFileCache = Maps.newHashMap(); |
47 private final Map<String, String> fileToUriCache = Maps.newHashMap(); | 47 private final Map<String, String> fileToUriCache = Maps.newHashMap(); |
48 | 48 |
49 private String executionContextId; | 49 private String executionContextId; |
50 | 50 |
51 public UriToFileResolver(ILaunch launch) { | 51 public UriToFileResolver(ILaunch launch) { |
52 ILaunchConfiguration launchConfiguration = launch.getLaunchConfiguration(); | 52 ILaunchConfiguration launchConfiguration = launch.getLaunchConfiguration(); |
53 DartLaunchConfigWrapper wrapper = new DartLaunchConfigWrapper(launchConfigur
ation); | 53 DartLaunchConfigWrapper wrapper = new DartLaunchConfigWrapper(launchConfigur
ation); |
54 resource = wrapper.getApplicationResource(); | 54 |
55 resourcePath = resource != null ? resource.getLocation().toOSString() : null
; | 55 container = wrapper.getSourceDirectory(); |
| 56 if (container == null) { |
| 57 IResource resource = wrapper.getApplicationResource(); |
| 58 |
| 59 if (resource instanceof IContainer) { |
| 60 container = resource.getParent(); |
| 61 } else if (resource != null) { |
| 62 container = resource.getParent(); |
| 63 } |
| 64 } |
| 65 |
| 66 String resourcePath = container != null ? container.getLocation().toOSString
() : null; |
| 67 |
56 // create an execution context | 68 // create an execution context |
57 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER && resourcePath != null) { | 69 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER && resourcePath != null) { |
58 DartCore.getAnalysisServer().execution_createContext( | 70 DartCore.getAnalysisServer().execution_createContext( |
59 resourcePath, | 71 resourcePath, |
60 new CreateContextConsumer() { | 72 new CreateContextConsumer() { |
61 @Override | 73 @Override |
62 public void computedExecutionContext(String contextId) { | 74 public void computedExecutionContext(String contextId) { |
63 executionContextId = contextId; | 75 executionContextId = contextId; |
64 } | 76 } |
65 }); | 77 }); |
66 } | 78 } |
67 } | 79 } |
68 | 80 |
69 public void dispose() { | 81 public void dispose() { |
70 // delete the execution context | 82 // delete the execution context |
71 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER && executionContextId != null) { | 83 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER && executionContextId != null) { |
72 DartCore.getAnalysisServer().execution_deleteContext(executionContextId); | 84 DartCore.getAnalysisServer().execution_deleteContext(executionContextId); |
73 executionContextId = null; | 85 executionContextId = null; |
74 } | 86 } |
75 } | 87 } |
76 | 88 |
77 public String getFileForUri(String url) { | 89 public String getFileForUri(String url) { |
78 if (resource == null) { | 90 if (container == null) { |
79 return null; | 91 return null; |
80 } | 92 } |
81 String file = urlToFileCache.get(url); | 93 String file = urlToFileCache.get(url); |
82 if (file == null) { | 94 if (file == null) { |
83 file = getFileForUri0(url); | 95 file = getFileForUri0(url); |
84 urlToFileCache.put(url, file); | 96 urlToFileCache.put(url, file); |
85 } | 97 } |
86 return file; | 98 return file; |
87 } | 99 } |
88 | 100 |
89 public String getUriForPath(String file) { | 101 public String getUriForPath(String file) { |
90 if (resource == null) { | 102 if (container == null) { |
91 return null; | 103 return null; |
92 } | 104 } |
93 String uri = fileToUriCache.get(file); | 105 String uri = fileToUriCache.get(file); |
94 if (uri == null) { | 106 if (uri == null) { |
95 uri = getUriForPath0(file); | 107 uri = getUriForPath0(file); |
96 fileToUriCache.put(file, uri); | 108 fileToUriCache.put(file, uri); |
97 } | 109 } |
98 return uri; | 110 return uri; |
99 } | 111 } |
100 | 112 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Handle both fully absolute path names and http: urls. | 152 // Handle both fully absolute path names and http: urls. |
141 if (url.startsWith("/")) { | 153 if (url.startsWith("/")) { |
142 return url; | 154 return url; |
143 } else if (uriScheme == null) { | 155 } else if (uriScheme == null) { |
144 // handle relative file path | 156 // handle relative file path |
145 filePath = resolveRelativePath(url); | 157 filePath = resolveRelativePath(url); |
146 } else { | 158 } else { |
147 filePath = uri.getPath(); | 159 filePath = uri.getPath(); |
148 } | 160 } |
149 | 161 |
150 // dart: | |
151 if (filePath == null) { | 162 if (filePath == null) { |
152 return null; | 163 return null; |
153 } else { | 164 } else { |
154 return filePath; | 165 return returnAbsoluteOrRelative(filePath); |
155 } | 166 } |
156 } catch (URISyntaxException e) { | 167 } catch (URISyntaxException e) { |
157 return null; | 168 return null; |
158 } | 169 } |
159 } | 170 } |
160 | 171 |
161 /** | 172 /** |
162 * It seems that {@link ProjectManager#resolvePathToPackage} works only in lim
ited cases. So, here | 173 * It seems that {@link ProjectManager#resolvePathToPackage} works only in lim
ited cases. So, here |
163 * is a copy of another implementation. | 174 * is a copy of another implementation. |
164 */ | 175 */ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS); | 231 Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS); |
221 return uriPtr[0]; | 232 return uriPtr[0]; |
222 } | 233 } |
223 } else { | 234 } else { |
224 { | 235 { |
225 String uri = getUriForPath_fromServerBreakpointManager(file); | 236 String uri = getUriForPath_fromServerBreakpointManager(file); |
226 if (uri != null) { | 237 if (uri != null) { |
227 return uri; | 238 return uri; |
228 } | 239 } |
229 } | 240 } |
230 return DartCore.getProjectManager().resolvePathToPackage(resource, file); | 241 return DartCore.getProjectManager().resolvePathToPackage(container, file); |
231 } | 242 } |
232 return null; | 243 return null; |
233 } | 244 } |
234 | 245 |
235 private String resolvePackageUri(String url) { | 246 private String resolvePackageUri(String url) { |
236 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER) { | 247 if (DartCoreDebug.ENABLE_ANALYSIS_SERVER) { |
237 if (executionContextId != null) { | 248 if (executionContextId != null) { |
238 final String[] filePtr = {null}; | 249 final String[] filePtr = {null}; |
239 final CountDownLatch latch = new CountDownLatch(1); | 250 final CountDownLatch latch = new CountDownLatch(1); |
240 DartCore.getAnalysisServer().execution_mapUri( | 251 DartCore.getAnalysisServer().execution_mapUri( |
241 executionContextId, | 252 executionContextId, |
242 null, | 253 null, |
243 url, | 254 url, |
244 new MapUriConsumer() { | 255 new MapUriConsumer() { |
245 @Override | 256 @Override |
246 public void computedFileOrUri(String file, String uri) { | 257 public void computedFileOrUri(String file, String uri) { |
247 filePtr[0] = file; | 258 filePtr[0] = file; |
248 latch.countDown(); | 259 latch.countDown(); |
249 } | 260 } |
250 }); | 261 }); |
251 Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS); | 262 Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS); |
252 return filePtr[0]; | 263 return filePtr[0]; |
253 } | 264 } |
254 } else { | 265 } else { |
255 ProjectManager projectManager = DartCore.getProjectManager(); | 266 ProjectManager projectManager = DartCore.getProjectManager(); |
256 IFileInfo fileInfo = projectManager.resolveUriToFileInfo(resource, url); | 267 IFileInfo fileInfo = projectManager.resolveUriToFileInfo(container, url); |
257 if (fileInfo != null) { | 268 if (fileInfo != null) { |
258 File file = fileInfo.getFile(); | 269 File file = fileInfo.getFile(); |
259 File absoluteFile = file.getAbsoluteFile(); | 270 File absoluteFile = file.getAbsoluteFile(); |
260 return absoluteFile.toURI().toString(); | 271 return absoluteFile.toURI().toString(); |
261 } | 272 } |
262 } | 273 } |
263 return null; | 274 return null; |
264 } | 275 } |
265 | 276 |
266 private String resolveRelativePath(String url) { | 277 private String resolveRelativePath(String url) { |
267 if (resource != null) { | 278 if (container != null) { |
268 IResource file = resource.getParent().findMember(url); | 279 IResource file = container.getParent().findMember(url); |
269 if (file != null) { | 280 if (file != null) { |
270 return file.getLocation().toOSString(); | 281 return file.getLocation().toOSString(); |
271 } | 282 } |
272 } | 283 } |
273 return null; | 284 return null; |
274 } | 285 } |
| 286 |
| 287 private String returnAbsoluteOrRelative(String path) { |
| 288 // If path exists, return that. |
| 289 File file = new File(path); |
| 290 if (file.exists()) { |
| 291 return path; |
| 292 } |
| 293 |
| 294 // Try and return a path relative to the resource. |
| 295 if (container != null) { |
| 296 int index = path.indexOf('/'); |
| 297 |
| 298 while (index != -1) { |
| 299 String subPath = path.substring(index + 1); |
| 300 IResource resource = container.findMember(subPath); |
| 301 if (resource != null) { |
| 302 return resource.getLocation().toFile().getAbsolutePath(); |
| 303 } |
| 304 index = path.indexOf('/', index + 1); |
| 305 } |
| 306 } |
| 307 |
| 308 // Else, return the original path. |
| 309 return path; |
| 310 } |
275 } | 311 } |
OLD | NEW |