@volar/kit 1.1.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Johnson Chu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/out/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { FormattingOptions, LanguageModule, LanguageServicePlugin, DiagnosticSeverity } from '@volar/language-service';
2
+ import type * as ts from 'typescript/lib/tsserverlibrary';
3
+ export declare function create(tsConfigPath: string, plugins: LanguageServicePlugin[], extraFileExtensions?: ts.FileExtensionInfo[], languageModules?: LanguageModule[], ts?: typeof import('typescript/lib/tsserverlibrary')): {
4
+ roots: string[];
5
+ lint: (fileName: string, severity?: DiagnosticSeverity, throwLevel?: DiagnosticSeverity) => Promise<(crossFileFix?: boolean) => Promise<void>>;
6
+ format: (fileName: string, options: FormattingOptions) => Promise<void>;
7
+ };
package/out/index.js ADDED
@@ -0,0 +1,158 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.create = void 0;
12
+ const language_service_1 = require("@volar/language-service");
13
+ const shared_1 = require("@volar/shared");
14
+ const fs = require("fs");
15
+ const path = require("path");
16
+ const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
17
+ const vscode_uri_1 = require("vscode-uri");
18
+ function create(tsConfigPath, plugins, extraFileExtensions = [], languageModules = [], ts = require('typescript')) {
19
+ let projectVersion = 0;
20
+ const scriptVersions = new Map();
21
+ const scriptSnapshots = new Map();
22
+ const textDocuments = new Map();
23
+ const jsonConfig = ts.readJsonConfigFile(tsConfigPath, ts.sys.readFile);
24
+ const parsedCommandLine = ts.parseJsonSourceFileConfigFileContent(jsonConfig, ts.sys, path.dirname(tsConfigPath), {}, tsConfigPath, undefined, extraFileExtensions);
25
+ const host = Object.assign(Object.assign({}, ts.sys), { getProjectVersion: () => projectVersion.toString(), getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options), useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames, getCompilationSettings: () => parsedCommandLine.options, getScriptFileNames: () => parsedCommandLine.fileNames, getProjectReferences: () => parsedCommandLine.projectReferences, getScriptVersion: (fileName) => { var _a, _b; return (_b = (_a = scriptVersions.get(fileName)) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ''; }, getScriptSnapshot: (fileName) => {
26
+ if (!scriptSnapshots.has(fileName)) {
27
+ const fileText = ts.sys.readFile(fileName);
28
+ if (fileText !== undefined) {
29
+ scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(fileText));
30
+ }
31
+ }
32
+ return scriptSnapshots.get(fileName);
33
+ }, getTypeScriptModule: () => ts });
34
+ const core = (0, language_service_1.createLanguageContext)(host, languageModules);
35
+ const ctx = {
36
+ typescript: {
37
+ module: ts,
38
+ languageServiceHost: core.typescript.languageServiceHost,
39
+ languageService: ts.createLanguageService(core.typescript.languageServiceHost),
40
+ },
41
+ env: {
42
+ rootUri: vscode_uri_1.URI.file(path.dirname(tsConfigPath)),
43
+ documentContext: {
44
+ resolveReference() {
45
+ return undefined; // TODO
46
+ },
47
+ },
48
+ },
49
+ host,
50
+ core,
51
+ documents: (0, language_service_1.createDocumentsAndSourceMaps)(core.virtualFiles),
52
+ get plugins() {
53
+ return plugins.map(plugin => plugin(ctx, service));
54
+ },
55
+ getTextDocument(uri) {
56
+ if (!textDocuments.has(uri)) {
57
+ const snapshot = host.getScriptSnapshot((0, shared_1.uriToFileName)(uri));
58
+ if (snapshot) {
59
+ textDocuments.set(uri, vscode_languageserver_textdocument_1.TextDocument.create(uri, (0, shared_1.syntaxToLanguageId)(uri.substring(uri.lastIndexOf('.') + 1)), 0, snapshot.getText(0, snapshot.getLength())));
60
+ }
61
+ else {
62
+ textDocuments.set(uri, undefined);
63
+ }
64
+ }
65
+ return textDocuments.get(uri);
66
+ },
67
+ };
68
+ const service = (0, language_service_1.createLanguageService)(ctx);
69
+ const formatHost = {
70
+ getCurrentDirectory: () => host.getCurrentDirectory(),
71
+ getCanonicalFileName: (fileName) => { var _a; return ((_a = host.useCaseSensitiveFileNames) === null || _a === void 0 ? void 0 : _a.call(host)) ? fileName : fileName.toLowerCase(); },
72
+ getNewLine: () => { var _a, _b; return (_b = (_a = host.getNewLine) === null || _a === void 0 ? void 0 : _a.call(host)) !== null && _b !== void 0 ? _b : '\n'; },
73
+ };
74
+ return {
75
+ roots: parsedCommandLine.fileNames,
76
+ lint,
77
+ format,
78
+ };
79
+ function lint(fileName, severity = language_service_1.DiagnosticSeverity.Hint, throwLevel = 0) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ const uri = (0, shared_1.fileNameToUri)(fileName);
82
+ const document = ctx.getTextDocument(uri);
83
+ let diagnostics = [];
84
+ if (document) {
85
+ diagnostics = yield service.doValidation(uri);
86
+ diagnostics = diagnostics.filter(diagnostic => { var _a; return ((_a = diagnostic.severity) !== null && _a !== void 0 ? _a : 1) <= severity; });
87
+ const errors = diagnostics.map(diagnostic => ({
88
+ category: diagnostic.severity === language_service_1.DiagnosticSeverity.Error ? ts.DiagnosticCategory.Error : ts.DiagnosticCategory.Warning,
89
+ code: diagnostic.code,
90
+ file: ts.createSourceFile(fileName, document.getText(), ts.ScriptTarget.JSON),
91
+ start: document.offsetAt(diagnostic.range.start),
92
+ length: document.offsetAt(diagnostic.range.end) - document.offsetAt(diagnostic.range.start),
93
+ messageText: diagnostic.message,
94
+ }));
95
+ const text = ts.formatDiagnosticsWithColorAndContext(errors, formatHost);
96
+ if (text) {
97
+ if (diagnostics.some(diagnostic => { var _a; return ((_a = diagnostic.severity) !== null && _a !== void 0 ? _a : 1) <= throwLevel; })) {
98
+ throw text;
99
+ }
100
+ else {
101
+ console.log(text);
102
+ }
103
+ }
104
+ }
105
+ return (crossFileFix = false) => __awaiter(this, void 0, void 0, function* () {
106
+ var _a;
107
+ const document = ctx.getTextDocument(uri);
108
+ if (document) {
109
+ const range = { start: document.positionAt(0), end: document.positionAt(document.getText().length) };
110
+ const codeActions = yield service.doCodeActions(uri, range, { diagnostics, only: ['source.fixAll'], triggerKind: language_service_1.CodeActionTriggerKind.Invoked });
111
+ if (codeActions) {
112
+ for (let i = 0; i < codeActions.length; i++) {
113
+ codeActions[i] = yield service.doCodeActionResolve(codeActions[i]);
114
+ }
115
+ const edits = codeActions.map(codeAction => codeAction.edit).filter((edit) => !!edit);
116
+ if (edits.length) {
117
+ const rootEdit = edits[0];
118
+ (0, language_service_1.mergeWorkspaceEdits)(rootEdit, ...edits.slice(1));
119
+ for (const uri in (_a = rootEdit.changes) !== null && _a !== void 0 ? _a : {}) {
120
+ if (uri === document.uri || crossFileFix) {
121
+ const edits = rootEdit.changes[uri];
122
+ if (edits.length) {
123
+ const editDocument = ctx.getTextDocument(uri);
124
+ if (editDocument) {
125
+ const newString = vscode_languageserver_textdocument_1.TextDocument.applyEdits(editDocument, edits);
126
+ writeFile((0, shared_1.uriToFileName)(uri), newString);
127
+ }
128
+ }
129
+ }
130
+ }
131
+ if (crossFileFix) {
132
+ // TODO: rootEdit.documentChanges
133
+ }
134
+ }
135
+ }
136
+ }
137
+ });
138
+ });
139
+ }
140
+ function format(fileName, options) {
141
+ return __awaiter(this, void 0, void 0, function* () {
142
+ const uri = (0, shared_1.fileNameToUri)(fileName);
143
+ const document = ctx.getTextDocument(uri);
144
+ if (document) {
145
+ const edits = yield service.format(uri, options);
146
+ if (edits === null || edits === void 0 ? void 0 : edits.length) {
147
+ const newString = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
148
+ writeFile(fileName, newString);
149
+ }
150
+ }
151
+ });
152
+ }
153
+ }
154
+ exports.create = create;
155
+ function writeFile(fileName, newText) {
156
+ fs.writeFileSync(fileName, newText, { encoding: 'utf8' });
157
+ }
158
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@volar/kit",
3
+ "version": "1.1.0-alpha.2",
4
+ "main": "out/index.js",
5
+ "license": "MIT",
6
+ "files": [
7
+ "out/**/*.js",
8
+ "out/**/*.d.ts"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/volarjs/volar.js.git",
13
+ "directory": "packages/kit"
14
+ },
15
+ "dependencies": {
16
+ "@volar/language-service": "1.1.0-alpha.2",
17
+ "@volar/shared": "1.1.0-alpha.2",
18
+ "vscode-languageserver-textdocument": "^1.0.8",
19
+ "vscode-uri": "^3.0.7"
20
+ },
21
+ "peerDependencies": {
22
+ "typescript": "*"
23
+ },
24
+ "gitHead": "2a5ff13854187a26c1b5e36c5eab8b94cbd08375"
25
+ }