Chromium Code Reviews| Index: pkg/path/lib/src/context.dart |
| diff --git a/pkg/path/lib/src/context.dart b/pkg/path/lib/src/context.dart |
| index 9c8e1e46465b489ccfa7c13d6610d80c0562b489..c137bb016961c4376b40db6809dcefc207a71557 100644 |
| --- a/pkg/path/lib/src/context.dart |
| +++ b/pkg/path/lib/src/context.dart |
| @@ -472,6 +472,47 @@ class Context { |
| } |
| } |
| + /// Returns a human-readable representation of [uri]. |
| + /// |
| + /// If [uri] can be made relative to the current working directory, that's |
| + /// done. Otherwise, it's returned as-is. This gracefully handles non-`file:` |
| + /// URIs for [Style.posix] and [Style.windows]. |
| + /// |
| + /// The returned value is meant for human consumption, and may be either URI- |
| + /// or path-formatted. |
| + /// |
| + /// // POSIX |
| + /// var context = new Context(current: '/root/path'); |
| + /// context.formatUri('file:///root/path/a/b.dart'); // -> 'a/b.dart' |
| + /// context.formatUri('http://dartlang.org/'); // -> 'http://dartlang.org' |
| + /// |
| + /// // Windows |
| + /// var context = new Context(current: r'C:\root\path'); |
| + /// context.formatUri('file:///C:/root/path/a/b.dart'); // -> r'a\b.dart' |
| + /// context.formatUri('http://dartlang.org/'); // -> 'http://dartlang.org' |
| + /// |
| + /// // URL |
| + /// var context = new Context(current: 'http://dartlang.org/root/path'); |
| + /// context.formatUri('http://dartlang.org/root/path/a/b.dart'); |
| + /// // -> r'a/b.dart' |
| + /// context.formatUri('file:///root/path'); // -> 'file:///root/path' |
| + String formatUri(uri) { |
| + if (uri is String) uri = Uri.parse(uri); |
| + if (uri.scheme == 'file' && style == Style.url) return uri.toString(); |
| + if (uri.scheme != 'file' && uri.scheme != '' && style != Style.url) { |
| + return uri.toString(); |
| + } |
| + |
| + var path = fromUri(uri); |
| + var rel = relative(path); |
| + var components = split(rel); |
| + |
| + // Return whichever of the relative path and the original path has the |
| + // fewest components. This avoids ugly things like long "../" chains to get |
|
Bob Nystrom
2014/05/23 21:57:52
How about replacing the first sentence with:
"Onl
nweiz
2014/05/23 22:18:04
Done.
|
| + // to the root and then go back down. |
| + return split(rel).length > split(path).length ? path : rel; |
| + } |
| + |
| ParsedPath _parse(String path) => new ParsedPath.parse(path, style); |
| } |