Merge pull request #137557 from sijakret/html-language-server/virtual-doc-support
html-language-features: text document provider support for customData.html
This commit is contained in:
commit
c67fd6e5cd
|
@ -6,6 +6,7 @@
|
||||||
import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
|
import { workspace, extensions, Uri, EventEmitter, Disposable } from 'vscode';
|
||||||
import { resolvePath, joinPath } from './requests';
|
import { resolvePath, joinPath } from './requests';
|
||||||
|
|
||||||
|
|
||||||
export function getCustomDataSource(toDispose: Disposable[]) {
|
export function getCustomDataSource(toDispose: Disposable[]) {
|
||||||
let pathsInWorkspace = getCustomDataPathsInAllWorkspaces();
|
let pathsInWorkspace = getCustomDataPathsInAllWorkspaces();
|
||||||
let pathsInExtensions = getCustomDataPathsFromAllExtensions();
|
let pathsInExtensions = getCustomDataPathsFromAllExtensions();
|
||||||
|
@ -14,7 +15,7 @@ export function getCustomDataSource(toDispose: Disposable[]) {
|
||||||
|
|
||||||
toDispose.push(extensions.onDidChange(_ => {
|
toDispose.push(extensions.onDidChange(_ => {
|
||||||
const newPathsInExtensions = getCustomDataPathsFromAllExtensions();
|
const newPathsInExtensions = getCustomDataPathsFromAllExtensions();
|
||||||
if (newPathsInExtensions.length !== pathsInExtensions.length || !newPathsInExtensions.every((val, idx) => val === pathsInExtensions[idx])) {
|
if (pathsInExtensions.size !== newPathsInExtensions.size || ![...pathsInExtensions].every(path => newPathsInExtensions.has(path))) {
|
||||||
pathsInExtensions = newPathsInExtensions;
|
pathsInExtensions = newPathsInExtensions;
|
||||||
onChange.fire();
|
onChange.fire();
|
||||||
}
|
}
|
||||||
|
@ -26,9 +27,16 @@ export function getCustomDataSource(toDispose: Disposable[]) {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
toDispose.push(workspace.onDidChangeTextDocument(e => {
|
||||||
|
const path = e.document.uri.toString();
|
||||||
|
if (pathsInExtensions.has(path) || pathsInWorkspace.has(path)) {
|
||||||
|
onChange.fire();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get uris() {
|
get uris() {
|
||||||
return pathsInWorkspace.concat(pathsInExtensions);
|
return [...pathsInWorkspace].concat([...pathsInExtensions]);
|
||||||
},
|
},
|
||||||
get onDidChange() {
|
get onDidChange() {
|
||||||
return onChange.event;
|
return onChange.event;
|
||||||
|
@ -36,21 +44,31 @@ export function getCustomDataSource(toDispose: Disposable[]) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isURI(uriOrPath: string) {
|
||||||
|
return /^(?<scheme>\w[\w\d+.-]*):/.test(uriOrPath);
|
||||||
|
}
|
||||||
|
|
||||||
function getCustomDataPathsInAllWorkspaces(): string[] {
|
|
||||||
|
function getCustomDataPathsInAllWorkspaces(): Set<string> {
|
||||||
const workspaceFolders = workspace.workspaceFolders;
|
const workspaceFolders = workspace.workspaceFolders;
|
||||||
|
|
||||||
const dataPaths: string[] = [];
|
const dataPaths = new Set<string>();
|
||||||
|
|
||||||
if (!workspaceFolders) {
|
if (!workspaceFolders) {
|
||||||
return dataPaths;
|
return dataPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
const collect = (paths: string[] | undefined, rootFolder: Uri) => {
|
const collect = (uriOrPaths: string[] | undefined, rootFolder: Uri) => {
|
||||||
if (Array.isArray(paths)) {
|
if (Array.isArray(uriOrPaths)) {
|
||||||
for (const path of paths) {
|
for (const uriOrPath of uriOrPaths) {
|
||||||
if (typeof path === 'string') {
|
if (typeof uriOrPath === 'string') {
|
||||||
dataPaths.push(resolvePath(rootFolder, path).toString());
|
if (!isURI(uriOrPath)) {
|
||||||
|
// path in the workspace
|
||||||
|
dataPaths.add(resolvePath(rootFolder, uriOrPath).toString());
|
||||||
|
} else {
|
||||||
|
// external uri
|
||||||
|
dataPaths.add(uriOrPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,13 +92,20 @@ function getCustomDataPathsInAllWorkspaces(): string[] {
|
||||||
return dataPaths;
|
return dataPaths;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCustomDataPathsFromAllExtensions(): string[] {
|
function getCustomDataPathsFromAllExtensions(): Set<string> {
|
||||||
const dataPaths: string[] = [];
|
const dataPaths = new Set<string>();
|
||||||
for (const extension of extensions.all) {
|
for (const extension of extensions.all) {
|
||||||
const customData = extension.packageJSON?.contributes?.html?.customData;
|
const customData = extension.packageJSON?.contributes?.html?.customData;
|
||||||
if (Array.isArray(customData)) {
|
if (Array.isArray(customData)) {
|
||||||
for (const rp of customData) {
|
for (const uriOrPath of customData) {
|
||||||
dataPaths.push(joinPath(extension.extensionUri, rp).toString());
|
if (!isURI(uriOrPath)) {
|
||||||
|
// relative path in an extension
|
||||||
|
dataPaths.add(joinPath(extension.extensionUri, uriOrPath).toString());
|
||||||
|
} else {
|
||||||
|
// external uri
|
||||||
|
dataPaths.add(uriOrPath);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import {
|
||||||
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, TextDocumentIdentifier, RequestType0, Range as LspRange, NotificationType, CommonLanguageClient
|
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, TextDocumentIdentifier, RequestType0, Range as LspRange, NotificationType, CommonLanguageClient
|
||||||
} from 'vscode-languageclient';
|
} from 'vscode-languageclient';
|
||||||
import { activateTagClosing } from './tagClosing';
|
import { activateTagClosing } from './tagClosing';
|
||||||
import { RequestService } from './requests';
|
import { RequestService, serveFileSystemRequests } from './requests';
|
||||||
import { getCustomDataSource } from './customData';
|
import { getCustomDataSource } from './customData';
|
||||||
|
|
||||||
namespace CustomDataChangedNotification {
|
namespace CustomDataChangedNotification {
|
||||||
|
@ -120,6 +120,8 @@ export function startClient(context: ExtensionContext, newLanguageClient: Langua
|
||||||
toDispose.push(disposable);
|
toDispose.push(disposable);
|
||||||
client.onReady().then(() => {
|
client.onReady().then(() => {
|
||||||
|
|
||||||
|
toDispose.push(serveFileSystemRequests(client, runtime));
|
||||||
|
|
||||||
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
|
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
|
||||||
customDataSource.onDidChange(() => {
|
customDataSource.onDidChange(() => {
|
||||||
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
|
client.sendNotification(CustomDataChangedNotification.type, customDataSource.uris);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import { Uri, workspace } from 'vscode';
|
import { Uri, workspace, Disposable } from 'vscode';
|
||||||
import { RequestType, CommonLanguageClient } from 'vscode-languageclient';
|
import { RequestType, CommonLanguageClient } from 'vscode-languageclient';
|
||||||
import { Runtime } from './htmlClient';
|
import { Runtime } from './htmlClient';
|
||||||
|
|
||||||
|
@ -18,8 +18,9 @@ export namespace FsReadDirRequest {
|
||||||
export const type: RequestType<string, [string, FileType][], any> = new RequestType('fs/readDir');
|
export const type: RequestType<string, [string, FileType][], any> = new RequestType('fs/readDir');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime) {
|
export function serveFileSystemRequests(client: CommonLanguageClient, runtime: Runtime): Disposable {
|
||||||
client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
|
const disposables = [];
|
||||||
|
disposables.push(client.onRequest(FsContentRequest.type, (param: { uri: string; encoding?: string; }) => {
|
||||||
const uri = Uri.parse(param.uri);
|
const uri = Uri.parse(param.uri);
|
||||||
if (uri.scheme === 'file' && runtime.fs) {
|
if (uri.scheme === 'file' && runtime.fs) {
|
||||||
return runtime.fs.getContent(param.uri);
|
return runtime.fs.getContent(param.uri);
|
||||||
|
@ -27,21 +28,22 @@ export function serveFileSystemRequests(client: CommonLanguageClient, runtime: R
|
||||||
return workspace.fs.readFile(uri).then(buffer => {
|
return workspace.fs.readFile(uri).then(buffer => {
|
||||||
return new runtime.TextDecoder(param.encoding).decode(buffer);
|
return new runtime.TextDecoder(param.encoding).decode(buffer);
|
||||||
});
|
});
|
||||||
});
|
}));
|
||||||
client.onRequest(FsReadDirRequest.type, (uriString: string) => {
|
disposables.push(client.onRequest(FsReadDirRequest.type, (uriString: string) => {
|
||||||
const uri = Uri.parse(uriString);
|
const uri = Uri.parse(uriString);
|
||||||
if (uri.scheme === 'file' && runtime.fs) {
|
if (uri.scheme === 'file' && runtime.fs) {
|
||||||
return runtime.fs.readDirectory(uriString);
|
return runtime.fs.readDirectory(uriString);
|
||||||
}
|
}
|
||||||
return workspace.fs.readDirectory(uri);
|
return workspace.fs.readDirectory(uri);
|
||||||
});
|
}));
|
||||||
client.onRequest(FsStatRequest.type, (uriString: string) => {
|
disposables.push(client.onRequest(FsStatRequest.type, (uriString: string) => {
|
||||||
const uri = Uri.parse(uriString);
|
const uri = Uri.parse(uriString);
|
||||||
if (uri.scheme === 'file' && runtime.fs) {
|
if (uri.scheme === 'file' && runtime.fs) {
|
||||||
return runtime.fs.stat(uriString);
|
return runtime.fs.stat(uriString);
|
||||||
}
|
}
|
||||||
return workspace.fs.stat(uri);
|
return workspace.fs.stat(uri);
|
||||||
});
|
}));
|
||||||
|
return Disposable.from(...disposables);
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum FileType {
|
export enum FileType {
|
||||||
|
|
Loading…
Reference in a new issue