[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

@ -104,6 +104,7 @@ export function basename(uri: string) {
}
const Slash = '/'.charCodeAt(0);
const Dot = '.'.charCodeAt(0);
export function isAbsolutePath(path: string) {
return path.charCodeAt(0) === Slash;
@ -111,18 +112,37 @@ export function isAbsolutePath(path: string) {
export function resolvePath(uri: Uri, path: string): Uri {
if (isAbsolutePath(path)) {
return uri.with({ path: path });
return uri.with({ path: normalizePath(path.split('/')) });
}
return joinPath(uri, path);
}
export function joinPath(uri: Uri, ...paths: string[]): Uri {
let uriPath = uri.path;
for (let path of paths) {
if (path.charCodeAt(path.length - 1) !== Slash && path.charCodeAt(0) !== Slash) {
uriPath += '/';
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);
}
uriPath += path;
}
return uri.with({ path: uriPath });
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(uri: Uri, ...paths: string[]): Uri {
const parts = uri.path.split('/');
for (let path of paths) {
parts.push(...path.split('/'));
}
return uri.with({ path: normalizePath(parts) });
}

View file

@ -10,9 +10,9 @@
"main": "./out/node/cssServerMain",
"browser": "./dist/browser/cssServerMain",
"dependencies": {
"vscode-css-languageservice": "4.3.0-next.1",
"vscode-css-languageservice": "4.3.0-next.2",
"vscode-languageserver": "7.0.0-next.3",
"vscode-uri": "^2.1.1"
"vscode-uri": "^2.1.2"
},
"devDependencies": {
"@types/mocha": "7.0.2",

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();
}

View file

@ -0,0 +1,54 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'mocha';
import * as assert from 'assert';
import { joinPath, normalizePath, resolvePath } from '../requests';
suite('requests', () => {
test('join', async function () {
assert.equal(joinPath('foo://a/foo/bar', 'x'), 'foo://a/foo/bar/x');
assert.equal(joinPath('foo://a/foo/bar/', 'x'), 'foo://a/foo/bar/x');
assert.equal(joinPath('foo://a/foo/bar/', '/x'), 'foo://a/foo/bar/x');
assert.equal(joinPath('foo://a/foo/bar/', 'x/'), 'foo://a/foo/bar/x/');
assert.equal(joinPath('foo://a/foo/bar/', 'x', 'y'), 'foo://a/foo/bar/x/y');
assert.equal(joinPath('foo://a/foo/bar/', 'x/', '/y'), 'foo://a/foo/bar/x/y');
assert.equal(joinPath('foo://a/foo/bar/', '.', '/y'), 'foo://a/foo/bar/y');
assert.equal(joinPath('foo://a/foo/bar/', 'x/y/z', '..'), 'foo://a/foo/bar/x/y');
});
test('resolve', async function () {
assert.equal(resolvePath('foo://a/foo/bar', 'x'), 'foo://a/foo/bar/x');
assert.equal(resolvePath('foo://a/foo/bar/', 'x'), 'foo://a/foo/bar/x');
assert.equal(resolvePath('foo://a/foo/bar/', '/x'), 'foo://a/x');
assert.equal(resolvePath('foo://a/foo/bar/', 'x/'), 'foo://a/foo/bar/x/');
});
test('normalize', async function () {
function assertNormalize(path: string, expected: string) {
assert.equal(normalizePath(path.split('/')), expected, path);
}
assertNormalize('a', 'a');
assertNormalize('/a', '/a');
assertNormalize('a/', 'a/');
assertNormalize('a/b', 'a/b');
assertNormalize('/a/foo/bar/x', '/a/foo/bar/x');
assertNormalize('/a/foo/bar//x', '/a/foo/bar/x');
assertNormalize('/a/foo/bar///x', '/a/foo/bar/x');
assertNormalize('/a/foo/bar/x/', '/a/foo/bar/x/');
assertNormalize('a/foo/bar/x/', 'a/foo/bar/x/');
assertNormalize('a/foo/bar/x//', 'a/foo/bar/x/');
assertNormalize('//a/foo/bar/x//', '/a/foo/bar/x/');
assertNormalize('a/.', 'a');
assertNormalize('a/./b', 'a/b');
assertNormalize('a/././b', 'a/b');
assertNormalize('a/n/../b', 'a/b');
assertNormalize('a/n/../', 'a/');
assertNormalize('a/n/../', 'a/');
assertNormalize('/a/n/../..', '/');
assertNormalize('..', '');
assertNormalize('/..', '/');
});
});

View file

@ -5,8 +5,8 @@
import { DocumentContext } from 'vscode-css-languageservice';
import { endsWith, startsWith } from '../utils/strings';
import * as url from 'url';
import { WorkspaceFolder } from 'vscode-languageserver';
import { resolvePath } from '../requests';
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
function getRootFolder(): string | undefined {
@ -30,7 +30,8 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
return folderUri + ref.substr(1);
}
}
return url.resolve(base, ref);
base = base.substr(0, base.lastIndexOf('/') + 1);
return resolvePath(base, ref);
},
};
}

View file

@ -701,15 +701,15 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"
vscode-css-languageservice@4.3.0-next.1:
version "4.3.0-next.1"
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-4.3.0-next.1.tgz#6e1ddf5c469468585bdb91b554ffdf961915cd10"
integrity sha512-Ks/MbHDyUFMMF5DQ3hqh6xjT+EoEjWsdeOwgWTCg+caConhweO2Y8VpZO3qDpbBO5dTnGlcxVG07sxOZyxF77Q==
vscode-css-languageservice@4.3.0-next.2:
version "4.3.0-next.2"
resolved "https://registry.yarnpkg.com/vscode-css-languageservice/-/vscode-css-languageservice-4.3.0-next.2.tgz#a7a1289d8d68ddcdee55d4f18b12a455acaf5962"
integrity sha512-4h/s/N7wt6If/5EUNMtfAbwWwImH6EvveqZMf9SmQdMMMqekZkRLA68E98hGzuzI13rHEiLckwlAC+RNLq6FXg==
dependencies:
vscode-languageserver-textdocument "^1.0.1"
vscode-languageserver-types "3.16.0-next.1"
vscode-languageserver-types "3.16.0-next.2"
vscode-nls "^4.1.2"
vscode-uri "^2.1.1"
vscode-uri "^2.1.2"
vscode-jsonrpc@6.0.0-next.2:
version "6.0.0-next.2"
@ -729,11 +729,6 @@ vscode-languageserver-textdocument@^1.0.1:
resolved "https://registry.yarnpkg.com/vscode-languageserver-textdocument/-/vscode-languageserver-textdocument-1.0.1.tgz#178168e87efad6171b372add1dea34f53e5d330f"
integrity sha512-UIcJDjX7IFkck7cSkNNyzIz5FyvpQfY7sdzVy+wkKN/BLaD4DQ0ppXQrKePomCxTS7RrolK1I0pey0bG9eh8dA==
vscode-languageserver-types@3.16.0-next.1:
version "3.16.0-next.1"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0-next.1.tgz#97e9803c4a999e81e3ff000a91b630173eaaf28c"
integrity sha512-tZFUSbyjUcrh+qQf13ALX4QDdOfDX0cVaBFgy7ktJ0VwS7AW/yRKgGPSxVqqP9OCMNPdqP57O5q47w2pEwfaUg==
vscode-languageserver-types@3.16.0-next.2:
version "3.16.0-next.2"
resolved "https://registry.yarnpkg.com/vscode-languageserver-types/-/vscode-languageserver-types-3.16.0-next.2.tgz#940bd15c992295a65eae8ab6b8568a1e8daa3083"
@ -751,10 +746,10 @@ vscode-nls@^4.1.2:
resolved "https://registry.yarnpkg.com/vscode-nls/-/vscode-nls-4.1.2.tgz#ca8bf8bb82a0987b32801f9fddfdd2fb9fd3c167"
integrity sha512-7bOHxPsfyuCqmP+hZXscLhiHwe7CSuFE4hyhbs22xPIhQ4jv99FcR4eBzfYYVLP356HNFpdvz63FFb/xw6T4Iw==
vscode-uri@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.1.tgz#5aa1803391b6ebdd17d047f51365cf62c38f6e90"
integrity sha512-eY9jmGoEnVf8VE8xr5znSah7Qt1P/xsCdErz+g8HYZtJ7bZqKH5E3d+6oVNm1AC/c6IHUDokbmVXKOi4qPAC9A==
vscode-uri@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.2.tgz#c8d40de93eb57af31f3c715dd650e2ca2c096f1c"
integrity sha512-8TEXQxlldWAuIODdukIb+TR5s+9Ds40eSJrw+1iDDA9IFORPjMELarNQE3myz5XIkWWpdprmJjm1/SxMlWOC8A==
which-module@^2.0.0:
version "2.0.0"