private static _asFormatted()

in language-service/src/utils/uri.ts [272:326]


	private static _asFormatted(uri: URI, skipEncoding: boolean): string {

		const encoder = !skipEncoding
			? encodeURIComponent2
			: encodeNoop;

		const parts: string[] = [];

		let { scheme, authority, path, query, fragment } = uri;
		if (scheme) {
			parts.push(scheme, ':');
		}
		if (authority || scheme === 'file') {
			parts.push('//');
		}
		if (authority) {
			authority = authority.toLowerCase();
			let idx = authority.indexOf(':');
			if (idx === -1) {
				parts.push(encoder(authority));
			} else {
				parts.push(encoder(authority.substr(0, idx)), authority.substr(idx));
			}
		}
		if (path) {
			// lower-case windown drive letters in /C:/fff
			const m = URI._upperCaseDrive.exec(path);
			if (m) {
				path = m[1] + m[2].toLowerCase() + path.substr(m[1].length + m[2].length);
			}

			// encode every segement but not slashes
			// make sure that # and ? are always encoded
			// when occurring in paths - otherwise the result
			// cannot be parsed back again
			let lastIdx = 0;
			while (true) {
				let idx = path.indexOf(URI._slash, lastIdx);
				if (idx === -1) {
					parts.push(encoder(path.substring(lastIdx)).replace(/[#?]/, _encode));
					break;
				}
				parts.push(encoder(path.substring(lastIdx, idx)).replace(/[#?]/, _encode), URI._slash);
				lastIdx = idx + 1;
			};
		}
		if (query) {
			parts.push('?', encoder(query));
		}
		if (fragment) {
			parts.push('#', encoder(fragment));
		}

		return parts.join(URI._empty);
	}