Skip to content

Commit

Permalink
Add volar-service-yaml (#45)
Browse files Browse the repository at this point in the history
Co-authored-by: Erika <[email protected]>
Co-authored-by: Johnson Chu <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2023
1 parent a2eee58 commit 6a13528
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 13 deletions.
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));
}
},
});

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

0 comments on commit 6a13528

Please sign in to comment.