Skip to content

Commit

Permalink
refactor(html): expose createDocumentContext()
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 17, 2024
1 parent 1aa5663 commit 66557bc
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions packages/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,7 @@ export function create({
},
readDirectory: async (uri) => context.env.fs?.readDirectory(uri) ?? [],
};
const documentContext: html.DocumentContext = {
resolveReference(ref, base) {
if (ref.match(/^\w[\w\d+.-]*:/)) {
// starts with a schema
return ref;
}
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
let folderUri = context.env.workspaceFolder;
if (!folderUri.endsWith('/')) {
folderUri += '/';
}
return folderUri + ref.substr(1);
}
const baseUri = URI.parse(base);
const baseUriDir = baseUri.path.endsWith('/') ? baseUri : Utils.dirname(baseUri);
return Utils.resolvePath(baseUriDir, ref).toString(true);
},
};
const documentContext = createDocumentContext(context.env.workspaceFolder);
const htmlLs = html.getLanguageService({
fileSystemProvider,
clientCapabilities: context.env.clientCapabilities,
Expand Down Expand Up @@ -344,6 +327,28 @@ export function create({
};
}

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

function isEOL(content: string, offset: number) {
return isNewlineCharacter(content.charCodeAt(offset));
}
Expand Down

0 comments on commit 66557bc

Please sign in to comment.