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
}
}
}
129 changes: 129 additions & 0 deletions packages/yaml/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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';

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 default (settings: LanguageSettings): Service<Provide> => {
return (context): ReturnType<Service<Provide>> => {

if (!context) {
return { triggerCharacters: [' ', ':'] } as any;
}

const ls = getLanguageService({
schemaRequestService: async (uri) => await context.env.fs?.readFile(uri) ?? '',
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