mirror of
https://github.com/microsoft/vscode.git
synced 2026-05-03 06:51:53 +01:00
Allow to run css language server headless
This commit is contained in:
@@ -7,29 +7,6 @@ import { DocumentContext } from 'vscode-css-languageservice';
|
||||
import { endsWith, startsWith } from '../utils/strings';
|
||||
import * as url from 'url';
|
||||
import { WorkspaceFolder } from 'vscode-languageserver';
|
||||
import { URI } from 'vscode-uri';
|
||||
import { join, dirname } from 'path';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
function getModuleNameFromPath(path: string) {
|
||||
// If a scoped module (starts with @) then get up until second instance of '/', otherwise get until first isntance of '/'
|
||||
if (path[0] === '@') {
|
||||
return path.substring(0, path.indexOf('/', path.indexOf('/') + 1));
|
||||
}
|
||||
return path.substring(0, path.indexOf('/'));
|
||||
}
|
||||
|
||||
function resolvePathToModule(_moduleName: string, _relativeToFolder: string, _rootFolder: string | undefined): string | undefined {
|
||||
// resolve the module relative to the document. We can't use `require` here as the code is webpacked.
|
||||
|
||||
const packPath = join(_relativeToFolder, 'node_modules', _moduleName, 'package.json');
|
||||
if (existsSync(packPath)) {
|
||||
return URI.file(packPath).toString();
|
||||
} else if (_rootFolder && _relativeToFolder.startsWith(_rootFolder) && (_relativeToFolder.length !== _rootFolder.length)) {
|
||||
return resolvePathToModule(_moduleName, dirname(_relativeToFolder), _rootFolder);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
|
||||
function getRootFolder(): string | undefined {
|
||||
@@ -46,37 +23,13 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
|
||||
}
|
||||
|
||||
return {
|
||||
resolveReference: (ref, base = documentUri) => {
|
||||
resolveReference: (ref: string, base = documentUri) => {
|
||||
if (ref[0] === '/') { // resolve absolute path against the current workspace folder
|
||||
if (startsWith(base, 'file://')) {
|
||||
let folderUri = getRootFolder();
|
||||
if (folderUri) {
|
||||
return folderUri + ref.substr(1);
|
||||
}
|
||||
let folderUri = getRootFolder();
|
||||
if (folderUri) {
|
||||
return folderUri + ref.substr(1);
|
||||
}
|
||||
}
|
||||
// Following [css-loader](https://github.com/webpack-contrib/css-loader#url)
|
||||
// and [sass-loader's](https://github.com/webpack-contrib/sass-loader#imports)
|
||||
// convention, if an import path starts with ~ then use node module resolution
|
||||
// *unless* it starts with "~/" as this refers to the user's home directory.
|
||||
if (ref[0] === '~' && ref[1] !== '/') {
|
||||
ref = ref.substring(1);
|
||||
if (startsWith(base, 'file://')) {
|
||||
const moduleName = getModuleNameFromPath(ref);
|
||||
const rootFolderUri = getRootFolder();
|
||||
let rootFolder;
|
||||
if (rootFolderUri) {
|
||||
rootFolder = URI.parse(rootFolderUri).fsPath;
|
||||
}
|
||||
const documentFolder = dirname(URI.parse(base).fsPath);
|
||||
const modulePath = resolvePathToModule(moduleName, documentFolder, rootFolder);
|
||||
if (modulePath) {
|
||||
const pathWithinModule = ref.substring(moduleName.length + 1);
|
||||
return url.resolve(modulePath, pathWithinModule);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return url.resolve(base, ref);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -38,30 +38,6 @@ export function runSafeAsync<T>(func: () => Thenable<T>, errorVal: T, errorMessa
|
||||
});
|
||||
}
|
||||
|
||||
export function runSafe<T, E>(func: () => T, errorVal: T, errorMessage: string, token: CancellationToken): Thenable<T | ResponseError<E>> {
|
||||
return new Promise<T | ResponseError<E>>((resolve) => {
|
||||
setImmediate(() => {
|
||||
if (token.isCancellationRequested) {
|
||||
resolve(cancelValue());
|
||||
} else {
|
||||
try {
|
||||
let result = func();
|
||||
if (token.isCancellationRequested) {
|
||||
resolve(cancelValue());
|
||||
return;
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.error(formatError(errorMessage, e));
|
||||
resolve(errorVal);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function cancelValue<E>() {
|
||||
return new ResponseError<E>(ErrorCodes.RequestCancelled, 'Request cancelled');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user