UNPKG

7 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.URI = void 0;
19const vscode_uri_1 = require("vscode-uri");
20const path_1 = require("./path");
21class URI {
22 constructor(uri = '') {
23 if (uri instanceof vscode_uri_1.URI) {
24 this.codeUri = uri;
25 }
26 else {
27 this.codeUri = vscode_uri_1.URI.parse(uri);
28 }
29 }
30 static fromComponents(components) {
31 return new URI(vscode_uri_1.URI.revive(components));
32 }
33 static fromFilePath(path) {
34 return new URI(vscode_uri_1.URI.file(path));
35 }
36 /**
37 * TODO move implementation to `DefaultUriLabelProviderContribution.getName`
38 *
39 * @deprecated use `LabelProvider.getName` instead
40 */
41 get displayName() {
42 const base = this.path.base;
43 if (base) {
44 return base;
45 }
46 if (this.path.isRoot) {
47 return this.path.fsPath();
48 }
49 return '';
50 }
51 /**
52 * Return all uri from the current to the top most.
53 */
54 get allLocations() {
55 const locations = [];
56 let location = this;
57 while (!location.path.isRoot && location.path.hasDir) {
58 locations.push(location);
59 location = location.parent;
60 }
61 locations.push(location);
62 return locations;
63 }
64 get parent() {
65 if (this.path.isRoot) {
66 return this;
67 }
68 return this.withPath(this.path.dir);
69 }
70 relative(uri) {
71 if (this.authority !== uri.authority || this.scheme !== uri.scheme) {
72 return undefined;
73 }
74 return this.path.relative(uri.path);
75 }
76 resolve(path) {
77 return this.withPath(this.path.join(path.toString()));
78 }
79 /**
80 * @returns a new, absolute URI if one can be computed from the path segments passed in.
81 */
82 resolveToAbsolute(...pathSegments) {
83 const absolutePath = this.path.resolve(...pathSegments.map(path => path.toString()));
84 if (absolutePath) {
85 return this.withPath(absolutePath);
86 }
87 }
88 /**
89 * return a new URI replacing the current with the given scheme
90 */
91 withScheme(scheme) {
92 const newCodeUri = vscode_uri_1.URI.from(Object.assign(Object.assign({}, this.codeUri.toJSON()), { scheme }));
93 return new URI(newCodeUri);
94 }
95 /**
96 * return a new URI replacing the current with the given authority
97 */
98 withAuthority(authority) {
99 const newCodeUri = vscode_uri_1.URI.from(Object.assign(Object.assign({}, this.codeUri.toJSON()), { scheme: this.codeUri.scheme, authority }));
100 return new URI(newCodeUri);
101 }
102 /**
103 * return this URI without a authority
104 */
105 withoutAuthority() {
106 return this.withAuthority('');
107 }
108 /**
109 * return a new URI replacing the current with the given path
110 */
111 withPath(path) {
112 const newCodeUri = vscode_uri_1.URI.from(Object.assign(Object.assign({}, this.codeUri.toJSON()), { scheme: this.codeUri.scheme, path: path.toString() }));
113 return new URI(newCodeUri);
114 }
115 /**
116 * return this URI without a path
117 */
118 withoutPath() {
119 return this.withPath('');
120 }
121 /**
122 * return a new URI replacing the current with the given query
123 */
124 withQuery(query) {
125 const newCodeUri = vscode_uri_1.URI.from(Object.assign(Object.assign({}, this.codeUri.toJSON()), { scheme: this.codeUri.scheme, query }));
126 return new URI(newCodeUri);
127 }
128 /**
129 * return this URI without a query
130 */
131 withoutQuery() {
132 return this.withQuery('');
133 }
134 /**
135 * return a new URI replacing the current with the given fragment
136 */
137 withFragment(fragment) {
138 const newCodeUri = vscode_uri_1.URI.from(Object.assign(Object.assign({}, this.codeUri.toJSON()), { scheme: this.codeUri.scheme, fragment }));
139 return new URI(newCodeUri);
140 }
141 /**
142 * return this URI without a fragment
143 */
144 withoutFragment() {
145 return this.withFragment('');
146 }
147 /**
148 * return a new URI replacing the current with its normalized path, resolving '..' and '.' segments
149 */
150 normalizePath() {
151 return this.withPath(this.path.normalize());
152 }
153 get scheme() {
154 return this.codeUri.scheme;
155 }
156 get authority() {
157 return this.codeUri.authority;
158 }
159 get path() {
160 if (this._path === undefined) {
161 this._path = new path_1.Path(this.codeUri.path);
162 }
163 return this._path;
164 }
165 get query() {
166 return this.codeUri.query;
167 }
168 get fragment() {
169 return this.codeUri.fragment;
170 }
171 toString(skipEncoding) {
172 return this.codeUri.toString(skipEncoding);
173 }
174 isEqual(uri, caseSensitive = true) {
175 if (!this.hasSameOrigin(uri)) {
176 return false;
177 }
178 return caseSensitive
179 ? this.toString() === uri.toString()
180 : this.toString().toLowerCase() === uri.toString().toLowerCase();
181 }
182 isEqualOrParent(uri, caseSensitive = true) {
183 if (!this.hasSameOrigin(uri)) {
184 return false;
185 }
186 let left = this.path;
187 let right = uri.path;
188 if (!caseSensitive) {
189 left = new path_1.Path(left.toString().toLowerCase());
190 right = new path_1.Path(right.toString().toLowerCase());
191 }
192 return left.isEqualOrParent(right);
193 }
194 static getDistinctParents(uris) {
195 const result = [];
196 uris.forEach((uri, i) => {
197 if (!uris.some((otherUri, index) => index !== i && otherUri.isEqualOrParent(uri))) {
198 result.push(uri);
199 }
200 });
201 return result;
202 }
203 hasSameOrigin(uri) {
204 return (this.authority === uri.authority) && (this.scheme === uri.scheme);
205 }
206 toComponents() {
207 return {
208 scheme: this.scheme,
209 authority: this.authority,
210 path: this.path.toString(),
211 query: this.query,
212 fragment: this.fragment
213 };
214 }
215}
216exports.URI = URI;
217exports.default = URI;
218//# sourceMappingURL=uri.js.map
\No newline at end of file