volar-service-markdown 0.0.13

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.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { Service } from '@volar/language-service';
2
+ import type { IMdLanguageService } from 'vscode-markdown-languageservice';
3
+ export interface Provide {
4
+ 'markdown/languageService': () => IMdLanguageService;
5
+ }
6
+ export declare function create(): Service<Provide>;
7
+ export default create;
package/out/index.js ADDED
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.create = void 0;
4
+ const vscode_jsonrpc_1 = require("vscode-jsonrpc");
5
+ const vscode_markdown_languageservice_1 = require("vscode-markdown-languageservice");
6
+ const vscode_uri_1 = require("vscode-uri");
7
+ const MarkdownIt = require("markdown-it");
8
+ const md = new MarkdownIt();
9
+ function isMarkdown(document) {
10
+ return document.languageId === 'markdown';
11
+ }
12
+ function assert(condition, message) {
13
+ if (!condition) {
14
+ throw new Error(message);
15
+ }
16
+ }
17
+ function create() {
18
+ return (context) => {
19
+ if (!context) {
20
+ return {};
21
+ }
22
+ let lastProjectVersion = context.host.getProjectVersion();
23
+ assert(context.env, 'context.env must be defined');
24
+ const { fs, onDidChangeWatchedFiles } = context.env;
25
+ assert(fs, 'context.env.fs must be defined');
26
+ assert(onDidChangeWatchedFiles, 'context.env.fs.onDidChangeWatchedFiles must be defined');
27
+ const logger = {
28
+ level: vscode_markdown_languageservice_1.LogLevel.Off,
29
+ log(_logLevel, message) {
30
+ context.env.console?.log(message);
31
+ }
32
+ };
33
+ const parser = {
34
+ slugifier: vscode_markdown_languageservice_1.githubSlugifier,
35
+ async tokenize(document) {
36
+ return md.parse(document.getText(), {});
37
+ }
38
+ };
39
+ const onDidChangeMarkdownDocument = new vscode_jsonrpc_1.Emitter();
40
+ const onDidCreateMarkdownDocument = new vscode_jsonrpc_1.Emitter();
41
+ const onDidDeleteMarkdownDocument = new vscode_jsonrpc_1.Emitter();
42
+ const fileWatcher = onDidChangeWatchedFiles((event) => {
43
+ for (const change of event.changes) {
44
+ switch (change.type) {
45
+ case 2: {
46
+ const document = context.getTextDocument(change.uri);
47
+ if (document) {
48
+ onDidChangeMarkdownDocument.fire(document);
49
+ }
50
+ break;
51
+ }
52
+ case 1: {
53
+ const document = context.getTextDocument(change.uri);
54
+ if (document) {
55
+ onDidCreateMarkdownDocument.fire(document);
56
+ }
57
+ break;
58
+ }
59
+ case 3: {
60
+ onDidDeleteMarkdownDocument.fire(vscode_uri_1.URI.parse(change.uri));
61
+ break;
62
+ }
63
+ }
64
+ }
65
+ });
66
+ const workspace = {
67
+ async getAllMarkdownDocuments() {
68
+ sync();
69
+ return syncedVersions.values();
70
+ },
71
+ getContainingDocument() {
72
+ return undefined;
73
+ },
74
+ hasMarkdownDocument(resource) {
75
+ const document = context.getTextDocument(String(resource));
76
+ return Boolean(document && isMarkdown(document));
77
+ },
78
+ onDidChangeMarkdownDocument: onDidChangeMarkdownDocument.event,
79
+ onDidCreateMarkdownDocument: onDidCreateMarkdownDocument.event,
80
+ onDidDeleteMarkdownDocument: onDidDeleteMarkdownDocument.event,
81
+ async openMarkdownDocument(resource) {
82
+ return context.getTextDocument(String(resource));
83
+ },
84
+ async readDirectory(resource) {
85
+ const directory = await fs.readDirectory(String(resource));
86
+ return directory.map(([fileName, fileType]) => [
87
+ fileName,
88
+ { isDirectory: fileType === 2 }
89
+ ]);
90
+ },
91
+ async stat(resource) {
92
+ const stat = await fs.stat(String(resource));
93
+ if (stat) {
94
+ return { isDirectory: stat.type === 2 };
95
+ }
96
+ },
97
+ workspaceFolders: []
98
+ };
99
+ const ls = (0, vscode_markdown_languageservice_1.createLanguageService)({
100
+ logger,
101
+ parser,
102
+ workspace
103
+ });
104
+ const syncedVersions = new Map();
105
+ const sync = () => {
106
+ const newProjectVersion = context.host.getProjectVersion();
107
+ const shouldUpdate = newProjectVersion !== lastProjectVersion;
108
+ if (!shouldUpdate) {
109
+ return;
110
+ }
111
+ lastProjectVersion = newProjectVersion;
112
+ const oldVersions = new Set(syncedVersions.keys());
113
+ const newVersions = new Map();
114
+ for (const { root } of context.virtualFiles.allSources()) {
115
+ const embeddeds = [root];
116
+ root.embeddedFiles.forEach(function walk(embedded) {
117
+ embeddeds.push(embedded);
118
+ embedded.embeddedFiles.forEach(walk);
119
+ });
120
+ for (const embedded of embeddeds) {
121
+ const document = context.getTextDocument(embedded.fileName);
122
+ if (document && isMarkdown(document)) {
123
+ newVersions.set(String(document.uri), document);
124
+ }
125
+ }
126
+ }
127
+ for (const [uri, document] of newVersions) {
128
+ const old = syncedVersions.get(uri);
129
+ syncedVersions.set(uri, document);
130
+ if (old) {
131
+ onDidChangeMarkdownDocument.fire(document);
132
+ }
133
+ else {
134
+ onDidCreateMarkdownDocument.fire(document);
135
+ }
136
+ }
137
+ for (const uri of oldVersions) {
138
+ if (!newVersions.has(uri)) {
139
+ syncedVersions.delete(uri);
140
+ onDidDeleteMarkdownDocument.fire(vscode_uri_1.URI.parse(uri));
141
+ }
142
+ }
143
+ };
144
+ const prepare = (document) => {
145
+ if (!isMarkdown(document)) {
146
+ return false;
147
+ }
148
+ sync();
149
+ return true;
150
+ };
151
+ return {
152
+ dispose() {
153
+ ls.dispose();
154
+ fileWatcher.dispose();
155
+ onDidDeleteMarkdownDocument.dispose();
156
+ onDidCreateMarkdownDocument.dispose();
157
+ onDidChangeMarkdownDocument.dispose();
158
+ },
159
+ provide: {
160
+ 'markdown/languageService': () => ls
161
+ },
162
+ provideCodeActions(document, range, context, token) {
163
+ if (prepare(document)) {
164
+ return ls.getCodeActions(document, range, context, token);
165
+ }
166
+ },
167
+ async provideCompletionItems(document, position, context, token) {
168
+ if (prepare(document)) {
169
+ const items = await ls.getCompletionItems(document, position, {}, token);
170
+ return {
171
+ isIncomplete: false,
172
+ items
173
+ };
174
+ }
175
+ },
176
+ provideDiagnostics(document, token) {
177
+ if (prepare(document)) {
178
+ return ls.computeDiagnostics(document, {
179
+ ignoreLinks: [],
180
+ validateDuplicateLinkDefinitions: vscode_markdown_languageservice_1.DiagnosticLevel.warning,
181
+ validateFileLinks: vscode_markdown_languageservice_1.DiagnosticLevel.warning,
182
+ validateFragmentLinks: vscode_markdown_languageservice_1.DiagnosticLevel.warning,
183
+ validateMarkdownFileLinkFragments: vscode_markdown_languageservice_1.DiagnosticLevel.warning,
184
+ validateReferences: vscode_markdown_languageservice_1.DiagnosticLevel.warning,
185
+ validateUnusedLinkDefinitions: vscode_markdown_languageservice_1.DiagnosticLevel.warning
186
+ }, token);
187
+ }
188
+ },
189
+ provideDocumentHighlights(document, position, token) {
190
+ if (prepare(document)) {
191
+ return ls.getDocumentHighlights(document, position, token);
192
+ }
193
+ },
194
+ provideDocumentLinks(document, token) {
195
+ if (prepare(document)) {
196
+ return ls.getDocumentLinks(document, token);
197
+ }
198
+ },
199
+ provideDocumentSymbols(document, token) {
200
+ if (prepare(document)) {
201
+ return ls.getDocumentSymbols(document, { includeLinkDefinitions: true }, token);
202
+ }
203
+ },
204
+ provideFileReferences(document, token) {
205
+ if (prepare(document)) {
206
+ return ls.getFileReferences(vscode_uri_1.URI.parse(document.uri), token);
207
+ }
208
+ },
209
+ provideFoldingRanges(document, token) {
210
+ if (prepare(document)) {
211
+ return ls.getFoldingRanges(document, token);
212
+ }
213
+ },
214
+ provideReferences(document, position, token) {
215
+ if (prepare(document)) {
216
+ return ls.getReferences(document, position, { includeDeclaration: true }, token);
217
+ }
218
+ },
219
+ provideRenameEdits(document, position, newName, token) {
220
+ if (prepare(document)) {
221
+ return ls.getRenameEdit(document, position, newName, token);
222
+ }
223
+ },
224
+ provideRenameRange(document, position, token) {
225
+ if (prepare(document)) {
226
+ return ls.prepareRename(document, position, token);
227
+ }
228
+ },
229
+ provideSelectionRanges(document, positions, token) {
230
+ if (prepare(document)) {
231
+ return ls.getSelectionRanges(document, positions, token);
232
+ }
233
+ },
234
+ provideWorkspaceSymbols(query, token) {
235
+ sync();
236
+ return ls.getWorkspaceSymbols(query, token);
237
+ },
238
+ async resolveDocumentLink(link, token) {
239
+ const result = await ls.resolveDocumentLink(link, token);
240
+ return result || link;
241
+ }
242
+ };
243
+ };
244
+ }
245
+ exports.create = create;
246
+ exports.default = create;
247
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "volar-service-markdown",
3
+ "version": "0.0.13",
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/services.git",
13
+ "directory": "packages/markdown"
14
+ },
15
+ "dependencies": {
16
+ "markdown-it": "^13.0.1",
17
+ "vscode-jsonrpc": "^8.1.0",
18
+ "vscode-markdown-languageservice": "~0.4.0-alpha.5",
19
+ "vscode-uri": "^3.0.7"
20
+ },
21
+ "devDependencies": {
22
+ "@types/markdown-it": "^13.0.1",
23
+ "vscode-languageserver-textdocument": "^1.0.8"
24
+ },
25
+ "peerDependencies": {
26
+ "@volar/language-service": "~1.10.0"
27
+ },
28
+ "peerDependenciesMeta": {
29
+ "@volar/language-service": {
30
+ "optional": true
31
+ }
32
+ }
33
+ }