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