[css] path completion in web

This commit is contained in:
Martin Aeschlimann
2020-05-28 21:54:35 +02:00
parent 1ece4c4c25
commit eba3d294a2
6 changed files with 137 additions and 34 deletions

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
import { URI } from 'vscode-uri';
import { endsWith, startsWith } from './utils/strings';
import { RequestType, Connection } from 'vscode-languageserver';
import { RuntimeEnvironment } from './cssServer';
@@ -114,14 +113,48 @@ export function basename(uri: string) {
return uri.substr(lastIndexOfSlash + 1);
}
const Slash = '/'.charCodeAt(0);
const Dot = '.'.charCodeAt(0);
export function isAbsolutePath(path: string) {
return path.charCodeAt(0) === Slash;
}
export function resolvePath(uriString: string, path: string): string {
if (isAbsolutePath(path)) {
const uri = URI.parse(uriString);
const parts = path.split('/');
return uri.with({ path: normalizePath(parts) }).toString();
}
return joinPath(uriString, path);
}
export function normalizePath(parts: string[]): string {
const newParts: string[] = [];
for (const part of parts) {
if (part.length === 0 || part.length === 1 && part.charCodeAt(0) === Dot) {
// ignore
} else if (part.length === 2 && part.charCodeAt(0) === Dot && part.charCodeAt(1) === Dot) {
newParts.pop();
} else {
newParts.push(part);
}
}
if (parts.length > 1 && parts[parts.length - 1].length === 0) {
newParts.push('');
}
let res = newParts.join('/');
if (parts[0].length === 0) {
res = '/' + res;
}
return res;
}
export function joinPath(uriString: string, ...paths: string[]): string {
const uri = URI.parse(uriString);
let uriPath = uri.path;
const parts = uri.path.split('/');
for (let path of paths) {
if (!endsWith(uriPath, '/') && !startsWith(path, '/')) {
uriPath += '/';
}
uriPath += path;
parts.push(...path.split('/'));
}
return uri.with({ path: uriPath }).toString();
return uri.with({ path: normalizePath(parts) }).toString();
}