Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add volar-service-yaml #45

Merged
merged 12 commits into from
Sep 3, 2023
29 changes: 29 additions & 0 deletions packages/yaml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "volar-service-yaml",
"version": "0.0.11",
"main": "out/index.js",
"license": "MIT",
"files": [
"out/**/*.js",
"out/**/*.d.ts"
],
"repository": {
"type": "git",
"url": "https://github.com/volarjs/services.git",
"directory": "packages/yaml"
},
"dependencies": {
"yaml-language-server": "1.14.0"
},
"devDependencies": {
"vscode-languageserver-textdocument": "^1.0.8"
},
"peerDependencies": {
"@volar/language-service": "~1.10.0"
},
"peerDependenciesMeta": {
"@volar/language-service": {
"optional": true
}
}
}
143 changes: 143 additions & 0 deletions packages/yaml/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { type Service } from '@volar/language-service';
import { type TextDocument } from 'vscode-languageserver-textdocument';
import { type LanguageSettings, type LanguageService } from 'yaml-language-server';
import { getLanguageService } from 'yaml-language-server/lib/umd/languageservice/yamlLanguageService.js';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good way to avoid downstream bundling hack! But since this is inconsistent with other services, I will temporarily change it to import from 'yaml-language-server' and change to import from umd path for all services in a separate PR, so we can retrace the reason.


export interface Provide {
'yaml/languageService': () => LanguageService;
}

function isYaml(document: TextDocument): boolean {
return document.languageId === 'yaml';
}

function noop(): undefined { }

/**
* Create a Volar language service for YAML documents.
*/
export function createYamlService(
johnsoncodehk marked this conversation as resolved.
Show resolved Hide resolved
settings: LanguageSettings
): Service<Provide | undefined> {
return (context) => {
const ls = getLanguageService({
async schemaRequestService(uri) {
if (uri.startsWith('file:') && context?.env.fs) {
const result = await context?.env.fs.readFile(uri);
if (result) {
return result;
}

throw new Error(`No such file: ${uri}`);
}

// @ts-expect-error This exists as an experimental API in Node 16.
const response = await fetch(uri);
Copy link
Member

@johnsoncodehk johnsoncodehk Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to handle http requests in the service because it is already handled in language server: https://github.com/volarjs/volar.js/blob/fc0bc2c262e693b6d41e87bff758ac1eb6eeb8c9/packages/language-server/src/node/index.ts#L54

if (response.ok) {
return response.text();
}

throw new Error(await response.text());
},
telemetry: {
send: noop,
sendError: noop,
sendTrack: noop
},
// @ts-expect-error https://github.com/redhat-developer/yaml-language-server/pull/910
clientCapabilities: context?.env?.clientCapabilities,
workspaceContext: {
resolveRelativePath(relativePath, resource) {
return String(new URL(relativePath, resource));
Copy link
Member

@johnsoncodehk johnsoncodehk Sep 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this is simpler than json service. The following code of json service is modified from vscode implementation. But I think your implementation also works, let's keep it.

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

}
}
});

ls.configure({
completion: true,
customTags: [],
format: true,
hover: true,
isKubernetes: false,
validate: true,
yamlVersion: '1.2',
...settings
});

return {
provide: {
'yaml/languageService': () => ls
},

triggerCharacters: [' ', ':'],

provideCodeActions(document, range, context) {
if (isYaml(document)) {
return ls.getCodeAction(document, {
context,
range,
textDocument: document
});
}
},

provideCodeLenses(document) {
if (isYaml(document)) {
return ls.getCodeLens(document);
}
},

provideCompletionItems(document, position) {
if (isYaml(document)) {
return ls.doComplete(document, position, false);
}
},

provideDefinition(document, position) {
if (isYaml(document)) {
return ls.doDefinition(document, { position, textDocument: document });
}
},

provideDiagnostics(document) {
if (isYaml(document)) {
return ls.doValidation(document, false);
}
},

provideDocumentSymbols(document) {
if (isYaml(document)) {
return ls.findDocumentSymbols2(document, {});
}
},

provideHover(document, position) {
if (isYaml(document)) {
return ls.doHover(document, position);
}
},

provideDocumentLinks(document) {
if (isYaml(document)) {
return ls.findLinks(document);
}
},

provideFoldingRanges(document) {
if (isYaml(document)) {
return ls.getFoldingRanges(document, {});
}
},

provideSelectionRanges(document, positions) {
if (isYaml(document)) {
return ls.getSelectionRanges(document, positions);
}
},

resolveCodeLens(codeLens) {
return ls.resolveCodeLens(codeLens);
}
};
};
}
11 changes: 11 additions & 0 deletions packages/yaml/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"outDir": "out",
"rootDir": "src",
},
"include": [
"src",
],
}
Loading