|
5 | 5 |
|
6 | 6 | package software.amazon.smithy.lsp.protocol; |
7 | 7 |
|
8 | | -import java.io.IOException; |
| 8 | +import java.net.MalformedURLException; |
9 | 9 | import java.net.URI; |
| 10 | +import java.net.URISyntaxException; |
10 | 11 | import java.net.URL; |
11 | | -import java.net.URLDecoder; |
12 | | -import java.nio.charset.StandardCharsets; |
13 | 12 | import java.nio.file.Paths; |
14 | | -import java.util.logging.Logger; |
15 | 13 | import org.eclipse.lsp4j.Location; |
16 | 14 | import org.eclipse.lsp4j.Position; |
17 | 15 | import org.eclipse.lsp4j.Range; |
|
28 | 26 | * 'smithyjar:' scheme we use. |
29 | 27 | */ |
30 | 28 | public final class LspAdapter { |
31 | | - private static final Logger LOGGER = Logger.getLogger(LspAdapter.class.getName()); |
32 | | - |
33 | 29 | private LspAdapter() { |
34 | 30 | } |
35 | 31 |
|
@@ -186,11 +182,11 @@ public static SourceLocation toSourceLocation(String path, Position position) { |
186 | 182 | * @return A path representation of the {@code uri}, with the scheme removed |
187 | 183 | */ |
188 | 184 | public static String toPath(String uri) { |
189 | | - if (uri.startsWith("file://")) { |
| 185 | + if (uri.startsWith("file:")) { |
190 | 186 | return Paths.get(URI.create(uri)).toString(); |
191 | 187 | } else if (isSmithyJarFile(uri)) { |
192 | | - String decoded = decode(uri); |
193 | | - return fixJarScheme(decoded); |
| 188 | + URI jarUri = smithyJarUriToJarModelFilename(uri); |
| 189 | + return jarUri.toString(); |
194 | 190 | } |
195 | 191 | return uri; |
196 | 192 | } |
@@ -231,36 +227,74 @@ public static boolean isJarFile(String uri) { |
231 | 227 | } |
232 | 228 |
|
233 | 229 | /** |
234 | | - * Get a {@link URL} for the Jar represented by the given URI or path. |
| 230 | + * Get a {@link URL} for the Jar represented by the given smithyjar URI. |
235 | 231 | * |
236 | | - * @param uriOrPath LSP URI or regular path |
237 | | - * @return The {@link URL}, or throw if the uri/path cannot be decoded |
| 232 | + * @param uri The smithyjar URI |
| 233 | + * @return A URL which can be used to read the contents of the file |
238 | 234 | */ |
239 | | - public static URL jarUrl(String uriOrPath) { |
| 235 | + public static URL smithyJarUriToReadableUrl(String uri) { |
240 | 236 | try { |
241 | | - String decodedUri = decode(uriOrPath); |
242 | | - return URI.create(fixJarScheme(decodedUri)).toURL(); |
243 | | - } catch (IOException e) { |
| 237 | + URI jarUri = smithyJarUriToJarModelFilename(uri); |
| 238 | + return jarUri.toURL(); |
| 239 | + } catch (MalformedURLException e) { |
244 | 240 | throw new RuntimeException(e); |
245 | 241 | } |
246 | 242 | } |
247 | 243 |
|
248 | | - private static String decode(String uriOrPath) { |
249 | | - // Some clients encode parts of the jar, like !/ |
250 | | - return URLDecoder.decode(uriOrPath, StandardCharsets.UTF_8); |
| 244 | + /** |
| 245 | + * Get a {@link URL} for the jar file from the filename in the model's |
| 246 | + * source location. |
| 247 | + * |
| 248 | + * @param modelFilename The filename from the model's source location |
| 249 | + * @return A URL which can be used to read the contents of the file |
| 250 | + */ |
| 251 | + public static URL jarModelFilenameToReadableUrl(String modelFilename) { |
| 252 | + try { |
| 253 | + // No need to decode these, they're already encoded |
| 254 | + return URI.create(modelFilename).toURL(); |
| 255 | + } catch (MalformedURLException e) { |
| 256 | + throw new RuntimeException(e); |
| 257 | + } |
251 | 258 | } |
252 | 259 |
|
253 | | - private static String fixJarScheme(String uriOrPath) { |
254 | | - if (uriOrPath.startsWith("smithyjar:")) { |
255 | | - uriOrPath = uriOrPath.replaceFirst("smithyjar:", ""); |
256 | | - } |
257 | | - if (uriOrPath.startsWith("jar:")) { |
258 | | - return uriOrPath; |
259 | | - } else if (uriOrPath.startsWith("file:")) { |
260 | | - return "jar:" + uriOrPath; |
261 | | - } else { |
262 | | - return "jar:file:" + uriOrPath; |
| 260 | + /** |
| 261 | + * Converts a smithyjar uri that was sent from the client into a URI that |
| 262 | + * is equivalent to what appears in the Smithy model. |
| 263 | + * |
| 264 | + * @param smithyJarUri smithyjar uri received from the client |
| 265 | + * @return The converted URI |
| 266 | + */ |
| 267 | + private static URI smithyJarUriToJarModelFilename(String smithyJarUri) { |
| 268 | + // Clients encode URIs differently. VSCode is particularly aggressive with |
| 269 | + // its encoding, so the URIs it produces aren't equivalent to what we get |
| 270 | + // from source locations in the model. |
| 271 | + // |
| 272 | + // For example, given a jar that lives in some directory with special characters: |
| 273 | + // /path with spaces/foo.jar |
| 274 | + // The model will have a source location with a filename like: |
| 275 | + // jar:file:/path%20with%20spaces/foo.jar!/baz.smithy |
| 276 | + // When sending requests/notifications for this file, VSCode will encode the URI like: |
| 277 | + // smithyjar:/path%20with%20spaces/foo.jar%21/baz.smithy |
| 278 | + // Note the ! is encoded. |
| 279 | + // |
| 280 | + // If we just used URI.create().toString(), we will end up with the exact same |
| 281 | + // URI that VSCode sent, because URI.create() (and its equivalent ctor) keep |
| 282 | + // the original input string to use for the toString() call. |
| 283 | + // |
| 284 | + // Instead, we use getSchemeSpecificPart() to fully decode everything after the |
| 285 | + // smithyjar: part, to get: |
| 286 | + // /path with spaces/foo.jar!/baz.smithy |
| 287 | + // Then, we reconstruct the URI from parts, using a different ctor that performs |
| 288 | + // encoding. The resulting URI.toString() call will give us what we want: |
| 289 | + // jar:file:/path%20with%20spaces/foo.jar!/baz.smithy |
| 290 | + |
| 291 | + URI encodedUri = URI.create(smithyJarUri); |
| 292 | + String decodedPath = encodedUri.getSchemeSpecificPart(); |
| 293 | + |
| 294 | + try { |
| 295 | + return new URI("jar", "file:" + decodedPath, null); |
| 296 | + } catch (URISyntaxException e) { |
| 297 | + throw new RuntimeException(e); |
263 | 298 | } |
264 | 299 | } |
265 | | - |
266 | 300 | } |
0 commit comments