Skip to content

Commit

Permalink
test(html): test resolveReference
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 22, 2024
1 parent 7ac2881 commit ecf0af2
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 30 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: testing

on: [push, pull_request]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [18]
os: [macos-latest, windows-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v2

- uses: pnpm/action-setup@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

# install pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm run build
- run: pnpm run test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"build": "tsc -b",
"watch": "tsc -b -w",
"test": "vitest run",
"prerelease": "npm run build",
"release:base": "lerna publish --exact --force-publish --yes --sync-workspace-lock",
"release": "npm run release:base",
Expand All @@ -20,6 +21,7 @@
"@tsslint/cli": "latest",
"@tsslint/config": "latest",
"@volar/language-service": "~2.3.1",
"typescript": "latest"
"typescript": "latest",
"vitest": "latest"
}
}
41 changes: 28 additions & 13 deletions packages/css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ export interface Provide {
'css/languageService': (document: TextDocument) => css.LanguageService | undefined;
}

export function resolveReference(ref: string, baseUri: URI, workspaceFolders: URI[]) {
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
const folderUri = getRootFolder();
if (folderUri) {
return folderUri + ref.substr(1);
}
}
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);

function getRootFolder(): string | undefined {
for (const folder of workspaceFolders) {
let folderURI = folder.toString();
if (!folderURI.endsWith('/')) {
folderURI = folderURI + '/';
}
if (baseUri.toString().startsWith(folderURI)) {
return folderURI;
}
}
}
}

export function create({
cssDocumentSelector = ['css'],
scssDocumentSelector = ['scss'],
Expand All @@ -21,19 +48,7 @@ export function create({
if (decoded) {
baseUri = decoded[0];
}
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/' && context.env.workspaceFolders.length) { // resolve absolute path against the current workspace folder
let folderUri = context.env.workspaceFolders[0].toString();
if (!folderUri.endsWith('/')) {
folderUri += '/';
}
return folderUri + ref.substring(1);
}
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);
return resolveReference(ref, baseUri, context.env.workspaceFolders);
},
};
},
Expand Down
43 changes: 29 additions & 14 deletions packages/html/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Disposable, DocumentSelector, FormattingOptions, ProviderResult, LanguageServiceContext, LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service';
import type { Disposable, DocumentSelector, FormattingOptions, LanguageServiceContext, LanguageServicePlugin, LanguageServicePluginInstance, ProviderResult } from '@volar/language-service';
import * as html from 'vscode-html-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { URI, Utils } from 'vscode-uri';
Expand All @@ -10,6 +10,33 @@ export interface Provide {
'html/documentContext': () => html.DocumentContext;
}

export function resolveReference(ref: string, baseUri: URI, workspaceFolders: URI[]) {
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
const folderUri = getRootFolder();
if (folderUri) {
return folderUri + ref.substr(1);
}
}
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);

function getRootFolder(): string | undefined {
for (const folder of workspaceFolders) {
let folderURI = folder.toString();
if (!folderURI.endsWith('/')) {
folderURI = folderURI + '/';
}
if (baseUri.toString().startsWith(folderURI)) {
return folderURI;
}
}
}
}

export function create({
documentSelector = ['html'],
configurationSections = {
Expand All @@ -25,19 +52,7 @@ export function create({
if (decoded) {
baseUri = decoded[0];
}
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/' && context.env.workspaceFolders.length) { // resolve absolute path against the current workspace folder
let folderUri = context.env.workspaceFolders[0].toString();
if (!folderUri.endsWith('/')) {
folderUri += '/';
}
return folderUri + ref.substring(1);
}
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);
return resolveReference(ref, baseUri, context.env.workspaceFolders);
},
};
},
Expand Down
16 changes: 16 additions & 0 deletions packages/html/tests/resolveReference.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, test } from 'vitest';
import { URI } from 'vscode-uri';
import { resolveReference } from '..';

describe('HTML Document Context', () => {

test('resolveReference', () => {
const docURI = URI.parse('file:///users/test/folder/test.html');
const rootFolders = [URI.parse('file:///users/test/')];

expect(resolveReference('/', docURI, rootFolders)).toBe('file:///users/test/');
expect(resolveReference('/message.html', docURI, rootFolders)).toBe('file:///users/test/message.html');
expect(resolveReference('message.html', docURI, rootFolders)).toBe('file:///users/test/folder/message.html');
expect(resolveReference('message.html', URI.parse('file:///users/test/'), rootFolders)).toBe('file:///users/test/message.html');
});
});
31 changes: 29 additions & 2 deletions packages/json/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,45 @@ export interface JSONSchemaSettings {
folderUri?: string;
}

export function resolveReference(ref: string, baseUri: URI, workspaceFolders: URI[]) {
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
const folderUri = getRootFolder();
if (folderUri) {
return folderUri + ref.substr(1);
}
}
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);

function getRootFolder(): string | undefined {
for (const folder of workspaceFolders) {
let folderURI = folder.toString();
if (!folderURI.endsWith('/')) {
folderURI = folderURI + '/';
}
if (baseUri.toString().startsWith(folderURI)) {
return folderURI;
}
}
}
}

export function create({
documentSelector = ['json', 'jsonc'],
getWorkspaceContextService = context => {
return {
resolveRelativePath(relativePath, resource) {
resolveRelativePath(ref, resource) {
const base = resource.substring(0, resource.lastIndexOf('/') + 1);
let baseUri = URI.parse(base);
const decoded = context.decodeEmbeddedDocumentUri(baseUri);
if (decoded) {
baseUri = decoded[0];
}
return Utils.resolvePath(baseUri, relativePath).toString();
return resolveReference(ref, baseUri, context.env.workspaceFolders);
},
};
},
Expand Down
Loading

0 comments on commit ecf0af2

Please sign in to comment.