mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
Allow to run css language server headless
This commit is contained in:
@@ -8,8 +8,10 @@ import * as path from 'path';
|
||||
import { URI } from 'vscode-uri';
|
||||
import { TextDocument, CompletionList } from 'vscode-languageserver-types';
|
||||
import { WorkspaceFolder } from 'vscode-languageserver-protocol';
|
||||
import { getPathCompletionParticipant } from '../pathCompletion';
|
||||
import { PathCompletionParticipant } from '../pathCompletion';
|
||||
import { getCSSLanguageService } from 'vscode-css-languageservice';
|
||||
import { getNodeFSRequestService } from '../node/nodeFs';
|
||||
import { getDocumentContext } from '../utils/documentContext';
|
||||
|
||||
export interface ItemDescription {
|
||||
label: string;
|
||||
@@ -18,6 +20,7 @@ export interface ItemDescription {
|
||||
|
||||
suite('Completions', () => {
|
||||
const cssLanguageService = getCSSLanguageService();
|
||||
const requestService = getNodeFSRequestService();
|
||||
|
||||
let assertCompletion = function (completions: CompletionList, expected: ItemDescription, document: TextDocument, _offset: number) {
|
||||
let matches = completions.items.filter(completion => {
|
||||
@@ -31,7 +34,7 @@ suite('Completions', () => {
|
||||
}
|
||||
};
|
||||
|
||||
function assertCompletions(value: string, expected: { count?: number, items?: ItemDescription[] }, testUri: string, workspaceFolders?: WorkspaceFolder[], lang: string = 'css'): void {
|
||||
async function assertCompletions(value: string, expected: { count?: number, items?: ItemDescription[] }, testUri: string, workspaceFolders?: WorkspaceFolder[], lang: string = 'css'): Promise<any> {
|
||||
const offset = value.indexOf('|');
|
||||
value = value.substr(0, offset) + value.substr(offset + 1);
|
||||
|
||||
@@ -42,11 +45,15 @@ suite('Completions', () => {
|
||||
workspaceFolders = [{ name: 'x', uri: testUri.substr(0, testUri.lastIndexOf('/')) }];
|
||||
}
|
||||
|
||||
let participantResult = CompletionList.create([]);
|
||||
cssLanguageService.setCompletionParticipants([getPathCompletionParticipant(document, workspaceFolders, participantResult)]);
|
||||
const participant = new PathCompletionParticipant(requestService);
|
||||
cssLanguageService.setCompletionParticipants([participant]);
|
||||
|
||||
const stylesheet = cssLanguageService.parseStylesheet(document);
|
||||
let list = cssLanguageService.doComplete(document, position, stylesheet)!;
|
||||
|
||||
const context = getDocumentContext(testUri, workspaceFolders);
|
||||
|
||||
let participantResult = await participant.computeCompletions(document, context);
|
||||
list.items = list.items.concat(participantResult.items);
|
||||
|
||||
if (expected.count) {
|
||||
@@ -59,17 +66,17 @@ suite('Completions', () => {
|
||||
}
|
||||
}
|
||||
|
||||
test('CSS url() Path completion', function () {
|
||||
test('CSS url() Path completion', async function () {
|
||||
let testUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/about/about.css')).toString();
|
||||
let folders = [{ name: 'x', uri: URI.file(path.resolve(__dirname, '../../test')).toString() }];
|
||||
|
||||
assertCompletions('html { background-image: url("./|")', {
|
||||
await assertCompletions('html { background-image: url("./|")', {
|
||||
items: [
|
||||
{ label: 'about.html', resultText: 'html { background-image: url("./about.html")' }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('../|')`, {
|
||||
await assertCompletions(`html { background-image: url('../|')`, {
|
||||
items: [
|
||||
{ label: 'about/', resultText: `html { background-image: url('../about/')` },
|
||||
{ label: 'index.html', resultText: `html { background-image: url('../index.html')` },
|
||||
@@ -77,7 +84,7 @@ suite('Completions', () => {
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('../src/a|')`, {
|
||||
await assertCompletions(`html { background-image: url('../src/a|')`, {
|
||||
items: [
|
||||
{ label: 'feature.js', resultText: `html { background-image: url('../src/feature.js')` },
|
||||
{ label: 'data/', resultText: `html { background-image: url('../src/data/')` },
|
||||
@@ -85,25 +92,25 @@ suite('Completions', () => {
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('../src/data/f|.asar')`, {
|
||||
await assertCompletions(`html { background-image: url('../src/data/f|.asar')`, {
|
||||
items: [
|
||||
{ label: 'foo.asar', resultText: `html { background-image: url('../src/data/foo.asar')` }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('|')`, {
|
||||
await assertCompletions(`html { background-image: url('|')`, {
|
||||
items: [
|
||||
{ label: 'about.html', resultText: `html { background-image: url('about.html')` },
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('/|')`, {
|
||||
await assertCompletions(`html { background-image: url('/|')`, {
|
||||
items: [
|
||||
{ label: 'pathCompletionFixtures/', resultText: `html { background-image: url('/pathCompletionFixtures/')` }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url('/pathCompletionFixtures/|')`, {
|
||||
await assertCompletions(`html { background-image: url('/pathCompletionFixtures/|')`, {
|
||||
items: [
|
||||
{ label: 'about/', resultText: `html { background-image: url('/pathCompletionFixtures/about/')` },
|
||||
{ label: 'index.html', resultText: `html { background-image: url('/pathCompletionFixtures/index.html')` },
|
||||
@@ -111,53 +118,53 @@ suite('Completions', () => {
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`html { background-image: url("/|")`, {
|
||||
await assertCompletions(`html { background-image: url("/|")`, {
|
||||
items: [
|
||||
{ label: 'pathCompletionFixtures/', resultText: `html { background-image: url("/pathCompletionFixtures/")` }
|
||||
]
|
||||
}, testUri, folders);
|
||||
});
|
||||
|
||||
test('CSS url() Path Completion - Unquoted url', function () {
|
||||
test('CSS url() Path Completion - Unquoted url', async function () {
|
||||
let testUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/about/about.css')).toString();
|
||||
let folders = [{ name: 'x', uri: URI.file(path.resolve(__dirname, '../../test')).toString() }];
|
||||
|
||||
assertCompletions('html { background-image: url(./|)', {
|
||||
await assertCompletions('html { background-image: url(./|)', {
|
||||
items: [
|
||||
{ label: 'about.html', resultText: 'html { background-image: url(./about.html)' }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions('html { background-image: url(./a|)', {
|
||||
await assertCompletions('html { background-image: url(./a|)', {
|
||||
items: [
|
||||
{ label: 'about.html', resultText: 'html { background-image: url(./about.html)' }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions('html { background-image: url(../|src/)', {
|
||||
await assertCompletions('html { background-image: url(../|src/)', {
|
||||
items: [
|
||||
{ label: 'about/', resultText: 'html { background-image: url(../about/)' }
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions('html { background-image: url(../s|rc/)', {
|
||||
await assertCompletions('html { background-image: url(../s|rc/)', {
|
||||
items: [
|
||||
{ label: 'about/', resultText: 'html { background-image: url(../about/)' }
|
||||
]
|
||||
}, testUri, folders);
|
||||
});
|
||||
|
||||
test('CSS @import Path completion', function () {
|
||||
test('CSS @import Path completion', async function () {
|
||||
let testUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/about/about.css')).toString();
|
||||
let folders = [{ name: 'x', uri: URI.file(path.resolve(__dirname, '../../test')).toString() }];
|
||||
|
||||
assertCompletions(`@import './|'`, {
|
||||
await assertCompletions(`@import './|'`, {
|
||||
items: [
|
||||
{ label: 'about.html', resultText: `@import './about.html'` },
|
||||
]
|
||||
}, testUri, folders);
|
||||
|
||||
assertCompletions(`@import '../|'`, {
|
||||
await assertCompletions(`@import '../|'`, {
|
||||
items: [
|
||||
{ label: 'about/', resultText: `@import '../about/'` },
|
||||
{ label: 'scss/', resultText: `@import '../scss/'` },
|
||||
@@ -170,14 +177,14 @@ suite('Completions', () => {
|
||||
/**
|
||||
* For SCSS, `@import 'foo';` can be used for importing partial file `_foo.scss`
|
||||
*/
|
||||
test('SCSS @import Path completion', function () {
|
||||
test('SCSS @import Path completion', async function () {
|
||||
let testCSSUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/about/about.css')).toString();
|
||||
let folders = [{ name: 'x', uri: URI.file(path.resolve(__dirname, '../../test')).toString() }];
|
||||
|
||||
/**
|
||||
* We are in a CSS file, so no special treatment for SCSS partial files
|
||||
*/
|
||||
assertCompletions(`@import '../scss/|'`, {
|
||||
await assertCompletions(`@import '../scss/|'`, {
|
||||
items: [
|
||||
{ label: 'main.scss', resultText: `@import '../scss/main.scss'` },
|
||||
{ label: '_foo.scss', resultText: `@import '../scss/_foo.scss'` }
|
||||
@@ -185,18 +192,18 @@ suite('Completions', () => {
|
||||
}, testCSSUri, folders);
|
||||
|
||||
let testSCSSUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/scss/main.scss')).toString();
|
||||
assertCompletions(`@import './|'`, {
|
||||
await assertCompletions(`@import './|'`, {
|
||||
items: [
|
||||
{ label: '_foo.scss', resultText: `@import './foo'` }
|
||||
]
|
||||
}, testSCSSUri, folders, 'scss');
|
||||
});
|
||||
|
||||
test('Completion should ignore files/folders starting with dot', function () {
|
||||
test('Completion should ignore files/folders starting with dot', async function () {
|
||||
let testUri = URI.file(path.resolve(__dirname, '../../test/pathCompletionFixtures/about/about.css')).toString();
|
||||
let folders = [{ name: 'x', uri: URI.file(path.resolve(__dirname, '../../test')).toString() }];
|
||||
|
||||
assertCompletions('html { background-image: url("../|")', {
|
||||
await assertCompletions('html { background-image: url("../|")', {
|
||||
count: 4
|
||||
}, testUri, folders);
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import { TextDocument, DocumentLink } from 'vscode-languageserver-types';
|
||||
import { WorkspaceFolder } from 'vscode-languageserver-protocol';
|
||||
import { getCSSLanguageService } from 'vscode-css-languageservice';
|
||||
import { getDocumentContext } from '../utils/documentContext';
|
||||
import { getNodeFSRequestService } from '../node/nodeFs';
|
||||
|
||||
export interface ItemDescription {
|
||||
offset: number;
|
||||
@@ -18,7 +19,7 @@ export interface ItemDescription {
|
||||
}
|
||||
|
||||
suite('Links', () => {
|
||||
const cssLanguageService = getCSSLanguageService();
|
||||
const cssLanguageService = getCSSLanguageService({ fileSystemProvider: getNodeFSRequestService() });
|
||||
|
||||
let assertLink = function (links: DocumentLink[], expected: ItemDescription, document: TextDocument) {
|
||||
let matches = links.filter(link => {
|
||||
@@ -31,7 +32,7 @@ suite('Links', () => {
|
||||
assert.equal(match.target, expected.target);
|
||||
};
|
||||
|
||||
function assertLinks(value: string, expected: ItemDescription[], testUri: string, workspaceFolders?: WorkspaceFolder[], lang: string = 'css'): void {
|
||||
async function assertLinks(value: string, expected: ItemDescription[], testUri: string, workspaceFolders?: WorkspaceFolder[], lang: string = 'css'): Promise<void> {
|
||||
const offset = value.indexOf('|');
|
||||
value = value.substr(0, offset) + value.substr(offset + 1);
|
||||
|
||||
@@ -44,7 +45,7 @@ suite('Links', () => {
|
||||
const context = getDocumentContext(testUri, workspaceFolders);
|
||||
|
||||
const stylesheet = cssLanguageService.parseStylesheet(document);
|
||||
let links = cssLanguageService.findDocumentLinks(document, stylesheet, context)!;
|
||||
let links = await cssLanguageService.findDocumentLinks2(document, stylesheet, context)!;
|
||||
|
||||
assert.equal(links.length, expected.length);
|
||||
|
||||
@@ -57,32 +58,32 @@ suite('Links', () => {
|
||||
return URI.file(resolve(__dirname, '../../test/linksTestFixtures', path)).toString();
|
||||
}
|
||||
|
||||
test('url links', function () {
|
||||
test('url links', async function () {
|
||||
|
||||
let testUri = getTestResource('about.css');
|
||||
let folders = [{ name: 'x', uri: getTestResource('') }];
|
||||
|
||||
assertLinks('html { background-image: url("hello.html|")',
|
||||
await assertLinks('html { background-image: url("hello.html|")',
|
||||
[{ offset: 29, value: '"hello.html"', target: getTestResource('hello.html') }], testUri, folders
|
||||
);
|
||||
});
|
||||
|
||||
test('node module resolving', function () {
|
||||
test('node module resolving', async function () {
|
||||
|
||||
let testUri = getTestResource('about.css');
|
||||
let folders = [{ name: 'x', uri: getTestResource('') }];
|
||||
|
||||
assertLinks('html { background-image: url("~foo/hello.html|")',
|
||||
await assertLinks('html { background-image: url("~foo/hello.html|")',
|
||||
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
|
||||
);
|
||||
});
|
||||
|
||||
test('node module subfolder resolving', function () {
|
||||
test('node module subfolder resolving', async function () {
|
||||
|
||||
let testUri = getTestResource('subdir/about.css');
|
||||
let folders = [{ name: 'x', uri: getTestResource('') }];
|
||||
|
||||
assertLinks('html { background-image: url("~foo/hello.html|")',
|
||||
await assertLinks('html { background-image: url("~foo/hello.html|")',
|
||||
[{ offset: 29, value: '"~foo/hello.html"', target: getTestResource('node_modules/foo/hello.html') }], testUri, folders
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user