@volar/kit 1.4.0-alpha.0 → 1.4.0-alpha.10
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/out/index.js +73 -75
- package/package.json +3 -3
package/out/index.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
|
|
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
|
-
};
|
|
1
|
+
"use strict";
|
|
10
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
3
|
exports.create = void 0;
|
|
12
4
|
const language_service_1 = require("@volar/language-service");
|
|
@@ -22,7 +14,16 @@ function create(tsConfigPath, config, extraFileExtensions = [], ts = require('ty
|
|
|
22
14
|
const scriptSnapshots = new Map();
|
|
23
15
|
const jsonConfig = ts.readJsonConfigFile(tsConfigPath, ts.sys.readFile);
|
|
24
16
|
const parsedCommandLine = ts.parseJsonSourceFileConfigFileContent(jsonConfig, ts.sys, path.dirname(tsConfigPath), {}, tsConfigPath, undefined, extraFileExtensions);
|
|
25
|
-
const host =
|
|
17
|
+
const host = {
|
|
18
|
+
...ts.sys,
|
|
19
|
+
getProjectVersion: () => projectVersion.toString(),
|
|
20
|
+
getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
|
|
21
|
+
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
|
|
22
|
+
getCompilationSettings: () => parsedCommandLine.options,
|
|
23
|
+
getScriptFileNames: () => parsedCommandLine.fileNames,
|
|
24
|
+
getProjectReferences: () => parsedCommandLine.projectReferences,
|
|
25
|
+
getScriptVersion: (fileName) => scriptVersions.get(fileName)?.toString() ?? '',
|
|
26
|
+
getScriptSnapshot: (fileName) => {
|
|
26
27
|
if (!scriptSnapshots.has(fileName)) {
|
|
27
28
|
const fileText = ts.sys.readFile(fileName);
|
|
28
29
|
if (fileText !== undefined) {
|
|
@@ -30,8 +31,10 @@ function create(tsConfigPath, config, extraFileExtensions = [], ts = require('ty
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
return scriptSnapshots.get(fileName);
|
|
33
|
-
},
|
|
34
|
+
},
|
|
35
|
+
};
|
|
34
36
|
const service = (0, language_service_1.createLanguageService)({
|
|
37
|
+
modules: { typescript: ts },
|
|
35
38
|
host,
|
|
36
39
|
config,
|
|
37
40
|
uriToFileName,
|
|
@@ -40,87 +43,82 @@ function create(tsConfigPath, config, extraFileExtensions = [], ts = require('ty
|
|
|
40
43
|
});
|
|
41
44
|
const formatHost = {
|
|
42
45
|
getCurrentDirectory: () => host.getCurrentDirectory(),
|
|
43
|
-
getCanonicalFileName: (fileName) =>
|
|
44
|
-
getNewLine: () =>
|
|
46
|
+
getCanonicalFileName: (fileName) => host.useCaseSensitiveFileNames?.() ? fileName : fileName.toLowerCase(),
|
|
47
|
+
getNewLine: () => host.getNewLine?.() ?? '\n',
|
|
45
48
|
};
|
|
46
49
|
return {
|
|
47
50
|
roots: parsedCommandLine.fileNames,
|
|
48
51
|
lint,
|
|
49
52
|
format,
|
|
50
53
|
};
|
|
51
|
-
function lint(fileName, severity = language_service_1.DiagnosticSeverity.Hint, throwLevel = 0) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
async function lint(fileName, severity = language_service_1.DiagnosticSeverity.Hint, throwLevel = 0) {
|
|
55
|
+
const uri = fileNameToUri(fileName);
|
|
56
|
+
const document = service.context.getTextDocument(uri);
|
|
57
|
+
let diagnostics = [];
|
|
58
|
+
if (document) {
|
|
59
|
+
diagnostics = await service.doValidation(uri, 'all');
|
|
60
|
+
diagnostics = diagnostics.filter(diagnostic => (diagnostic.severity ?? 1) <= severity);
|
|
61
|
+
const errors = diagnostics.map(diagnostic => ({
|
|
62
|
+
category: diagnostic.severity === language_service_1.DiagnosticSeverity.Error ? ts.DiagnosticCategory.Error : ts.DiagnosticCategory.Warning,
|
|
63
|
+
code: diagnostic.code,
|
|
64
|
+
file: ts.createSourceFile(fileName, document.getText(), ts.ScriptTarget.JSON),
|
|
65
|
+
start: document.offsetAt(diagnostic.range.start),
|
|
66
|
+
length: document.offsetAt(diagnostic.range.end) - document.offsetAt(diagnostic.range.start),
|
|
67
|
+
messageText: diagnostic.message,
|
|
68
|
+
}));
|
|
69
|
+
const text = ts.formatDiagnosticsWithColorAndContext(errors, formatHost);
|
|
70
|
+
if (text) {
|
|
71
|
+
if (diagnostics.some(diagnostic => (diagnostic.severity ?? 1) <= throwLevel)) {
|
|
72
|
+
throw text;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
console.log(text);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return async (crossFileFix = false) => {
|
|
54
80
|
const document = service.context.getTextDocument(uri);
|
|
55
|
-
let diagnostics = [];
|
|
56
81
|
if (document) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
file: ts.createSourceFile(fileName, document.getText(), ts.ScriptTarget.JSON),
|
|
63
|
-
start: document.offsetAt(diagnostic.range.start),
|
|
64
|
-
length: document.offsetAt(diagnostic.range.end) - document.offsetAt(diagnostic.range.start),
|
|
65
|
-
messageText: diagnostic.message,
|
|
66
|
-
}));
|
|
67
|
-
const text = ts.formatDiagnosticsWithColorAndContext(errors, formatHost);
|
|
68
|
-
if (text) {
|
|
69
|
-
if (diagnostics.some(diagnostic => { var _a; return ((_a = diagnostic.severity) !== null && _a !== void 0 ? _a : 1) <= throwLevel; })) {
|
|
70
|
-
throw text;
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
console.log(text);
|
|
82
|
+
const range = { start: document.positionAt(0), end: document.positionAt(document.getText().length) };
|
|
83
|
+
const codeActions = await service.doCodeActions(uri, range, { diagnostics, only: ['source.fixAll'], triggerKind: language_service_1.CodeActionTriggerKind.Invoked }, language_service_1.CancellationToken.None);
|
|
84
|
+
if (codeActions) {
|
|
85
|
+
for (let i = 0; i < codeActions.length; i++) {
|
|
86
|
+
codeActions[i] = await service.doCodeActionResolve(codeActions[i], language_service_1.CancellationToken.None);
|
|
74
87
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const edits = codeActions.map(codeAction => codeAction.edit).filter((edit) => !!edit);
|
|
88
|
-
if (edits.length) {
|
|
89
|
-
const rootEdit = edits[0];
|
|
90
|
-
(0, language_service_1.mergeWorkspaceEdits)(rootEdit, ...edits.slice(1));
|
|
91
|
-
for (const uri in (_a = rootEdit.changes) !== null && _a !== void 0 ? _a : {}) {
|
|
92
|
-
if (uri === document.uri || crossFileFix) {
|
|
93
|
-
const edits = rootEdit.changes[uri];
|
|
94
|
-
if (edits.length) {
|
|
95
|
-
const editDocument = service.context.getTextDocument(uri);
|
|
96
|
-
if (editDocument) {
|
|
97
|
-
const newString = vscode_languageserver_textdocument_1.TextDocument.applyEdits(editDocument, edits);
|
|
98
|
-
writeFile(uriToFileName(uri), newString);
|
|
99
|
-
}
|
|
88
|
+
const edits = codeActions.map(codeAction => codeAction.edit).filter((edit) => !!edit);
|
|
89
|
+
if (edits.length) {
|
|
90
|
+
const rootEdit = edits[0];
|
|
91
|
+
(0, language_service_1.mergeWorkspaceEdits)(rootEdit, ...edits.slice(1));
|
|
92
|
+
for (const uri in rootEdit.changes ?? {}) {
|
|
93
|
+
if (uri === document.uri || crossFileFix) {
|
|
94
|
+
const edits = rootEdit.changes[uri];
|
|
95
|
+
if (edits.length) {
|
|
96
|
+
const editDocument = service.context.getTextDocument(uri);
|
|
97
|
+
if (editDocument) {
|
|
98
|
+
const newString = vscode_languageserver_textdocument_1.TextDocument.applyEdits(editDocument, edits);
|
|
99
|
+
writeFile(uriToFileName(uri), newString);
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
103
|
+
}
|
|
104
|
+
if (crossFileFix) {
|
|
105
|
+
// TODO: rootEdit.documentChanges
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
111
|
}
|
|
112
|
-
function format(fileName, options) {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
writeFile(fileName, newString);
|
|
121
|
-
}
|
|
112
|
+
async function format(fileName, options) {
|
|
113
|
+
const uri = fileNameToUri(fileName);
|
|
114
|
+
const document = service.context.getTextDocument(uri);
|
|
115
|
+
if (document) {
|
|
116
|
+
const edits = await service.format(uri, options, undefined, undefined, language_service_1.CancellationToken.None);
|
|
117
|
+
if (edits?.length) {
|
|
118
|
+
const newString = vscode_languageserver_textdocument_1.TextDocument.applyEdits(document, edits);
|
|
119
|
+
writeFile(fileName, newString);
|
|
122
120
|
}
|
|
123
|
-
}
|
|
121
|
+
}
|
|
124
122
|
}
|
|
125
123
|
}
|
|
126
124
|
exports.create = create;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volar/kit",
|
|
3
|
-
"version": "1.4.0-alpha.
|
|
3
|
+
"version": "1.4.0-alpha.10",
|
|
4
4
|
"main": "out/index.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
"directory": "packages/kit"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@volar/language-service": "1.4.0-alpha.
|
|
16
|
+
"@volar/language-service": "1.4.0-alpha.10",
|
|
17
17
|
"vscode-languageserver-textdocument": "^1.0.8",
|
|
18
18
|
"vscode-uri": "^3.0.7"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
21
|
"typescript": "*"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "715bc5f17a0d1cadd4190712dcaff732989eb330"
|
|
24
24
|
}
|